@@ -87,7 +87,6 @@ typedef struct
87
87
List * fkconstraints ; /* FOREIGN KEY constraints */
88
88
List * ixconstraints ; /* index-creating constraints */
89
89
List * likeclauses ; /* LIKE clauses that need post-processing */
90
- List * extstats ; /* cloned extended statistics */
91
90
List * blist ; /* "before list" of things to do before
92
91
* creating the table */
93
92
List * alist ; /* "after list" of things to do after creating
@@ -120,13 +119,14 @@ static void transformTableLikeClause(CreateStmtContext *cxt,
120
119
static void transformOfType (CreateStmtContext * cxt ,
121
120
TypeName * ofTypename );
122
121
static CreateStatsStmt * generateClonedExtStatsStmt (RangeVar * heapRel ,
123
- Oid heapRelid , Oid source_statsid );
122
+ Oid heapRelid ,
123
+ Oid source_statsid ,
124
+ const AttrMap * attmap );
124
125
static List * get_collation (Oid collation , Oid actual_datatype );
125
126
static List * get_opclass (Oid opclass , Oid actual_datatype );
126
127
static void transformIndexConstraints (CreateStmtContext * cxt );
127
128
static IndexStmt * transformIndexConstraint (Constraint * constraint ,
128
129
CreateStmtContext * cxt );
129
- static void transformExtendedStatistics (CreateStmtContext * cxt );
130
130
static void transformFKConstraints (CreateStmtContext * cxt ,
131
131
bool skipValidation ,
132
132
bool isAddConstraint );
@@ -246,7 +246,6 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
246
246
cxt .fkconstraints = NIL ;
247
247
cxt .ixconstraints = NIL ;
248
248
cxt .likeclauses = NIL ;
249
- cxt .extstats = NIL ;
250
249
cxt .blist = NIL ;
251
250
cxt .alist = NIL ;
252
251
cxt .pkey = NULL ;
@@ -339,11 +338,6 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
339
338
*/
340
339
transformCheckConstraints (& cxt , !cxt .isforeign );
341
340
342
- /*
343
- * Postprocess extended statistics.
344
- */
345
- transformExtendedStatistics (& cxt );
346
-
347
341
/*
348
342
* Output results.
349
343
*/
@@ -1111,61 +1105,25 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
1111
1105
}
1112
1106
1113
1107
/*
1114
- * We cannot yet deal with defaults, CHECK constraints, or indexes, since
1115
- * we don't yet know what column numbers the copied columns will have in
1116
- * the finished table. If any of those options are specified, add the
1117
- * LIKE clause to cxt->likeclauses so that expandTableLikeClause will be
1118
- * called after we do know that. Also, remember the relation OID so that
1119
- * expandTableLikeClause is certain to open the same table.
1108
+ * We cannot yet deal with defaults, CHECK constraints, indexes, or
1109
+ * statistics, since we don't yet know what column numbers the copied
1110
+ * columns will have in the finished table. If any of those options are
1111
+ * specified, add the LIKE clause to cxt->likeclauses so that
1112
+ * expandTableLikeClause will be called after we do know that. Also,
1113
+ * remember the relation OID so that expandTableLikeClause is certain to
1114
+ * open the same table.
1120
1115
*/
1121
1116
if (table_like_clause -> options &
1122
1117
(CREATE_TABLE_LIKE_DEFAULTS |
1123
1118
CREATE_TABLE_LIKE_GENERATED |
1124
1119
CREATE_TABLE_LIKE_CONSTRAINTS |
1125
- CREATE_TABLE_LIKE_INDEXES ))
1120
+ CREATE_TABLE_LIKE_INDEXES |
1121
+ CREATE_TABLE_LIKE_STATISTICS ))
1126
1122
{
1127
1123
table_like_clause -> relationOid = RelationGetRelid (relation );
1128
1124
cxt -> likeclauses = lappend (cxt -> likeclauses , table_like_clause );
1129
1125
}
1130
1126
1131
- /*
1132
- * We may copy extended statistics if requested, since the representation
1133
- * of CreateStatsStmt doesn't depend on column numbers.
1134
- */
1135
- if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1136
- {
1137
- List * parent_extstats ;
1138
- ListCell * l ;
1139
-
1140
- parent_extstats = RelationGetStatExtList (relation );
1141
-
1142
- foreach (l , parent_extstats )
1143
- {
1144
- Oid parent_stat_oid = lfirst_oid (l );
1145
- CreateStatsStmt * stats_stmt ;
1146
-
1147
- stats_stmt = generateClonedExtStatsStmt (cxt -> relation ,
1148
- RelationGetRelid (relation ),
1149
- parent_stat_oid );
1150
-
1151
- /* Copy comment on statistics object, if requested */
1152
- if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1153
- {
1154
- comment = GetComment (parent_stat_oid , StatisticExtRelationId , 0 );
1155
-
1156
- /*
1157
- * We make use of CreateStatsStmt's stxcomment option, so as
1158
- * not to need to know now what name the statistics will have.
1159
- */
1160
- stats_stmt -> stxcomment = comment ;
1161
- }
1162
-
1163
- cxt -> extstats = lappend (cxt -> extstats , stats_stmt );
1164
- }
1165
-
1166
- list_free (parent_extstats );
1167
- }
1168
-
1169
1127
/*
1170
1128
* Close the parent rel, but keep our AccessShareLock on it until xact
1171
1129
* commit. That will prevent someone else from deleting or ALTERing the
@@ -1423,6 +1381,44 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
1423
1381
}
1424
1382
}
1425
1383
1384
+ /*
1385
+ * Process extended statistics if required.
1386
+ */
1387
+ if (table_like_clause -> options & CREATE_TABLE_LIKE_STATISTICS )
1388
+ {
1389
+ List * parent_extstats ;
1390
+ ListCell * l ;
1391
+
1392
+ parent_extstats = RelationGetStatExtList (relation );
1393
+
1394
+ foreach (l , parent_extstats )
1395
+ {
1396
+ Oid parent_stat_oid = lfirst_oid (l );
1397
+ CreateStatsStmt * stats_stmt ;
1398
+
1399
+ stats_stmt = generateClonedExtStatsStmt (heapRel ,
1400
+ RelationGetRelid (childrel ),
1401
+ parent_stat_oid ,
1402
+ attmap );
1403
+
1404
+ /* Copy comment on statistics object, if requested */
1405
+ if (table_like_clause -> options & CREATE_TABLE_LIKE_COMMENTS )
1406
+ {
1407
+ comment = GetComment (parent_stat_oid , StatisticExtRelationId , 0 );
1408
+
1409
+ /*
1410
+ * We make use of CreateStatsStmt's stxcomment option, so as
1411
+ * not to need to know now what name the statistics will have.
1412
+ */
1413
+ stats_stmt -> stxcomment = comment ;
1414
+ }
1415
+
1416
+ result = lappend (result , stats_stmt );
1417
+ }
1418
+
1419
+ list_free (parent_extstats );
1420
+ }
1421
+
1426
1422
/* Done with child rel */
1427
1423
table_close (childrel , NoLock );
1428
1424
@@ -1837,10 +1833,12 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx,
1837
1833
* Generate a CreateStatsStmt node using information from an already existing
1838
1834
* extended statistic "source_statsid", for the rel identified by heapRel and
1839
1835
* heapRelid.
1836
+ *
1837
+ * Attribute numbers in expression Vars are adjusted according to attmap.
1840
1838
*/
1841
1839
static CreateStatsStmt *
1842
1840
generateClonedExtStatsStmt (RangeVar * heapRel , Oid heapRelid ,
1843
- Oid source_statsid )
1841
+ Oid source_statsid , const AttrMap * attmap )
1844
1842
{
1845
1843
HeapTuple ht_stats ;
1846
1844
Form_pg_statistic_ext statsrec ;
@@ -1923,10 +1921,19 @@ generateClonedExtStatsStmt(RangeVar *heapRel, Oid heapRelid,
1923
1921
1924
1922
foreach (lc , exprs )
1925
1923
{
1924
+ Node * expr = (Node * ) lfirst (lc );
1926
1925
StatsElem * selem = makeNode (StatsElem );
1926
+ bool found_whole_row ;
1927
+
1928
+ /* Adjust Vars to match new table's column numbering */
1929
+ expr = map_variable_attnos (expr ,
1930
+ 1 , 0 ,
1931
+ attmap ,
1932
+ InvalidOid ,
1933
+ & found_whole_row );
1927
1934
1928
1935
selem -> name = NULL ;
1929
- selem -> expr = ( Node * ) lfirst ( lc ) ;
1936
+ selem -> expr = expr ;
1930
1937
1931
1938
def_names = lappend (def_names , selem );
1932
1939
}
@@ -2652,19 +2659,6 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
2652
2659
return index ;
2653
2660
}
2654
2661
2655
- /*
2656
- * transformExtendedStatistics
2657
- * Handle extended statistic objects
2658
- *
2659
- * Right now, there's nothing to do here, so we just append the list to
2660
- * the existing "after" list.
2661
- */
2662
- static void
2663
- transformExtendedStatistics (CreateStmtContext * cxt )
2664
- {
2665
- cxt -> alist = list_concat (cxt -> alist , cxt -> extstats );
2666
- }
2667
-
2668
2662
/*
2669
2663
* transformCheckConstraints
2670
2664
* handle CHECK constraints
@@ -3457,7 +3451,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3457
3451
cxt .fkconstraints = NIL ;
3458
3452
cxt .ixconstraints = NIL ;
3459
3453
cxt .likeclauses = NIL ;
3460
- cxt .extstats = NIL ;
3461
3454
cxt .blist = NIL ;
3462
3455
cxt .alist = NIL ;
3463
3456
cxt .pkey = NULL ;
@@ -3763,9 +3756,6 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
3763
3756
newcmds = lappend (newcmds , newcmd );
3764
3757
}
3765
3758
3766
- /* Append extended statistics objects */
3767
- transformExtendedStatistics (& cxt );
3768
-
3769
3759
/* Close rel */
3770
3760
relation_close (rel , NoLock );
3771
3761
0 commit comments