From 698fa924b11a4ff55ac83b340dbae1e6cee00e59 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 24 Nov 2025 17:00:16 -0500 Subject: Improve detection of implicitly-temporary views. We've long had a practice of making views temporary by default if they reference any temporary tables. However the implementation was pretty incomplete, in that it only searched for RangeTblEntry references to temp relations. Uses of temporary types, regclass constants, etc were not detected even though the dependency mechanism considers them grounds for dropping the view. Thus a view not believed to be temp could silently go away at session exit anyhow. To improve matters, replace the ad-hoc isQueryUsingTempRelation() logic with use of the dependency-based infrastructure introduced by commit 572c40ba9. This is complete by definition, and it's less code overall. While we're at it, we can also extend the warning NOTICE (or ERROR in the case of a materialized view) to mention one of the temp objects motivating the classification of the view as temp, as was done for functions in 572c40ba9. Author: Tom Lane Reviewed-by: Jim Jones Discussion: https://postgr.es/m/19cf6ae1-04cd-422c-a760-d7e75fe6cba9@uni-muenster.de --- src/backend/commands/view.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/backend/commands/view.c') diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c index 6f0301555e0..4cc2af7b5ec 100644 --- a/src/backend/commands/view.c +++ b/src/backend/commands/view.c @@ -22,7 +22,6 @@ #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" #include "parser/analyze.h" -#include "parser/parse_relation.h" #include "rewrite/rewriteDefine.h" #include "rewrite/rewriteHandler.h" #include "rewrite/rewriteSupport.h" @@ -362,6 +361,7 @@ DefineView(ViewStmt *stmt, const char *queryString, ListCell *cell; bool check_option; ObjectAddress address; + ObjectAddress temp_object; /* * Run parse analysis to convert the raw parse tree to a Query. Note this @@ -484,12 +484,14 @@ DefineView(ViewStmt *stmt, const char *queryString, */ view = copyObject(stmt->view); /* don't corrupt original command */ if (view->relpersistence == RELPERSISTENCE_PERMANENT - && isQueryUsingTempRelation(viewParse)) + && query_uses_temp_object(viewParse, &temp_object)) { view->relpersistence = RELPERSISTENCE_TEMP; ereport(NOTICE, (errmsg("view \"%s\" will be a temporary view", - view->relname))); + view->relname), + errdetail("It depends on temporary %s.", + getObjectDescription(&temp_object, false)))); } /* -- cgit v1.2.3