From 889592344c48d3965567f331b4ea89dfe6447bce Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 2 Jun 2021 14:38:14 -0400 Subject: Fix planner's row-mark code for inheritance from a foreign table. Commit 428b260f8 broke planning of cases where row marks are needed (SELECT FOR UPDATE, etc) and one of the query's tables is a foreign table that has regular table(s) as inheritance children. We got the reverse case right, but apparently were thinking that foreign tables couldn't be inheritance parents. Not so; so we need to be able to add a CTID junk column while adding a new child, not only a wholerow junk column. Back-patch to v12 where the faulty code came in. Amit Langote Discussion: https://postgr.es/m/CA+HiwqEmo3FV1LAQ4TVyS2h1WM=kMkZUmbNuZSCnfHvMcUcPeA@mail.gmail.com --- src/backend/optimizer/util/inherit.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c index 13f67ab7449..992ef87b9da 100644 --- a/src/backend/optimizer/util/inherit.c +++ b/src/backend/optimizer/util/inherit.c @@ -232,8 +232,25 @@ expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel, char resname[32]; List *newvars = NIL; - /* The old PlanRowMark should already have necessitated adding TID */ - Assert(old_allMarkTypes & ~(1 << ROW_MARK_COPY)); + /* Add TID junk Var if needed, unless we had it already */ + if (new_allMarkTypes & ~(1 << ROW_MARK_COPY) && + !(old_allMarkTypes & ~(1 << ROW_MARK_COPY))) + { + /* Need to fetch TID */ + var = makeVar(oldrc->rti, + SelfItemPointerAttributeNumber, + TIDOID, + -1, + InvalidOid, + 0); + snprintf(resname, sizeof(resname), "ctid%u", oldrc->rowmarkId); + tle = makeTargetEntry((Expr *) var, + list_length(root->processed_tlist) + 1, + pstrdup(resname), + true); + root->processed_tlist = lappend(root->processed_tlist, tle); + newvars = lappend(newvars, var); + } /* Add whole-row junk Var if needed, unless we had it already */ if ((new_allMarkTypes & (1 << ROW_MARK_COPY)) && -- cgit v1.2.3