@@ -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
*/
@@ -1132,61 +1126,25 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
1132
1126
}
1133
1127
1134
1128
/*
1135
- * We cannot yet deal with defaults, CHECK constraints, or indexes, since
1136
- * we don't yet know what column numbers the copied columns will have in
1137
- * the finished table. If any of those options are specified, add the
1138
- * LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1139
- * called after we do know that. Also, remember the relation OID so that
1140
- * expandTableLikeClause is certain to open the same table.
1129
+ * We cannot yet deal with defaults, CHECK constraints, indexes, or
1130
+ * statistics, since we don't yet know what column numbers the copied
1131
+ * columns will have in the finished table. If any of those options are
1132
+ * specified, add the LIKE clause to cxt->likeclauses so that
1133
+ * expandTableLikeClause will be called after we do know that. Also,
1134
+ * remember the relation OID so that expandTableLikeClause is certain to
1135
+ * open the same table.
1141
1136
*/
1142
1137
if (table_like_clause -> options &
1143
1138
(CREATE_TABLE_LIKE_DEFAULTS |
1144
1139
CREATE_TABLE_LIKE_GENERATED |
1145
1140
CREATE_TABLE_LIKE_CONSTRAINTS |
1146
- CREATE_TABLE_LIKE_INDEXES ))
1141
+ CREATE_TABLE_LIKE_INDEXES |
1142
+ CREATE_TABLE_LIKE_STATISTICS ))
1147
1143
{
1148
1144
table_like_clause -> relationOid = RelationGetRelid (relation );
1149
1145
cxt -> likeclauses = lappend (cxt -> likeclauses ,table_like_clause );
1150
1146
}
1151
1147
1152
- /*
1153
- * We may copy extended statistics if requested, since the representation
1154
- * of CreateStatsStmt doesn't depend on column numbers.
1155
- */
1156
- if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1157
- {
1158
- List * parent_extstats ;
1159
- ListCell * l ;
1160
-
1161
- parent_extstats = RelationGetStatExtList (relation );
1162
-
1163
- foreach (l ,parent_extstats )
1164
- {
1165
- Oid parent_stat_oid = lfirst_oid (l );
1166
- CreateStatsStmt * stats_stmt ;
1167
-
1168
- stats_stmt = generateClonedExtStatsStmt (cxt -> relation ,
1169
- RelationGetRelid (relation ),
1170
- parent_stat_oid );
1171
-
1172
- /* Copy comment on statistics object, if requested */
1173
- if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1174
- {
1175
- comment = GetComment (parent_stat_oid ,StatisticExtRelationId ,0 );
1176
-
1177
- /*
1178
- * We make use of CreateStatsStmt's stxcomment option, so as
1179
- * not to need to know now what name the statistics will have.
1180
- */
1181
- stats_stmt -> stxcomment = comment ;
1182
- }
1183
-
1184
- cxt -> extstats = lappend (cxt -> extstats ,stats_stmt );
1185
- }
1186
-
1187
- list_free (parent_extstats );
1188
- }
1189
-
1190
1148
/*
1191
1149
* Close the parent rel, but keep our AccessShareLock on it until xact
1192
1150
* commit. That will prevent someone else from deleting or ALTERing the
@@ -1452,6 +1410,44 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
1452
1410
}
1453
1411
}
1454
1412
1413
+ /*
1414
+ * Process extended statistics if required.
1415
+ */
1416
+ if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1417
+ {
1418
+ List * parent_extstats ;
1419
+ ListCell * l ;
1420
+
1421
+ parent_extstats = RelationGetStatExtList (relation );
1422
+
1423
+ foreach (l ,parent_extstats )
1424
+ {
1425
+ Oid parent_stat_oid = lfirst_oid (l );
1426
+ CreateStatsStmt * stats_stmt ;
1427
+
1428
+ stats_stmt = generateClonedExtStatsStmt (heapRel ,
1429
+ RelationGetRelid (childrel ),
1430
+ parent_stat_oid ,
1431
+ attmap );
1432
+
1433
+ /* Copy comment on statistics object, if requested */
1434
+ if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1435
+ {
1436
+ comment = GetComment (parent_stat_oid ,StatisticExtRelationId ,0 );
1437
+
1438
+ /*
1439
+ * We make use of CreateStatsStmt's stxcomment option, so as
1440
+ * not to need to know now what name the statistics will have.
1441
+ */
1442
+ stats_stmt -> stxcomment = comment ;
1443
+ }
1444
+
1445
+ result = lappend (result ,stats_stmt );
1446
+ }
1447
+
1448
+ list_free (parent_extstats );
1449
+ }
1450
+
1455
1451
/* Done with child rel */
1456
1452
table_close (childrel ,NoLock );
1457
1453
@@ -1886,10 +1882,12 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
1886
1882
* Generate a CreateStatsStmt node using information from an already existing
1887
1883
* extended statistic "source_statsid", for the rel identified by heapRel and
1888
1884
* heapRelid.
1885
+ *
1886
+ * Attribute numbers in expression Vars are adjusted according to attmap.
1889
1887
*/
1890
1888
static CreateStatsStmt *
1891
1889
generateClonedExtStatsStmt (RangeVar * heapRel ,Oid heapRelid ,
1892
- Oid source_statsid )
1890
+ Oid source_statsid , const AttrMap * attmap )
1893
1891
{
1894
1892
HeapTuple ht_stats ;
1895
1893
Form_pg_statistic_ext statsrec ;
@@ -1973,10 +1971,19 @@ generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
1973
1971
1974
1972
foreach (lc ,exprs )
1975
1973
{
1974
+ Node * expr = (Node * )lfirst (lc );
1976
1975
StatsElem * selem = makeNode (StatsElem );
1976
+ bool found_whole_row ;
1977
+
1978
+ /* Adjust Vars to match new table's column numbering */
1979
+ expr = map_variable_attnos (expr ,
1980
+ 1 ,0 ,
1981
+ attmap ,
1982
+ InvalidOid ,
1983
+ & found_whole_row );
1977
1984
1978
1985
selem -> name = NULL ;
1979
- selem -> expr = ( Node * ) lfirst ( lc ) ;
1986
+ selem -> expr = expr ;
1980
1987
1981
1988
def_names = lappend (def_names ,selem );
1982
1989
}
@@ -2703,19 +2710,6 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
2703
2710
return index ;
2704
2711
}
2705
2712
2706
- /*
2707
- * transformExtendedStatistics
2708
- * Handle extended statistic objects
2709
- *
2710
- * Right now, there's nothing to do here, so we just append the list to
2711
- * the existing "after" list.
2712
- */
2713
- static void
2714
- transformExtendedStatistics (CreateStmtContext * cxt )
2715
- {
2716
- cxt -> alist = list_concat (cxt -> alist ,cxt -> extstats );
2717
- }
2718
-
2719
2713
/*
2720
2714
* transformCheckConstraints
2721
2715
*handle CHECK constraints
@@ -3358,7 +3352,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3358
3352
cxt .fkconstraints = NIL ;
3359
3353
cxt .ixconstraints = NIL ;
3360
3354
cxt .likeclauses = NIL ;
3361
- cxt .extstats = NIL ;
3362
3355
cxt .blist = NIL ;
3363
3356
cxt .alist = NIL ;
3364
3357
cxt .pkey = NULL ;
@@ -3640,9 +3633,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3640
3633
newcmds = lappend (newcmds ,newcmd );
3641
3634
}
3642
3635
3643
- /* Append extended statistics objects */
3644
- transformExtendedStatistics (& cxt );
3645
-
3646
3636
/* Close rel */
3647
3637
relation_close (rel ,NoLock );
3648
3638