[PATCH] Remove make_temptable_name_n()

Started by Aleksander Alekseev3 months ago10 messages
#1Aleksander Alekseev
aleksander@tigerdata.com
1 attachment(s)

Hi,

The proposed patch removes make_temptable_name_n() in matview.c. This
function is used only once and can be replaced with psprintf().

--
Best regards,
Aleksander Alekseev

Attachments:

v1-0001-Remove-make_temptable_name_n.patchapplication/x-patch; name=v1-0001-Remove-make_temptable_name_n.patchDownload
From 6f2084f4663173c35f3d7f06f4f6a941d37c3ca1 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Wed, 8 Oct 2025 17:11:54 +0300
Subject: [PATCH v1] Remove make_temptable_name_n()

The named function is used in matview.c only once and doesn't do much. Replace
it with a simple psprintf() call.

Author: Aleksander Alekseev <aleksander@tigerdata.com>
Reviewed-by: TODO FIXME
Discussion: TODO FIXME
---
 src/backend/commands/matview.c | 25 +------------------------
 1 file changed, 1 insertion(+), 24 deletions(-)

diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c
index 441de55ac24..961cd8c6242 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
  *
@@ -634,7 +611,7 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
 	tempRel = table_open(tempOid, NoLock);
 	tempname = quote_qualified_identifier(get_namespace_name(RelationGetNamespace(tempRel)),
 										  RelationGetRelationName(tempRel));
-	diffname = make_temptable_name_n(tempname, 2);
+	diffname = psprintf("%s_%d", tempname, 2);
 
 	relnatts = RelationGetNumberOfAttributes(matviewRel);
 
-- 
2.43.0

#2Nathan Bossart
nathandbossart@gmail.com
In reply to: Aleksander Alekseev (#1)
Re: [PATCH] Remove make_temptable_name_n()

On Fri, Oct 10, 2025 at 06:22:46PM +0300, Aleksander Alekseev wrote:

The proposed patch removes make_temptable_name_n() in matview.c. This
function is used only once and can be replaced with psprintf().

This dates back to commit cc1965a, and I see some discussion about this
function in the corresponding thread [0]/messages/by-id/CAP7Qgm=jb3xkzQXfGtX9STx8fzd8EDDQ-oJ8ekcyeOud+yLCoA@mail.gmail.com. One thing I don't like about
the patch is that we lose the comment about relying on the name to never be
double-quoted. IMHO that's worth retaining in some form.

[0]: /messages/by-id/CAP7Qgm=jb3xkzQXfGtX9STx8fzd8EDDQ-oJ8ekcyeOud+yLCoA@mail.gmail.com

--
nathan

#3Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Nathan Bossart (#2)
1 attachment(s)
Re: [PATCH] Remove make_temptable_name_n()

Hi Nathan,

This dates back to commit cc1965a, and I see some discussion about this
function in the corresponding thread [0]. One thing I don't like about
the patch is that we lose the comment about relying on the name to never be
double-quoted. IMHO that's worth retaining in some form.

Fair enough. Here is the corrected patch v2.

--
Best regards,
Aleksander Alekseev

Attachments:

v2-0001-Remove-make_temptable_name_n.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Remove-make_temptable_name_n.patchDownload
From b7ac7079219451a37f02503d194a4caa48c68054 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Wed, 15 Oct 2025 13:28:24 +0300
Subject: [PATCH v2] Remove make_temptable_name_n()

The named function is used in matview.c only once and doesn't do much. Replace
it with a simple psprintf() call.

Author: Aleksander Alekseev <aleksander@tigerdata.com>
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://postgr.es/m/CAJ7c6TO3a5q2NKRsjdJ6sLf8isVe4aMaaX1-Hj2TdHdhFw8zRA%40mail.gmail.com
---
 src/backend/commands/matview.c | 35 +++++++++++-----------------------
 1 file changed, 11 insertions(+), 24 deletions(-)

diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c
index 441de55ac24..00fed53268b 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
  *
@@ -634,7 +611,17 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
 	tempRel = table_open(tempOid, NoLock);
 	tempname = quote_qualified_identifier(get_namespace_name(RelationGetNamespace(tempRel)),
 										  RelationGetRelationName(tempRel));
-	diffname = make_temptable_name_n(tempname, 2);
+	/*
+	 * Create a name for the temporary diff table by appending an underscore
+	 * followed by the given integer to the qualified temporary table name.
+	 * 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.
+	 */
+	diffname = psprintf("%s_%d", tempname, 2);
 
 	relnatts = RelationGetNumberOfAttributes(matviewRel);
 
-- 
2.43.0

#4Álvaro Herrera
alvherre@kurilemu.de
In reply to: Aleksander Alekseev (#3)
Re: [PATCH] Remove make_temptable_name_n()

On 2025-Oct-15, Aleksander Alekseev wrote:

@@ -634,7 +611,17 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
tempRel = table_open(tempOid, NoLock);
tempname = quote_qualified_identifier(get_namespace_name(RelationGetNamespace(tempRel)),
RelationGetRelationName(tempRel));
-	diffname = make_temptable_name_n(tempname, 2);
+	/*
+	 * Create a name for the temporary diff table by appending an underscore
+	 * followed by the given integer to the qualified temporary table name.
+	 * 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.
+	 */
+	diffname = psprintf("%s_%d", tempname, 2);

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.

I think it would be better to rewrite this code not to rely on SPI.

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/

#5Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Álvaro Herrera (#4)
1 attachment(s)
Re: [PATCH] Remove make_temptable_name_n()

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

Attachments:

v3-0001-Remove-make_temptable_name_n.patchtext/x-patch; charset=UTF-8; name=v3-0001-Remove-make_temptable_name_n.patchDownload
From 31c04d1bbddd5c1dfe48ec11825c589f34ae7d3f Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
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 <aleksander@tigerdata.com>
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
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

#6Shinya Kato
shinya11.kato@gmail.com
In reply to: Aleksander Alekseev (#5)
Re: [PATCH] Remove make_temptable_name_n()

Hi,

On Thu, Oct 16, 2025 at 8:11 PM Aleksander Alekseev
<aleksander@tigerdata.com> wrote:

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.

Thank you for the patch.

The v1 revision removed make_temptable_name_n and added psprintf,
which reduced the code size. However, the code size in v3 is almost
unchanged, so it's unclear how beneficial this change actually is.

Anyway, I have a minor comment about the patch.

+ char* nsp = get_namespace_name(RelationGetNamespace(tempRel));
+ char* temprelname = RelationGetRelationName(tempRel);
+ char* diffrelname = psprintf("%s_%d", temprelname, 2);

In PostgreSQL code, "char *xxx" seems to be more commonly used than "char* xxx".

--
Best regards,
Shinya Kato
NTT OSS Center

#7Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Shinya Kato (#6)
1 attachment(s)
Re: [PATCH] Remove make_temptable_name_n()

Hi Shinya,

Thanks for your feedback.

The v1 revision removed make_temptable_name_n and added psprintf,
which reduced the code size. However, the code size in v3 is almost
unchanged, so it's unclear how beneficial this change actually is.

Right, the concept has changed a bit, see Álvaro's comment above.

Anyway, I have a minor comment about the patch.

+ char* nsp = get_namespace_name(RelationGetNamespace(tempRel));
+ char* temprelname = RelationGetRelationName(tempRel);
+ char* diffrelname = psprintf("%s_%d", temprelname, 2);

In PostgreSQL code, "char *xxx" seems to be more commonly used than "char* xxx".

My bad, I forgot to run pgindent. Here is the corrected patch.

--
Best regards,
Aleksander Alekseev

Attachments:

v4-0001-Remove-make_temptable_name_n.patchtext/x-patch; charset=UTF-8; name=v4-0001-Remove-make_temptable_name_n.patchDownload
From 1f688832ee4605d88dedc4777a008ca289f38c8b Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Wed, 15 Oct 2025 13:28:24 +0300
Subject: [PATCH v4] 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 <aleksander@tigerdata.com>
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Reviewed-by: Shinya Kato <shinya11.kato@gmail.com>
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..7a0a02b44d4 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

#8Nathan Bossart
nathandbossart@gmail.com
In reply to: Aleksander Alekseev (#7)
Re: [PATCH] Remove make_temptable_name_n()

On Tue, Oct 21, 2025 at 03:36:46PM +0300, Aleksander Alekseev wrote:

+	{
+		char	   *nsp = get_namespace_name(RelationGetNamespace(tempRel));
+		char	   *temprelname = RelationGetRelationName(tempRel);
+		char	   *diffrelname = psprintf("%s_%d", temprelname, 2);

I assume the intent of the extra set of curly brackets is to keep the
declarations of these variables close to where they are used. In this
case, the top of the function is only a few lines up, so IMHO we should
declare them there and save a level of indentation.

+		pfree(diffrelname);
+		if (nsp)
+			pfree(nsp);

Any reason to be so careful about freeing these? We ordinarily let the
memory context take care of freeing, and refresh_by_match_merge() looks no
different.

--
nathan

#9Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Nathan Bossart (#8)
1 attachment(s)
Re: [PATCH] Remove make_temptable_name_n()

Hi Nathan,

+     {
+             char       *nsp = get_namespace_name(RelationGetNamespace(tempRel));
+             char       *temprelname = RelationGetRelationName(tempRel);
+             char       *diffrelname = psprintf("%s_%d", temprelname, 2);

I assume the intent of the extra set of curly brackets is to keep the
declarations of these variables close to where they are used. In this
case, the top of the function is only a few lines up, so IMHO we should
declare them there and save a level of indentation.

+             pfree(diffrelname);
+             if (nsp)
+                     pfree(nsp);

Any reason to be so careful about freeing these? We ordinarily let the
memory context take care of freeing, and refresh_by_match_merge() looks no
different.

These were just a matter of my personal preferences. I have no strong
opinion on this. Here is the updated patch.

--
Best regards,
Aleksander Alekseev

Attachments:

v5-0001-Remove-make_temptable_name_n.patchtext/x-patch; charset=UTF-8; name=v5-0001-Remove-make_temptable_name_n.patchDownload
From f1f8da46e2bd160e4eec1774d6ef9fb93d8bc434 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Wed, 15 Oct 2025 13:28:24 +0300
Subject: [PATCH v5] 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 <aleksander@tigerdata.com>
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Reviewed-by: Shinya Kato <shinya11.kato@gmail.com>
Discussion: https://postgr.es/m/CAJ7c6TO3a5q2NKRsjdJ6sLf8isVe4aMaaX1-Hj2TdHdhFw8zRA%40mail.gmail.com
---
 src/backend/commands/matview.c | 41 +++++++++++++---------------------
 1 file changed, 15 insertions(+), 26 deletions(-)

diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c
index 441de55ac24..7fb41307d9c 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,18 @@ 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.
+	 */
+	nsp = get_namespace_name(RelationGetNamespace(tempRel));
+	temprelname = RelationGetRelationName(tempRel);
+	diffrelname = psprintf("%s_%d", temprelname, 2);
+
+	tempname = quote_qualified_identifier(nsp, temprelname);
+	diffname = quote_qualified_identifier(nsp, diffrelname);
 
 	relnatts = RelationGetNumberOfAttributes(matviewRel);
 
-- 
2.43.0

#10Nathan Bossart
nathandbossart@gmail.com
In reply to: Aleksander Alekseev (#9)
Re: [PATCH] Remove make_temptable_name_n()

On Wed, Oct 22, 2025 at 04:20:01PM +0300, Aleksander Alekseev wrote:

Here is the updated patch.

Committed.

--
nathan