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 1gNZYq-0001Vg-I6 for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Nov 2018 08:33:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gNZYn-0006Dr-Hz for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Nov 2018 08:33:05 +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 1gNZYl-0006Dk-Py for pgsql-bugs@lists.postgresql.org; Fri, 16 Nov 2018 08:33:05 +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 1gNZYh-0002S8-Ss for pgsql-bugs@lists.postgresql.org; Fri, 16 Nov 2018 08:33:02 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 46A4921E44; Fri, 16 Nov 2018 03:32:58 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 16 Nov 2018 03:32:58 -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=mit9geUg1X4JPycvowO1tSG6sR5 77r2U09Mw19XhLkk=; b=RZ35QZz+U0SLQQzxQOvgkrp41a44bOTWMtn293HAk2l G9LocOHi34Td+vleN+VMMRuZ+TW1HrmTKXhxyRFvAaioRc/JMIh1j/QvIo89QMGR TAU2AzOPowIUOYe4avEj1j2ttYY489GGSejaUMFbfyd3PkpX9ZZbJ0dSC5TKT6tY 7KtR/B8yyWLi5yU2w1ozAFN9PmE3jianX4kOBRlFHuuLudjlLIt+0ownsLxatpgr 1A3MNDo4d5t/Xre3ZHP6UPTgvS9BZ1/lhlqMQIk7LwktsJcp2BljmaN5QvYNZmvv cp3w2e0+0+8zwqY1Usz8CFYH7AoSiFDsM43FNo+s4KA== 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=mit9ge Ug1X4JPycvowO1tSG6sR577r2U09Mw19XhLkk=; b=HQzEitCLJGer1s6t3RLsrw XF9g5YUMRJ9tono847H1Z01Pjhif76yq4GNo04Syz/tGp5fbpAdHT8t5lu72Rt2F 6XS+FKbvbP47hSgQZRzoOPw8r72835HUl2LqY2UhBttggIL3gz1gaS5FBTCrEFwC Ar6Laewf8nbcHkPaEw2ISnSkmypV7B/J1wKYfC8A8dWcdkS9bstAvWQQVkTVsE3G zwUBK+wB8xmLaoM2QylfalyVK6NT03e99UkZ2Ghk5QPpVsQmt/d5/Gp154CRrFdE 0HsenT/44EZnsKyoKFrMlmUGZuO9/WFMdtcfYgj3S5om86GheUbouNn6Bzt5B4ww == X-ME-Sender: X-ME-Proxy: Received: from paquier.xyz (mp76f1f9fe.ap.nuro.jp [118.241.249.254]) by mail.messagingengine.com (Postfix) with ESMTPA id 37719E4678; Fri, 16 Nov 2018 03:32:55 -0500 (EST) Date: Fri, 16 Nov 2018 17:32:52 +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: <20181116083252.GF6613@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> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="p7qwJlK53pWzbayA" Content-Disposition: inline In-Reply-To: <20181116004505.GC6613@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --p7qwJlK53pWzbayA Content-Type: multipart/mixed; boundary="pyE8wggRBhVBcj8z" Content-Disposition: inline --pyE8wggRBhVBcj8z Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Nov 16, 2018 at 09:45:05AM +0900, Michael Paquier wrote: > A good thing is that when ALTER INDEX .. SET STATISTICS is applied on an > index of a partitioned table, the statement is not cascaded to the > existing partitions. We may want in the future to support ONLY and make > the inheritance automatic. But that's another topic, and the fix for > v11 should be chirurgical. 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. -- Michael --pyE8wggRBhVBcj8z Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="dump-alter-index-stats.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..5c1bee0cb7 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 ", + 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 --pyE8wggRBhVBcj8z-- --p7qwJlK53pWzbayA Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlvugLQACgkQnvQgOdby QH0ULA/9EdzqGNGPizV+Aue3XvCAbJhHoMhO8T9GpEE92FAv+hUExL8NNam8Vg1R yESO1LqEFjHJVAM3B1OKusGPRsStnnaHnSWDj6oP5YJEAXW+Fwz1deTV6T7cDtIO G1HXTr921oGXFIYFtAY7RIO4acScqoagcVnBWLHRi4zK7zrCJYZSrRUhVlAfG5Z/ B1ZKO/36gx0lvYp4VdMWU8h/t4DJ82PE1q5EA78AyJ/J1hsRlB7TFMBHH/jCSCXV igHzf2bA3/gq5i01rE30XuQKet3Bgd0MZgMJXSIwrqMcglgKOvHpvPzcAc30JrHc rkJOugm7L/Iqo37CEgSUqjpAzJlQ91UgCWmfvOEMwREbKZagQQyBIe9xgqX4UtLj qLCsVlGKv6dXeF89Q5tLVyxxmG2V3nMHvNn0omS8hHxRxg1DHoPQMk+pGzjK+Er8 +O82hQQc650MpUX4bVsnC2EchkkFD0vQoST99pD4kn3QPeL81qpXAno4VrPgwURy KzQB5VtJwemoUYs4g3rxY1PEg3sRy45FRSAJK7MAGdt0bYFR58tzTpVsy3DnIWcG mg++cwkutInxkOBsc1LatpZhch1YS3aVjxeR/7cIY5vwEcvkdqF+GaE0+a+2+Edj yR29kksCFNEe61ARFh00CXbG0IYNS2tRKnWf9bYpl26Jl5oMV5Y= =zGST -----END PGP SIGNATURE----- --p7qwJlK53pWzbayA--