Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gNeFV-0001Co-2M for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Nov 2018 13:33:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gNeDM-0005z0-G0 for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Nov 2018 13:31:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gNeDK-0005yA-La for pgsql-bugs@lists.postgresql.org; Fri, 16 Nov 2018 13:31:16 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gNeDG-0000VC-QF for pgsql-bugs@lists.postgresql.org; Fri, 16 Nov 2018 13:31:13 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 5995D22005; Fri, 16 Nov 2018 08:31:09 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 16 Nov 2018 08:31:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=JC3ZSkM2gGEf2ikUSPCyk/JQal5 jzYnLc6H7nrkEcMA=; b=IJJs/MnyjJEQJa2y7Ee0Uf23UAodHa6tdoxQg07uezi IOilwmSFlBROcawtSPnXf66JjXRR3c9GKyhw8WfA0q0TZoJSXCjHATIKDlTkaIwA 1pBg0EitaLvN5DNswUuvP1Q4TLC0hDWe6UIF35Q2yeh2cmPTRA6uyhDO+fArbHep 5XL01yoas2bhc3HtNZQ3tXsUMbY/p0h/LlgU0YDw987+4ocH5mD6YMfh1bYD50KB AlxapC9AXjy39kQgS7sY8tsT3zGKV+BrDM5LJoLsZOYT5qkL7WGiydAXMlw9hOf3 mP/NWJ25MwOaZfmIfG0klE45wzhi2rO7xhZrcrM8RPQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=JC3ZSk M2gGEf2ikUSPCyk/JQal5jzYnLc6H7nrkEcMA=; b=eXjyRsj/HxgAlzT5GzlUeq BC8NZSLsMIBr9pLr3lGlEw2C0DJhmTpu24VM9wCNHos6PQjrE36QFF5hkMRl+e2n OLiOzJVMVoGI3qFisFcD4e0041jRp1xIr88gZ31zNLbQVxdRLXCbBvVO2vvIZA0g cXhcSKBW2iG+rbP+vJDROG10V+Kv2M/2trGTyhPe1ANAPz4fNUJskvuXqN/gmuaA iFNIUvMsvdwsQoe0/ed6ZD15V+8uS5aopBFS16S/OKxXI0UggTKRznoJGuqODGoR UO+TH8+2QFUWkLDCx7mraN4sy0tLx5UU9wKMU2dKa3g6dEFPst1dFJyK+dqO9keQ == X-ME-Sender: X-ME-Proxy: Received: from paquier.xyz (c137162.net61215.cablenet.ne.jp [61.215.137.162]) by mail.messagingengine.com (Postfix) with ESMTPA id 1D518E405D; Fri, 16 Nov 2018 08:31:06 -0500 (EST) Date: Fri, 16 Nov 2018 22:31:03 +0900 From: Michael Paquier To: Adrien NAYRAT Cc: pgsql-bugs@lists.postgresql.org Subject: Re: ALTER INDEX ... ALTER COLUMN not present in dump Message-ID: <20181116133103.GA3427@paquier.xyz> References: <4498621542278682@iva3-0d9a30469759.qloud-c.yandex.net> <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> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="H+4ONPRPur6+Ovig" Content-Disposition: inline In-Reply-To: <20181116083252.GF6613@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --H+4ONPRPur6+Ovig Content-Type: multipart/mixed; boundary="ReaqsoxgOBHFXBhH" Content-Disposition: inline --ReaqsoxgOBHFXBhH Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --ReaqsoxgOBHFXBhH Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="dump-alter-index-stats-v2.patch" Content-Transfer-Encoding: quoted-printable 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 nu= mTables) i_conoid, i_condef, i_tablespace, - i_indreloptions; + i_indreloptions, + i_indstatcols, + i_indstatvals; int ntups; =20 for (i =3D 0; i < numTables; i++) @@ -6826,7 +6828,15 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int n= umTables) "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 =3D = 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 =3D i.indexrelid AND " + " attstattarget >=3D 0) AS indstatcols," + "(SELECT array_agg(attstattarget ORDER BY attname::text COLLATE \= "C\") " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid =3D i.indexrelid AND " + " attstattarget >=3D 0) AS indstatvals " "FROM pg_catalog.pg_index i " "JOIN pg_catalog.pg_class t ON (t.oid =3D i.indexrelid) " "JOIN pg_catalog.pg_class t2 ON (t2.oid =3D i.indrelid) " @@ -6863,7 +6873,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int nu= mTables) "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 =3D = 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 =3D i.indexrelid) " "LEFT JOIN pg_catalog.pg_constraint c " @@ -6896,7 +6908,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int nu= mTables) "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 =3D = 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 =3D i.indexrelid) " "LEFT JOIN pg_catalog.pg_constraint c " @@ -6925,7 +6939,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int nu= mTables) "c.oid AS conoid, " "null AS condef, " "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid =3D = 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 =3D i.indexrelid) " "LEFT JOIN pg_catalog.pg_depend d " @@ -6957,7 +6973,9 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int nu= mTables) "c.oid AS conoid, " "null AS condef, " "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid =3D = 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 =3D i.indexrelid) " "LEFT JOIN pg_catalog.pg_depend d " @@ -6995,6 +7013,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int nu= mTables) i_condef =3D PQfnumber(res, "condef"); i_tablespace =3D PQfnumber(res, "tablespace"); i_indreloptions =3D PQfnumber(res, "indreloptions"); + i_indstatcols =3D PQfnumber(res, "indstatcols"); + i_indstatvals =3D PQfnumber(res, "indstatvals"); =20 tbinfo->indexes =3D indxinfo =3D (IndxInfo *) pg_malloc(ntups * sizeof(IndxInfo)); @@ -7018,6 +7038,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int nu= mTables) indxinfo[j].indnattrs =3D atoi(PQgetvalue(res, j, i_indnatts)); indxinfo[j].tablespace =3D pg_strdup(PQgetvalue(res, j, i_tablespace)); indxinfo[j].indreloptions =3D pg_strdup(PQgetvalue(res, j, i_indrelopti= ons)); + indxinfo[j].indstatcols =3D pg_strdup(PQgetvalue(res, j, i_indstatcols)= ); + indxinfo[j].indstatvals =3D pg_strdup(PQgetvalue(res, j, i_indstatvals)= ); indxinfo[j].indkeys =3D (Oid *) pg_malloc(indxinfo[j].indnattrs * sizeo= f(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 =3D indxinfo->indstatcols; + char *indstatvals =3D indxinfo->indstatvals; + char **indstatcolsarray =3D NULL; + char **indstatvalsarray =3D 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); } =20 + /* + * 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 =3D=3D nstatvals) + { + int j; + + for (j =3D 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_du= mp.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 =3D ( unlike =3D> { exclude_dump_test_schema =3D> 1, }, }, =20 + 'CREATE TABLE table_with_stats' =3D> { + create_order =3D> 98, + create_sql =3D> '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 =3D> qr/^ + \QALTER INDEX dump_test.index_with_stats ALTER COLUMN expr SET STATISTI= CS 1000;\E + /xm, + like =3D> { + binary_upgrade =3D> 1, + clean =3D> 1, + clean_if_exists =3D> 1, + createdb =3D> 1, + defaults =3D> 1, + exclude_test_table =3D> 1, + exclude_test_table_data =3D> 1, + no_blobs =3D> 1, + no_privs =3D> 1, + no_owner =3D> 1, + only_dump_test_schema =3D> 1, + pg_dumpall_dbprivs =3D> 1, + schema_only =3D> 1, + section_post_data =3D> 1, + test_schema_plus_blobs =3D> 1, + with_oids =3D> 1, + }, + unlike =3D> { + exclude_dump_test_schema =3D> 1, + only_dump_test_table =3D> 1, + pg_dumpall_globals =3D> 1, + pg_dumpall_globals_clean =3D> 1, + role =3D> 1, + section_pre_data =3D> 1, + }, + }, + 'CREATE STATISTICS extended_stats_no_options' =3D> { create_order =3D> 97, create_sql =3D> 'CREATE STATISTICS dump_test.test_ext_stats_no_options --ReaqsoxgOBHFXBhH-- --H+4ONPRPur6+Ovig Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlvuxpcACgkQnvQgOdby QH3BMQ/8CqY9Hq6YElDnWZazTbFk/2556M9vHl2+R5JY9ig+v2V6rub0cO53GYTh QyW2EIGswOqwF7Po1aj0IsUyjYrw+UpHfGvxV5fbS7AARSQSJ6SB1xo7yW6z1mzO HwMXFU1hSPD2cxKsAjQeSwyFGO88UUKlLiB9umoEY3rAV6y1gylAiwi9cljerHJ5 P30ianRQy9owOmsJQ/dse0mwfJ7pHtMrjHWBLE/d3cQ6lJNYrMK4uTdxvySJvnj/ qYMIYkVSCsKEbOx6/fKG6YKNacI63Moy8SZrBmhOnAb8cX9a5yYz+CeFxfZkAsm0 50qtcI/SnZX8lGmmkm6byaHCXxz4U+w9pT7HXs6Q9ZVlhOxe62ksOlsM91aQvHYz o2YYxH1UFD0IO8bGmVmH45FtAgkiNzwK2oxu5D5xXIPJhd2byu17vjNBRvceeqti nkKhKWfsxjZFEn7BL6OHMJtJadgzWIUluMulp5mdUjDKHg9jU4fafnCpQitLoVnA laUHuS/n7I0dS4G2GqvPb/1POR40fwNECTyaN5SDWbqMB0nmFWUVLmK+4QJ1Q5Rb zyRHLOApLd7HTbXrRqcX8rSBkMkopEZ6G8jsE4KXdEOFWZMZJMFtnxYwq6sMqdzj uuzbKxhoLgEFrqFLM+WF2/qc3NViGF4AmHvENaMbNSBeZ7vCswM= =fYTx -----END PGP SIGNATURE----- --H+4ONPRPur6+Ovig--