From 4c5e1d0785ce150c3e6c65b009ea56815acbc8cd Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 22 Oct 2025 12:31:55 -0500 Subject: [PATCH] Remove make_temptable_name_n(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This small function is only used in one place, and it fails to handle quoted table names (although the table name portion of the input should never be quoted in current usage). In addition to removing make_temptable_name_n() in favor of open-coding it where needed, this commit ensures the "diff" table name is properly quoted in order to future-proof this area a bit. Author: Aleksander Alekseev Reviewed-by: Álvaro Herrera Reviewed-by: Shinya Kato Discussion: https://postgr.es/m/CAJ7c6TO3a5q2NKRsjdJ6sLf8isVe4aMaaX1-Hj2TdHdhFw8zRA%40mail.gmail.com --- src/backend/commands/matview.c | 40 ++++++++++++---------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index 441de55ac24..ef7c0d624f1 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -61,7 +61,6 @@ static void transientrel_shutdown(DestReceiver *self); static void transientrel_destroy(DestReceiver *self); static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query, const char *queryString, bool is_create); -static char *make_temptable_name_n(char *tempname, int n); static void refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner, int save_sec_context); static void refresh_by_heap_swap(Oid matviewOid, Oid OIDNewHeap, char relpersistence); @@ -556,28 +555,6 @@ transientrel_destroy(DestReceiver *self) pfree(self); } - -/* - * Given a qualified temporary table name, append an underscore followed by - * the given integer, to make a new table name based on the old one. - * The result is a palloc'd string. - * - * As coded, this would fail to make a valid SQL name if the given name were, - * say, "FOO"."BAR". Currently, the table name portion of the input will - * never be double-quoted because it's of the form "pg_temp_NNN", cf - * make_new_heap(). But we might have to work harder someday. - */ -static char * -make_temptable_name_n(char *tempname, int n) -{ - StringInfoData namebuf; - - initStringInfo(&namebuf); - appendStringInfoString(&namebuf, tempname); - appendStringInfo(&namebuf, "_%d", n); - return namebuf.data; -} - /* * refresh_by_match_merge * @@ -620,6 +597,9 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner, char *matviewname; char *tempname; char *diffname; + char *temprelname; + char *diffrelname; + char *nsp; TupleDesc tupdesc; bool foundUniqueIndex; List *indexoidlist; @@ -632,9 +612,17 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner, matviewname = quote_qualified_identifier(get_namespace_name(RelationGetNamespace(matviewRel)), RelationGetRelationName(matviewRel)); tempRel = table_open(tempOid, NoLock); - tempname = quote_qualified_identifier(get_namespace_name(RelationGetNamespace(tempRel)), - RelationGetRelationName(tempRel)); - diffname = make_temptable_name_n(tempname, 2); + + /* + * Build qualified names of the temporary table and the diff table. The + * only difference between them is the "_2" suffix on the diff table name. + */ + nsp = get_namespace_name(RelationGetNamespace(tempRel)); + temprelname = RelationGetRelationName(tempRel); + diffrelname = psprintf("%s_2", temprelname); + + tempname = quote_qualified_identifier(nsp, temprelname); + diffname = quote_qualified_identifier(nsp, diffrelname); relnatts = RelationGetNumberOfAttributes(matviewRel); -- 2.47.3