From 6dc274ea1530eb20178b3e833fb8fe3a70fae289 Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Tue, 16 May 2017 13:14:09 +0900
Subject: [PATCH 2/2] Fix a bug of creating partitions

A partition's CreateStmt.tableElts may become invalid upon return
from MergeAttributes(), which would cause crash the server when
accessed later.  Set it to the new value computed by MergeAttributes
upon return from that function.
---
 src/backend/commands/tablecmds.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index e4d2bfb6b0..9e0aac7588 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -622,6 +622,20 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 							 &inheritOids, &old_constraints, &parentOidCount);
 
 	/*
+	 * The original value of stmt->tableElts may have been obsoleted by
+	 * MergeAttributes(), especially those for partitions, wherein the
+	 * cells of stmt->tableElts are deleted.  Note that in a partition's
+	 * case, stmt->tableElts contains dummy ColumnDef's created to capture
+	 * the partition's own column constraints which are merged into the actual
+	 * column definitions derived from the parent, before deleting the
+	 * aforementioned dummy ones.  We don't need to refer to stmt->tableElts
+	 * hereafter in this function, although the caller expects it to contain
+	 * valid elements upon return.  So replace the original list with the
+	 * one that's known to be valid.
+	 */
+	stmt->tableElts = schema;
+
+	/*
 	 * Create a tuple descriptor from the relation schema.  Note that this
 	 * deals with column names, types, and NOT NULL constraints, but not
 	 * default values or CHECK constraints; we handle those below.
-- 
2.11.0

