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 1gYlZ9-00032R-K7 for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Dec 2018 05:35:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYlZ7-0000XP-Iv for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Dec 2018 05:35:41 +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 1gYlZ7-0000X6-3W for pgsql-hackers@lists.postgresql.org; Mon, 17 Dec 2018 05:35:41 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYlZ4-000391-4J for pgsql-hackers@lists.postgresql.org; Mon, 17 Dec 2018 05:35:39 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 5CC8C21F2A; Mon, 17 Dec 2018 00:35:37 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 17 Dec 2018 00:35:37 -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=YIcqgsMBhgYGKjjvY0cUgJ869J5 eb+j1Cvswsfcqjmg=; b=MDWK1c9yXSICFXfeloS69+imlgtvh1kXCVekhrzjj7m t5QwHMsWCL58QJNkOaTiT1hv0dwY6t69OojPnal0/IKiedowN75VXa+h0OoCIWZt 8VIerLndkcDkcLpkaQlMjI45PM152ctoCIBQbnl/TSFgEvjSPc6ZAWpYckVwRfk/ 54nzAZdrDALuGFt/QFCQlyHXc2zctjMAvZGT2hOXC7YnVJMmW2wwTZ5Gh0W1HNGW Wd4f6/8+4b6n1rnpnA+bE06nY8ye7Odk6G3Mt6o+3SXtL6J6AxMkwc3MOSYaQIyk W5z82UmNBnqK7gFxm+C4Eb0FPItgCa2WE3AP7dX7SAg== 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=YIcqgs MBhgYGKjjvY0cUgJ869J5eb+j1Cvswsfcqjmg=; b=h6g37mcWVUc1x6ApceVkAP E0+x0huBcJ4DdEdUYsKVEYI22+CbkagfKLb15hGPSAV/jRElbbALw3f9dgFZW2Tk 1v9IPWXMezq9Rou0OdY4Cq8RRY2kIxCvT43UlYJye03KyZpWUjvUMq83Eat7loaO 3uysOEjM2JfoQ51JXGbZozEzume6D8wHOw1e2hiQho2hmHXYqLMXm4sI0UVOJOhc fulJWt5TARbZKBBBwwQT3OH+Vp2ef73lSu7cZ6Co+Fwotwvh9cr5pv+ran+8ftTJ jdOMz5+wfHR6I64XpccUHgvCvk1tfH+TPpoLHWf60CU9TNicm7JjnLFR2Gq6OrKQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtkedrudeiuddgvdejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlhcuvffnffculdejtd dmnecujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefoihgt hhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesphgrqhhuihgvrhdrgiihiieqne cukfhppeeiuddrvdduhedrudefjedrudeivdenucfrrghrrghmpehmrghilhhfrhhomhep mhhitghhrggvlhesphgrqhhuihgvrhdrgiihiienucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from paquier.xyz (c137162.net61215.cablenet.ne.jp [61.215.137.162]) by mail.messagingengine.com (Postfix) with ESMTPA id A9650E409D; Mon, 17 Dec 2018 00:35:34 -0500 (EST) Date: Mon, 17 Dec 2018 14:35:31 +0900 From: Michael Paquier To: amul sul Cc: PostgreSQL Hackers , Tom Lane , Alexander Korotkov Subject: Re: ALTER INDEX ... ALTER COLUMN not present in dump Message-ID: <20181217053531.GH31474@paquier.xyz> References: <20181122003147.GA3369@paquier.xyz> <154477492593.1386.13640101253353637283.pgcf@coridan.postgresql.org> <20181217051439.GG31474@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="cVp8NMj01v+Em8Se" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --cVp8NMj01v+Em8Se Content-Type: multipart/mixed; boundary="SdvjNjn6lL3tIsv0" Content-Disposition: inline --SdvjNjn6lL3tIsv0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Dec 17, 2018 at 10:59:08AM +0530, amul sul wrote: > Patch is missing? Here you go. The patch is still using atttribute names, which is a bad idea ;) -- Michael --SdvjNjn6lL3tIsv0 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="dump-alter-index-stats-v3.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 637c79af48..09e90ea62c 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -6712,7 +6712,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++) @@ -6766,7 +6768,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 pg_catalog.array_agg(attname ORDER BY attname::text COLL= ATE \"C\") " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid =3D i.indexrelid AND " + " attstattarget >=3D 0) AS indstatcols," + "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attname::tex= t 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) " @@ -6803,7 +6813,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 " @@ -6836,7 +6848,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 " @@ -6865,7 +6879,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 " @@ -6897,7 +6913,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 " @@ -6935,6 +6953,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)); @@ -6958,6 +6978,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); @@ -16148,6 +16170,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); @@ -16171,6 +16200,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) { @@ -16194,6 +16244,11 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo) q->data, delq->data, NULL, NULL, 0, NULL, NULL); + + if (indstatcolsarray) + free(indstatcolsarray); + if (indstatvalsarray) + free(indstatvalsarray); } =20 /* Dump Index Comments */ diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 789d6a24e2..527c233851 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -360,6 +360,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 46dbb078cf..c5a2b7485e 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -2343,6 +2343,24 @@ 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> + { %full_runs, %dump_test_schema_runs, section_post_data =3D> 1, }, + unlike =3D> { exclude_dump_test_schema =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 --SdvjNjn6lL3tIsv0-- --cVp8NMj01v+Em8Se Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlwXNaMACgkQnvQgOdby QH3RFhAAjWpc94boVdkA5gb1ffwmDh6UJc2BQKpBIUF55ZjlGsfsSRTe+YjOxQHr L56Nb9+AqzNjF+0aqNHG6Kk4SsxssFqlvsfZdsfZF81PyBiJ6E0Bh6c732vrCERx XPg/CV8GZM7NXBGWBze6tbftfiwcns+9aKtdbOL9OnLVWBs6oiEpLCkEDaaDNTTK GT0DKK6Ty6uW78YU2qQBm4eh/KnacZ+5vLphBucEWiNx5phyodrhHqr8xEo/2/9D /3via1AxYFG1CGXfEjwN1vBF6ZE8qlpC9ha7pM1n4Z+rUmCsCkZBMZnt3HUJ/rJg OIFmBDYs6QmSdSEFF4IE7Ef8ybmxl5L+pIAkEv0mh9POYxPOL2pVkx0kkD3XVr/g q3aOSwzj8apm9TeBeWrtO3BesniHxIqh4WcAj5TM9c1w2f95toHgT1BUWQqwFrGy 7QeksT1J/h7qSsuSogMUlhUKEeiVDeEVnsZ9RNUx70wI7lwSHH5VRHcUcTUKdsKn ZOpgfnZaSaM/iSYMiKFlqDGUF/xIgOl8aZQghTTba1jOs3YXlA24b4EuECOb5HMP of/t+p6+zBYtrnKT09yR2WD9kYoLinQJpKGjaYj0EmYKSWl9CNovDaib1IytwSqY vUajR1AKb7+5gFk4ceFFCAgNPs6ATYPmXnAL4gzcMuteKvzrcwo= =hpGX -----END PGP SIGNATURE----- --cVp8NMj01v+Em8Se--