From 37448aa3999cecd00ad3da0e13c670d3bd0ff2fa Mon Sep 17 00:00:00 2001 From: Zsolt Parragi Date: Wed, 18 Nov 2026 19:10:16 +0000 Subject: [PATCH v1] Quote reloption values too long to be identifiers pg_dump's appendReloptionsArray() and ruleutils' get_reloptions() print an option value without quotes when it is a valid identifier that needs no quoting. A string value of NAMEDATALEN bytes or more passes that test, so when the output is replayed the scanner truncates it, emitting only a NOTICE, and the option silently gets a different value. None of the built-in options can have such a value, but string options defined by extensions can, for example dummy_index_am's. Quote values that are too long to be identifiers. --- src/backend/utils/adt/ruleutils.c | 10 +++++----- src/fe_utils/string_utils.c | 10 +++++----- .../modules/dummy_index_am/expected/reloptions.out | 9 +++++++++ src/test/modules/dummy_index_am/sql/reloptions.sql | 5 +++++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index e9c1c9db1b1..ed170ec2a81 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -13906,12 +13906,12 @@ get_reloptions(StringInfo buf, Datum reloptions) /* * In general we need to quote the value; but to avoid unnecessary * clutter, do not quote if it is an identifier that would not need - * quoting. (We could also allow numbers, but that is a bit trickier - * than it looks --- for example, are leading zeroes significant? We - * don't want to assume very much here about what custom reloptions - * might mean.) + * quoting and is short enough not to be truncated as one. (We could + * also allow numbers, but that is a bit trickier than it looks --- + * for example, are leading zeroes significant? We don't want to + * assume very much here about what custom reloptions might mean.) */ - if (quote_identifier(value) == value) + if (strlen(value) < NAMEDATALEN && quote_identifier(value) == value) appendStringInfoString(buf, value); else simple_quote_literal(buf, value); diff --git a/src/fe_utils/string_utils.c b/src/fe_utils/string_utils.c index 78ffe6c890a..bafaecb84b8 100644 --- a/src/fe_utils/string_utils.c +++ b/src/fe_utils/string_utils.c @@ -1103,12 +1103,12 @@ appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, /* * In general we need to quote the value; but to avoid unnecessary * clutter, do not quote if it is an identifier that would not need - * quoting. (We could also allow numbers, but that is a bit trickier - * than it looks --- for example, are leading zeroes significant? We - * don't want to assume very much here about what custom reloptions - * might mean.) + * quoting and is short enough not to be truncated as one. (We could + * also allow numbers, but that is a bit trickier than it looks --- + * for example, are leading zeroes significant? We don't want to + * assume very much here about what custom reloptions might mean.) */ - if (strcmp(fmtId(value), value) == 0) + if (strlen(value) < NAMEDATALEN && strcmp(fmtId(value), value) == 0) appendPQExpBufferStr(buffer, value); else appendStringLiteral(buffer, value, encoding, std_strings); diff --git a/src/test/modules/dummy_index_am/expected/reloptions.out b/src/test/modules/dummy_index_am/expected/reloptions.out index 3b06d514995..f77ed52066d 100644 --- a/src/test/modules/dummy_index_am/expected/reloptions.out +++ b/src/test/modules/dummy_index_am/expected/reloptions.out @@ -76,6 +76,15 @@ SELECT unnest(reloptions) FROM pg_class WHERE relname = 'dummy_test_idx'; -------- (0 rows) +-- A value too long to be an identifier must be quoted when deparsed +ALTER INDEX dummy_test_idx SET (option_string_null = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); +SELECT pg_get_indexdef('dummy_test_idx'::regclass); + pg_get_indexdef +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + CREATE INDEX dummy_test_idx ON public.dummy_test_tab USING dummy_index_am (i) WITH (option_string_null='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') +(1 row) + +ALTER INDEX dummy_test_idx RESET (option_string_null); -- Cross-type checks for reloption values -- Integer ALTER INDEX dummy_test_idx SET (option_int = 3.3); -- ok diff --git a/src/test/modules/dummy_index_am/sql/reloptions.sql b/src/test/modules/dummy_index_am/sql/reloptions.sql index 2cdff0820f6..984db747aee 100644 --- a/src/test/modules/dummy_index_am/sql/reloptions.sql +++ b/src/test/modules/dummy_index_am/sql/reloptions.sql @@ -49,6 +49,11 @@ ALTER INDEX dummy_test_idx RESET (option_string_val); ALTER INDEX dummy_test_idx RESET (option_string_null); SELECT unnest(reloptions) FROM pg_class WHERE relname = 'dummy_test_idx'; +-- A value too long to be an identifier must be quoted when deparsed +ALTER INDEX dummy_test_idx SET (option_string_null = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); +SELECT pg_get_indexdef('dummy_test_idx'::regclass); +ALTER INDEX dummy_test_idx RESET (option_string_null); + -- Cross-type checks for reloption values -- Integer ALTER INDEX dummy_test_idx SET (option_int = 3.3); -- ok -- 2.56.0