1616#include "miscadmin.h"
1717
1818#include "access/htup_details.h"
19+ #include "access/parallel.h"
1920#include "access/reloptions.h"
2021#include "access/sysattr.h"
2122#include "access/xact.h"
8384
8485PG_MODULE_MAGIC ;
8586
87+ #define NOT_IN_PARALLEL_WORKER (ParallelWorkerNumber < 0)
88+
8689#if PG_VERSION_NUM >= 140000
8790#define STMT_OBJTYPE (stmt ) stmt->objtype
8891#else
@@ -235,6 +238,8 @@ _PG_init(void)
235238{
236239 elog (DEBUG1 , "_PG_init()" );
237240
241+ if (ParallelWorkerNumber >= 0 )
242+ return ;
238243 /*
239244 * If we are loaded via shared_preload_libraries exit.
240245 */
@@ -349,7 +354,7 @@ gtt_ProcessUtility(GTT_PROCESSUTILITY_PROTO)
349354 elog (DEBUG1 , "gtt_ProcessUtility()" );
350355
351356 /* Do not waste time here if the feature is not enabled for this session */
352- if (pgtt_is_enabled )
357+ if (pgtt_is_enabled && NOT_IN_PARALLEL_WORKER )
353358 {
354359 /*
355360 * Be sure that extension schema is at end of the search path so that
@@ -388,6 +393,8 @@ gtt_ProcessUtility(GTT_PROCESSUTILITY_PROTO)
388393 PG_RE_THROW ();
389394 }
390395 PG_END_TRY ();
396+
397+ elog (DEBUG1 , "End of gtt_ProcessUtility()" );
391398}
392399
393400/*
@@ -939,7 +946,7 @@ gtt_ExecutorStart(QueryDesc *queryDesc, int eflags)
939946 elog (DEBUG1 , "gtt_ExecutorStart()" );
940947
941948 /* Do not waste time here if the feature is not enabled for this session */
942- if (pgtt_is_enabled )
949+ if (pgtt_is_enabled && NOT_IN_PARALLEL_WORKER )
943950 {
944951
945952 /* check if we are working on a GTT and create it if it doesn't exist */
@@ -961,13 +968,16 @@ gtt_ExecutorStart(QueryDesc *queryDesc, int eflags)
961968 prev_ExecutorStart (queryDesc , eflags );
962969 else
963970 standard_ExecutorStart (queryDesc , eflags );
971+
972+ elog (DEBUG1 , "End of gtt_ExecutorStart()" );
964973}
965974
966975static bool
967976gtt_table_exists (QueryDesc * queryDesc )
968977{
969978 bool is_gtt = false;
970979 char * name = NULL ;
980+ char relpersistence ;
971981 RangeTblEntry * rte ;
972982 Relation rel ;
973983 Gtt gtt ;
@@ -980,24 +990,28 @@ gtt_table_exists(QueryDesc *queryDesc)
980990 if (list_length (pstmt -> rtable ) == 0 )
981991 return false;
982992
983- /* This must be a valid relation */
993+ /* This must be a valid relation and not a temporary table */
984994 rte = (RangeTblEntry * ) linitial (pstmt -> rtable );
985- if (rte -> relid != InvalidOid )
995+ if (rte -> relid != InvalidOid && rte -> relkind == RELKIND_RELATION
996+ && !is_catalog_relid (rte -> relid ))
986997 {
987- Assert (rte -> relkind == RELKIND_RELATION );
988-
989998#if (PG_VERSION_NUM >= 120000 )
990999 rel = table_open (rte -> relid , NoLock );
9911000#else
9921001 rel = heap_open (rte -> relid , NoLock );
9931002#endif
9941003 name = RelationGetRelationName (rel );
1004+ relpersistence = rel -> rd_rel -> relpersistence ;
9951005#if (PG_VERSION_NUM >= 120000 )
9961006 table_close (rel , NoLock );
9971007#else
9981008 heap_close (rel , NoLock );
9991009#endif
10001010
1011+ /* Do not go further with temporary tables, catalog or toast table */
1012+ if (relpersistence != RELPERSISTENCE_TEMP )
1013+ return false;
1014+
10011015 gtt .relid = 0 ;
10021016 gtt .temp_relid = 0 ;
10031017 gtt .relname [0 ] = '\0' ;
@@ -1615,7 +1629,7 @@ gtt_post_parse_analyze(ParseState *pstate, Query *query, struct JumbleState * js
16151629gtt_post_parse_analyze (ParseState * pstate , Query * query )
16161630#endif
16171631{
1618- if (pgtt_is_enabled && query -> rtable != NIL )
1632+ if (NOT_IN_PARALLEL_WORKER && pgtt_is_enabled && query -> rtable != NIL )
16191633 {
16201634 /* replace the Oid of the template table by our new table in the rtable */
16211635 RangeTblEntry * rte = (RangeTblEntry * ) linitial (query -> rtable );
@@ -1720,9 +1734,9 @@ is_catalog_relid(Oid relid)
17201734 relform = (Form_pg_class ) GETSTRUCT (reltup );
17211735 relnamespace = relform -> relnamespace ;
17221736 ReleaseSysCache (reltup );
1723- if (relnamespace == PG_CATALOG_NAMESPACE )
1737+ if (relnamespace == PG_CATALOG_NAMESPACE || relnamespace == PG_TOAST_NAMESPACE )
17241738 {
1725- elog (DEBUG1 , "relation %d is in pg_catalog schema, nothing to do." , relid );
1739+ elog (DEBUG1 , "relation %d is in pg_catalog or pg_toast schema, nothing to do." , relid );
17261740 return true;
17271741 }
17281742
0 commit comments