Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bdd096f

Browse files
committed
Fix planner's row-mark code for inheritance from a foreign table.
Commit 428b260 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
1 parent 762fe98 commit bdd096f

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7190,6 +7190,92 @@ select * from bar where f1 in (select f1 from foo) for share;
71907190
4 | 44
71917191
(4 rows)
71927192

7193+
-- Now check SELECT FOR UPDATE/SHARE with an inherited source table,
7194+
-- where the parent is itself a foreign table
7195+
create table loct4 (f1 int, f2 int, f3 int);
7196+
create foreign table foo2child (f3 int) inherits (foo2)
7197+
server loopback options (table_name 'loct4');
7198+
NOTICE: moving and merging column "f3" with inherited definition
7199+
DETAIL: User-specified column moved to the position of the inherited column.
7200+
explain (verbose, costs off)
7201+
select * from bar where f1 in (select f1 from foo2) for share;
7202+
QUERY PLAN
7203+
---------------------------------------------------------------------------------------
7204+
LockRows
7205+
Output: bar.f1, bar.f2, bar.ctid, foo2.*, bar.*, bar.tableoid, foo2.tableoid
7206+
-> Hash Join
7207+
Output: bar.f1, bar.f2, bar.ctid, foo2.*, bar.*, bar.tableoid, foo2.tableoid
7208+
Inner Unique: true
7209+
Hash Cond: (bar.f1 = foo2.f1)
7210+
-> Append
7211+
-> Seq Scan on public.bar
7212+
Output: bar.f1, bar.f2, bar.ctid, bar.*, bar.tableoid
7213+
-> Foreign Scan on public.bar2
7214+
Output: bar2.f1, bar2.f2, bar2.ctid, bar2.*, bar2.tableoid
7215+
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR SHARE
7216+
-> Hash
7217+
Output: foo2.*, foo2.f1, foo2.tableoid
7218+
-> HashAggregate
7219+
Output: foo2.*, foo2.f1, foo2.tableoid
7220+
Group Key: foo2.f1
7221+
-> Append
7222+
-> Foreign Scan on public.foo2
7223+
Output: foo2.*, foo2.f1, foo2.tableoid
7224+
Remote SQL: SELECT f1, f2, f3 FROM public.loct1
7225+
-> Foreign Scan on public.foo2child
7226+
Output: foo2child.*, foo2child.f1, foo2child.tableoid
7227+
Remote SQL: SELECT f1, f2, f3 FROM public.loct4
7228+
(24 rows)
7229+
7230+
select * from bar where f1 in (select f1 from foo2) for share;
7231+
f1 | f2
7232+
----+----
7233+
2 | 22
7234+
4 | 44
7235+
(2 rows)
7236+
7237+
drop foreign table foo2child;
7238+
-- And with a local child relation of the foreign table parent
7239+
create table foo2child (f3 int) inherits (foo2);
7240+
NOTICE: moving and merging column "f3" with inherited definition
7241+
DETAIL: User-specified column moved to the position of the inherited column.
7242+
explain (verbose, costs off)
7243+
select * from bar where f1 in (select f1 from foo2) for share;
7244+
QUERY PLAN
7245+
-------------------------------------------------------------------------------------------------------
7246+
LockRows
7247+
Output: bar.f1, bar.f2, bar.ctid, foo2.*, bar.*, bar.tableoid, foo2.ctid, foo2.tableoid
7248+
-> Hash Join
7249+
Output: bar.f1, bar.f2, bar.ctid, foo2.*, bar.*, bar.tableoid, foo2.ctid, foo2.tableoid
7250+
Inner Unique: true
7251+
Hash Cond: (bar.f1 = foo2.f1)
7252+
-> Append
7253+
-> Seq Scan on public.bar
7254+
Output: bar.f1, bar.f2, bar.ctid, bar.*, bar.tableoid
7255+
-> Foreign Scan on public.bar2
7256+
Output: bar2.f1, bar2.f2, bar2.ctid, bar2.*, bar2.tableoid
7257+
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR SHARE
7258+
-> Hash
7259+
Output: foo2.*, foo2.f1, foo2.ctid, foo2.tableoid
7260+
-> HashAggregate
7261+
Output: foo2.*, foo2.f1, foo2.ctid, foo2.tableoid
7262+
Group Key: foo2.f1
7263+
-> Append
7264+
-> Foreign Scan on public.foo2
7265+
Output: foo2.*, foo2.f1, foo2.ctid, foo2.tableoid
7266+
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1
7267+
-> Seq Scan on public.foo2child
7268+
Output: foo2child.*, foo2child.f1, foo2child.ctid, foo2child.tableoid
7269+
(23 rows)
7270+
7271+
select * from bar where f1 in (select f1 from foo2) for share;
7272+
f1 | f2
7273+
----+----
7274+
2 | 22
7275+
4 | 44
7276+
(2 rows)
7277+
7278+
drop table foo2child;
71937279
-- Check UPDATE with inherited target and an inherited source table
71947280
explain (verbose, costs off)
71957281
update bar set f2 = f2 + 100 where f1 in (select f1 from foo);

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,27 @@ explain (verbose, costs off)
18341834
select * from bar where f1 in (select f1 from foo) for share;
18351835
select * from bar where f1 in (select f1 from foo) for share;
18361836

1837+
-- Now check SELECT FOR UPDATE/SHARE with an inherited source table,
1838+
-- where the parent is itself a foreign table
1839+
create table loct4 (f1 int, f2 int, f3 int);
1840+
create foreign table foo2child (f3 int) inherits (foo2)
1841+
server loopback options (table_name 'loct4');
1842+
1843+
explain (verbose, costs off)
1844+
select * from bar where f1 in (select f1 from foo2) for share;
1845+
select * from bar where f1 in (select f1 from foo2) for share;
1846+
1847+
drop foreign table foo2child;
1848+
1849+
-- And with a local child relation of the foreign table parent
1850+
create table foo2child (f3 int) inherits (foo2);
1851+
1852+
explain (verbose, costs off)
1853+
select * from bar where f1 in (select f1 from foo2) for share;
1854+
select * from bar where f1 in (select f1 from foo2) for share;
1855+
1856+
drop table foo2child;
1857+
18371858
-- Check UPDATE with inherited target and an inherited source table
18381859
explain (verbose, costs off)
18391860
update bar set f2 = f2 + 100 where f1 in (select f1 from foo);

src/backend/optimizer/util/inherit.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,25 @@ expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel,
231231
char resname[32];
232232
List *newvars = NIL;
233233

234-
/* The old PlanRowMark should already have necessitated adding TID */
235-
Assert(old_allMarkTypes & ~(1 << ROW_MARK_COPY));
234+
/* Add TID junk Var if needed, unless we had it already */
235+
if (new_allMarkTypes & ~(1 << ROW_MARK_COPY) &&
236+
!(old_allMarkTypes & ~(1 << ROW_MARK_COPY)))
237+
{
238+
/* Need to fetch TID */
239+
var = makeVar(oldrc->rti,
240+
SelfItemPointerAttributeNumber,
241+
TIDOID,
242+
-1,
243+
InvalidOid,
244+
0);
245+
snprintf(resname, sizeof(resname), "ctid%u", oldrc->rowmarkId);
246+
tle = makeTargetEntry((Expr *) var,
247+
list_length(root->processed_tlist) + 1,
248+
pstrdup(resname),
249+
true);
250+
root->processed_tlist = lappend(root->processed_tlist, tle);
251+
newvars = lappend(newvars, var);
252+
}
236253

237254
/* Add whole-row junk Var if needed, unless we had it already */
238255
if ((new_allMarkTypes & (1 << ROW_MARK_COPY)) &&

0 commit comments

Comments
 (0)