pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Adrien NAYRAT <adrien.nayrat@anayrat.info>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: ALTER INDEX ... ALTER COLUMN not present in dump
Date: Fri, 16 Nov 2018 22:31:03 +0900
Message-ID: <20181116133103.GA3427@paquier.xyz> (raw)
In-Reply-To: <20181116083252.GF6613@paquier.xyz>
References: <CAARsnT3UQ4V=yDNW468w8RqHfYiY9mpn2r_c5UkBJ97NAApUEw@mail.gmail.com>
	<4498621542278682@iva3-0d9a30469759.qloud-c.yandex.net>
	<CAARsnT1bK2473gUq09GZXVdn7nCE3eSSgSfsxFzYHgADCEhoUA@mail.gmail.com>
	<4847941542280801@iva7-4f557d6b60d4.qloud-c.yandex.net>
	<2b5f4be0-9a67-a5d7-4561-09871378a991@anayrat.info>
	<20181115224601.GB6613@paquier.xyz>
	<20181116004505.GC6613@paquier.xyz>
	<20181116083252.GF6613@paquier.xyz>

On Fri, Nov 16, 2018 at 05:32:52PM +0900, Michael Paquier wrote:
> And here you go as attached.  Looking closer, in v10 and older versions,
> ALTER INDEX SET STATISTICS is able to work as it is an alias of ALTER
> TABLE.  The attached patch does not bother generating the ALTER INDEX
> queries for v10 and older and feeds queries with empty strings.  Perhaps
> we should support that case?  Or the lack of complains would be an
> argument sufficient to care only about v11 and newer versions?  I would
> tend to think that supporting only v11 and above is enough.  Thoughts
> are welcome.

+       appendPQExpBuffer(q, "ALTER COLUMN %s ",
+                         indstatcolsarray[j])

This forgot a wrapping with fmtId().  Friday hits hard..
--
Michael

Attachments:

dump-alter-index-stats-v2.patch (text/x-diff, 8.2K) download diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index c8d01ed4a4..767644cf49 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -6772,7 +6772,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) i_conoid, i_condef, i_tablespace, - i_indreloptions; + i_indreloptions, + i_indstatcols, + i_indstatvals; int ntups; for (i = 0; i < numTables; i++) @@ -6826,7 +6828,15 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) "c.oid AS conoid, " "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, " "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " - "t.reloptions AS indreloptions " + "t.reloptions AS indreloptions, " + "(SELECT array_agg(attname ORDER BY attname::text COLLATE \"C\") " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid = i.indexrelid AND " + " attstattarget >= 0) AS indstatcols," + "(SELECT array_agg(attstattarget ORDER BY attname::text COLLATE \"C\") " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid = i.indexrelid AND " + " attstattarget >= 0) AS indstatvals " "FROM pg_catalog.pg_index i " "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " "JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) " @@ -6863,7 +6873,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) "c.oid AS conoid, " "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, " "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " - "t.reloptions AS indreloptions " + "t.reloptions AS indreloptions, " + "'' AS indstatcols, " + "'' AS indstatvals " "FROM pg_catalog.pg_index i " "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " "LEFT JOIN pg_catalog.pg_constraint c " @@ -6896,7 +6908,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) "c.oid AS conoid, " "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, " "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " - "t.reloptions AS indreloptions " + "t.reloptions AS indreloptions, " + "'' AS indstatcols, " + "'' AS indstatvals " "FROM pg_catalog.pg_index i " "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " "LEFT JOIN pg_catalog.pg_constraint c " @@ -6925,7 +6939,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) "c.oid AS conoid, " "null AS condef, " "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " - "t.reloptions AS indreloptions " + "t.reloptions AS indreloptions, " + "'' AS indstatcols, " + "'' AS indstatvals " "FROM pg_catalog.pg_index i " "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " "LEFT JOIN pg_catalog.pg_depend d " @@ -6957,7 +6973,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) "c.oid AS conoid, " "null AS condef, " "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " - "null AS indreloptions " + "null AS indreloptions, " + "'' AS indstatcols, " + "'' AS indstatvals " "FROM pg_catalog.pg_index i " "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " "LEFT JOIN pg_catalog.pg_depend d " @@ -6995,6 +7013,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) i_condef = PQfnumber(res, "condef"); i_tablespace = PQfnumber(res, "tablespace"); i_indreloptions = PQfnumber(res, "indreloptions"); + i_indstatcols = PQfnumber(res, "indstatcols"); + i_indstatvals = PQfnumber(res, "indstatvals"); tbinfo->indexes = indxinfo = (IndxInfo *) pg_malloc(ntups * sizeof(IndxInfo)); @@ -7018,6 +7038,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts)); indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace)); indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions)); + indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols)); + indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals)); indxinfo[j].indkeys = (Oid *) pg_malloc(indxinfo[j].indnattrs * sizeof(Oid)); parseOidArray(PQgetvalue(res, j, i_indkey), indxinfo[j].indkeys, indxinfo[j].indnattrs); @@ -16207,6 +16229,13 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo) */ if (!is_constraint) { + char *indstatcols = indxinfo->indstatcols; + char *indstatvals = indxinfo->indstatvals; + char **indstatcolsarray = NULL; + char **indstatvalsarray = NULL; + int nstatcols; + int nstatvals; + if (dopt->binary_upgrade) binary_upgrade_set_pg_class_oids(fout, q, indxinfo->dobj.catId.oid, true); @@ -16230,6 +16259,27 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo) qindxname); } + /* + * If the index has any statistics on some of its columns, + * generate the associated ALTER INDEX queries. + */ + if (parsePGArray(indstatcols, &indstatcolsarray, &nstatcols) && + parsePGArray(indstatvals, &indstatvalsarray, &nstatvals) && + nstatcols == nstatvals) + { + int j; + + for (j = 0; j < nstatcols; j++) + { + appendPQExpBuffer(q, "ALTER INDEX %s ", + fmtQualifiedDumpable(indxinfo)); + appendPQExpBuffer(q, "ALTER COLUMN %s ", + fmtId(indstatcolsarray[j])); + appendPQExpBuffer(q, "SET STATISTICS %s;\n", + indstatvalsarray[j]); + } + } + /* If the index defines identity, we need to record that. */ if (indxinfo->indisreplident) { diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 685ad78669..58ca0f7d3d 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -361,6 +361,8 @@ typedef struct _indxInfo char *indexdef; char *tablespace; /* tablespace in which index is stored */ char *indreloptions; /* options specified by WITH (...) */ + char *indstatcols; /* column names with statistics */ + char *indstatvals; /* statistic values for columns */ int indnkeyattrs; /* number of index key attributes */ int indnattrs; /* total number of index attributes */ Oid *indkeys; /* In spite of the name 'indkeys' this field diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index ec751a7c23..94a2b404c8 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -2420,6 +2420,47 @@ my %tests = ( unlike => { exclude_dump_test_schema => 1, }, }, + 'CREATE TABLE table_with_stats' => { + create_order => 98, + create_sql => 'CREATE TABLE dump_test.table_index_stats ( + col1 int, + col2 int); + CREATE INDEX index_with_stats + ON dump_test.table_index_stats + ((col1 + 1), (col2 + 1)); + ALTER INDEX dump_test.index_with_stats + ALTER COLUMN expr SET STATISTICS 1000;', + regexp => qr/^ + \QALTER INDEX dump_test.index_with_stats ALTER COLUMN expr SET STATISTICS 1000;\E + /xm, + like => { + binary_upgrade => 1, + clean => 1, + clean_if_exists => 1, + createdb => 1, + defaults => 1, + exclude_test_table => 1, + exclude_test_table_data => 1, + no_blobs => 1, + no_privs => 1, + no_owner => 1, + only_dump_test_schema => 1, + pg_dumpall_dbprivs => 1, + schema_only => 1, + section_post_data => 1, + test_schema_plus_blobs => 1, + with_oids => 1, + }, + unlike => { + exclude_dump_test_schema => 1, + only_dump_test_table => 1, + pg_dumpall_globals => 1, + pg_dumpall_globals_clean => 1, + role => 1, + section_pre_data => 1, + }, + }, + 'CREATE STATISTICS extended_stats_no_options' => { create_order => 97, create_sql => 'CREATE STATISTICS dump_test.test_ext_stats_no_options
signature.asc (application/pgp-signature, 832B) download

view thread (28+ messages)  latest in thread

Message-ID: <20181116133103.GA3427@paquier.xyz>
Permalink:  ../20181116133103.GA3427@paquier.xyz/
Also on:    postgresql.org/message-id/20181116133103.GA3427@paquier.xyz

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: michael@paquier.xyz, adrien.nayrat@anayrat.info
  Subject: Re: ALTER INDEX ... ALTER COLUMN not present in dump
  In-Reply-To: <20181116133103.GA3427@paquier.xyz>
  References: <CAARsnT3UQ4V=yDNW468w8RqHfYiY9mpn2r_c5UkBJ97NAApUEw@mail.gmail.com> <4498621542278682@iva3-0d9a30469759.qloud-c.yandex.net> <CAARsnT1bK2473gUq09GZXVdn7nCE3eSSgSfsxFzYHgADCEhoUA@mail.gmail.com> <4847941542280801@iva7-4f557d6b60d4.qloud-c.yandex.net> <2b5f4be0-9a67-a5d7-4561-09871378a991@anayrat.info> <20181115224601.GB6613@paquier.xyz> <20181116004505.GC6613@paquier.xyz> <20181116083252.GF6613@paquier.xyz> <20181116133103.GA3427@paquier.xyz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox