Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1loC0V-0007Wc-4b for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Jun 2021 21:33:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1loC0T-0004Hs-Pl for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Jun 2021 21:33:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1loC0T-0004Hk-E3 for pgsql-hackers@lists.postgresql.org; Tue, 01 Jun 2021 21:33:01 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1loC0M-0001hd-Vg for pgsql-hackers@lists.postgresql.org; Tue, 01 Jun 2021 21:33:00 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 151LWp092854977; Tue, 1 Jun 2021 17:32:52 -0400 From: Tom Lane To: Alexander Pyhalov cc: Amit Langote , PostgreSQL Hackers Subject: Re: join pushdown and issue with foreign update In-reply-to: <2845636.1622571541@sss.pgh.pa.us> References: <7817fb9ebd6661cdf9b67dec6e129a78@postgrespro.ru> <050b90460c024dd21af53dbaaf8fd510@postgrespro.ru> <2845636.1622571541@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 01 Jun 2021 14:19:01 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2854965.1622583151.0@sss.pgh.pa.us> Date: Tue, 01 Jun 2021 17:32:51 -0400 Message-ID: <2854976.1622583171@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2854965.1622583151.1@sss.pgh.pa.us> I wrote: > I think a preferable fix involves making sure that the correct > record-type typmod is propagated to record_in in this context. > Alternatively, maybe we could insert the foreign table's rowtype > during execution of the input operation, without touching the > plan as such. Here's a draft-quality patch based on that idea. It resolves the offered test case, but I haven't beat on it beyond that. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="use-proper-rowtype-for-postgres_fdw-row-ids.patch"; charset="us-ascii" Content-ID: <2854965.1622583151.2@sss.pgh.pa.us> Content-Description: use-proper-rowtype-for-postgres_fdw-row-ids.patch Content-Transfer-Encoding: quoted-printable diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/post= gres_fdw/expected/postgres_fdw.out index 7df30010f2..79bc08efb4 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -10231,3 +10231,50 @@ DROP TABLE result_tbl; DROP TABLE join_tbl; ALTER SERVER loopback OPTIONS (DROP async_capable); ALTER SERVER loopback2 OPTIONS (DROP async_capable); +CREATE TABLE base_tbl (a int, b int); +CREATE FOREIGN TABLE remote_tbl (a int, b int) + SERVER loopback OPTIONS (table_name 'base_tbl'); +INSERT INTO base_tbl SELECT a, a+1 FROM generate_series(1,10) a; +ANALYZE base_tbl; +ANALYZE remote_tbl; +EXPLAIN (VERBOSE, COSTS OFF) +UPDATE remote_tbl d SET a =3D CASE WHEN random() >=3D 0 THEN 5 ELSE 6 END + FROM remote_tbl AS t WHERE d.a =3D t.a; + = QUERY PLAN = = = +-------------------------------------------------------------------------= --------------------------------------------------------------------------= --------------------------------------------------------------------------= ------------------------------- + Update on public.remote_tbl d + Remote SQL: UPDATE public.base_tbl SET a =3D $2 WHERE ctid =3D $1 + -> Foreign Scan + Output: CASE WHEN (random() >=3D '0'::double precision) THEN 5 E= LSE 6 END, d.ctid, d.*, t.* + Relations: (public.remote_tbl d) INNER JOIN (public.remote_tbl t= ) + Remote SQL: SELECT r1.ctid, CASE WHEN (r1.*)::text IS NOT NULL T= HEN ROW(r1.a, r1.b) END, CASE WHEN (r2.*)::text IS NOT NULL THEN ROW(r2.a,= r2.b) END FROM (public.base_tbl r1 INNER JOIN public.base_tbl r2 ON (((r1= .a =3D r2.a)))) FOR UPDATE OF r1 + -> Hash Join + Output: d.ctid, d.*, t.* + Hash Cond: (d.a =3D t.a) + -> Foreign Scan on public.remote_tbl d + Output: d.ctid, d.*, d.a + Remote SQL: SELECT a, b, ctid FROM public.base_tbl F= OR UPDATE + -> Hash + Output: t.*, t.a + -> Foreign Scan on public.remote_tbl t + Output: t.*, t.a + Remote SQL: SELECT a, b FROM public.base_tbl +(17 rows) + +UPDATE remote_tbl d SET a =3D CASE WHEN random() >=3D 0 THEN 5 ELSE 6 END + FROM remote_tbl AS t WHERE d.a =3D t.a; +SELECT * FROM base_tbl ORDER BY b; + a | b = +---+---- + 5 | 2 + 5 | 3 + 5 | 4 + 5 | 5 + 5 | 6 + 5 | 7 + 5 | 8 + 5 | 9 + 5 | 10 + 5 | 11 +(10 rows) + diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/po= stgres_fdw.c index c48a421e88..24ba60e00a 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -1439,6 +1439,57 @@ postgresGetForeignPlan(PlannerInfo *root, outer_plan); } = +/* + * Construct a tuple descriptor for the scan tuples handled by a foreign = join. + */ +static TupleDesc +get_tupdesc_for_join_scan_tuples(ForeignScanState *node) +{ + ForeignScan *fsplan =3D (ForeignScan *) node->ss.ps.plan; + EState *estate =3D node->ss.ps.state; + TupleDesc tupdesc; + + /* + * The core code has already set up a scan tuple slot based on + * fsplan->fdw_scan_tlist, and this slot's tupdesc is mostly good enough= , + * but there's one case where it isn't. If we have any whole-row row + * identifier Vars, they may have vartype RECORD, and we need to replace + * that with the associated table's actual composite type. This ensures + * that when we read those ROW() expression values from the remote serve= r, + * we can convert them to a composite type the local server knows. + */ + tupdesc =3D CreateTupleDescCopy(node->ss.ss_ScanTupleSlot->tts_tupleDesc= riptor); + for (int i =3D 0; i < tupdesc->natts; i++) + { + Form_pg_attribute att =3D TupleDescAttr(tupdesc, i); + Var *var; + RangeTblEntry *rte; + Oid reltype; + + /* Nothing to do if it's not a generic RECORD attribute */ + if (att->atttypid !=3D RECORDOID || att->atttypmod >=3D 0) + continue; + + /* + * If we can't identify the referenced table, do nothing. This'll + * likely lead to failure later, but perhaps we can muddle through. + */ + var =3D (Var *) list_nth_node(TargetEntry, fsplan->fdw_scan_tlist, + i)->expr; + if (!IsA(var, Var)) + continue; + rte =3D list_nth(estate->es_range_table, var->varno - 1); + if (rte->rtekind !=3D RTE_RELATION) + continue; + reltype =3D get_rel_type_id(rte->relid); + if (!OidIsValid(reltype)) + continue; + att->atttypid =3D reltype; + /* shouldn't need to change anything else */ + } + return tupdesc; +} + /* * postgresBeginForeignScan * Initiate an executor scan of a foreign PostgreSQL table. @@ -1523,7 +1574,7 @@ postgresBeginForeignScan(ForeignScanState *node, int= eflags) else { fsstate->rel =3D NULL; - fsstate->tupdesc =3D node->ss.ss_ScanTupleSlot->tts_tupleDescriptor; + fsstate->tupdesc =3D get_tupdesc_for_join_scan_tuples(node); } = fsstate->attinmeta =3D TupleDescGetAttInMetadata(fsstate->tupdesc); @@ -2631,7 +2682,7 @@ postgresBeginDirectModify(ForeignScanState *node, in= t eflags) TupleDesc tupdesc; = if (fsplan->scan.scanrelid =3D=3D 0) - tupdesc =3D node->ss.ss_ScanTupleSlot->tts_tupleDescriptor; + tupdesc =3D get_tupdesc_for_join_scan_tuples(node); else tupdesc =3D RelationGetDescr(dmstate->rel); = diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_= fdw/sql/postgres_fdw.sql index 78379bdea5..a7070870c7 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3262,3 +3262,20 @@ DROP TABLE join_tbl; = ALTER SERVER loopback OPTIONS (DROP async_capable); ALTER SERVER loopback2 OPTIONS (DROP async_capable); + +CREATE TABLE base_tbl (a int, b int); +CREATE FOREIGN TABLE remote_tbl (a int, b int) + SERVER loopback OPTIONS (table_name 'base_tbl'); + +INSERT INTO base_tbl SELECT a, a+1 FROM generate_series(1,10) a; + +ANALYZE base_tbl; +ANALYZE remote_tbl; + +EXPLAIN (VERBOSE, COSTS OFF) +UPDATE remote_tbl d SET a =3D CASE WHEN random() >=3D 0 THEN 5 ELSE 6 END + FROM remote_tbl AS t WHERE d.a =3D t.a; +UPDATE remote_tbl d SET a =3D CASE WHEN random() >=3D 0 THEN 5 ELSE 6 END + FROM remote_tbl AS t WHERE d.a =3D t.a; + +SELECT * FROM base_tbl ORDER BY b; ------- =_aaaaaaaaaa0--