Store querystring on create.

This commit is contained in:
Adam Guo 2025-03-26 18:32:09 +00:00
parent 7a36cc501a
commit 3524de96ac
5 changed files with 32 additions and 3 deletions

View file

@ -41,6 +41,7 @@
#include "rewrite/rewriteManip.h" #include "rewrite/rewriteManip.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/fmgroids.h" #include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#include "utils/regproc.h" #include "utils/regproc.h"
#include "utils/rel.h" #include "utils/rel.h"
@ -1710,20 +1711,27 @@ static void
StoreImmvQuery(Oid viewOid, Query *viewQuery) StoreImmvQuery(Oid viewOid, Query *viewQuery)
{ {
char *querytree = nodeToString((Node *) viewQuery); char *querytree = nodeToString((Node *) viewQuery);
char *querystring;
Datum values[Natts_pg_ivm_immv]; Datum values[Natts_pg_ivm_immv];
bool isNulls[Natts_pg_ivm_immv]; bool isNulls[Natts_pg_ivm_immv];
Relation matviewRel;
Relation pgIvmImmv; Relation pgIvmImmv;
TupleDesc tupleDescriptor; TupleDesc tupleDescriptor;
HeapTuple heapTuple; HeapTuple heapTuple;
ObjectAddress address; 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(values, 0, sizeof(values));
memset(isNulls, false, sizeof(isNulls)); 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_immvrelid -1 ] = ObjectIdGetDatum(viewOid);
values[Anum_pg_ivm_immv_ispopulated -1 ] = BoolGetDatum(false); values[Anum_pg_ivm_immv_ispopulated -1 ] = BoolGetDatum(false);
values[Anum_pg_ivm_immv_viewdef -1 ] = CStringGetTextDatum(querytree); values[Anum_pg_ivm_immv_viewdef -1 ] = CStringGetTextDatum(querytree);
values[Anum_pg_ivm_immv_querystring - 1] = CStringGetTextDatum(querystring);
pgIvmImmv = table_open(PgIvmImmvRelationId(), RowExclusiveLock); pgIvmImmv = table_open(PgIvmImmvRelationId(), RowExclusiveLock);

View file

@ -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() CREATE FUNCTION pgivm.refresh_query_strings()
RETURNS event_trigger LANGUAGE plpgsql AS RETURNS event_trigger LANGUAGE plpgsql SECURITY DEFINER AS
$$ $$
DECLARE DECLARE
old_search_path text; old_search_path text;

View file

@ -458,3 +458,12 @@ PgIvmFuncName(char *name)
{ {
return list_make2(makeString("pgivm"), makeString(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

View file

@ -65,8 +65,13 @@ extern bool isIvmName(const char *s);
/* ruleutils.c */ /* ruleutils.c */
extern char *pg_ivm_get_viewdef(Relation immvrel, bool pretty); 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 */ /* subselect.c */
extern void inline_cte(PlannerInfo *root, CommonTableExpr *cte); extern void inline_cte(PlannerInfo *root, CommonTableExpr *cte);
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM < 170000)
extern void RestrictSearchPath(void);
#endif
#endif #endif

View file

@ -43,6 +43,13 @@ char *
pg_ivm_get_viewdef(Relation immvrel, bool pretty) pg_ivm_get_viewdef(Relation immvrel, bool pretty)
{ {
Query *query = get_immv_query(immvrel); 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); TupleDesc resultDesc = RelationGetDescr(immvrel);
#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 150000) #if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 150000)