Skip to content

Commit

Permalink
Merge branch 'master' of github.com:darold/pgtt
Browse files Browse the repository at this point in the history
  • Loading branch information
darold committed Nov 23, 2021
2 parents 9df01af + 99391ee commit 544744f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion COPYING
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2018-2020 Gilles Darold
Copyright (c) 2018-2021 Gilles Darold

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,5 @@ [email protected]
This extension is free software distributed under the PostgreSQL
Licence.

Copyright (c) 2018-2020, Gilles Darold
Copyright (c) 2018-2021, Gilles Darold

34 changes: 24 additions & 10 deletions pgtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Author: Gilles Darold <[email protected]>
* Licence: PostgreSQL
* Copyright (c) 2018-2020, Gilles Darold,
* Copyright (c) 2018-2021, Gilles Darold,
*
*-------------------------------------------------------------------------
*/
Expand All @@ -16,6 +16,7 @@
#include "miscadmin.h"

#include "access/htup_details.h"
#include "access/parallel.h"
#include "access/reloptions.h"
#include "access/sysattr.h"
#include "access/xact.h"
Expand Down Expand Up @@ -83,6 +84,8 @@

PG_MODULE_MAGIC;

#define NOT_IN_PARALLEL_WORKER (ParallelWorkerNumber < 0)

#if PG_VERSION_NUM >= 140000
#define STMT_OBJTYPE(stmt) stmt->objtype
#else
Expand Down Expand Up @@ -235,6 +238,8 @@ _PG_init(void)
{
elog(DEBUG1, "_PG_init()");

if (ParallelWorkerNumber >= 0)
return;
/*
* If we are loaded via shared_preload_libraries exit.
*/
Expand Down Expand Up @@ -349,7 +354,7 @@ gtt_ProcessUtility(GTT_PROCESSUTILITY_PROTO)
elog(DEBUG1, "gtt_ProcessUtility()");

/* Do not waste time here if the feature is not enabled for this session */
if (pgtt_is_enabled)
if (pgtt_is_enabled && NOT_IN_PARALLEL_WORKER)
{
/*
* Be sure that extension schema is at end of the search path so that
Expand Down Expand Up @@ -388,6 +393,8 @@ gtt_ProcessUtility(GTT_PROCESSUTILITY_PROTO)
PG_RE_THROW();
}
PG_END_TRY();

elog(DEBUG1, "End of gtt_ProcessUtility()");
}

/*
Expand Down Expand Up @@ -939,7 +946,7 @@ gtt_ExecutorStart(QueryDesc *queryDesc, int eflags)
elog(DEBUG1, "gtt_ExecutorStart()");

/* Do not waste time here if the feature is not enabled for this session */
if (pgtt_is_enabled)
if (pgtt_is_enabled && NOT_IN_PARALLEL_WORKER)
{

/* check if we are working on a GTT and create it if it doesn't exist */
Expand All @@ -961,13 +968,16 @@ gtt_ExecutorStart(QueryDesc *queryDesc, int eflags)
prev_ExecutorStart(queryDesc, eflags);
else
standard_ExecutorStart(queryDesc, eflags);

elog(DEBUG1, "End of gtt_ExecutorStart()");
}

static bool
gtt_table_exists(QueryDesc *queryDesc)
{
bool is_gtt = false;
char *name = NULL;
char relpersistence;
RangeTblEntry *rte;
Relation rel;
Gtt gtt;
Expand All @@ -980,24 +990,28 @@ gtt_table_exists(QueryDesc *queryDesc)
if (list_length(pstmt->rtable) == 0)
return false;

/* This must be a valid relation */
/* This must be a valid relation and not a temporary table */
rte = (RangeTblEntry *) linitial(pstmt->rtable);
if (rte->relid != InvalidOid)
if (rte->relid != InvalidOid && rte->relkind == RELKIND_RELATION
&& !is_catalog_relid(rte->relid))
{
Assert(rte->relkind == RELKIND_RELATION);

#if (PG_VERSION_NUM >= 120000)
rel = table_open(rte->relid, NoLock);
#else
rel = heap_open(rte->relid, NoLock);
#endif
name = RelationGetRelationName(rel);
relpersistence = rel->rd_rel->relpersistence;
#if (PG_VERSION_NUM >= 120000)
table_close(rel, NoLock);
#else
heap_close(rel, NoLock);
#endif

/* Do not go further with temporary tables, catalog or toast table */
if (relpersistence != RELPERSISTENCE_TEMP)
return false;

gtt.relid = 0;
gtt.temp_relid = 0;
gtt.relname[0] = '\0';
Expand Down Expand Up @@ -1615,7 +1629,7 @@ gtt_post_parse_analyze(ParseState *pstate, Query *query, struct JumbleState * js
gtt_post_parse_analyze(ParseState *pstate, Query *query)
#endif
{
if (pgtt_is_enabled && query->rtable != NIL)
if (NOT_IN_PARALLEL_WORKER && pgtt_is_enabled && query->rtable != NIL)
{
/* replace the Oid of the template table by our new table in the rtable */
RangeTblEntry *rte = (RangeTblEntry *) linitial(query->rtable);
Expand Down Expand Up @@ -1720,9 +1734,9 @@ is_catalog_relid(Oid relid)
relform = (Form_pg_class) GETSTRUCT(reltup);
relnamespace = relform->relnamespace;
ReleaseSysCache(reltup);
if (relnamespace == PG_CATALOG_NAMESPACE)
if (relnamespace == PG_CATALOG_NAMESPACE || relnamespace == PG_TOAST_NAMESPACE)
{
elog(DEBUG1, "relation %d is in pg_catalog schema, nothing to do.", relid);
elog(DEBUG1, "relation %d is in pg_catalog or pg_toast schema, nothing to do.", relid);
return true;
}

Expand Down

0 comments on commit 544744f

Please sign in to comment.