@@ -84,7 +84,6 @@ typedef struct
8484List * fkconstraints ;/* FOREIGN KEY constraints */
8585List * ixconstraints ;/* index-creating constraints */
8686List * likeclauses ;/* LIKE clauses that need post-processing */
87- List * extstats ;/* cloned extended statistics */
8887List * blist ;/* "before list" of things to do before
8988 * creating the table */
9089List * alist ;/* "after list" of things to do after creating
@@ -117,13 +116,14 @@ static void transformTableLikeClause(CreateStmtContext *cxt,
117116static void transformOfType (CreateStmtContext * cxt ,
118117TypeName * ofTypename );
119118static CreateStatsStmt * generateClonedExtStatsStmt (RangeVar * heapRel ,
120- Oid heapRelid ,Oid source_statsid );
119+ Oid heapRelid ,
120+ Oid source_statsid ,
121+ const AttrMap * attmap );
121122static List * get_collation (Oid collation ,Oid actual_datatype );
122123static List * get_opclass (Oid opclass ,Oid actual_datatype );
123124static void transformIndexConstraints (CreateStmtContext * cxt );
124125static IndexStmt * transformIndexConstraint (Constraint * constraint ,
125126CreateStmtContext * cxt );
126- static void transformExtendedStatistics (CreateStmtContext * cxt );
127127static void transformFKConstraints (CreateStmtContext * cxt ,
128128bool skipValidation ,
129129bool isAddConstraint );
@@ -243,7 +243,6 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
243243cxt .fkconstraints = NIL ;
244244cxt .ixconstraints = NIL ;
245245cxt .likeclauses = NIL ;
246- cxt .extstats = NIL ;
247246cxt .blist = NIL ;
248247cxt .alist = NIL ;
249248cxt .pkey = NULL ;
@@ -336,11 +335,6 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
336335 */
337336transformCheckConstraints (& cxt , !cxt .isforeign );
338337
339- /*
340- * Postprocess extended statistics.
341- */
342- transformExtendedStatistics (& cxt );
343-
344338/*
345339 * Output results.
346340 */
@@ -1127,61 +1121,25 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
11271121}
11281122
11291123/*
1130- * We cannot yet deal with defaults, CHECK constraints, or indexes, since
1131- * we don't yet know what column numbers the copied columns will have in
1132- * the finished table. If any of those options are specified, add the
1133- * LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1134- * called after we do know that. Also, remember the relation OID so that
1135- * expandTableLikeClause is certain to open the same table.
1124+ * We cannot yet deal with defaults, CHECK constraints, indexes, or
1125+ * statistics, since we don't yet know what column numbers the copied
1126+ * columns will have in the finished table. If any of those options are
1127+ * specified, add the LIKE clause to cxt->likeclauses so that
1128+ * expandTableLikeClause will be called after we do know that. Also,
1129+ * remember the relation OID so that expandTableLikeClause is certain to
1130+ * open the same table.
11361131 */
11371132if (table_like_clause -> options &
11381133(CREATE_TABLE_LIKE_DEFAULTS |
11391134CREATE_TABLE_LIKE_GENERATED |
11401135CREATE_TABLE_LIKE_CONSTRAINTS |
1141- CREATE_TABLE_LIKE_INDEXES ))
1136+ CREATE_TABLE_LIKE_INDEXES |
1137+ CREATE_TABLE_LIKE_STATISTICS ))
11421138{
11431139table_like_clause -> relationOid = RelationGetRelid (relation );
11441140cxt -> likeclauses = lappend (cxt -> likeclauses ,table_like_clause );
11451141}
11461142
1147- /*
1148- * We may copy extended statistics if requested, since the representation
1149- * of CreateStatsStmt doesn't depend on column numbers.
1150- */
1151- if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1152- {
1153- List * parent_extstats ;
1154- ListCell * l ;
1155-
1156- parent_extstats = RelationGetStatExtList (relation );
1157-
1158- foreach (l ,parent_extstats )
1159- {
1160- Oid parent_stat_oid = lfirst_oid (l );
1161- CreateStatsStmt * stats_stmt ;
1162-
1163- stats_stmt = generateClonedExtStatsStmt (cxt -> relation ,
1164- RelationGetRelid (relation ),
1165- parent_stat_oid );
1166-
1167- /* Copy comment on statistics object, if requested */
1168- if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1169- {
1170- comment = GetComment (parent_stat_oid ,StatisticExtRelationId ,0 );
1171-
1172- /*
1173- * We make use of CreateStatsStmt's stxcomment option, so as
1174- * not to need to know now what name the statistics will have.
1175- */
1176- stats_stmt -> stxcomment = comment ;
1177- }
1178-
1179- cxt -> extstats = lappend (cxt -> extstats ,stats_stmt );
1180- }
1181-
1182- list_free (parent_extstats );
1183- }
1184-
11851143/*
11861144 * Close the parent rel, but keep our AccessShareLock on it until xact
11871145 * commit. That will prevent someone else from deleting or ALTERing the
@@ -1448,6 +1406,44 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
14481406}
14491407}
14501408
1409+ /*
1410+ * Process extended statistics if required.
1411+ */
1412+ if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1413+ {
1414+ List * parent_extstats ;
1415+ ListCell * l ;
1416+
1417+ parent_extstats = RelationGetStatExtList (relation );
1418+
1419+ foreach (l ,parent_extstats )
1420+ {
1421+ Oid parent_stat_oid = lfirst_oid (l );
1422+ CreateStatsStmt * stats_stmt ;
1423+
1424+ stats_stmt = generateClonedExtStatsStmt (heapRel ,
1425+ RelationGetRelid (childrel ),
1426+ parent_stat_oid ,
1427+ attmap );
1428+
1429+ /* Copy comment on statistics object, if requested */
1430+ if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1431+ {
1432+ comment = GetComment (parent_stat_oid ,StatisticExtRelationId ,0 );
1433+
1434+ /*
1435+ * We make use of CreateStatsStmt's stxcomment option, so as
1436+ * not to need to know now what name the statistics will have.
1437+ */
1438+ stats_stmt -> stxcomment = comment ;
1439+ }
1440+
1441+ result = lappend (result ,stats_stmt );
1442+ }
1443+
1444+ list_free (parent_extstats );
1445+ }
1446+
14511447/* Done with child rel */
14521448table_close (childrel ,NoLock );
14531449
@@ -1872,10 +1868,12 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
18721868 * Generate a CreateStatsStmt node using information from an already existing
18731869 * extended statistic "source_statsid", for the rel identified by heapRel and
18741870 * heapRelid.
1871+ *
1872+ * Attribute numbers in expression Vars are adjusted according to attmap.
18751873 */
18761874static CreateStatsStmt *
18771875generateClonedExtStatsStmt (RangeVar * heapRel ,Oid heapRelid ,
1878- Oid source_statsid )
1876+ Oid source_statsid , const AttrMap * attmap )
18791877{
18801878HeapTuple ht_stats ;
18811879Form_pg_statistic_ext statsrec ;
@@ -1958,10 +1956,19 @@ generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
19581956
19591957foreach (lc ,exprs )
19601958{
1959+ Node * expr = (Node * )lfirst (lc );
19611960StatsElem * selem = makeNode (StatsElem );
1961+ bool found_whole_row ;
1962+
1963+ /* Adjust Vars to match new table's column numbering */
1964+ expr = map_variable_attnos (expr ,
1965+ 1 ,0 ,
1966+ attmap ,
1967+ InvalidOid ,
1968+ & found_whole_row );
19621969
19631970selem -> name = NULL ;
1964- selem -> expr = ( Node * ) lfirst ( lc ) ;
1971+ selem -> expr = expr ;
19651972
19661973def_names = lappend (def_names ,selem );
19671974}
@@ -2687,19 +2694,6 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
26872694return index ;
26882695}
26892696
2690- /*
2691- * transformExtendedStatistics
2692- * Handle extended statistic objects
2693- *
2694- * Right now, there's nothing to do here, so we just append the list to
2695- * the existing "after" list.
2696- */
2697- static void
2698- transformExtendedStatistics (CreateStmtContext * cxt )
2699- {
2700- cxt -> alist = list_concat (cxt -> alist ,cxt -> extstats );
2701- }
2702-
27032697/*
27042698 * transformCheckConstraints
27052699 *handle CHECK constraints
@@ -3338,7 +3332,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
33383332cxt .fkconstraints = NIL ;
33393333cxt .ixconstraints = NIL ;
33403334cxt .likeclauses = NIL ;
3341- cxt .extstats = NIL ;
33423335cxt .blist = NIL ;
33433336cxt .alist = NIL ;
33443337cxt .pkey = NULL ;
@@ -3618,9 +3611,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
36183611newcmds = lappend (newcmds ,newcmd );
36193612}
36203613
3621- /* Append extended statistics objects */
3622- transformExtendedStatistics (& cxt );
3623-
36243614/* Close rel */
36253615relation_close (rel ,NoLock );
36263616