From 5a5cd9ec422c1d6ea1852ed94d03667ae0f6a3b3 Mon Sep 17 00:00:00 2001 From: Teodor Sigaev Date: Thu, 7 Oct 2021 18:35:33 +0300 Subject: [PATCH] Fix usage of include_tables/exclude_tables in case of preloaded libraries. 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 | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/online_analyze.c b/online_analyze.c index 1a50250..f5a36e5 100644 --- a/online_analyze.c +++ b/online_analyze.c @@ -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; iinited == 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) && -- 2.37.3