Skip to content

Commit da05d0e

Browse files
committed
Don't mess up pstate->p_next_resno in transformOnConflictClause().
transformOnConflictClause incremented p_next_resno while generating the phony targetlist for the EXCLUDED pseudo-rel. Then that field got incremented some more during transformTargetList, possibly leading to free_parsestate concluding that we'd overrun the allowed length of a tlist, as reported by Justin Pryzby. We could fix this by resetting p_next_resno to 1 after using it for the EXCLUDED pseudo-rel tlist, but it seems easier and less coupled to other places if we just don't use that field at all in this loop. (Note that this doesn't change anything about the resnos that end up appearing in the main target list, because those are all replaced with target-column numbers by updateTargetListEntry.) In passing, fix incorrect type OID assigned to the whole-row Var for "EXCLUDED.*" (somehow this escaped having any bad consequences so far, but it's certainly wrong); remove useless assignment to var->location; pstrdup the column names in case of a relcache flush; and improve nearby comments. Back-patch to 9.5 where ON CONFLICT was introduced. Report: https://postgr.es/m/20161204163237.GA8030@telsasoft.com
1 parent 7840545 commit da05d0e

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/backend/parser/analyze.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -986,11 +986,10 @@ transformOnConflictClause(ParseState *pstate,
986986
exclRelIndex = list_length(pstate->p_rtable);
987987

988988
/*
989-
* Build a targetlist for the EXCLUDED pseudo relation. Have to be
990-
* careful to use resnos that correspond to attnos of the underlying
991-
* relation.
989+
* Build a targetlist representing the columns of the EXCLUDED pseudo
990+
* relation. Have to be careful to use resnos that correspond to
991+
* attnos of the underlying relation.
992992
*/
993-
Assert(pstate->p_next_resno == 1);
994993
for (attno = 0; attno < targetrel->rd_rel->relnatts; attno++)
995994
{
996995
Form_pg_attribute attr = targetrel->rd_att->attrs[attno];
@@ -1011,14 +1010,11 @@ transformOnConflictClause(ParseState *pstate,
10111010
attr->atttypid, attr->atttypmod,
10121011
attr->attcollation,
10131012
0);
1014-
var->location = -1;
1015-
1016-
name = NameStr(attr->attname);
1013+
name = pstrdup(NameStr(attr->attname));
10171014
}
10181015

1019-
Assert(pstate->p_next_resno == attno + 1);
10201016
te = makeTargetEntry((Expr *) var,
1021-
pstate->p_next_resno++,
1017+
attno + 1,
10221018
name,
10231019
false);
10241020

@@ -1027,15 +1023,16 @@ transformOnConflictClause(ParseState *pstate,
10271023
}
10281024

10291025
/*
1030-
* Additionally add a whole row tlist entry for EXCLUDED. That's
1031-
* really only needed for ruleutils' benefit, which expects to find
1032-
* corresponding entries in child tlists. Alternatively we could do
1033-
* this only when required, but that doesn't seem worth the trouble.
1026+
* Add a whole-row-Var entry to support references to "EXCLUDED.*".
1027+
* Like the other entries in exclRelTlist, its resno must match the
1028+
* Var's varattno, else the wrong things happen while resolving
1029+
* references in setrefs.c. This is against normal conventions for
1030+
* targetlists, but it's okay since we don't use this as a real tlist.
10341031
*/
10351032
var = makeVar(exclRelIndex, InvalidAttrNumber,
1036-
RelationGetRelid(targetrel),
1033+
targetrel->rd_rel->reltype,
10371034
-1, InvalidOid, 0);
1038-
te = makeTargetEntry((Expr *) var, 0, NULL, true);
1035+
te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
10391036
exclRelTlist = lappend(exclRelTlist, te);
10401037

10411038
/*

0 commit comments

Comments
 (0)