@@ -84,7 +84,6 @@ typedef struct
84
84
List * fkconstraints ;/* FOREIGN KEY constraints */
85
85
List * ixconstraints ;/* index-creating constraints */
86
86
List * likeclauses ;/* LIKE clauses that need post-processing */
87
- List * extstats ;/* cloned extended statistics */
88
87
List * blist ;/* "before list" of things to do before
89
88
* creating the table */
90
89
List * alist ;/* "after list" of things to do after creating
@@ -117,13 +116,14 @@ static void transformTableLikeClause(CreateStmtContext *cxt,
117
116
static void transformOfType (CreateStmtContext * cxt ,
118
117
TypeName * ofTypename );
119
118
static CreateStatsStmt * generateClonedExtStatsStmt (RangeVar * heapRel ,
120
- Oid heapRelid ,Oid source_statsid );
119
+ Oid heapRelid ,
120
+ Oid source_statsid ,
121
+ const AttrMap * attmap );
121
122
static List * get_collation (Oid collation ,Oid actual_datatype );
122
123
static List * get_opclass (Oid opclass ,Oid actual_datatype );
123
124
static void transformIndexConstraints (CreateStmtContext * cxt );
124
125
static IndexStmt * transformIndexConstraint (Constraint * constraint ,
125
126
CreateStmtContext * cxt );
126
- static void transformExtendedStatistics (CreateStmtContext * cxt );
127
127
static void transformFKConstraints (CreateStmtContext * cxt ,
128
128
bool skipValidation ,
129
129
bool isAddConstraint );
@@ -243,7 +243,6 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
243
243
cxt .fkconstraints = NIL ;
244
244
cxt .ixconstraints = NIL ;
245
245
cxt .likeclauses = NIL ;
246
- cxt .extstats = NIL ;
247
246
cxt .blist = NIL ;
248
247
cxt .alist = NIL ;
249
248
cxt .pkey = NULL ;
@@ -336,11 +335,6 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
336
335
*/
337
336
transformCheckConstraints (& cxt , !cxt .isforeign );
338
337
339
- /*
340
- * Postprocess extended statistics.
341
- */
342
- transformExtendedStatistics (& cxt );
343
-
344
338
/*
345
339
* Output results.
346
340
*/
@@ -1127,61 +1121,25 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
1127
1121
}
1128
1122
1129
1123
/*
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.
1136
1131
*/
1137
1132
if (table_like_clause -> options &
1138
1133
(CREATE_TABLE_LIKE_DEFAULTS |
1139
1134
CREATE_TABLE_LIKE_GENERATED |
1140
1135
CREATE_TABLE_LIKE_CONSTRAINTS |
1141
- CREATE_TABLE_LIKE_INDEXES ))
1136
+ CREATE_TABLE_LIKE_INDEXES |
1137
+ CREATE_TABLE_LIKE_STATISTICS ))
1142
1138
{
1143
1139
table_like_clause -> relationOid = RelationGetRelid (relation );
1144
1140
cxt -> likeclauses = lappend (cxt -> likeclauses ,table_like_clause );
1145
1141
}
1146
1142
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
-
1185
1143
/*
1186
1144
* Close the parent rel, but keep our AccessShareLock on it until xact
1187
1145
* commit. That will prevent someone else from deleting or ALTERing the
@@ -1448,6 +1406,44 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
1448
1406
}
1449
1407
}
1450
1408
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
+
1451
1447
/* Done with child rel */
1452
1448
table_close (childrel ,NoLock );
1453
1449
@@ -1872,10 +1868,12 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
1872
1868
* Generate a CreateStatsStmt node using information from an already existing
1873
1869
* extended statistic "source_statsid", for the rel identified by heapRel and
1874
1870
* heapRelid.
1871
+ *
1872
+ * Attribute numbers in expression Vars are adjusted according to attmap.
1875
1873
*/
1876
1874
static CreateStatsStmt *
1877
1875
generateClonedExtStatsStmt (RangeVar * heapRel ,Oid heapRelid ,
1878
- Oid source_statsid )
1876
+ Oid source_statsid , const AttrMap * attmap )
1879
1877
{
1880
1878
HeapTuple ht_stats ;
1881
1879
Form_pg_statistic_ext statsrec ;
@@ -1958,10 +1956,19 @@ generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
1958
1956
1959
1957
foreach (lc ,exprs )
1960
1958
{
1959
+ Node * expr = (Node * )lfirst (lc );
1961
1960
StatsElem * 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 );
1962
1969
1963
1970
selem -> name = NULL ;
1964
- selem -> expr = ( Node * ) lfirst ( lc ) ;
1971
+ selem -> expr = expr ;
1965
1972
1966
1973
def_names = lappend (def_names ,selem );
1967
1974
}
@@ -2687,19 +2694,6 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
2687
2694
return index ;
2688
2695
}
2689
2696
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
-
2703
2697
/*
2704
2698
* transformCheckConstraints
2705
2699
*handle CHECK constraints
@@ -3338,7 +3332,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3338
3332
cxt .fkconstraints = NIL ;
3339
3333
cxt .ixconstraints = NIL ;
3340
3334
cxt .likeclauses = NIL ;
3341
- cxt .extstats = NIL ;
3342
3335
cxt .blist = NIL ;
3343
3336
cxt .alist = NIL ;
3344
3337
cxt .pkey = NULL ;
@@ -3618,9 +3611,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3618
3611
newcmds = lappend (newcmds ,newcmd );
3619
3612
}
3620
3613
3621
- /* Append extended statistics objects */
3622
- transformExtendedStatistics (& cxt );
3623
-
3624
3614
/* Close rel */
3625
3615
relation_close (rel ,NoLock );
3626
3616