@@ -1391,11 +1391,14 @@ deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs,
13911391 */
13921392 deparseSubqueryTargetList (context );
13931393 }
1394- else if (IS_JOIN_REL (foreignrel ) || IS_UPPER_REL (foreignrel ))
1394+ else if (IS_JOIN_REL (foreignrel ) || IS_UPPER_REL (foreignrel ) ||
1395+ tlist != NIL )
13951396 {
13961397 /*
13971398 * For a join or upper relation the input tlist gives the list of
1398- * columns required to be fetched from the foreign server.
1399+ * columns required to be fetched from the foreign server; likewise
1400+ * for a base-relation scan given an explicit tlist (fdw_scan_tlist)
1401+ * to fetch the remote tableoid (see postgresGetForeignPlan()).
13991402 */
14001403 deparseExplicitTargetList (tlist , false, retrieved_attrs , context );
14011404 }
@@ -1721,13 +1724,55 @@ get_jointype_name(JoinType jointype)
17211724 return NULL ;
17221725}
17231726
1727+ /*
1728+ * Does tlist contain an individual real column (varattno > 0, excluding the
1729+ * RemoteTableOidAttributeNumber pseudo-column) of relid, besides a whole-row
1730+ * entry for it? Used to tell whether a whole-row reference has anything to
1731+ * be locally reconstructed from (see deparseExplicitTargetList()).
1732+ */
1733+ static bool
1734+ tlist_has_other_column_of_rel (List * tlist , Index relid )
1735+ {
1736+ ListCell * lc ;
1737+
1738+ foreach (lc , tlist )
1739+ {
1740+ TargetEntry * tle = lfirst_node (TargetEntry , lc );
1741+
1742+ if (IsA (tle -> expr , Var ) &&
1743+ ((Var * ) tle -> expr )-> varno == relid &&
1744+ ((Var * ) tle -> expr )-> varattno > 0 &&
1745+ ((Var * ) tle -> expr )-> varattno != RemoteTableOidAttributeNumber )
1746+ return true;
1747+ }
1748+ return false;
1749+ }
1750+
17241751/*
17251752 * Deparse given targetlist and append it to context->buf.
17261753 *
17271754 * tlist is list of TargetEntry's which in turn contain Var nodes.
17281755 *
1729- * retrieved_attrs is the list of continuously increasing integers starting
1730- * from 1. It has same number of entries as tlist.
1756+ * retrieved_attrs is normally the list of continuously increasing integers
1757+ * starting from 1, with the same number of entries as tlist. There are two
1758+ * exceptions, both specific to a base relation's SELECT list built as an
1759+ * explicit fdw_scan_tlist for row-identity purposes:
1760+ *
1761+ * - the local-tableoid pseudo-entry (a Var on TableOidAttributeNumber): its
1762+ * value is already known at plan time, so instead of shipping it to the
1763+ * remote server as a literal Const, it's skipped here and filled in
1764+ * locally afterward, by make_tuple_from_result_row().
1765+ *
1766+ * - a plain whole-row reference to the relation itself (a Var on attno 0),
1767+ * when its columns are also being fetched individually elsewhere in this
1768+ * tlist (e.g. for EPQ recheck): shipping the whole row too would transfer
1769+ * those columns twice, so it's skipped here and reconstructed locally
1770+ * afterward instead, again by make_tuple_from_result_row(). Absent such
1771+ * sibling columns (e.g. the relation has none), it is shipped as a
1772+ * literal ROW(...) as usual, since there is nothing to reconstruct it
1773+ * from. There can be more than one such whole-row entry for the same
1774+ * relation (e.g. one from the query itself, another added by core code
1775+ * for row identity); each is skipped and reconstructed independently.
17311776 *
17321777 * This is used for both SELECT and RETURNING targetlists; the is_returning
17331778 * parameter is true only for a RETURNING targetlist.
@@ -1740,26 +1785,48 @@ deparseExplicitTargetList(List *tlist,
17401785{
17411786 ListCell * lc ;
17421787 StringInfo buf = context -> buf ;
1788+ bool is_base_rel = !is_returning && IS_SIMPLE_REL (context -> scanrel );
17431789 int i = 0 ;
1790+ bool first = true;
17441791
17451792 * retrieved_attrs = NIL ;
17461793
17471794 foreach (lc , tlist )
17481795 {
17491796 TargetEntry * tle = lfirst_node (TargetEntry , lc );
17501797
1751- if (i > 0 )
1798+ i ++ ;
1799+
1800+ /* Local tableoid: known locally, skip the remote round trip. */
1801+ if (is_base_rel &&
1802+ IsA (tle -> expr , Var ) &&
1803+ ((Var * ) tle -> expr )-> varattno == TableOidAttributeNumber )
1804+ continue ;
1805+
1806+ /*
1807+ * Whole-row reference to this relation with a sibling column
1808+ * elsewhere in tlist: skip the redundant ROW(...) and reconstruct
1809+ * it locally instead (see make_tuple_from_result_row()).
1810+ */
1811+ if (is_base_rel &&
1812+ IsA (tle -> expr , Var ) &&
1813+ ((Var * ) tle -> expr )-> varattno == InvalidAttrNumber &&
1814+ ((Var * ) tle -> expr )-> varno == context -> scanrel -> relid &&
1815+ tlist_has_other_column_of_rel (tlist , context -> scanrel -> relid ))
1816+ continue ;
1817+
1818+ if (!first )
17521819 appendStringInfoString (buf , ", " );
17531820 else if (is_returning )
17541821 appendStringInfoString (buf , " RETURNING " );
1822+ first = false;
17551823
17561824 deparseExpr ((Expr * ) tle -> expr , context );
17571825
1758- * retrieved_attrs = lappend_int (* retrieved_attrs , i + 1 );
1759- i ++ ;
1826+ * retrieved_attrs = lappend_int (* retrieved_attrs , i );
17601827 }
17611828
1762- if (i == 0 && !is_returning )
1829+ if (first && !is_returning )
17631830 appendStringInfoString (buf , "NULL" );
17641831}
17651832
@@ -2364,7 +2431,7 @@ deparseUpdateSql(StringInfo buf, RangeTblEntry *rte,
23642431 deparseRelation (buf , rel );
23652432 appendStringInfoString (buf , " SET " );
23662433
2367- pindex = 2 ; /* ctid is always the first param */
2434+ pindex = 3 ; /* ctid ($1) and tableoid ($2) come first */
23682435 first = true;
23692436 foreach (lc , targetAttrs )
23702437 {
@@ -2384,7 +2451,7 @@ deparseUpdateSql(StringInfo buf, RangeTblEntry *rte,
23842451 pindex ++ ;
23852452 }
23862453 }
2387- appendStringInfoString (buf , " WHERE ctid = $1" );
2454+ appendStringInfoString (buf , " WHERE ctid = $1 AND tableoid = $2 " );
23882455
23892456 deparseReturningList (buf , rte , rtindex , rel ,
23902457 rel -> trigdesc && rel -> trigdesc -> trig_update_after_row ,
@@ -2502,7 +2569,7 @@ deparseDeleteSql(StringInfo buf, RangeTblEntry *rte,
25022569{
25032570 appendStringInfoString (buf , "DELETE FROM " );
25042571 deparseRelation (buf , rel );
2505- appendStringInfoString (buf , " WHERE ctid = $1" );
2572+ appendStringInfoString (buf , " WHERE ctid = $1 AND tableoid = $2 " );
25062573
25072574 deparseReturningList (buf , rte , rtindex , rel ,
25082575 rel -> trigdesc && rel -> trigdesc -> trig_delete_after_row ,
@@ -2889,6 +2956,17 @@ deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte,
28892956 ADD_REL_QUALIFIER (buf , varno );
28902957 appendStringInfoString (buf , "ctid" );
28912958 }
2959+ else if (varattno == RemoteTableOidAttributeNumber )
2960+ {
2961+ /*
2962+ * Pseudo-column carrying the remote table OID as part of the row
2963+ * identity for UPDATE/DELETE (see postgresAddForeignUpdateTargets);
2964+ * fetch it as the remote "tableoid" system column.
2965+ */
2966+ if (qualify_col )
2967+ ADD_REL_QUALIFIER (buf , varno );
2968+ appendStringInfoString (buf , "tableoid" );
2969+ }
28922970 else if (varattno < 0 )
28932971 {
28942972 /*
0 commit comments