From adbd6b4e1dc9196a5c185521985c47de91b2fe69 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Wed, 18 Nov 2026 19:24:51 +0900 Subject: [PATCH 4/9] Print routine parameters by number inside DEFINE when deparsing A SQL-standard routine body is stored parsed, and pg_dump recreates it from the text get_parameter() prints. A named parameter read in a query with a range table is printed qualified with the routine name, but DEFINE rejects every qualified name, since the qualifier slot belongs to the pattern variables. Such a routine failed to restore with "qualified expression ... is not allowed in DEFINE clause". Print every parameter as $n inside DEFINE. The bare name is not an option there, as a column of the same name would capture it. --- doc/src/sgml/ref/select.sgml | 5 ++- src/backend/executor/README.rpr | 7 ++- src/backend/utils/adt/ruleutils.c | 8 +++- src/test/regress/expected/rpr_base.out | 60 ++++++++++++++++++++++++++ src/test/regress/sql/rpr_base.sql | 45 +++++++++++++++++++ 5 files changed, 122 insertions(+), 3 deletions(-) diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 31fdae7d94c..f5e897117ed 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -1223,7 +1223,10 @@ DEFINE definition_variable_name AS rejected there. To read a field of a composite parameter or record variable, parenthesize it, as in (p).amount; that is field selection on a value rather than a qualified name, so the slot - stays free. + stays free. A parameter that a column of the same name shadows can be + read only by its number, as in $1. When a + SQL-standard routine body is displayed, every parameter read in a + DEFINE condition is shown by its number. diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index c2a953c962e..170280fd95d 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -375,6 +375,9 @@ variable, and every other qualified name -- including one that p_post_columnref_hook resolves, such as a SQL function's parameter or a PL/pgSQL variable spelled with its routine name or block label. Unqualified, those remain readable; it is the spelling that is refused, not the value. +A parameter shadowed by a column of the same name is reachable only as $n, +and get_parameter() prints every parameter that way inside DEFINE, so a +deparsed SQL-standard routine body re-parses. A pattern variable qualifier such as A.price is decided before name resolution, by matching the qualifier against the PATTERN variable names @@ -2599,7 +2602,9 @@ parser's downcased comparison. inRPRDefine is part of the deparse context, so every routine that builds one initializes it, and get_rule_define() saves and restores it around the clause -the way it does varprefix. +the way it does varprefix. get_parameter() reads the same flag to print a +routine parameter as $n, since the spelling qualified with the routine name is +rejected in DEFINE and the bare one may be captured by a column (III-4). XIII-7. DEFINE Vars Under a GROUP RTE diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index d3e3b69ec9e..ce7e97e92e3 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -9399,7 +9399,13 @@ get_parameter(Param *param, deparse_context *context) { char *argname = dpns->argnames[param->paramid - 1]; - if (argname) + /* + * Inside a DEFINE clause a name qualified with the function name + * is rejected, since the qualifier slot belongs to the pattern + * variables, and a bare name would bind to a column of the same + * name. Print $N there, which always reads the parameter. + */ + if (argname && !context->inRPRDefine) { bool should_qualify = false; ListCell *lc; diff --git a/src/test/regress/expected/rpr_base.out b/src/test/regress/expected/rpr_base.out index 1f16f16e57a..6b30235ed33 100644 --- a/src/test/regress/expected/rpr_base.out +++ b/src/test/regress/expected/rpr_base.out @@ -6510,6 +6510,66 @@ SELECT pg_get_viewdef('rpr_quant_n_plus_v'::regclass); a AS (val > 0)); (1 row) +-- A routine parameter read in DEFINE deparses as $n. +CREATE TABLE rpr_fnp (id INT, v INT); +INSERT INTO rpr_fnp SELECT g, g % 5 FROM generate_series(1, 10) g; +CREATE FUNCTION rpr_fnp_named(threshold INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY id) FROM ( + SELECT id, count(*) OVER w AS c FROM rpr_fnp + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS v > threshold)) s; +END; +-- column v would capture a bare v, so the parameter must stay $1 +CREATE FUNCTION rpr_fnp_dollar(v INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY id) FROM ( + SELECT id, count(*) OVER w AS c FROM rpr_fnp + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS id > $1)) s; +END; +-- $1 in DEFINE must still match the GROUP BY expression v + k +CREATE FUNCTION rpr_fnp_group(k INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY s) FROM ( + SELECT v + k AS s, count(*) OVER w AS c FROM rpr_fnp GROUP BY v + k + WINDOW w AS (ORDER BY v + k ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS v + k > 2)) s; +END; +-- WHERE keeps the qualified name while DEFINE prints $1 +CREATE FUNCTION rpr_fnp_where(threshold INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY id) FROM ( + SELECT id, count(*) OVER w AS c FROM rpr_fnp WHERE v < threshold + 3 + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS v > threshold)) s; +END; +SELECT proname, (regexp_match(pg_get_functiondef(oid), '^ a AS (.*)$', 'n'))[1] AS define +FROM pg_proc WHERE proname LIKE 'rpr\_fnp\_%' ORDER BY proname; + proname | define +----------------+--------------------- + rpr_fnp_dollar | (id > $1))) s; + rpr_fnp_group | ((v + $1) > 2))) s; + rpr_fnp_named | (v > $1))) s; + rpr_fnp_where | (v > $1))) s; +(4 rows) + +SELECT (regexp_match(pg_get_functiondef('rpr_fnp_where'::regproc), '^\s*WHERE (.*)$', 'n'))[1] AS "where"; + where +--------------------------------------------- + (rpr_fnp.v < (rpr_fnp_where.threshold + 3)) +(1 row) + +-- recreate each from its definition +\set ECHO none +SELECT rpr_fnp_named(1) AS named, rpr_fnp_dollar(5) AS dollar, + rpr_fnp_group(1) AS "group", rpr_fnp_where(1) AS "where"; + named | dollar | group | where +-----------------------+-----------------------+-------------+------------------- + {0,3,0,0,0,0,3,0,0,0} | {0,0,0,0,0,5,0,0,0,0} | {0,0,3,0,0} | {0,2,0,0,0,2,0,0} +(1 row) + +-- left in place so pg_dump/pg_upgrade also round-trip them -- ============================================================ -- Glued Quantifier / Alternation Tests -- ============================================================ diff --git a/src/test/regress/sql/rpr_base.sql b/src/test/regress/sql/rpr_base.sql index adbedd1ea80..ebe0a50a7e3 100644 --- a/src/test/regress/sql/rpr_base.sql +++ b/src/test/regress/sql/rpr_base.sql @@ -3792,6 +3792,51 @@ WINDOW w AS (ORDER BY id DEFINE A AS val > 0); SELECT pg_get_viewdef('rpr_quant_n_plus_v'::regclass); +-- A routine parameter read in DEFINE deparses as $n. +CREATE TABLE rpr_fnp (id INT, v INT); +INSERT INTO rpr_fnp SELECT g, g % 5 FROM generate_series(1, 10) g; +CREATE FUNCTION rpr_fnp_named(threshold INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY id) FROM ( + SELECT id, count(*) OVER w AS c FROM rpr_fnp + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS v > threshold)) s; +END; +-- column v would capture a bare v, so the parameter must stay $1 +CREATE FUNCTION rpr_fnp_dollar(v INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY id) FROM ( + SELECT id, count(*) OVER w AS c FROM rpr_fnp + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS id > $1)) s; +END; +-- $1 in DEFINE must still match the GROUP BY expression v + k +CREATE FUNCTION rpr_fnp_group(k INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY s) FROM ( + SELECT v + k AS s, count(*) OVER w AS c FROM rpr_fnp GROUP BY v + k + WINDOW w AS (ORDER BY v + k ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS v + k > 2)) s; +END; +-- WHERE keeps the qualified name while DEFINE prints $1 +CREATE FUNCTION rpr_fnp_where(threshold INT) RETURNS BIGINT[] LANGUAGE sql +BEGIN ATOMIC + SELECT array_agg(c ORDER BY id) FROM ( + SELECT id, count(*) OVER w AS c FROM rpr_fnp WHERE v < threshold + 3 + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) DEFINE A AS v > threshold)) s; +END; +SELECT proname, (regexp_match(pg_get_functiondef(oid), '^ a AS (.*)$', 'n'))[1] AS define +FROM pg_proc WHERE proname LIKE 'rpr\_fnp\_%' ORDER BY proname; +SELECT (regexp_match(pg_get_functiondef('rpr_fnp_where'::regproc), '^\s*WHERE (.*)$', 'n'))[1] AS "where"; +-- recreate each from its definition +\set ECHO none +SELECT pg_get_functiondef(oid) FROM pg_proc WHERE proname LIKE 'rpr\_fnp\_%' \gexec +\set ECHO all +SELECT rpr_fnp_named(1) AS named, rpr_fnp_dollar(5) AS dollar, + rpr_fnp_group(1) AS "group", rpr_fnp_where(1) AS "where"; +-- left in place so pg_dump/pg_upgrade also round-trip them + -- ============================================================ -- Glued Quantifier / Alternation Tests -- ============================================================ -- 2.54.0 (Apple Git-157)