diff options
Diffstat (limited to 'contrib/postgres_fdw/deparse.c')
-rw-r--r-- | contrib/postgres_fdw/deparse.c | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c index bdc410d1a35..35c27e7fb3c 100644 --- a/contrib/postgres_fdw/deparse.c +++ b/contrib/postgres_fdw/deparse.c @@ -1571,13 +1571,38 @@ deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, { RangeTblEntry *rte; - /* varattno can be a whole-row reference, ctid or a regular table column */ if (varattno == SelfItemPointerAttributeNumber) { + /* We support fetching the remote side's CTID. */ if (qualify_col) ADD_REL_QUALIFIER(buf, varno); appendStringInfoString(buf, "ctid"); } + else if (varattno < 0) + { + /* + * All other system attributes are fetched as 0, except for table OID, + * which is fetched as the local table OID. However, we must be + * careful; the table could be beneath an outer join, in which case + * it must go to NULL whenever the rest of the row does. + */ + Oid fetchval = 0; + + if (varattno == TableOidAttributeNumber) + { + rte = planner_rt_fetch(varno, root); + fetchval = rte->relid; + } + + if (qualify_col) + { + appendStringInfoString(buf, "CASE WHEN "); + ADD_REL_QUALIFIER(buf, varno); + appendStringInfo(buf, "* IS NOT NULL THEN %u END", fetchval); + } + else + appendStringInfo(buf, "%u", fetchval); + } else if (varattno == 0) { /* Whole row reference */ @@ -1606,10 +1631,29 @@ deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root, */ attrs_used = bms_add_member(NULL, 0 - FirstLowInvalidHeapAttributeNumber); + + /* + * In case the whole-row reference is under an outer join then it has to + * go NULL whenver the rest of the row goes NULL. Deparsing a join query + * would always involve multiple relations, thus qualify_col would be + * true. + */ + if (qualify_col) + { + appendStringInfoString(buf, "CASE WHEN "); + ADD_REL_QUALIFIER(buf, varno); + appendStringInfo(buf, "* IS NOT NULL THEN "); + } + appendStringInfoString(buf, "ROW("); deparseTargetList(buf, root, varno, rel, false, attrs_used, qualify_col, &retrieved_attrs); appendStringInfoString(buf, ")"); + + /* Complete the CASE WHEN statement started above. */ + if (qualify_col) + appendStringInfo(buf," END"); + heap_close(rel, NoLock); bms_free(attrs_used); } |