Skip to content

Commit d4d3cd8

Browse files
authored
Avoid checking permission of Babelfish temp tables on parallel worker (#560) (#567)
Consider following facts, 1. Babelfish temp tables are implemented using ENR which is not shared between different backends. So if Parallel worker tries to check permissions on Babelfish then it will fail. 2. Any user should be able to access Babelfish temp tables under given session. 3. Postgres by default does not allow parallel operations on temp tables. Attempt to do so will result in run time error. Due to above facts, we should avoid permission check on temp tables within parallel workers while ensuring that leader does required permission check on other tables. This commits achieves this behaviour by introducing following three hooks, ParallelQueryMain_hook -- Hook that allows other extensions to pass on additional details from Leader node to parallel worker. For example, Babelfish extension can pass details of Babelfish temp table defined under current session with Parallel workers. ExecInitParallelPlan_hook -- Hook that allows Parallel worker to gather additional details passed by Leader node. For example, Babelfish extension can collect the details of Babelfish temp table shared by Leader node so that it can avoid permission checks. ExecCheckOneRelPerms_hook -- Hook that allows extension control permission checking on given relation/table. For example, Babelfish can use it to avoid permission check on temp tables under parallel worker. Task: BABEL-5703 Signed-off-by: Dipesh Dhameliya <[email protected]> (cherry picked from commit 5e62ffa)
1 parent 2220872 commit d4d3cd8

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

src/backend/executor/execMain.c

+26-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
7878

7979
TriggerRecuresiveCheck_hook_type TriggerRecuresiveCheck_hook = NULL;
8080
check_rowcount_hook_type check_rowcount_hook = NULL;
81+
ExecCheckOneRelPerms_hook_type ExecCheckOneRelPerms_hook = NULL;
82+
8183
/* Hook for plugin to get control in ExecCheckPermissions() */
8284
ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook = NULL;
8385

@@ -650,6 +652,19 @@ ExecCheckOneRelPerms(RTEPermissionInfo *perminfo)
650652
requiredPerms = perminfo->requiredPerms;
651653
Assert(requiredPerms != 0);
652654

655+
/*
656+
* Babelfish specific logic - Babelfish temp table is implemented
657+
* using ENR which is not shared with parallel worker and parallel
658+
* operations are not allowed for temp table in Postgres. Babelfish
659+
* can skip permission check for such use cases under parallel worker
660+
* using this hook.
661+
* Note - This hook must not be used outside of Babelfish parallel worker
662+
*/
663+
if (ExecCheckOneRelPerms_hook &&
664+
IsBabelfishParallelWorker() &&
665+
(*ExecCheckOneRelPerms_hook)(perminfo))
666+
return true;
667+
653668
/*
654669
* userid to check as: current user unless we have a setuid indication.
655670
*
@@ -842,10 +857,9 @@ InitPlan(QueryDesc *queryDesc, int eflags)
842857
int i;
843858

844859
/*
845-
* Do permissions checks if not Babelfish parallel worker
860+
* Do permissions checks
846861
*/
847-
if (!IsBabelfishParallelWorker())
848-
ExecCheckPermissions(rangeTable, plannedstmt->permInfos, true);
862+
ExecCheckPermissions(rangeTable, plannedstmt->permInfos, true);
849863

850864
/*
851865
* initialize the node's execution state
@@ -3066,3 +3080,12 @@ EvalPlanQualEnd(EPQState *epqstate)
30663080
epqstate->relsubs_done = NULL;
30673081
epqstate->relsubs_blocked = NULL;
30683082
}
3083+
3084+
/*
3085+
* ExecCheckOneRelPerms_wrapper - wrapper around ExecCheckOneRelPerms
3086+
*/
3087+
bool
3088+
ExecCheckOneRelPerms_wrapper(RTEPermissionInfo *perminfo)
3089+
{
3090+
return ExecCheckOneRelPerms(perminfo);
3091+
}

src/backend/executor/execParallel.c

+19
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ typedef struct ExecParallelInitializeDSMContext
124124
int nnodes;
125125
} ExecParallelInitializeDSMContext;
126126

127+
ExecInitParallelPlan_hook_type ExecInitParallelPlan_hook = NULL;
128+
ParallelQueryMain_hook_type ParallelQueryMain_hook = NULL;
129+
127130
/* Helper functions that run in the parallel leader. */
128131
static char *ExecSerializePlan(Plan *plan, EState *estate);
129132
static bool ExecParallelEstimate(PlanState *planstate,
@@ -685,6 +688,10 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate,
685688
mul_size(PARALLEL_TUPLE_QUEUE_SIZE, pcxt->nworkers));
686689
shm_toc_estimate_keys(&pcxt->estimator, 1);
687690

691+
/* Let extension estimate a dynamic shared memory needed to communicate additional context */
692+
if (ExecInitParallelPlan_hook)
693+
(*ExecInitParallelPlan_hook)(estate, pcxt, true);
694+
688695
/*
689696
* Give parallel-aware nodes a chance to add to the estimates, and get a
690697
* count of how many PlanState nodes there are.
@@ -768,6 +775,12 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate,
768775
shm_toc_insert(pcxt->toc, PARALLEL_KEY_WAL_USAGE, walusage_space);
769776
pei->wal_usage = walusage_space;
770777

778+
/* Give extension a chance to share additional context */
779+
if (ExecInitParallelPlan_hook)
780+
{
781+
(*ExecInitParallelPlan_hook)(estate, pcxt,false);
782+
}
783+
771784
/* Set up the tuple queues that the workers will write into. */
772785
pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
773786

@@ -1428,6 +1441,12 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
14281441
area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
14291442
area = dsa_attach_in_place(area_space, seg);
14301443

1444+
/* Give extension chance to retrieve additional context shared by leader node */
1445+
if (ParallelQueryMain_hook)
1446+
{
1447+
(*ParallelQueryMain_hook)(toc);
1448+
}
1449+
14311450
/* Start up the executor */
14321451
queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
14331452
ExecutorStart(queryDesc, fpes->eflags);

src/include/access/parallel.h

-4
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,11 @@ extern void ParallelWorkerMain(Datum main_arg);
8282
/* Below helpers are added to support parallel workers in Babelfish context */
8383
extern bool IsBabelfishParallelWorker(void);
8484

85-
/* Key for BabelfishFixedParallelState */
86-
#define BABELFISH_PARALLEL_KEY_FIXED UINT64CONST(0xBBF0000000000001)
87-
8885
/* Hooks for communicating babelfish related information to parallel worker */
8986
typedef void (*bbf_InitializeParallelDSM_hook_type)(ParallelContext *pcxt, bool estimate);
9087
extern PGDLLIMPORT bbf_InitializeParallelDSM_hook_type bbf_InitializeParallelDSM_hook;
9188

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

95-
9692
#endif /* PARALLEL_H */

src/include/executor/execParallel.h

+11
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ extern void ExecParallelReinitialize(PlanState *planstate,
4848

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

51+
typedef void (*ParallelQueryMain_hook_type)(shm_toc *toc);
52+
extern PGDLLIMPORT ParallelQueryMain_hook_type ParallelQueryMain_hook;
53+
54+
/*
55+
* When estimate = true passed then caller wants extension to estimate a dynamic shared memory (DSM)
56+
* needed by that extension to communicate additional context with Parallel worker.
57+
* When estimate = false then caller wants to insert additional context to DSM.
58+
*/
59+
typedef void (*ExecInitParallelPlan_hook_type)(EState *estate, ParallelContext *pcxt, bool estimate);
60+
extern PGDLLIMPORT ExecInitParallelPlan_hook_type ExecInitParallelPlan_hook;
61+
5162
#endif /* EXECPARALLEL_H */

src/include/executor/executor.h

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ extern PGDLLIMPORT ExecUpdateResultTypeTL_hook_type ExecUpdateResultTypeTL_hook;
111111
typedef bool (*check_rowcount_hook_type) (int es_processed);
112112
extern PGDLLEXPORT check_rowcount_hook_type check_rowcount_hook;
113113

114+
typedef bool (*ExecCheckOneRelPerms_hook_type) (RTEPermissionInfo *perminfo);
115+
extern PGDLLEXPORT ExecCheckOneRelPerms_hook_type ExecCheckOneRelPerms_hook;
116+
114117
/*
115118
* prototypes from functions in execAmi.c
116119
*/
@@ -224,6 +227,8 @@ extern void standard_ExecutorEnd(QueryDesc *queryDesc);
224227
extern void ExecutorRewind(QueryDesc *queryDesc);
225228
extern bool ExecCheckPermissions(List *rangeTable,
226229
List *rteperminfos, bool ereport_on_violation);
230+
/* Wrapper around ExecCheckOneRelPerms */
231+
extern bool ExecCheckOneRelPerms_wrapper(RTEPermissionInfo *perminfo);
227232
extern void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation);
228233
extern void InitResultRelInfo(ResultRelInfo *resultRelInfo,
229234
Relation resultRelationDesc,

0 commit comments

Comments
 (0)