Skip to content

Avoid checking permission of Babelfish temp tables on parallel worker #560

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

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/backend/executor/execMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ ExecutorEnd_hook_type ExecutorEnd_hook = NULL;

TriggerRecuresiveCheck_hook_type TriggerRecuresiveCheck_hook = NULL;
check_rowcount_hook_type check_rowcount_hook = NULL;
ExecCheckOneRelPerms_hook_type ExecCheckOneRelPerms_hook = NULL;

/* Hook for plugin to get control in ExecCheckPermissions() */
ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook = NULL;

Expand Down Expand Up @@ -648,6 +650,19 @@ ExecCheckOneRelPerms(RTEPermissionInfo *perminfo)
requiredPerms = perminfo->requiredPerms;
Assert(requiredPerms != 0);

/*
* Babelfish specific logic - Babelfish temp table is implemented
* using ENR which is not shared with parallel worker and parallel
* operations are not allowed for temp table in Postgres. Babelfish
* can skip permission check for such use cases under parallel worker
* using this hook.
* Note - This hook must not be used outside of Babelfish parallel worker
*/
if (IsBabelfishParallelWorker() &&
ExecCheckOneRelPerms_hook &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put extension hook check first to be more safe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what difference will it make?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the parallel worker check be in the extension?

(*ExecCheckOneRelPerms_hook)(perminfo))
return true;

/*
* userid to check as: current user unless we have a setuid indication.
*
Expand Down Expand Up @@ -840,10 +855,9 @@ InitPlan(QueryDesc *queryDesc, int eflags)
int i;

/*
* Do permissions checks if not Babelfish parallel worker
* Do permissions checks
*/
if (!IsBabelfishParallelWorker())
ExecCheckPermissions(rangeTable, plannedstmt->permInfos, true);
ExecCheckPermissions(rangeTable, plannedstmt->permInfos, true);

/*
* initialize the node's execution state
Expand Down Expand Up @@ -3046,3 +3060,12 @@ EvalPlanQualEnd(EPQState *epqstate)
epqstate->relsubs_done = NULL;
epqstate->relsubs_blocked = NULL;
}

/*
* ExecCheckOneRelPerms_wrapper - wrapper around ExecCheckOneRelPerms
*/
bool
ExecCheckOneRelPerms_wrapper(RTEPermissionInfo *perminfo)
{
return ExecCheckOneRelPerms(perminfo);
}
21 changes: 21 additions & 0 deletions src/backend/executor/execParallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ typedef struct ExecParallelInitializeDSMContext
int nnodes;
} ExecParallelInitializeDSMContext;

ExecInitParallelPlan_hook_type ExecInitParallelPlan_hook = NULL;
ParallelQueryMain_hook_type ParallelQueryMain_hook = NULL;

/* Helper functions that run in the parallel leader. */
static char *ExecSerializePlan(Plan *plan, EState *estate);
static bool ExecParallelEstimate(PlanState *planstate,
Expand Down Expand Up @@ -683,6 +686,12 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate,
mul_size(PARALLEL_TUPLE_QUEUE_SIZE, pcxt->nworkers));
shm_toc_estimate_keys(&pcxt->estimator, 1);

/* Let extension estimate a dynamic shared memory needed to communicate additional context */
if (ExecInitParallelPlan_hook)
{
(*ExecInitParallelPlan_hook)(estate, pcxt, true);
}

/*
* Give parallel-aware nodes a chance to add to the estimates, and get a
* count of how many PlanState nodes there are.
Expand Down Expand Up @@ -773,6 +782,12 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate,
shm_toc_insert(pcxt->toc, PARALLEL_KEY_WAL_USAGE, walusage_space);
pei->wal_usage = walusage_space;

/* Give extension a chance to share additional context */
if (ExecInitParallelPlan_hook)
{
(*ExecInitParallelPlan_hook)(estate, pcxt,false);
}

/* Set up the tuple queues that the workers will write into. */
pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);

Expand Down Expand Up @@ -1433,6 +1448,12 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
area = dsa_attach_in_place(area_space, seg);

/* Give extension chance to retrieve additional context shared by leader node */
if (ParallelQueryMain_hook)
{
(*ParallelQueryMain_hook)(toc);
}

/* Start up the executor */
queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
ExecutorStart(queryDesc, fpes->eflags);
Expand Down
4 changes: 0 additions & 4 deletions src/include/access/parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,11 @@ extern void ParallelWorkerMain(Datum main_arg);
/* Below helpers are added to support parallel workers in Babelfish context */
extern bool IsBabelfishParallelWorker(void);

/* Key for BabelfishFixedParallelState */
#define BABELFISH_PARALLEL_KEY_FIXED UINT64CONST(0xBBF0000000000001)

/* Hooks for communicating babelfish related information to parallel worker */
typedef void (*bbf_InitializeParallelDSM_hook_type)(ParallelContext *pcxt, bool estimate);
extern PGDLLIMPORT bbf_InitializeParallelDSM_hook_type bbf_InitializeParallelDSM_hook;

typedef void (*bbf_ParallelWorkerMain_hook_type)(shm_toc *toc);
extern PGDLLIMPORT bbf_ParallelWorkerMain_hook_type bbf_ParallelWorkerMain_hook;


#endif /* PARALLEL_H */
11 changes: 11 additions & 0 deletions src/include/executor/execParallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ extern void ExecParallelReinitialize(PlanState *planstate,

extern void ParallelQueryMain(dsm_segment *seg, shm_toc *toc);

typedef void (*ParallelQueryMain_hook_type)(shm_toc *toc);
extern PGDLLIMPORT ParallelQueryMain_hook_type ParallelQueryMain_hook;

/*
* When estimate = true passed then caller wants extension to estimate a dynamic shared memory (DSM)
* needed by that extension to communicate additional context with Parallel worker.
* When estimate = false then caller wants to insert additional context to DSM.
*/
typedef void (*ExecInitParallelPlan_hook_type)(EState *estate, ParallelContext *pcxt, bool estimate);
extern PGDLLIMPORT ExecInitParallelPlan_hook_type ExecInitParallelPlan_hook;

#endif /* EXECPARALLEL_H */
5 changes: 5 additions & 0 deletions src/include/executor/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ extern PGDLLIMPORT ExecUpdateResultTypeTL_hook_type ExecUpdateResultTypeTL_hook;
typedef bool (*check_rowcount_hook_type) (int es_processed);
extern PGDLLEXPORT check_rowcount_hook_type check_rowcount_hook;

typedef bool (*ExecCheckOneRelPerms_hook_type) (RTEPermissionInfo *perminfo);
extern PGDLLEXPORT ExecCheckOneRelPerms_hook_type ExecCheckOneRelPerms_hook;

/*
* prototypes from functions in execAmi.c
*/
Expand Down Expand Up @@ -224,6 +227,8 @@ extern void standard_ExecutorEnd(QueryDesc *queryDesc);
extern void ExecutorRewind(QueryDesc *queryDesc);
extern bool ExecCheckPermissions(List *rangeTable,
List *rteperminfos, bool ereport_on_violation);
/* Wrapper around ExecCheckOneRelPerms */
extern bool ExecCheckOneRelPerms_wrapper(RTEPermissionInfo *perminfo);
extern void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation,
List *mergeActions);
extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
Expand Down