From 3524de96acdb554af3faf4619206a293abf8c659 Mon Sep 17 00:00:00 2001 From: Adam Guo Date: Wed, 26 Mar 2025 18:32:09 +0000 Subject: [PATCH] Store querystring on create. --- createas.c | 10 +++++++++- pg_ivm--1.10--1.11.sql | 4 ++-- pg_ivm.c | 9 +++++++++ pg_ivm.h | 5 +++++ ruleutils.c | 7 +++++++ 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/createas.c b/createas.c index 8c6ce4d..026cce3 100644 --- a/createas.c +++ b/createas.c @@ -41,6 +41,7 @@ #include "rewrite/rewriteManip.h" #include "utils/builtins.h" #include "utils/fmgroids.h" +#include "utils/guc.h" #include "utils/lsyscache.h" #include "utils/regproc.h" #include "utils/rel.h" @@ -1710,20 +1711,27 @@ static void StoreImmvQuery(Oid viewOid, Query *viewQuery) { char *querytree = nodeToString((Node *) viewQuery); + char *querystring; Datum values[Natts_pg_ivm_immv]; bool isNulls[Natts_pg_ivm_immv]; + Relation matviewRel; Relation pgIvmImmv; TupleDesc tupleDescriptor; HeapTuple heapTuple; ObjectAddress address; + RestrictSearchPath(); + matviewRel = table_open(viewOid, AccessShareLock); + querystring = pg_ivm_get_viewdef_internal(viewQuery, matviewRel, true); + table_close(matviewRel, NoLock); + memset(values, 0, sizeof(values)); memset(isNulls, false, sizeof(isNulls)); - isNulls[Anum_pg_ivm_immv_querystring - 1] = true; values[Anum_pg_ivm_immv_immvrelid -1 ] = ObjectIdGetDatum(viewOid); values[Anum_pg_ivm_immv_ispopulated -1 ] = BoolGetDatum(false); values[Anum_pg_ivm_immv_viewdef -1 ] = CStringGetTextDatum(querytree); + values[Anum_pg_ivm_immv_querystring - 1] = CStringGetTextDatum(querystring); pgIvmImmv = table_open(PgIvmImmvRelationId(), RowExclusiveLock); diff --git a/pg_ivm--1.10--1.11.sql b/pg_ivm--1.10--1.11.sql index 6819be4..745f3dc 100644 --- a/pg_ivm--1.10--1.11.sql +++ b/pg_ivm--1.10--1.11.sql @@ -1,7 +1,7 @@ -ALTER TABLE pgivm.pg_ivm_immv ADD COLUMN querystring text; +ALTER TABLE pgivm.pg_ivm_immv ADD COLUMN querystring text NOT NULL; CREATE FUNCTION pgivm.refresh_query_strings() -RETURNS event_trigger LANGUAGE plpgsql AS +RETURNS event_trigger LANGUAGE plpgsql SECURITY DEFINER AS $$ DECLARE old_search_path text; diff --git a/pg_ivm.c b/pg_ivm.c index 19d0e0e..fe5c9bb 100644 --- a/pg_ivm.c +++ b/pg_ivm.c @@ -458,3 +458,12 @@ PgIvmFuncName(char *name) { return list_make2(makeString("pgivm"), makeString(name)); } + +#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM < 170000) +void +RestrictSearchPath(void) +{ + set_config_option("search_path", "pg_catalog, pg_temp", PGC_USERSET, + PGC_S_SESSION, GUC_ACTION_SAVE, true, 0, false); +} +#endif diff --git a/pg_ivm.h b/pg_ivm.h index ae79e45..e334141 100644 --- a/pg_ivm.h +++ b/pg_ivm.h @@ -65,8 +65,13 @@ extern bool isIvmName(const char *s); /* ruleutils.c */ extern char *pg_ivm_get_viewdef(Relation immvrel, bool pretty); +extern char *pg_ivm_get_viewdef_internal(Query *query, Relation immvrel, bool pretty); /* subselect.c */ extern void inline_cte(PlannerInfo *root, CommonTableExpr *cte); +#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM < 170000) +extern void RestrictSearchPath(void); +#endif + #endif diff --git a/ruleutils.c b/ruleutils.c index dfe1fe4..2c7e080 100644 --- a/ruleutils.c +++ b/ruleutils.c @@ -43,6 +43,13 @@ char * pg_ivm_get_viewdef(Relation immvrel, bool pretty) { Query *query = get_immv_query(immvrel); + + return pg_ivm_get_viewdef_internal(query, immvrel, pretty); +} + +char * +pg_ivm_get_viewdef_internal(Query *query, Relation immvrel, bool pretty) +{ TupleDesc resultDesc = RelationGetDescr(immvrel); #if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 150000)