From 7137843932e5e22ae641c8518963a796420f8912 Mon Sep 17 00:00:00 2001
From: Zsolt Parragi <zsolt.parragi@percona.com>
Date: Fri, 9 Oct 2026 11:00:12 +0000
Subject: [PATCH v1 2/3] Allow extensions to define tablespace options

Tablespaces accept only the built-in options from TableSpaceOpts.  Let
extensions add their own with add_*_reloption(RELOPT_KIND_TABLESPACE,
...).  The values are stored in pg_tablespace.spcoptions, from where
the extension reads them.

As tablespaces are shared by all databases, such options can only be
registered from shared_preload_libraries.  Their names must be
qualified like custom GUC names, as in "ext.option", so that they
cannot clash with built-in options or each other, and shorter than
NAMEDATALEN, so that they survive being dumped as one identifier.  The
grammar accepts qualified names without quoting.
---
 doc/src/sgml/ref/alter_tablespace.sgml        |  17 +-
 doc/src/sgml/ref/create_tablespace.sgml       |  11 +-
 src/backend/access/common/reloptions.c        |  59 +++++-
 src/backend/commands/tablespace.c             |  31 ++-
 src/backend/utils/misc/guc.c                  |   3 +-
 src/include/utils/guc.h                       |   2 +
 src/test/modules/Makefile                     |   1 +
 src/test/modules/meson.build                  |   1 +
 .../test_tablespace_options/.gitignore        |   2 +
 .../modules/test_tablespace_options/Makefile  |  17 ++
 .../test_tablespace_options/meson.build       |  28 +++
 .../t/001_tablespace_options.pl               | 185 ++++++++++++++++++
 .../test_tablespace_options.c                 |  66 +++++++
 13 files changed, 410 insertions(+), 13 deletions(-)
 create mode 100644 src/test/modules/test_tablespace_options/.gitignore
 create mode 100644 src/test/modules/test_tablespace_options/Makefile
 create mode 100644 src/test/modules/test_tablespace_options/meson.build
 create mode 100644 src/test/modules/test_tablespace_options/t/001_tablespace_options.pl
 create mode 100644 src/test/modules/test_tablespace_options/test_tablespace_options.c

diff --git a/doc/src/sgml/ref/alter_tablespace.sgml b/doc/src/sgml/ref/alter_tablespace.sgml
index d0e08089ddb..5a5461d1974 100644
--- a/doc/src/sgml/ref/alter_tablespace.sgml
+++ b/doc/src/sgml/ref/alter_tablespace.sgml
@@ -82,8 +82,8 @@ ALTER TABLESPACE <replaceable>name</replaceable> RESET ( <replaceable class="par
     <term><replaceable class="parameter">tablespace_option</replaceable></term>
     <listitem>
      <para>
-      A tablespace parameter to be set or reset.  Currently, the only
-      available parameters are <varname>seq_page_cost</varname>,
+      A tablespace parameter to be set or reset.  The built-in
+      parameters are <varname>seq_page_cost</varname>,
       <varname>random_page_cost</varname>, <varname>effective_io_concurrency</varname>
       and <varname>maintenance_io_concurrency</varname>.
       Setting these values for a particular tablespace will override the
@@ -97,6 +97,19 @@ ALTER TABLESPACE <replaceable>name</replaceable> RESET ( <replaceable class="par
       one tablespace is located on a disk which is faster or slower than the
       remainder of the I/O subsystem.
      </para>
+     <para>
+      Extensions loaded via <xref linkend="guc-shared-preload-libraries"/>
+      can define additional parameters.  Their names are qualified with a
+      prefix chosen by the extension, as in
+      <literal>my_extension.my_option</literal>, and can be written without
+      quotes.  See the documentation of the extension for their meaning.
+     </para>
+     <para>
+      While the extension that defined a parameter is not loaded, the
+      parameter has no effect, and <literal>SET</literal> and
+      <literal>RESET</literal> fail on tablespaces that have it set, except
+      for a <literal>RESET</literal> that removes all such parameters.
+     </para>
     </listitem>
    </varlistentry>
 
diff --git a/doc/src/sgml/ref/create_tablespace.sgml b/doc/src/sgml/ref/create_tablespace.sgml
index b77e774c53f..a9f0944f424 100644
--- a/doc/src/sgml/ref/create_tablespace.sgml
+++ b/doc/src/sgml/ref/create_tablespace.sgml
@@ -104,8 +104,8 @@ CREATE TABLESPACE <replaceable class="parameter">tablespace_name</replaceable>
       <term><replaceable class="parameter">tablespace_option</replaceable></term>
       <listitem>
        <para>
-        A tablespace parameter to be set or reset.  Currently, the only
-        available parameters are <varname>seq_page_cost</varname>,
+        A tablespace parameter to be set or reset.  The built-in
+        parameters are <varname>seq_page_cost</varname>,
         <varname>random_page_cost</varname>, <varname>effective_io_concurrency</varname>
         and <varname>maintenance_io_concurrency</varname>.
         Setting these values for a particular tablespace will override the
@@ -119,6 +119,13 @@ CREATE TABLESPACE <replaceable class="parameter">tablespace_name</replaceable>
         one tablespace is located on a disk which is faster or slower than the
         remainder of the I/O subsystem.
        </para>
+       <para>
+        Extensions loaded via <xref linkend="guc-shared-preload-libraries"/>
+        can define additional parameters.  Their names are qualified with a
+        prefix chosen by the extension, as in
+        <literal>my_extension.my_option</literal>, and can be written without
+        quotes.  See the documentation of the extension for their meaning.
+       </para>
       </listitem>
      </varlistentry>
   </variablelist>
diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c
index ea9a0417909..fb867e72e5b 100644
--- a/src/backend/access/common/reloptions.c
+++ b/src/backend/access/common/reloptions.c
@@ -27,6 +27,7 @@
 #include "catalog/pg_type.h"
 #include "commands/defrem.h"
 #include "commands/tablespace.h"
+#include "miscadmin.h"
 #include "nodes/makefuncs.h"
 #include "storage/lock.h"
 #include "utils/array.h"
@@ -930,6 +931,34 @@ allocate_reloption(uint32 kinds, int type, const char *name, const char *desc,
 	size_t		size;
 	relopt_gen *newoption;
 
+	/*
+	 * Tablespaces are shared by all databases, so their options must be known
+	 * to every backend.  All extensions share one namespace for them with the
+	 * built-in options, so insist on qualified names like custom GUCs have.
+	 * These errors are FATAL because the calling module may be partly
+	 * initialized already.
+	 */
+	if (kinds & RELOPT_KIND_TABLESPACE)
+	{
+		if (!process_shared_preload_libraries_in_progress)
+			ereport(FATAL,
+					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+					 errmsg("could not register tablespace option \"%s\"", name),
+					 errdetail("Tablespace options must be registered while initializing modules in \"shared_preload_libraries\".")));
+		if (!valid_custom_variable_name(name))
+			ereport(FATAL,
+					(errcode(ERRCODE_INVALID_NAME),
+					 errmsg("invalid tablespace option name \"%s\"", name),
+					 errdetail("Tablespace option names must be two or more simple identifiers separated by dots.")));
+		/* longer names would be truncated when dumped as one identifier */
+		if (strlen(name) >= NAMEDATALEN)
+			ereport(FATAL,
+					(errcode(ERRCODE_NAME_TOO_LONG),
+					 errmsg("tablespace option name \"%s\" is too long", name),
+					 errdetail("Tablespace option names must be shorter than %d bytes.",
+							   NAMEDATALEN)));
+	}
+
 	if (kinds != RELOPT_KIND_LOCAL)
 		oldcxt = MemoryContextSwitchTo(TopMemoryContext);
 	else
@@ -2429,6 +2458,13 @@ attribute_reloptions(Datum reloptions, bool validate)
 
 /*
  * Option parser for tablespace reloptions
+ *
+ * Extensions loaded via shared_preload_libraries may register additional
+ * tablespace options with add_*_reloption(RELOPT_KIND_TABLESPACE, ...).
+ * parseRelOptions validates those exactly like the built-in ones, but
+ * TableSpaceOpts has no slot to put them in, so we run fillRelOptions
+ * without its parse-table check instead of going through build_reloptions.
+ * Readers of such options fetch them from pg_tablespace.spcoptions directly.
  */
 bytea *
 tablespace_reloptions(Datum reloptions, bool validate)
@@ -2439,11 +2475,26 @@ tablespace_reloptions(Datum reloptions, bool validate)
 		{"effective_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, effective_io_concurrency)},
 		{"maintenance_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, maintenance_io_concurrency)}
 	};
+	int			numoptions;
+	relopt_value *options;
+	void	   *rdopts;
 
-	return (bytea *) build_reloptions(reloptions, validate,
-									  RELOPT_KIND_TABLESPACE,
-									  sizeof(TableSpaceOpts),
-									  tab, lengthof(tab));
+	options = parseRelOptions(reloptions, validate, RELOPT_KIND_TABLESPACE,
+							  &numoptions);
+
+	if (numoptions == 0)
+	{
+		Assert(options == NULL);
+		return NULL;
+	}
+
+	rdopts = allocateReloptStruct(sizeof(TableSpaceOpts), options, numoptions);
+	fillRelOptions(rdopts, sizeof(TableSpaceOpts), options, numoptions,
+				   false, tab, lengthof(tab));
+
+	pfree(options);
+
+	return (bytea *) rdopts;
 }
 
 /*
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 7c40176b7b9..37b8f0eada0 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -92,6 +92,7 @@ Oid			binary_upgrade_next_pg_tablespace_oid = InvalidOid;
 static void create_tablespace_directories(const char *location,
 										  const Oid tablespaceoid);
 static bool destroy_tablespace_directories(Oid tablespaceoid, bool redo);
+static List *qualify_tablespace_options(List *options);
 
 
 /*
@@ -343,7 +344,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
 
 	/* Generate new proposed spcoptions (text array) */
 	newOptions = transformRelOptions((Datum) 0,
-									 stmt->options,
+									 qualify_tablespace_options(stmt->options),
 									 NULL, NULL, false, false);
 	(void) tablespace_reloptions(newOptions, true);
 	if (newOptions != (Datum) 0)
@@ -936,6 +937,30 @@ remove_tablespace_symlink(const char *linkloc)
 	}
 }
 
+/*
+ * Tablespace options have no namespaces, but extensions register theirs
+ * under qualified names like "ext.option", which the grammar splits into
+ * namespace and name.  Glue them back together.
+ */
+static List *
+qualify_tablespace_options(List *options)
+{
+	List	   *result = NIL;
+
+	foreach_node(DefElem, def, options)
+	{
+		if (def->defnamespace != NULL)
+		{
+			def = copyObject(def);
+			def->defname = psprintf("%s.%s", def->defnamespace, def->defname);
+			def->defnamespace = NULL;
+		}
+		result = lappend(result, def);
+	}
+
+	return result;
+}
+
 /*
  * Rename a tablespace
  */
@@ -1080,8 +1105,8 @@ AlterTableSpaceOptions(AlterTableSpaceOptionsStmt *stmt)
 	datum = heap_getattr(tup, Anum_pg_tablespace_spcoptions,
 						 RelationGetDescr(rel), &isnull);
 	newOptions = transformRelOptions(isnull ? (Datum) 0 : datum,
-									 stmt->options, NULL, NULL, false,
-									 stmt->isReset);
+									 qualify_tablespace_options(stmt->options),
+									 NULL, NULL, false, stmt->isReset);
 	(void) tablespace_reloptions(newOptions, true);
 
 	/* Build new tuple. */
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 21c9ccec1e2..d5c9337a3c3 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -257,7 +257,6 @@ static bool validate_option_array_item(const char *name, const char *value,
 static void write_auto_conf_file(int fd, const char *filename, ConfigVariable *head);
 static void replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
 									  const char *name, const char *value);
-static bool valid_custom_variable_name(const char *name);
 static bool assignable_custom_variable_name(const char *name, bool skip_errors,
 											int elevel);
 static void do_serialize(char **destptr, Size *maxbytes,
@@ -953,7 +952,7 @@ add_guc_variable(struct config_generic *var, int elevel)
  * for what is an identifier agree with scan.l.  (If you change this rule,
  * adjust the errdetail in assignable_custom_variable_name().)
  */
-static bool
+bool
 valid_custom_variable_name(const char *name)
 {
 	bool		saw_sep = false;
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index 164efba6b51..89ce4b4afc9 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -422,6 +422,8 @@ extern void MarkGUCPrefixReserved(const char *className);
 /* old name for MarkGUCPrefixReserved, for backwards compatibility: */
 #define EmitWarningsOnPlaceholders(className) MarkGUCPrefixReserved(className)
 
+extern bool valid_custom_variable_name(const char *name);
+
 extern const char *GetConfigOption(const char *name, bool missing_ok,
 								   bool restrict_privileged);
 extern const char *GetConfigOptionResetString(const char *name);
diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile
index 6c1504ac932..a7971fc9c0c 100644
--- a/src/test/modules/Makefile
+++ b/src/test/modules/Makefile
@@ -53,6 +53,7 @@ SUBDIRS = \
 		  test_shmem \
 		  test_shm_mq \
 		  test_slru \
+		  test_tablespace_options \
 		  test_tidstore \
 		  test_wait_lsn \
 		  unsafe_tests \
diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build
index ef3cdb8faf9..a24c4edf027 100644
--- a/src/test/modules/meson.build
+++ b/src/test/modules/meson.build
@@ -55,6 +55,7 @@ subdir('test_saslprep')
 subdir('test_shmem')
 subdir('test_shm_mq')
 subdir('test_slru')
+subdir('test_tablespace_options')
 subdir('test_tidstore')
 subdir('test_wait_lsn')
 subdir('typcache')
diff --git a/src/test/modules/test_tablespace_options/.gitignore b/src/test/modules/test_tablespace_options/.gitignore
new file mode 100644
index 00000000000..716e17f5a2a
--- /dev/null
+++ b/src/test/modules/test_tablespace_options/.gitignore
@@ -0,0 +1,2 @@
+# Generated subdirectories
+/tmp_check/
diff --git a/src/test/modules/test_tablespace_options/Makefile b/src/test/modules/test_tablespace_options/Makefile
new file mode 100644
index 00000000000..80025909ef7
--- /dev/null
+++ b/src/test/modules/test_tablespace_options/Makefile
@@ -0,0 +1,17 @@
+# src/test/modules/test_tablespace_options/Makefile
+
+MODULES = test_tablespace_options
+PGFILEDESC = "test_tablespace_options - test extension-defined tablespace options"
+
+TAP_TESTS = 1
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_tablespace_options
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_tablespace_options/meson.build b/src/test/modules/test_tablespace_options/meson.build
new file mode 100644
index 00000000000..8230b1273e4
--- /dev/null
+++ b/src/test/modules/test_tablespace_options/meson.build
@@ -0,0 +1,28 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+test_tablespace_options_sources = files(
+  'test_tablespace_options.c',
+)
+
+if host_system == 'windows'
+  test_tablespace_options_sources += rc_lib_gen.process(win32ver_rc, extra_args: [
+    '--NAME', 'test_tablespace_options',
+    '--FILEDESC', 'test_tablespace_options - test extension-defined tablespace options',])
+endif
+
+test_tablespace_options = shared_module('test_tablespace_options',
+  test_tablespace_options_sources,
+  kwargs: pg_test_mod_args,
+)
+test_install_libs += test_tablespace_options
+
+tests += {
+  'name': 'test_tablespace_options',
+  'sd': meson.current_source_dir(),
+  'bd': meson.current_build_dir(),
+  'tap': {
+    'tests': [
+      't/001_tablespace_options.pl',
+    ],
+  },
+}
diff --git a/src/test/modules/test_tablespace_options/t/001_tablespace_options.pl b/src/test/modules/test_tablespace_options/t/001_tablespace_options.pl
new file mode 100644
index 00000000000..35a80537c39
--- /dev/null
+++ b/src/test/modules/test_tablespace_options/t/001_tablespace_options.pl
@@ -0,0 +1,185 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Tablespace options defined by an extension
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $node = PostgreSQL::Test::Cluster->new('main');
+$node->init;
+$node->append_conf(
+	'postgresql.conf', q{
+allow_in_place_tablespaces = on
+shared_preload_libraries = 'test_tablespace_options'
+test_tablespace_options.bad_option_name = ''
+});
+$node->start;
+
+my ($ret, $stdout, $stderr);
+
+sub spcoptions
+{
+	my $name = shift;
+	return $node->safe_psql('postgres',
+		"SELECT spcoptions FROM pg_tablespace WHERE spcname = '$name'");
+}
+
+# Qualified names work without quotes, next to built-in options
+$node->safe_psql('postgres',
+	"CREATE TABLESPACE regress_ts1 LOCATION '' WITH (test_tablespace_options.level = 3, test_tablespace_options.mode = safe, random_page_cost = 2)"
+);
+is( spcoptions('regress_ts1'),
+	'{test_tablespace_options.level=3,test_tablespace_options.mode=safe,random_page_cost=2}',
+	'CREATE TABLESPACE with extension options');
+
+$node->safe_psql('postgres',
+	"ALTER TABLESPACE regress_ts1 SET (\"test_tablespace_options.flag\" = true, test_tablespace_options.level = 4)"
+);
+is( spcoptions('regress_ts1'),
+	'{test_tablespace_options.mode=safe,random_page_cost=2,test_tablespace_options.flag=true,test_tablespace_options.level=4}',
+	'ALTER TABLESPACE SET with quoted and unquoted names');
+
+$node->safe_psql('postgres',
+	"ALTER TABLESPACE regress_ts1 RESET (test_tablespace_options.flag)");
+is( spcoptions('regress_ts1'),
+	'{test_tablespace_options.mode=safe,random_page_cost=2,test_tablespace_options.level=4}',
+	'ALTER TABLESPACE RESET with an unquoted name');
+
+# Values are validated
+($ret, $stdout, $stderr) = $node->psql('postgres',
+	"ALTER TABLESPACE regress_ts1 SET (test_tablespace_options.level = 11)");
+like(
+	$stderr,
+	qr/value 11 out of bounds for option "test_tablespace_options.level"/,
+	'out of range value is rejected');
+
+($ret, $stdout, $stderr) = $node->psql('postgres',
+	"ALTER TABLESPACE regress_ts1 SET (test_tablespace_options.mode = slow)");
+like(
+	$stderr,
+	qr/invalid value for enum option "test_tablespace_options.mode": slow/,
+	'invalid enum value is rejected');
+
+($ret, $stdout, $stderr) = $node->psql('postgres',
+	"ALTER TABLESPACE regress_ts1 SET (test_tablespace_options.nonexistent = 1)"
+);
+like(
+	$stderr,
+	qr/unrecognized parameter "test_tablespace_options.nonexistent"/,
+	'unknown qualified name is rejected');
+
+($ret, $stdout, $stderr) = $node->psql('postgres',
+	"ALTER TABLESPACE regress_ts1 SET (other.level = 1)");
+like(
+	$stderr,
+	qr/unrecognized parameter "other.level"/,
+	'unknown prefix is rejected');
+
+# Values that need quoting survive pg_dumpall
+$node->safe_psql('postgres',
+	"CREATE TABLESPACE regress_ts2 LOCATION '' WITH (test_tablespace_options.label = 'a,b''); SELECT 1; --')"
+);
+my $before = $node->safe_psql('postgres',
+	"SELECT spcname, spcoptions FROM pg_tablespace WHERE spcname LIKE 'regress_ts%' ORDER BY 1"
+);
+
+my $dumpfile = $node->basedir . '/tablespaces.sql';
+$node->command_ok(
+	[
+		'pg_dumpall', '--tablespaces-only',
+		'--file' => $dumpfile,
+		'--port' => $node->port,
+	],
+	'pg_dumpall of tablespaces');
+
+$node->safe_psql('postgres', 'DROP TABLESPACE regress_ts1');
+$node->safe_psql('postgres', 'DROP TABLESPACE regress_ts2');
+$node->command_ok(
+	[
+		'psql', '--no-psqlrc',
+		'--set' => 'ON_ERROR_STOP=1',
+		'--file' => $dumpfile,
+		'--port' => $node->port,
+		'postgres',
+	],
+	'restore of tablespaces');
+is( $node->safe_psql('postgres',
+		"SELECT spcname, spcoptions FROM pg_tablespace WHERE spcname LIKE 'regress_ts%' ORDER BY 1"
+	),
+	$before,
+	'tablespace options survive dump and restore');
+
+# Without the extension, its options have no effect and block other changes
+$node->safe_psql('postgres',
+	"CREATE TABLE regress_tab (i int) TABLESPACE regress_ts1");
+$node->adjust_conf('postgresql.conf', 'shared_preload_libraries', "''");
+$node->restart;
+
+is($node->safe_psql('postgres', 'SELECT count(*) FROM regress_tab'),
+	'0', 'table in tablespace with unknown options is usable');
+
+($ret, $stdout, $stderr) = $node->psql('postgres',
+	"ALTER TABLESPACE regress_ts1 SET (seq_page_cost = 2)");
+like(
+	$stderr,
+	qr/unrecognized parameter "test_tablespace_options\.\w+"/,
+	'SET fails while unknown options are set');
+
+($ret, $stdout, $stderr) = $node->psql('postgres',
+	"ALTER TABLESPACE regress_ts1 RESET (test_tablespace_options.level)");
+like(
+	$stderr,
+	qr/unrecognized parameter "test_tablespace_options.mode"/,
+	'RESET of only some unknown options fails');
+
+$node->safe_psql('postgres',
+	"ALTER TABLESPACE regress_ts1 RESET (test_tablespace_options.level, test_tablespace_options.mode)"
+);
+is(spcoptions('regress_ts1'), '{random_page_cost=2}',
+	'RESET of all unknown options works');
+
+# Loading the library later is refused
+($ret, $stdout, $stderr) =
+  $node->psql('postgres', "LOAD 'test_tablespace_options'");
+like(
+	$stderr,
+	qr/FATAL:  could not register tablespace option "test_tablespace_options.level"/,
+	'LOAD outside shared_preload_libraries is refused');
+is($node->safe_psql('postgres', 'SELECT 1'),
+	'1', 'server survives the refused LOAD');
+
+# Invalid names stop the server from starting
+$node->adjust_conf('postgresql.conf', 'shared_preload_libraries',
+	"'test_tablespace_options'");
+$node->stop;
+
+my @bad_names = (
+	[ 'nodot', qr/invalid tablespace option name "nodot"/ ],
+	[ 'bad-char.option', qr/invalid tablespace option name "bad-char.option"/ ],
+	[
+		'a' x 32 . '.' . 'b' x 32,
+		qr/tablespace option name "a{32}\.b{32}" is too long/
+	]);
+
+foreach my $case (@bad_names)
+{
+	my ($name, $error) = @$case;
+	my $log_offset = -s $node->logfile;
+
+	$node->adjust_conf('postgresql.conf',
+		'test_tablespace_options.bad_option_name', "'$name'");
+	ok(!$node->start(fail_ok => 1), "server does not start with option \"$name\"");
+	ok($node->log_contains($error, $log_offset),
+		"error reported for option \"$name\"");
+}
+
+$node->adjust_conf('postgresql.conf',
+	'test_tablespace_options.bad_option_name', "''");
+$node->start;
+$node->stop;
+
+done_testing();
diff --git a/src/test/modules/test_tablespace_options/test_tablespace_options.c b/src/test/modules/test_tablespace_options/test_tablespace_options.c
new file mode 100644
index 00000000000..4d87c5fa866
--- /dev/null
+++ b/src/test/modules/test_tablespace_options/test_tablespace_options.c
@@ -0,0 +1,66 @@
+/*--------------------------------------------------------------------------
+ *
+ * test_tablespace_options.c
+ *		Test module defining tablespace options.
+ *
+ * Copyright (c) 2026, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		src/test/modules/test_tablespace_options/test_tablespace_options.c
+ *
+ * -------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/reloptions.h"
+#include "fmgr.h"
+#include "utils/guc.h"
+
+PG_MODULE_MAGIC;
+
+/* name of an extra, invalid option to register, for testing the checks */
+static char *bad_option_name = NULL;
+
+static relopt_enum_elt_def mode_values[] =
+{
+	{"fast", 0},
+	{"safe", 1},
+	{NULL, 0}
+};
+
+void
+_PG_init(void)
+{
+	DefineCustomStringVariable("test_tablespace_options.bad_option_name",
+							   "Name of an invalid tablespace option to register.",
+							   NULL,
+							   &bad_option_name,
+							   "",
+							   PGC_SIGHUP,
+							   0,
+							   NULL, NULL, NULL);
+
+	MarkGUCPrefixReserved("test_tablespace_options");
+
+	add_int_reloption(RELOPT_KIND_TABLESPACE,
+					  "test_tablespace_options.level",
+					  "Integer tablespace option for testing",
+					  1, 0, 10, AccessExclusiveLock);
+	add_string_reloption(RELOPT_KIND_TABLESPACE,
+						 "test_tablespace_options.label",
+						 "String tablespace option for testing",
+						 NULL, NULL, AccessExclusiveLock);
+	add_enum_reloption(RELOPT_KIND_TABLESPACE,
+					   "test_tablespace_options.mode",
+					   "Enum tablespace option for testing",
+					   mode_values, 0,
+					   "Valid values are \"fast\" and \"safe\".",
+					   AccessExclusiveLock);
+	add_bool_reloption(RELOPT_KIND_TABLESPACE,
+					   "test_tablespace_options.flag",
+					   NULL, false, AccessExclusiveLock);
+
+	if (bad_option_name[0] != '\0')
+		add_bool_reloption(RELOPT_KIND_TABLESPACE, bad_option_name,
+						   NULL, false, AccessExclusiveLock);
+}
-- 
2.56.0

