@@ -6752,7 +6752,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6752
6752
i_condef,
6753
6753
i_tablespace,
6754
6754
i_indreloptions,
6755
- i_relpages;
6755
+ i_relpages,
6756
+ i_indstatcols,
6757
+ i_indstatvals;
6756
6758
int ntups;
6757
6759
6758
6760
for (i = 0; i < numTables; i++)
@@ -6806,7 +6808,15 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6806
6808
"c.oid AS conoid, "
6807
6809
"pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6808
6810
"(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6809
- "t.reloptions AS indreloptions "
6811
+ "t.reloptions AS indreloptions, "
6812
+ "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) "
6813
+ " FROM pg_catalog.pg_attribute "
6814
+ " WHERE attrelid = i.indexrelid AND "
6815
+ " attstattarget >= 0) AS indstatcols,"
6816
+ "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) "
6817
+ " FROM pg_catalog.pg_attribute "
6818
+ " WHERE attrelid = i.indexrelid AND "
6819
+ " attstattarget >= 0) AS indstatvals "
6810
6820
"FROM pg_catalog.pg_index i "
6811
6821
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6812
6822
"JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) "
@@ -6843,7 +6853,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6843
6853
"c.oid AS conoid, "
6844
6854
"pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6845
6855
"(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6846
- "t.reloptions AS indreloptions "
6856
+ "t.reloptions AS indreloptions, "
6857
+ "'' AS indstatcols, "
6858
+ "'' AS indstatvals "
6847
6859
"FROM pg_catalog.pg_index i "
6848
6860
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6849
6861
"LEFT JOIN pg_catalog.pg_constraint c "
@@ -6876,7 +6888,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6876
6888
"c.oid AS conoid, "
6877
6889
"pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6878
6890
"(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6879
- "t.reloptions AS indreloptions "
6891
+ "t.reloptions AS indreloptions, "
6892
+ "'' AS indstatcols, "
6893
+ "'' AS indstatvals "
6880
6894
"FROM pg_catalog.pg_index i "
6881
6895
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6882
6896
"LEFT JOIN pg_catalog.pg_constraint c "
@@ -6905,7 +6919,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6905
6919
"c.oid AS conoid, "
6906
6920
"null AS condef, "
6907
6921
"(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6908
- "t.reloptions AS indreloptions "
6922
+ "t.reloptions AS indreloptions, "
6923
+ "'' AS indstatcols, "
6924
+ "'' AS indstatvals "
6909
6925
"FROM pg_catalog.pg_index i "
6910
6926
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6911
6927
"LEFT JOIN pg_catalog.pg_depend d "
@@ -6937,7 +6953,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6937
6953
"c.oid AS conoid, "
6938
6954
"null AS condef, "
6939
6955
"(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6940
- "null AS indreloptions "
6956
+ "null AS indreloptions, "
6957
+ "'' AS indstatcols, "
6958
+ "'' AS indstatvals "
6941
6959
"FROM pg_catalog.pg_index i "
6942
6960
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6943
6961
"LEFT JOIN pg_catalog.pg_depend d "
@@ -6976,6 +6994,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6976
6994
i_condef = PQfnumber(res, "condef");
6977
6995
i_tablespace = PQfnumber(res, "tablespace");
6978
6996
i_indreloptions = PQfnumber(res, "indreloptions");
6997
+ i_indstatcols = PQfnumber(res, "indstatcols");
6998
+ i_indstatvals = PQfnumber(res, "indstatvals");
6979
6999
6980
7000
tbinfo->indexes = indxinfo =
6981
7001
(IndxInfo *) pg_malloc(ntups * sizeof(IndxInfo));
@@ -6999,6 +7019,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6999
7019
indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts));
7000
7020
indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
7001
7021
indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions));
7022
+ indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols));
7023
+ indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals));
7002
7024
indxinfo[j].indkeys = (Oid *) pg_malloc(indxinfo[j].indnattrs * sizeof(Oid));
7003
7025
parseOidArray(PQgetvalue(res, j, i_indkey),
7004
7026
indxinfo[j].indkeys, indxinfo[j].indnattrs);
@@ -16242,6 +16264,13 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
16242
16264
*/
16243
16265
if (!is_constraint)
16244
16266
{
16267
+ char *indstatcols = indxinfo->indstatcols;
16268
+ char *indstatvals = indxinfo->indstatvals;
16269
+ char **indstatcolsarray = NULL;
16270
+ char **indstatvalsarray = NULL;
16271
+ int nstatcols;
16272
+ int nstatvals;
16273
+
16245
16274
if (dopt->binary_upgrade)
16246
16275
binary_upgrade_set_pg_class_oids(fout, q,
16247
16276
indxinfo->dobj.catId.oid, true);
@@ -16265,6 +16294,32 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
16265
16294
qindxname);
16266
16295
}
16267
16296
16297
+ /*
16298
+ * If the index has any statistics on some of its columns, generate
16299
+ * the associated ALTER INDEX queries.
16300
+ */
16301
+ if (parsePGArray(indstatcols, &indstatcolsarray, &nstatcols) &&
16302
+ parsePGArray(indstatvals, &indstatvalsarray, &nstatvals) &&
16303
+ nstatcols == nstatvals)
16304
+ {
16305
+ int j;
16306
+
16307
+ for (j = 0; j < nstatcols; j++)
16308
+ {
16309
+ appendPQExpBuffer(q, "ALTER INDEX %s ",
16310
+ fmtQualifiedDumpable(indxinfo));
16311
+
16312
+ /*
16313
+ * Note that this is a column number, so no quotes should be
16314
+ * used.
16315
+ */
16316
+ appendPQExpBuffer(q, "ALTER COLUMN %s ",
16317
+ indstatcolsarray[j]);
16318
+ appendPQExpBuffer(q, "SET STATISTICS %s;\n",
16319
+ indstatvalsarray[j]);
16320
+ }
16321
+ }
16322
+
16268
16323
/* If the index defines identity, we need to record that. */
16269
16324
if (indxinfo->indisreplident)
16270
16325
{
@@ -16288,6 +16343,11 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
16288
16343
q->data, delq->data, NULL,
16289
16344
NULL, 0,
16290
16345
NULL, NULL);
16346
+
16347
+ if (indstatcolsarray)
16348
+ free(indstatcolsarray);
16349
+ if (indstatvalsarray)
16350
+ free(indstatvalsarray);
16291
16351
}
16292
16352
16293
16353
/* Dump Index Comments */
0 commit comments