From 102ce391913e0ae20ec7b82809a31a08926849b3 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 18 Jun 2025 16:15:19 +0900
Subject: [PATCH v2 06/13] Add GUC default_toast_type

This GUC controls the data type used for newly-created TOAST values,
with two modes supported: "oid" and "int8".  This will be used by an
upcoming patch.
---
 src/include/access/toast_type.h               | 30 +++++++++++++++++++
 src/backend/catalog/toasting.c                |  4 +++
 src/backend/utils/misc/guc_tables.c           | 19 ++++++++++++
 src/backend/utils/misc/postgresql.conf.sample |  1 +
 doc/src/sgml/config.sgml                      | 17 +++++++++++
 5 files changed, 71 insertions(+)
 create mode 100644 src/include/access/toast_type.h

diff --git a/src/include/access/toast_type.h b/src/include/access/toast_type.h
new file mode 100644
index 000000000000..494c2a3e852e
--- /dev/null
+++ b/src/include/access/toast_type.h
@@ -0,0 +1,30 @@
+/*-------------------------------------------------------------------------
+ *
+ * toast_type.h
+ *	  Internal definitions for the types supported by values in TOAST
+ *	  relations.
+ *
+ * Copyright (c) 2000-2025, PostgreSQL Global Development Group
+ *
+ * src/include/access/toast_type.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef TOAST_TYPE_H
+#define TOAST_TYPE_H
+
+/*
+ * GUC support
+ *
+ * Detault value type in toast table.
+ */
+extern PGDLLIMPORT int default_toast_type;
+
+typedef enum ToastTypeId
+{
+	TOAST_TYPE_INVALID = 0,
+	TOAST_TYPE_OID = 1,
+	TOAST_TYPE_INT8 = 2,
+} ToastTypeId;
+
+#endif							/* TOAST_TYPE_H */
diff --git a/src/backend/catalog/toasting.c b/src/backend/catalog/toasting.c
index 874a8fc89adb..e595cb61b375 100644
--- a/src/backend/catalog/toasting.c
+++ b/src/backend/catalog/toasting.c
@@ -16,6 +16,7 @@
 
 #include "access/heapam.h"
 #include "access/toast_compression.h"
+#include "access/toast_type.h"
 #include "access/xact.h"
 #include "catalog/binary_upgrade.h"
 #include "catalog/catalog.h"
@@ -33,6 +34,9 @@
 #include "utils/rel.h"
 #include "utils/syscache.h"
 
+/* GUC support */
+int			default_toast_type = TOAST_TYPE_OID;
+
 static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
 									 LOCKMODE lockmode, bool check,
 									 Oid OIDOldToast);
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 511dc32d5192..0999a2b00b16 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -33,6 +33,7 @@
 #include "access/gin.h"
 #include "access/slru.h"
 #include "access/toast_compression.h"
+#include "access/toast_type.h"
 #include "access/twophase.h"
 #include "access/xlog_internal.h"
 #include "access/xlogprefetcher.h"
@@ -464,6 +465,13 @@ static const struct config_enum_entry default_toast_compression_options[] = {
 	{NULL, 0, false}
 };
 
+
+static const struct config_enum_entry default_toast_type_options[] = {
+	{"oid", TOAST_TYPE_OID, false},
+	{"int8", TOAST_TYPE_INT8, false},
+	{NULL, 0, false}
+};
+
 static const struct config_enum_entry wal_compression_options[] = {
 	{"pglz", WAL_COMPRESSION_PGLZ, false},
 #ifdef USE_LZ4
@@ -5058,6 +5066,17 @@ struct config_enum ConfigureNamesEnum[] =
 		NULL, NULL, NULL
 	},
 
+	{
+		{"default_toast_type", PGC_USERSET, CLIENT_CONN_STATEMENT,
+			gettext_noop("Sets the default type used for TOAST values."),
+			NULL
+		},
+		&default_toast_type,
+		TOAST_TYPE_OID,
+		default_toast_type_options,
+		NULL, NULL, NULL
+	},
+
 	{
 		{"default_transaction_isolation", PGC_USERSET, CLIENT_CONN_STATEMENT,
 			gettext_noop("Sets the transaction isolation level of each new transaction."),
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index 341f88adc87b..4fbf76e48ec4 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -753,6 +753,7 @@ autovacuum_worker_slots = 16	# autovacuum worker slots to allocate
 #default_table_access_method = 'heap'
 #default_tablespace = ''		# a tablespace name, '' uses the default
 #default_toast_compression = 'pglz'	# 'pglz' or 'lz4'
+#default_toast_type = 'oid'		# 'oid' or 'int8'
 #temp_tablespaces = ''			# a list of tablespace names, '' uses
 					# only default tablespace
 #check_function_bodies = on
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 59a0874528a3..ad75c62d7fbc 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -9830,6 +9830,23 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
       </listitem>
      </varlistentry>
 
+     <varlistentry id="guc-default-toast-type" xreflabel="default_toast_type">
+      <term><varname>default_toast_type</varname> (<type>enum</type>)
+      <indexterm>
+       <primary><varname>default_toast_type</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        This variable sets the default type for
+        <link linkend="storage-toast">TOAST</link> values.
+        The value types supported are <literal>oid</literal> and
+        <literal>int8</literal>.
+        The default is <literal>oid</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-temp-tablespaces" xreflabel="temp_tablespaces">
       <term><varname>temp_tablespaces</varname> (<type>string</type>)
       <indexterm>
-- 
2.50.0

