Skip to content

Commit f61db90

Browse files
committed
Fix postgres_fdw failure with whole-row Vars of type RECORD.
Commit 86dc900 expects that FDWs can cope with whole-row Vars for their tables, even if the Vars are marked with vartype RECORDOID. Previously, whole-row Vars generated by the planner had vartype equal to the relevant table's rowtype OID. (The point behind this change is to enable sharing of resjunk columns across inheritance child tables.) It turns out that postgres_fdw fails to cope with this, though through bad fortune none of its test cases exposed that. Things mostly work, but when we try to read back a value of such a Var, the expected rowtype is not available to record_in(). Fortunately, it's not difficult to hack up the tupdesc that controls this process to substitute the foreign table's rowtype for RECORDOID. Thus we can solve the runtime problem while still sharing the resjunk column with other tables. Per report from Alexander Pyhalov. Discussion: https://postgr.es/m/[email protected]
1 parent 8f3c06c commit f61db90

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

+25
Original file line numberDiff line numberDiff line change
@@ -5531,6 +5531,31 @@ UPDATE ft2 AS target SET (c2) = (
55315531
FROM ft2 AS src
55325532
WHERE target.c1 = src.c1
55335533
) WHERE c1 > 1100;
5534+
-- Test UPDATE involving a join that can be pushed down,
5535+
-- but a SET clause that can't be
5536+
EXPLAIN (VERBOSE, COSTS OFF)
5537+
UPDATE ft2 d SET c2 = CASE WHEN random() >= 0 THEN d.c2 ELSE 0 END
5538+
FROM ft2 AS t WHERE d.c1 = t.c1 AND d.c1 > 1100;
5539+
QUERY PLAN
5540+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5541+
Update on public.ft2 d
5542+
Remote SQL: UPDATE "S 1"."T 1" SET c2 = $2 WHERE ctid = $1
5543+
-> Foreign Scan
5544+
Output: CASE WHEN (random() >= '0'::double precision) THEN d.c2 ELSE 0 END, d.ctid, d.*, t.*
5545+
Relations: (public.ft2 d) INNER JOIN (public.ft2 t)
5546+
Remote SQL: SELECT r1.c2, r1.ctid, CASE WHEN (r1.*)::text IS NOT NULL THEN ROW(r1."C 1", r1.c2, r1.c3, r1.c4, r1.c5, r1.c6, r1.c7, r1.c8) END, CASE WHEN (r2.*)::text IS NOT NULL THEN ROW(r2."C 1", r2.c2, r2.c3, r2.c4, r2.c5, r2.c6, r2.c7, r2.c8) END FROM ("S 1"."T 1" r1 INNER JOIN "S 1"."T 1" r2 ON (((r1."C 1" = r2."C 1")) AND ((r1."C 1" > 1100)))) FOR UPDATE OF r1
5547+
-> Nested Loop
5548+
Output: d.c2, d.ctid, d.*, t.*
5549+
-> Foreign Scan on public.ft2 d
5550+
Output: d.c2, d.ctid, d.*, d.c1
5551+
Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8, ctid FROM "S 1"."T 1" WHERE (("C 1" > 1100)) ORDER BY "C 1" ASC NULLS LAST FOR UPDATE
5552+
-> Foreign Scan on public.ft2 t
5553+
Output: t.*, t.c1
5554+
Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8 FROM "S 1"."T 1" WHERE (($1::integer = "C 1"))
5555+
(14 rows)
5556+
5557+
UPDATE ft2 d SET c2 = CASE WHEN random() >= 0 THEN d.c2 ELSE 0 END
5558+
FROM ft2 AS t WHERE d.c1 = t.c1 AND d.c1 > 1100;
55345559
-- Test UPDATE/DELETE with WHERE or JOIN/ON conditions containing
55355560
-- user-defined operators/functions
55365561
ALTER SERVER loopback OPTIONS (DROP extensions);

contrib/postgres_fdw/postgres_fdw.c

+53-2
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,57 @@ postgresGetForeignPlan(PlannerInfo *root,
14391439
outer_plan);
14401440
}
14411441

1442+
/*
1443+
* Construct a tuple descriptor for the scan tuples handled by a foreign join.
1444+
*/
1445+
static TupleDesc
1446+
get_tupdesc_for_join_scan_tuples(ForeignScanState *node)
1447+
{
1448+
ForeignScan *fsplan = (ForeignScan *) node->ss.ps.plan;
1449+
EState *estate = node->ss.ps.state;
1450+
TupleDesc tupdesc;
1451+
1452+
/*
1453+
* The core code has already set up a scan tuple slot based on
1454+
* fsplan->fdw_scan_tlist, and this slot's tupdesc is mostly good enough,
1455+
* but there's one case where it isn't. If we have any whole-row row
1456+
* identifier Vars, they may have vartype RECORD, and we need to replace
1457+
* that with the associated table's actual composite type. This ensures
1458+
* that when we read those ROW() expression values from the remote server,
1459+
* we can convert them to a composite type the local server knows.
1460+
*/
1461+
tupdesc = CreateTupleDescCopy(node->ss.ss_ScanTupleSlot->tts_tupleDescriptor);
1462+
for (int i = 0; i < tupdesc->natts; i++)
1463+
{
1464+
Form_pg_attribute att = TupleDescAttr(tupdesc, i);
1465+
Var *var;
1466+
RangeTblEntry *rte;
1467+
Oid reltype;
1468+
1469+
/* Nothing to do if it's not a generic RECORD attribute */
1470+
if (att->atttypid != RECORDOID || att->atttypmod >= 0)
1471+
continue;
1472+
1473+
/*
1474+
* If we can't identify the referenced table, do nothing. This'll
1475+
* likely lead to failure later, but perhaps we can muddle through.
1476+
*/
1477+
var = (Var *) list_nth_node(TargetEntry, fsplan->fdw_scan_tlist,
1478+
i)->expr;
1479+
if (!IsA(var, Var) || var->varattno != 0)
1480+
continue;
1481+
rte = list_nth(estate->es_range_table, var->varno - 1);
1482+
if (rte->rtekind != RTE_RELATION)
1483+
continue;
1484+
reltype = get_rel_type_id(rte->relid);
1485+
if (!OidIsValid(reltype))
1486+
continue;
1487+
att->atttypid = reltype;
1488+
/* shouldn't need to change anything else */
1489+
}
1490+
return tupdesc;
1491+
}
1492+
14421493
/*
14431494
* postgresBeginForeignScan
14441495
* Initiate an executor scan of a foreign PostgreSQL table.
@@ -1523,7 +1574,7 @@ postgresBeginForeignScan(ForeignScanState *node, int eflags)
15231574
else
15241575
{
15251576
fsstate->rel = NULL;
1526-
fsstate->tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
1577+
fsstate->tupdesc = get_tupdesc_for_join_scan_tuples(node);
15271578
}
15281579

15291580
fsstate->attinmeta = TupleDescGetAttInMetadata(fsstate->tupdesc);
@@ -2631,7 +2682,7 @@ postgresBeginDirectModify(ForeignScanState *node, int eflags)
26312682
TupleDesc tupdesc;
26322683

26332684
if (fsplan->scan.scanrelid == 0)
2634-
tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
2685+
tupdesc = get_tupdesc_for_join_scan_tuples(node);
26352686
else
26362687
tupdesc = RelationGetDescr(dmstate->rel);
26372688

contrib/postgres_fdw/sql/postgres_fdw.sql

+8
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,14 @@ UPDATE ft2 AS target SET (c2) = (
12551255
WHERE target.c1 = src.c1
12561256
) WHERE c1 > 1100;
12571257

1258+
-- Test UPDATE involving a join that can be pushed down,
1259+
-- but a SET clause that can't be
1260+
EXPLAIN (VERBOSE, COSTS OFF)
1261+
UPDATE ft2 d SET c2 = CASE WHEN random() >= 0 THEN d.c2 ELSE 0 END
1262+
FROM ft2 AS t WHERE d.c1 = t.c1 AND d.c1 > 1100;
1263+
UPDATE ft2 d SET c2 = CASE WHEN random() >= 0 THEN d.c2 ELSE 0 END
1264+
FROM ft2 AS t WHERE d.c1 = t.c1 AND d.c1 > 1100;
1265+
12581266
-- Test UPDATE/DELETE with WHERE or JOIN/ON conditions containing
12591267
-- user-defined operators/functions
12601268
ALTER SERVER loopback OPTIONS (DROP extensions);

0 commit comments

Comments
 (0)