Fix usage of include_tables/exclude_tables in case of preloaded libraries.
authorTeodor Sigaev <teodor@sigaev.ru>
Thu, 7 Oct 2021 15:35:33 +0000 (18:35 +0300)
committerTeodor Sigaev <teodor@sigaev.ru>
Thu, 7 Oct 2021 15:35:33 +0000 (18:35 +0300)
Code makes a catalog search while process is not yet connected
to database and fails. Now catalog lookups are delayed until
system catalog is accessible. The idea was taken from plantuner module.

online_analyze.c

index 1a50250..f5a36e5 100644 (file)
@@ -30,6 +30,7 @@
 #include "postgres.h"
 
 #include "pgstat.h"
+#include "miscadmin.h"
 #include "access/transam.h"
 #include "access/xact.h"
 #include "catalog/namespace.h"
@@ -121,10 +122,11 @@ typedef struct TableList {
        int             nTables;
        Oid             *tables;
        char    *tableStr;
+       bool    inited;
 } TableList;
 
-static TableList excludeTables = {0, NULL, NULL};
-static TableList includeTables = {0, NULL, NULL};
+static TableList excludeTables = {0, NULL, NULL, false};
+static TableList includeTables = {0, NULL, NULL, false};
 
 typedef struct OnlineAnalyzeTableStat {
        Oid                             tableid;
@@ -165,6 +167,18 @@ tableListAssign(const char * newval, bool doit, TableList *tbl)
        if (!SplitIdentifierString(rawname, ',', &namelist))
                goto cleanup;
 
+       /*
+       * follow work could be done only in normal processing because of
+       * accsess to system catalog
+       */
+       if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
+               !IsTransactionState())
+       {
+               includeTables.inited = false;
+               excludeTables.inited = false;
+               return newval;
+       }
+
        if (doit)
        {
                nOids = list_length(namelist);
@@ -291,6 +305,26 @@ includeTablesAssign(const char * newval, bool doit, GucSource source)
 
 #endif
 
+static void
+lateInit()
+{
+       TableList       *tl[] = {&includeTables, &excludeTables};
+       int i;
+
+       if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
+               !IsTransactionState())
+               return; /* we aren't in connected state */
+
+       for(i=0; i<lengthof(tl); i++)
+       {
+               TableList       *tbl = tl[i];
+
+               if (tbl->inited == false)
+                       tableListAssign(tbl->tableStr, true, tbl);
+               tbl->inited = true;
+       }
+}
+
 static const char*
 tableListShow(TableList *tbl)
 {
@@ -298,6 +332,8 @@ tableListShow(TableList *tbl)
        int             i,
                        len;
 
+       lateInit();
+
        len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
        ptr = val = palloc(len);
        *ptr ='\0';
@@ -718,6 +754,8 @@ onlineAnalyzeHooker(QueryDesc *queryDesc)
        if (queryDesc->estate)
                naffected = queryDesc->estate->es_processed;
 
+       lateInit();
+
 #if PG_VERSION_NUM >= 90200
        if (online_analyze_enable &&
                (constval = isFastTruncateCall(queryDesc)) != NULL)
@@ -872,6 +910,8 @@ onlineAnalyzeHookerUtility(
                parsetree = pstmt->utilityStmt;
 #endif
 
+       lateInit();
+
        if (parsetree && online_analyze_enable)
        {
                if (IsA(parsetree, CreateTableAsStmt) &&