Skip to content

Commit 82406d6

Browse files
committed
Fix deparsing of ON CONFLICT arbiter WHERE clauses.
The parser doesn't allow qualification of column names appearing in these clauses, but ruleutils.c would sometimes qualify them, leading to dump/reload failures. Per bug #13891 from Onder Kalaci. (In passing, make stanzas in ruleutils.c that save/restore varprefix more consistent.) Peter Geoghegan
1 parent bd0302c commit 82406d6

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5526,9 +5526,21 @@ get_insert_query_def(Query *query, deparse_context *context)
55265526
/* Add a WHERE clause (for partial indexes) if given */
55275527
if (confl->arbiterWhere != NULL)
55285528
{
5529+
bool save_varprefix;
5530+
5531+
/*
5532+
* Force non-prefixing of Vars, since parser assumes that they
5533+
* belong to target relation. WHERE clause does not use
5534+
* InferenceElem, so this is separately required.
5535+
*/
5536+
save_varprefix = context->varprefix;
5537+
context->varprefix = false;
5538+
55295539
appendContextKeyword(context, " WHERE ",
55305540
-PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
55315541
get_rule_expr(confl->arbiterWhere, context, false);
5542+
5543+
context->varprefix = save_varprefix;
55325544
}
55335545
}
55345546
else if (confl->constraint != InvalidOid)
@@ -7950,13 +7962,14 @@ get_rule_expr(Node *node, deparse_context *context,
79507962
case T_InferenceElem:
79517963
{
79527964
InferenceElem *iexpr = (InferenceElem *) node;
7953-
bool varprefix = context->varprefix;
7965+
bool save_varprefix;
79547966
bool need_parens;
79557967

79567968
/*
79577969
* InferenceElem can only refer to target relation, so a
7958-
* prefix is never useful.
7970+
* prefix is not useful, and indeed would cause parse errors.
79597971
*/
7972+
save_varprefix = context->varprefix;
79607973
context->varprefix = false;
79617974

79627975
/*
@@ -7976,7 +7989,7 @@ get_rule_expr(Node *node, deparse_context *context,
79767989
if (need_parens)
79777990
appendStringInfoChar(buf, ')');
79787991

7979-
context->varprefix = varprefix;
7992+
context->varprefix = save_varprefix;
79807993

79817994
if (iexpr->infercollid)
79827995
appendStringInfo(buf, " COLLATE %s",

src/test/regress/expected/rules.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,7 +2832,7 @@ SELECT definition FROM pg_rules WHERE tablename = 'hats' ORDER BY rulename;
28322832
CREATE RULE hat_nosert AS +
28332833
ON INSERT TO hats DO INSTEAD INSERT INTO hat_data (hat_name, hat_color) +
28342834
VALUES (new.hat_name, new.hat_color) ON CONFLICT(hat_name COLLATE "C" bpchar_pattern_ops)+
2835-
WHERE (hat_data.hat_color = 'green'::bpchar) DO NOTHING +
2835+
WHERE (hat_color = 'green'::bpchar) DO NOTHING +
28362836
RETURNING hat_data.hat_name, +
28372837
hat_data.hat_color;
28382838
(1 row)
@@ -2857,7 +2857,7 @@ SELECT tablename, rulename, definition FROM pg_rules
28572857
hats | hat_nosert | CREATE RULE hat_nosert AS +
28582858
| | ON INSERT TO hats DO INSTEAD INSERT INTO hat_data (hat_name, hat_color) +
28592859
| | VALUES (new.hat_name, new.hat_color) ON CONFLICT(hat_name COLLATE "C" bpchar_pattern_ops)+
2860-
| | WHERE (hat_data.hat_color = 'green'::bpchar) DO NOTHING +
2860+
| | WHERE (hat_color = 'green'::bpchar) DO NOTHING +
28612861
| | RETURNING hat_data.hat_name, +
28622862
| | hat_data.hat_color;
28632863
(1 row)

0 commit comments

Comments
 (0)