From 0385efab8355eb42120380e298ca34b35331bfac Mon Sep 17 00:00:00 2001 From: Shameem Ahmed Date: Wed, 10 Jan 2024 14:26:25 +0000 Subject: [PATCH 1/5] [BABEL-4538] Fix database does not exists error when is_member(), schema_id() function gets called in parallel query mode. Signed-off-by: Shameem Ahmed --- src/backend/access/transam/parallel.c | 21 +++++++++++++++++++++ src/include/access/parallel.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 2c14f3b17d0..dd89ef01a27 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -77,6 +77,10 @@ #define PARALLEL_KEY_RELMAPPER_STATE UINT64CONST(0xFFFFFFFFFFFF000D) #define PARALLEL_KEY_UNCOMMITTEDENUMS UINT64CONST(0xFFFFFFFFFFFF000E) +/* Hooks for communicating babelfish related information to parallel worker */ +bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook_type bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook; +bbf_parallel_restore_babelfishfixedparallelstate_hook_type bbf_parallel_restore_babelfishfixedparallelstate_hook; + /* Fixed-size parallel state. */ typedef struct FixedParallelState { @@ -291,8 +295,14 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_estimate_chunk(&pcxt->estimator, strlen(pcxt->library_name) + strlen(pcxt->function_name) + 2); shm_toc_estimate_keys(&pcxt->estimator, 1); + + /* Estimate how much we'll need for the babelfish fixed parallel state */ + if (MyProcPort->is_tds_conn && bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) + (*bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) (pcxt, true); } + + /* * Create DSM and initialize with new table of contents. But if the user * didn't request any workers, then don't bother creating a dynamic shared @@ -465,7 +475,12 @@ InitializeParallelDSM(ParallelContext *pcxt) strcpy(entrypointstate, pcxt->library_name); strcpy(entrypointstate + lnamelen + 1, pcxt->function_name); shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate); + + /* Initialize babelfish fixed-size state in shared memory. */ + if (MyProcPort->is_tds_conn && bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) + (*bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) (pcxt, false); } + /* Restore previous memory context. */ MemoryContextSwitchTo(oldcontext); @@ -1486,6 +1501,12 @@ ParallelWorkerMain(Datum main_arg) /* Attach to the leader's serializable transaction, if SERIALIZABLE. */ AttachSerializableXact(fps->serializable_xact_handle); + + /* Hook for babelfish to restore babelfish fixed parallel state */ + if (MyFixedParallelState->babelfish_context && bbf_parallel_restore_babelfishfixedparallelstate_hook) + (*bbf_parallel_restore_babelfishfixedparallelstate_hook) (toc); + + /* * We've initialized all of our state now; nothing should change * hereafter. diff --git a/src/include/access/parallel.h b/src/include/access/parallel.h index 0d124da351a..fbf90c9d1fe 100644 --- a/src/include/access/parallel.h +++ b/src/include/access/parallel.h @@ -82,4 +82,12 @@ extern void ParallelWorkerMain(Datum main_arg); /* Below helpers are added to support parallel workers in Babelfish context */ extern bool IsBabelfishParallelWorker(void); +/* Hooks for communicating babelfish related information to parallel worker */ +typedef void (*bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook_type)(ParallelContext *pcxt, bool estimate); +extern PGDLLIMPORT bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook_type bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook; + +typedef void (*bbf_parallel_restore_babelfishfixedparallelstate_hook_type)(shm_toc *toc); +extern PGDLLIMPORT bbf_parallel_restore_babelfishfixedparallelstate_hook_type bbf_parallel_restore_babelfishfixedparallelstate_hook; + + #endif /* PARALLEL_H */ From cdffd626d539dac1c1b058180dfd7d2d228e9118 Mon Sep 17 00:00:00 2001 From: Shameem Ahmed Date: Fri, 12 Jan 2024 10:44:08 +0000 Subject: [PATCH 2/5] Modify hooks names Signed-off-by: Shameem Ahmed --- src/backend/access/transam/parallel.c | 16 ++++++++-------- src/include/access/parallel.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index dd89ef01a27..23c83ca72da 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -78,8 +78,8 @@ #define PARALLEL_KEY_UNCOMMITTEDENUMS UINT64CONST(0xFFFFFFFFFFFF000E) /* Hooks for communicating babelfish related information to parallel worker */ -bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook_type bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook; -bbf_parallel_restore_babelfishfixedparallelstate_hook_type bbf_parallel_restore_babelfishfixedparallelstate_hook; +babelfixedparallelstate_insert_hook_type babelfixedparallelstate_insert_hook; +babelfixedparallelstate_restore_hook_type babelfixedparallelstate_restore_hook; /* Fixed-size parallel state. */ typedef struct FixedParallelState @@ -297,8 +297,8 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_estimate_keys(&pcxt->estimator, 1); /* Estimate how much we'll need for the babelfish fixed parallel state */ - if (MyProcPort->is_tds_conn && bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) - (*bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) (pcxt, true); + if (MyProcPort->is_tds_conn && babelfixedparallelstate_insert_hook) + (*babelfixedparallelstate_insert_hook) (pcxt, true); } @@ -477,8 +477,8 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate); /* Initialize babelfish fixed-size state in shared memory. */ - if (MyProcPort->is_tds_conn && bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) - (*bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook) (pcxt, false); + if (MyProcPort->is_tds_conn && babelfixedparallelstate_insert_hook) + (*babelfixedparallelstate_insert_hook) (pcxt, false); } @@ -1503,8 +1503,8 @@ ParallelWorkerMain(Datum main_arg) /* Hook for babelfish to restore babelfish fixed parallel state */ - if (MyFixedParallelState->babelfish_context && bbf_parallel_restore_babelfishfixedparallelstate_hook) - (*bbf_parallel_restore_babelfishfixedparallelstate_hook) (toc); + if (MyFixedParallelState->babelfish_context && babelfixedparallelstate_restore_hook) + (*babelfixedparallelstate_restore_hook) (toc); /* diff --git a/src/include/access/parallel.h b/src/include/access/parallel.h index fbf90c9d1fe..2a24cad72b9 100644 --- a/src/include/access/parallel.h +++ b/src/include/access/parallel.h @@ -83,11 +83,11 @@ extern void ParallelWorkerMain(Datum main_arg); extern bool IsBabelfishParallelWorker(void); /* Hooks for communicating babelfish related information to parallel worker */ -typedef void (*bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook_type)(ParallelContext *pcxt, bool estimate); -extern PGDLLIMPORT bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook_type bbf_parallel_serialise_babelfixedparallelstate_and_insert_into_dsm_hook; +typedef void (*babelfixedparallelstate_insert_hook_type)(ParallelContext *pcxt, bool estimate); +extern PGDLLIMPORT babelfixedparallelstate_insert_hook_type babelfixedparallelstate_insert_hook; -typedef void (*bbf_parallel_restore_babelfishfixedparallelstate_hook_type)(shm_toc *toc); -extern PGDLLIMPORT bbf_parallel_restore_babelfishfixedparallelstate_hook_type bbf_parallel_restore_babelfishfixedparallelstate_hook; +typedef void (*babelfixedparallelstate_restore_hook_type)(shm_toc *toc); +extern PGDLLIMPORT babelfixedparallelstate_restore_hook_type babelfixedparallelstate_restore_hook; #endif /* PARALLEL_H */ From d22064736fe43a1cdb55d7ff95a7ac11120c0e6f Mon Sep 17 00:00:00 2001 From: Shameem Ahmed Date: Fri, 12 Jan 2024 14:30:14 +0000 Subject: [PATCH 3/5] Address review comments Signed-off-by: Shameem Ahmed --- src/backend/access/transam/parallel.c | 24 ++++++++++-------------- src/include/access/parallel.h | 8 ++++---- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 23c83ca72da..55f08cd926c 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -78,8 +78,8 @@ #define PARALLEL_KEY_UNCOMMITTEDENUMS UINT64CONST(0xFFFFFFFFFFFF000E) /* Hooks for communicating babelfish related information to parallel worker */ -babelfixedparallelstate_insert_hook_type babelfixedparallelstate_insert_hook; -babelfixedparallelstate_restore_hook_type babelfixedparallelstate_restore_hook; +InitializeParallelDSM_hook_type InitializeParallelDSM_hook; +ParallelWorkerMain_hook_type ParallelWorkerMain_hook; /* Fixed-size parallel state. */ typedef struct FixedParallelState @@ -297,12 +297,10 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_estimate_keys(&pcxt->estimator, 1); /* Estimate how much we'll need for the babelfish fixed parallel state */ - if (MyProcPort->is_tds_conn && babelfixedparallelstate_insert_hook) - (*babelfixedparallelstate_insert_hook) (pcxt, true); + if (MyProcPort->is_tds_conn && InitializeParallelDSM_hook) + (*InitializeParallelDSM_hook) (pcxt, true); } - - /* * Create DSM and initialize with new table of contents. But if the user * didn't request any workers, then don't bother creating a dynamic shared @@ -477,8 +475,8 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate); /* Initialize babelfish fixed-size state in shared memory. */ - if (MyProcPort->is_tds_conn && babelfixedparallelstate_insert_hook) - (*babelfixedparallelstate_insert_hook) (pcxt, false); + if (MyProcPort->is_tds_conn && InitializeParallelDSM_hook) + (*InitializeParallelDSM_hook) (pcxt, false); } @@ -1493,6 +1491,10 @@ ParallelWorkerMain(Datum main_arg) relmapperspace = shm_toc_lookup(toc, PARALLEL_KEY_RELMAPPER_STATE, false); RestoreRelationMap(relmapperspace); + /* Hook for babelfish to restore babelfish fixed parallel state */ + if (MyFixedParallelState->babelfish_context && ParallelWorkerMain_hook) + (*ParallelWorkerMain_hook) (toc); + /* Restore uncommitted enums. */ uncommittedenumsspace = shm_toc_lookup(toc, PARALLEL_KEY_UNCOMMITTEDENUMS, false); @@ -1501,12 +1503,6 @@ ParallelWorkerMain(Datum main_arg) /* Attach to the leader's serializable transaction, if SERIALIZABLE. */ AttachSerializableXact(fps->serializable_xact_handle); - - /* Hook for babelfish to restore babelfish fixed parallel state */ - if (MyFixedParallelState->babelfish_context && babelfixedparallelstate_restore_hook) - (*babelfixedparallelstate_restore_hook) (toc); - - /* * We've initialized all of our state now; nothing should change * hereafter. diff --git a/src/include/access/parallel.h b/src/include/access/parallel.h index 2a24cad72b9..b79862db7ab 100644 --- a/src/include/access/parallel.h +++ b/src/include/access/parallel.h @@ -83,11 +83,11 @@ extern void ParallelWorkerMain(Datum main_arg); extern bool IsBabelfishParallelWorker(void); /* Hooks for communicating babelfish related information to parallel worker */ -typedef void (*babelfixedparallelstate_insert_hook_type)(ParallelContext *pcxt, bool estimate); -extern PGDLLIMPORT babelfixedparallelstate_insert_hook_type babelfixedparallelstate_insert_hook; +typedef void (*InitializeParallelDSM_hook_type)(ParallelContext *pcxt, bool estimate); +extern PGDLLIMPORT InitializeParallelDSM_hook_type InitializeParallelDSM_hook; -typedef void (*babelfixedparallelstate_restore_hook_type)(shm_toc *toc); -extern PGDLLIMPORT babelfixedparallelstate_restore_hook_type babelfixedparallelstate_restore_hook; +typedef void (*ParallelWorkerMain_hook_type)(shm_toc *toc); +extern PGDLLIMPORT ParallelWorkerMain_hook_type ParallelWorkerMain_hook; #endif /* PARALLEL_H */ From 6888ab227e0489fa79444fe5d5af6fb06bff39c3 Mon Sep 17 00:00:00 2001 From: Shameem Ahmed Date: Mon, 15 Jan 2024 14:36:32 +0000 Subject: [PATCH 4/5] Address review comments Signed-off-by: Shameem Ahmed --- src/backend/access/transam/parallel.c | 17 ++++++++--------- src/include/access/parallel.h | 8 ++++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 55f08cd926c..5bc309804be 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -78,8 +78,8 @@ #define PARALLEL_KEY_UNCOMMITTEDENUMS UINT64CONST(0xFFFFFFFFFFFF000E) /* Hooks for communicating babelfish related information to parallel worker */ -InitializeParallelDSM_hook_type InitializeParallelDSM_hook; -ParallelWorkerMain_hook_type ParallelWorkerMain_hook; +bbf_InitializeParallelDSM_hook_type bbf_InitializeParallelDSM_hook = NULL; +bbf_ParallelWorkerMain_hook_type bbf_ParallelWorkerMain_hook = NULL; /* Fixed-size parallel state. */ typedef struct FixedParallelState @@ -297,8 +297,8 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_estimate_keys(&pcxt->estimator, 1); /* Estimate how much we'll need for the babelfish fixed parallel state */ - if (MyProcPort->is_tds_conn && InitializeParallelDSM_hook) - (*InitializeParallelDSM_hook) (pcxt, true); + if (MyProcPort->is_tds_conn && bbf_InitializeParallelDSM_hook) + (*bbf_InitializeParallelDSM_hook) (pcxt, true); } /* @@ -475,10 +475,9 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate); /* Initialize babelfish fixed-size state in shared memory. */ - if (MyProcPort->is_tds_conn && InitializeParallelDSM_hook) - (*InitializeParallelDSM_hook) (pcxt, false); + if (MyProcPort->is_tds_conn && bbf_InitializeParallelDSM_hook) + (*bbf_InitializeParallelDSM_hook) (pcxt, false); } - /* Restore previous memory context. */ MemoryContextSwitchTo(oldcontext); @@ -1492,8 +1491,8 @@ ParallelWorkerMain(Datum main_arg) RestoreRelationMap(relmapperspace); /* Hook for babelfish to restore babelfish fixed parallel state */ - if (MyFixedParallelState->babelfish_context && ParallelWorkerMain_hook) - (*ParallelWorkerMain_hook) (toc); + if (MyFixedParallelState->babelfish_context && bbf_ParallelWorkerMain_hook) + (*bbf_ParallelWorkerMain_hook) (toc); /* Restore uncommitted enums. */ uncommittedenumsspace = shm_toc_lookup(toc, PARALLEL_KEY_UNCOMMITTEDENUMS, diff --git a/src/include/access/parallel.h b/src/include/access/parallel.h index b79862db7ab..fcd8360011f 100644 --- a/src/include/access/parallel.h +++ b/src/include/access/parallel.h @@ -83,11 +83,11 @@ extern void ParallelWorkerMain(Datum main_arg); extern bool IsBabelfishParallelWorker(void); /* Hooks for communicating babelfish related information to parallel worker */ -typedef void (*InitializeParallelDSM_hook_type)(ParallelContext *pcxt, bool estimate); -extern PGDLLIMPORT InitializeParallelDSM_hook_type InitializeParallelDSM_hook; +typedef void (*bbf_InitializeParallelDSM_hook_type)(ParallelContext *pcxt, bool estimate); +extern PGDLLIMPORT bbf_InitializeParallelDSM_hook_type bbf_InitializeParallelDSM_hook; -typedef void (*ParallelWorkerMain_hook_type)(shm_toc *toc); -extern PGDLLIMPORT ParallelWorkerMain_hook_type ParallelWorkerMain_hook; +typedef void (*bbf_ParallelWorkerMain_hook_type)(shm_toc *toc); +extern PGDLLIMPORT bbf_ParallelWorkerMain_hook_type bbf_ParallelWorkerMain_hook; #endif /* PARALLEL_H */ From 30feba2ce5d7ef7f4bedfcf1849b9240bbcbf52b Mon Sep 17 00:00:00 2001 From: Shameem Ahmed Date: Tue, 16 Jan 2024 10:18:28 +0000 Subject: [PATCH 5/5] Address review comments Signed-off-by: Shameem Ahmed --- src/backend/access/transam/parallel.c | 8 ++++---- src/include/access/parallel.h | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 5bc309804be..96f973a7dec 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -1490,15 +1490,15 @@ ParallelWorkerMain(Datum main_arg) relmapperspace = shm_toc_lookup(toc, PARALLEL_KEY_RELMAPPER_STATE, false); RestoreRelationMap(relmapperspace); - /* Hook for babelfish to restore babelfish fixed parallel state */ - if (MyFixedParallelState->babelfish_context && bbf_ParallelWorkerMain_hook) - (*bbf_ParallelWorkerMain_hook) (toc); - /* Restore uncommitted enums. */ uncommittedenumsspace = shm_toc_lookup(toc, PARALLEL_KEY_UNCOMMITTEDENUMS, false); RestoreUncommittedEnums(uncommittedenumsspace); + /* Hook for babelfish to restore babelfish fixed parallel state */ + if (MyFixedParallelState->babelfish_context && bbf_ParallelWorkerMain_hook) + (*bbf_ParallelWorkerMain_hook) (toc); + /* Attach to the leader's serializable transaction, if SERIALIZABLE. */ AttachSerializableXact(fps->serializable_xact_handle); diff --git a/src/include/access/parallel.h b/src/include/access/parallel.h index fcd8360011f..9126fd0e5da 100644 --- a/src/include/access/parallel.h +++ b/src/include/access/parallel.h @@ -82,6 +82,9 @@ 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;