@@ -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
*/
@@ -1123,61 +1117,25 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
1123
1117
}
1124
1118
1125
1119
/*
1126
- * We cannot yet deal with defaults, CHECK constraints, or indexes, since
1127
- * we don't yet know what column numbers the copied columns will have in
1128
- * the finished table. If any of those options are specified, add the
1129
- * LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1130
- * called after we do know that. Also, remember the relation OID so that
1131
- * expandTableLikeClause is certain to open the same table.
1120
+ * We cannot yet deal with defaults, CHECK constraints, indexes, or
1121
+ * statistics, since we don't yet know what column numbers the copied
1122
+ * columns will have in the finished table. If any of those options are
1123
+ * specified, add the LIKE clause to cxt->likeclauses so that
1124
+ * expandTableLikeClause will be called after we do know that. Also,
1125
+ * remember the relation OID so that expandTableLikeClause is certain to
1126
+ * open the same table.
1132
1127
*/
1133
1128
if (table_like_clause -> options &
1134
1129
(CREATE_TABLE_LIKE_DEFAULTS |
1135
1130
CREATE_TABLE_LIKE_GENERATED |
1136
1131
CREATE_TABLE_LIKE_CONSTRAINTS |
1137
- CREATE_TABLE_LIKE_INDEXES ))
1132
+ CREATE_TABLE_LIKE_INDEXES |
1133
+ CREATE_TABLE_LIKE_STATISTICS ))
1138
1134
{
1139
1135
table_like_clause -> relationOid = RelationGetRelid (relation );
1140
1136
cxt -> likeclauses = lappend (cxt -> likeclauses , table_like_clause );
1141
1137
}
1142
1138
1143
- /*
1144
- * We may copy extended statistics if requested, since the representation
1145
- * of CreateStatsStmt doesn't depend on column numbers.
1146
- */
1147
- if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1148
- {
1149
- List * parent_extstats ;
1150
- ListCell * l ;
1151
-
1152
- parent_extstats = RelationGetStatExtList (relation );
1153
-
1154
- foreach (l , parent_extstats )
1155
- {
1156
- Oid parent_stat_oid = lfirst_oid (l );
1157
- CreateStatsStmt * stats_stmt ;
1158
-
1159
- stats_stmt = generateClonedExtStatsStmt (cxt -> relation ,
1160
- RelationGetRelid (relation ),
1161
- parent_stat_oid );
1162
-
1163
- /* Copy comment on statistics object, if requested */
1164
- if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1165
- {
1166
- comment = GetComment (parent_stat_oid , StatisticExtRelationId , 0 );
1167
-
1168
- /*
1169
- * We make use of CreateStatsStmt's stxcomment option, so as
1170
- * not to need to know now what name the statistics will have.
1171
- */
1172
- stats_stmt -> stxcomment = comment ;
1173
- }
1174
-
1175
- cxt -> extstats = lappend (cxt -> extstats , stats_stmt );
1176
- }
1177
-
1178
- list_free (parent_extstats );
1179
- }
1180
-
1181
1139
/*
1182
1140
* Close the parent rel, but keep our AccessShareLock on it until xact
1183
1141
* commit. That will prevent someone else from deleting or ALTERing the
@@ -1443,6 +1401,44 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
1443
1401
}
1444
1402
}
1445
1403
1404
+ /*
1405
+ * Process extended statistics if required.
1406
+ */
1407
+ if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1408
+ {
1409
+ List * parent_extstats ;
1410
+ ListCell * l ;
1411
+
1412
+ parent_extstats = RelationGetStatExtList (relation );
1413
+
1414
+ foreach (l , parent_extstats )
1415
+ {
1416
+ Oid parent_stat_oid = lfirst_oid (l );
1417
+ CreateStatsStmt * stats_stmt ;
1418
+
1419
+ stats_stmt = generateClonedExtStatsStmt (heapRel ,
1420
+ RelationGetRelid (childrel ),
1421
+ parent_stat_oid ,
1422
+ attmap );
1423
+
1424
+ /* Copy comment on statistics object, if requested */
1425
+ if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1426
+ {
1427
+ comment = GetComment (parent_stat_oid , StatisticExtRelationId , 0 );
1428
+
1429
+ /*
1430
+ * We make use of CreateStatsStmt's stxcomment option, so as
1431
+ * not to need to know now what name the statistics will have.
1432
+ */
1433
+ stats_stmt -> stxcomment = comment ;
1434
+ }
1435
+
1436
+ result = lappend (result , stats_stmt );
1437
+ }
1438
+
1439
+ list_free (parent_extstats );
1440
+ }
1441
+
1446
1442
/* Done with child rel */
1447
1443
table_close (childrel , NoLock );
1448
1444
@@ -1876,10 +1872,12 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
1876
1872
* Generate a CreateStatsStmt node using information from an already existing
1877
1873
* extended statistic "source_statsid", for the rel identified by heapRel and
1878
1874
* heapRelid.
1875
+ *
1876
+ * Attribute numbers in expression Vars are adjusted according to attmap.
1879
1877
*/
1880
1878
static CreateStatsStmt *
1881
1879
generateClonedExtStatsStmt (RangeVar * heapRel , Oid heapRelid ,
1882
- Oid source_statsid )
1880
+ Oid source_statsid , const AttrMap * attmap )
1883
1881
{
1884
1882
HeapTuple ht_stats ;
1885
1883
Form_pg_statistic_ext statsrec ;
@@ -1963,10 +1961,19 @@ generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
1963
1961
1964
1962
foreach (lc , exprs )
1965
1963
{
1964
+ Node * expr = (Node * ) lfirst (lc );
1966
1965
StatsElem * selem = makeNode (StatsElem );
1966
+ bool found_whole_row ;
1967
+
1968
+ /* Adjust Vars to match new table's column numbering */
1969
+ expr = map_variable_attnos (expr ,
1970
+ 1 , 0 ,
1971
+ attmap ,
1972
+ InvalidOid ,
1973
+ & found_whole_row );
1967
1974
1968
1975
selem -> name = NULL ;
1969
- selem -> expr = ( Node * ) lfirst ( lc ) ;
1976
+ selem -> expr = expr ;
1970
1977
1971
1978
def_names = lappend (def_names , selem );
1972
1979
}
@@ -2691,19 +2698,6 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
2691
2698
return index ;
2692
2699
}
2693
2700
2694
- /*
2695
- * transformExtendedStatistics
2696
- * Handle extended statistic objects
2697
- *
2698
- * Right now, there's nothing to do here, so we just append the list to
2699
- * the existing "after" list.
2700
- */
2701
- static void
2702
- transformExtendedStatistics (CreateStmtContext * cxt )
2703
- {
2704
- cxt -> alist = list_concat (cxt -> alist , cxt -> extstats );
2705
- }
2706
-
2707
2701
/*
2708
2702
* transformCheckConstraints
2709
2703
* handle CHECK constraints
@@ -3346,7 +3340,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3346
3340
cxt .fkconstraints = NIL ;
3347
3341
cxt .ixconstraints = NIL ;
3348
3342
cxt .likeclauses = NIL ;
3349
- cxt .extstats = NIL ;
3350
3343
cxt .blist = NIL ;
3351
3344
cxt .alist = NIL ;
3352
3345
cxt .pkey = NULL ;
@@ -3628,9 +3621,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3628
3621
newcmds = lappend (newcmds , newcmd );
3629
3622
}
3630
3623
3631
- /* Append extended statistics objects */
3632
- transformExtendedStatistics (& cxt );
3633
-
3634
3624
/* Close rel */
3635
3625
relation_close (rel , NoLock );
3636
3626
0 commit comments