From 4e4759fbafc6c364b48cb35e8403725c56a69932 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 24 Aug 2018 13:07:08 +0900
Subject: [PATCH 2/4] Core side modification for PgFDW foreign update fix

Currently core doesn't allow add a column that is not in relation
definition to columns in fdw_scan_tlist. This patch allows that.
---
 src/backend/optimizer/util/plancat.c | 72 +++++++++++++++++++++++++++++++++++-
 src/backend/utils/adt/ruleutils.c    | 35 +++++++++++++++---
 2 files changed, 101 insertions(+), 6 deletions(-)

diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index 8369e3ad62..abef30dfd4 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -33,6 +33,7 @@
 #include "foreign/fdwapi.h"
 #include "miscadmin.h"
 #include "nodes/makefuncs.h"
+#include "nodes/nodeFuncs.h"
 #include "optimizer/clauses.h"
 #include "optimizer/cost.h"
 #include "optimizer/plancat.h"
@@ -58,7 +59,15 @@ int			constraint_exclusion = CONSTRAINT_EXCLUSION_PARTITION;
 /* Hook for plugins to get control in get_relation_info() */
 get_relation_info_hook_type get_relation_info_hook = NULL;
 
+/* context type for max_varattno() */
+typedef struct
+{
+	AttrNumber  maxattrnum;
+	Index		varno;
+} max_varattno_context;
 
+static bool max_varattno_walker(Node *node, max_varattno_context *context);
+static AttrNumber max_varattno(List *tlist, Index varno);
 static void get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
 						  Relation relation, bool inhparent);
 static bool infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
@@ -76,6 +85,43 @@ static PartitionScheme find_partition_scheme(PlannerInfo *root, Relation rel);
 static void set_baserel_partition_key_exprs(Relation relation,
 								RelOptInfo *rel);
 
+/*
+ * max_varattno
+ *   Find the largest varattno in targetlist
+ *
+ * FDWs may add junk columns for internal usage. This function finds the
+ * maximum attribute number in the tlist.
+ */
+static AttrNumber
+max_varattno(List *tlist, Index varno)
+{
+	max_varattno_context context;
+
+	context.maxattrnum = FirstLowInvalidHeapAttributeNumber;
+	context.varno = varno;
+
+	max_varattno_walker((Node*) tlist, &context);
+
+	return context.maxattrnum;
+}
+
+static bool
+max_varattno_walker(Node *node, max_varattno_context *context)
+{
+	if (node == NULL)
+		return false;
+	if (IsA(node, Var))
+	{
+		Var	*var = (Var *) node;
+
+		if (var->varno == context->varno && var->varlevelsup == 0 &&
+			context->maxattrnum < var->varattno)
+			context->maxattrnum = var->varattno;
+		return false;
+	}
+	return expression_tree_walker(node, max_varattno_walker, (void *)context);
+}
+
 /*
  * get_relation_info -
  *	  Retrieves catalog information for a given relation.
@@ -112,6 +158,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
 	Relation	relation;
 	bool		hasindex;
 	List	   *indexinfos = NIL;
+	AttrNumber  max_attrnum;
 
 	/*
 	 * We need not lock the relation since it was already locked, either by
@@ -126,8 +173,18 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				 errmsg("cannot access temporary or unlogged relations during recovery")));
 
+	max_attrnum = RelationGetNumberOfAttributes(relation);
+
+	/* Foreign table may have exanded this relation with junk columns */
+	if (root->simple_rte_array[varno]->relkind == RELKIND_FOREIGN_TABLE)
+	{
+		AttrNumber maxattno = max_varattno(root->parse->targetList, varno);
+		if (max_attrnum < maxattno)
+			max_attrnum = maxattno;
+	}
+
 	rel->min_attr = FirstLowInvalidHeapAttributeNumber + 1;
-	rel->max_attr = RelationGetNumberOfAttributes(relation);
+	rel->max_attr = max_attrnum;
 	rel->reltablespace = RelationGetForm(relation)->reltablespace;
 
 	Assert(rel->max_attr >= rel->min_attr);
@@ -1575,6 +1632,19 @@ build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
 			relation = heap_open(rte->relid, NoLock);
 
 			numattrs = RelationGetNumberOfAttributes(relation);
+
+			/*
+			 * Foreign tables may have expanded with some junk columns. Punt
+			 * in the case.
+			 */
+			if (numattrs < rel->max_attr)
+			{
+				Assert(root->simple_rte_array[rel->relid]->relkind ==
+					   RELKIND_FOREIGN_TABLE);
+				heap_close(relation, NoLock);
+				break;
+			}
+
 			for (attrno = 1; attrno <= numattrs; attrno++)
 			{
 				Form_pg_attribute att_tup = TupleDescAttr(relation->rd_att,
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 03e9a28a63..d9d525e896 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3815,16 +3815,42 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
 		tupdesc = RelationGetDescr(rel);
 
 		ncolumns = tupdesc->natts;
+
+		/* eref may hold names of junk columns  */
+		if (ncolumns < list_length(rte->eref->colnames))
+			ncolumns = list_length(rte->eref->colnames);
+
 		real_colnames = (char **) palloc(ncolumns * sizeof(char *));
 
 		for (i = 0; i < ncolumns; i++)
 		{
-			Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
+			if (i < tupdesc->natts)
+			{
+				Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
 
-			if (attr->attisdropped)
-				real_colnames[i] = NULL;
+				if (attr->attisdropped)
+					real_colnames[i] = NULL;
+				else
+					real_colnames[i] = pstrdup(NameStr(attr->attname));
+			}
 			else
-				real_colnames[i] = pstrdup(NameStr(attr->attname));
+			{
+				/*
+				 * This columns is an extended column, the name of which may
+				 * be stored in eref
+				 */
+				if (i < list_length(rte->eref->colnames))
+				{
+					char *cname = strVal(list_nth(rte->eref->colnames, i));
+
+					if (cname[0] == '\0')
+						real_colnames[i] = NULL;
+					else
+						real_colnames[i] = cname;
+				}
+				else
+					real_colnames[i] = NULL;
+			}
 		}
 		relation_close(rel, AccessShareLock);
 	}
@@ -4152,7 +4178,6 @@ set_join_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
 	for (jc = 0; jc < rightcolinfo->num_new_cols; jc++)
 	{
 		char	   *child_colname = rightcolinfo->new_colnames[jc];
-
 		if (!rightcolinfo->is_new_col[jc])
 		{
 			/* Advance ic to next non-dropped old column of right child */
-- 
2.16.3

