Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT make ORCA a shared lib #1098

Draft
wants to merge 16 commits into
base: adb-7.2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 3 additions & 0 deletions gpcontrib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ endif
ifeq "$(with_zstd)" "yes"
recurse_targets += zstd
endif
ifeq "$(enable_orca)" "yes"
recurse_targets += gp_orca
endif
$(call recurse,all install clean distclean, $(recurse_targets))

all: gpcloud orafce
Expand Down
1 change: 1 addition & 0 deletions gpcontrib/gp_orca/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/results/
69 changes: 69 additions & 0 deletions gpcontrib/gp_orca/Makefile

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions gpcontrib/gp_orca/gp_orca.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# gp_orca extension

comment = 'make ORCA shared again'
default_version = '1.0'
module_pathname = '$libdir/gp_orca'
relocatable = false
File renamed without changes.
File renamed without changes.
139 changes: 139 additions & 0 deletions gpcontrib/gp_orca/gporca.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "postgres.h"

#include "cdb/cdbvars.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "optimizer/orca.h"
#include "optimizer/planner.h"
#include "storage/shmem.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/guc.h"

extern bool optimizer;

extern void InitGPOPT();
extern void TerminateGPOPT();

extern void compute_jit_flags(PlannedStmt* pstmt);

PG_MODULE_MAGIC;

void _PG_init(void);
void _PG_fini(void);

planner_hook_type next_planner_hook = NULL;

static planner_hook_type prev_planner = NULL;

static PlannedStmt *
gp_orca_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
{
PlannedStmt *result = NULL;

PG_TRY();
{
/*
* Use ORCA only if it is enabled and we are in a coordinator QD process.
*
* ORCA excels in complex queries, most of which will access distributed
* tables. We can't run such queries from the segments slices anyway because
* they require dispatching a query within another - which is not allowed in
* GPDB (see querytree_safe_for_qe()). Note that this restriction also
* applies to non-QD coordinator slices. Furthermore, ORCA doesn't currently
* support pl/<lang> statements (relevant when they are planned on the segments).
* For these reasons, restrict to using ORCA on the coordinator QD processes only.
*
* PARALLEL RETRIEVE CURSOR is not supported by ORCA yet.
*/
if (optimizer &&
GP_ROLE_DISPATCH == Gp_role &&
IS_QUERY_DISPATCHER() &&
(cursorOptions & CURSOR_OPT_SKIP_FOREIGN_PARTITIONS) == 0 &&
(cursorOptions & CURSOR_OPT_PARALLEL_RETRIEVE) == 0)
{
instr_time starttime;
instr_time endtime;

if (gp_log_optimization_time)
INSTR_TIME_SET_CURRENT(starttime);

result = optimize_query(parse, cursorOptions, boundParams);

/* decide jit state */
if (result)
{
/*
* Setting Jit flags for Optimizer
*/
compute_jit_flags(result);
}

if (gp_log_optimization_time)
{
INSTR_TIME_SET_CURRENT(endtime);
INSTR_TIME_SUBTRACT(endtime, starttime);
elog(LOG, "Optimizer Time: %.3f ms", INSTR_TIME_GET_MILLISEC(endtime));
}
}
}
PG_CATCH();
{
PG_RE_THROW();
}
PG_END_TRY();

if (result)
return result;

if (prev_planner)
result = (*prev_planner) (parse, cursorOptions, boundParams);
else
result = standard_planner(parse, cursorOptions, boundParams);


return result;
}

void
_PG_init(void)
{
if (!process_shared_preload_libraries_in_progress)
return;

if (Gp_role == GP_ROLE_DISPATCH)
{
/* Initialize GPOPT */
OptimizerMemoryContext = AllocSetContextCreate(TopMemoryContext,
"GPORCA Top-level Memory Context",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);

InitGPOPT();

prev_planner = planner_hook;
planner_hook = gp_orca_planner;

/* enable orca here */
optimizer = true;
}
}

void
_PG_fini(void)
{
if (Gp_role == GP_ROLE_DISPATCH)
{
/* disable orca here */
optimizer = false;

planner_hook = prev_planner;

TerminateGPOPT();

if (OptimizerMemoryContext != NULL)
MemoryContextDelete(OptimizerMemoryContext);
}
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading