On Wed, Apr 13, 2016 at 2:36 PM, Robert Haas <robertmh...@gmail.com> wrote:
> On Wed, Apr 13, 2016 at 2:11 PM, Robert Haas <robertmh...@gmail.com> wrote:
>> So, clearly that's not good.  It should at least be consistent.  But
>> more than that, the fact that postgres_fdw sets the xmax to 0xffffffff
>> is also pretty wacky.  We might use such a value as a sentinel for
>> some data type, but for transaction IDs that's just some random normal
>> transaction ID, and it's NOT coming from t1.  I haven't tracked down
>> where it *is* coming from yet, but can't imagine it's any place very
>> principled.
>
> And, yeah, it's not very principled.
>
> rhaas=# select ft1.xmin, ft1.xmax, ft1.cmin from ft1;
>  xmin |    xmax    | cmin
> ------+------------+-------
>    96 | 4294967295 | 16392
>    96 | 4294967295 | 16392
>    96 | 4294967295 | 16392
>    96 | 4294967295 | 16392
> (4 rows)
>
> What's happening here is that heap_getattr() is being applied to a
> HeapTupleHeaderData which contains DatumTupleFields.  So 96 is
> datum_len_, 4294967295 is the -1 recorded in datum_typmod, and 16392
> is the compose type OID recorded in datum_typeid, which happens in
> this case to be the OID of ft1.  Isn't that special?
>
> It's hard for me to view this as anything other than a bug in
> postgres_fdw - which of course means that this open item boils down to
> the complaint that the way system columns are handled by join pushdown
> isn't bug-compatible with the existing behavior....

OK, here's a patch.  What I did is:

1. For a regular FDW scan, zero the xmin, xmax, and cid of the tuple
before returning it from postgres_fdw, so that we don't expose the
datum-tuple fields.   I can't see any reason this isn't safe, but I
might be missing something.

2. When a join is pushed down, deparse system columns using something
like "CASE WHEN r1.* IS NOT NULL THEN 0 END", except for the table OID
column, which gets deparsed with the table OID in place of 0.  This
delivers the correct behavior in the presence of outer joins.

Review appreciated.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index bdc410d..1c2f165 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 */
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index ee0220a..066cffb 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -4410,6 +4410,18 @@ make_tuple_from_result_row(PGresult *res,
 	if (ctid)
 		tuple->t_self = tuple->t_data->t_ctid = *ctid;
 
+	/*
+	 * Stomp on the xmin, xmax, and cmin fields from the tuple created by
+	 * heap_form_tuple.  heap_form_tuple actually creates the tuple with
+	 * DatumTupleFields, not HeapTupleFields, but the executor expects
+	 * HeapTupleFields and will happily extract system columns on that
+	 * assumption.  If we don't do this then, for example, the tuple length
+	 * ends up in the xmin field, which isn't what we want.
+	 */
+	HeapTupleHeaderSetXmax(tuple->t_data, InvalidTransactionId);
+	HeapTupleHeaderSetXmin(tuple->t_data, InvalidTransactionId);
+	HeapTupleHeaderSetCmin(tuple->t_data, InvalidTransactionId);
+
 	/* Clean up */
 	MemoryContextReset(temp_context);
 
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to