Hi Álvaro,
Thanks for your feedback.
> Hmm, but instead of keeping the comment about why this is bogus, why not
> just fix it and remove the comment? You could do something like
>
> nsp = get_namespace_name( .. );
> diffname = psprintf("%s_%s_%d", nsp, RelationGetRelationName( .. ), 2);
> tempname = quote_qualified_identifier(nsp, RelationGetRelationName( ... ));
>
> and then that should be fairly okay, I think, keeping in mind that both
> the names involved are internally-generated short strings -- something
> like pg_temp_19.pg_temp_28356_2.
Sounds good to me. Here is the updated patch v3.
> I think it would be better to rewrite this code not to rely on SPI.
I will investigate this and start a new thread for better visibility.
This is an invasive change which requires broader discussion.
--
Best regards,
Aleksander Alekseev
From 31c04d1bbddd5c1dfe48ec11825c589f34ae7d3f Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <[email protected]>
Date: Wed, 15 Oct 2025 13:28:24 +0300
Subject: [PATCH v3] Remove make_temptable_name_n()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The named function is used in matview.c only once and doesn't do much. Replace
it with a simple psprintf() call. On top of that, properly quote qualified
identifiers when generating diff table name.
Author: Aleksander Alekseev <[email protected]>
Reviewed-by: Nathan Bossart <[email protected]>
Reviewed-by: Álvaro Herrera <[email protected]>
Discussion: https://postgr.es/m/CAJ7c6TO3a5q2NKRsjdJ6sLf8isVe4aMaaX1-Hj2TdHdhFw8zRA%40mail.gmail.com
---
src/backend/commands/matview.c | 44 ++++++++++++++--------------------
1 file changed, 18 insertions(+), 26 deletions(-)
diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c
index 441de55ac24..6884d4bf6f7 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
*
@@ -632,9 +609,24 @@ 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);
+
+ /*
+ * Create names for the temporary table and diff table.
+ * For the diff table, append an underscore followed by the given integer
+ * to the relation name.
+ */
+ {
+ char* nsp = get_namespace_name(RelationGetNamespace(tempRel));
+ char* temprelname = RelationGetRelationName(tempRel);
+ char* diffrelname = psprintf("%s_%d", temprelname, 2);
+
+ tempname = quote_qualified_identifier(nsp, temprelname);
+ diffname = quote_qualified_identifier(nsp, diffrelname);
+
+ pfree(diffrelname);
+ if (nsp)
+ pfree(nsp);
+ }
relnatts = RelationGetNumberOfAttributes(matviewRel);
--
2.43.0