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 1gYmKY-00054B-HM for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Dec 2018 06:24: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 1gYmKW-0006If-Hm for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Dec 2018 06:24:40 +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 1gYmKU-0006IS-8c for pgsql-hackers@lists.postgresql.org; Mon, 17 Dec 2018 06:24:40 +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 1gYmKO-0004G4-8Z for pgsql-hackers@lists.postgresql.org; Mon, 17 Dec 2018 06:24:37 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 45EB121D8B; Mon, 17 Dec 2018 01:24:31 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 17 Dec 2018 01:24:31 -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=PG9leDqquB+JJdKiIoQbHcRgVSS pUo3Hk+f7nCDSbsM=; b=T2jX0H6jjhWPT54Xq7V9Jijp+1YM4A0fWwBdP3Dcsei V0vbVyjnHLdxhp+HBk9WJUIyDlGv9F/fjHC1pVZ6VHZFcXREx3Picg/E7+y66Aba TPapsSoiLSjArdAF0d41w4HUUuXCB2JiB8iI8cebD/yLRR4LUnXYpm963sdLt5Jr zh1POkSRKiZ2dfcM8OYfjTmX6VKxJda7+OfJ3FGwwKW28a6wyMJbPgOKnzTmbhfE YZip0o3CqVDiyygIQXbY73l954sxFBWrLFSIHYKfmaO0MvniHCmRebfkayNDaolA 0NH4HrdjWzOY6kmclXrZc37XyrHIy3x1fWSDbcu3ifg== 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=PG9leD qquB+JJdKiIoQbHcRgVSSpUo3Hk+f7nCDSbsM=; b=NmpOUXWRS4TWNjUba8xKDC qaEM3EmboicA+UNzOTb8J2HR4CoK49yLybdjN5UYPiCTL5xozEa8tUii4hFFyOM1 0aZ27ZbUl1tBbpu9MIZdnOWxg9tiYdFMFtJleKt1ghJJWzi+QvktalZfNvgVgJEI IqmSyYWR/UPYQ+QExJmNHORFB4sg2XNXZvUy3vzbGskh7zCJSLScb5QNp3rxGtcf L3CZFjbvbwLpMZx9MxSUK3M6t8QiNJq86vjWaQ+Kpp0dwQOzKuu4WjGjrhZgW9At Wgp/AA0cTKh+ZA88nU58QL57eI6XYuD+F8zfSQ6TNGJ383xXEQSLIrqtp8V4nPug == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtkedrudeiuddgfeekucetufdoteggodetrfdotf 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 6C1F4100E5; Mon, 17 Dec 2018 01:24:28 -0500 (EST) Date: Mon, 17 Dec 2018 15:24:25 +0900 From: Michael Paquier To: Tom Lane Cc: Amul Sul , pgsql-hackers@lists.postgresql.org, a.korotkov@postgrespro.ru Subject: Re: ALTER INDEX ... ALTER COLUMN not present in dump Message-ID: <20181217062425.GI31474@paquier.xyz> References: <20181122003147.GA3369@paquier.xyz> <154477492593.1386.13640101253353637283.pgcf@coridan.postgresql.org> <20181217051439.GG31474@paquier.xyz> <8776.1545024255@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="mGCtrYeZ202LI9ZG" Content-Disposition: inline In-Reply-To: <8776.1545024255@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --mGCtrYeZ202LI9ZG Content-Type: multipart/mixed; boundary="Xssso5lpTBgMxDfe" Content-Disposition: inline --Xssso5lpTBgMxDfe Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Dec 17, 2018 at 12:24:15AM -0500, Tom Lane wrote: > If we were to rename the "foo.expr" column at this point, > and then dump and reload, the expression column in the > second index would presumably acquire the name "expr" > not "expr1", because "expr" would no longer be taken. > So if pg_dump were to try to use that index column name > in ALTER ... SET STATISTICS, it'd fail. Good point, thanks! I did not think about the case where a table uses an attribute name matching what would be generated for indexes. So this settles the argument that we had better not do anything before v11. Switching the dump code to use column numbers has not proved to be complicated as only the query and some comments had to be tweaked. Attached is an updated patch, and I am switching back the patch to "Needs review" to have an extra pair of eyes look at that in case I missed something. -- Michael --Xssso5lpTBgMxDfe Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="dump-alter-index-stats-v4.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..a46dd4c8e6 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(attnum ORDER BY attnum) " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid =3D i.indexrelid AND " + " attstattarget >=3D 0) AS indstatcols," + "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) " + " 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,31 @@ 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)); + /* + * Note that this is a column number, so no quotes should + * be used. + */ + 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) { @@ -16194,6 +16248,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..1d997fa36f 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 numbers 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..8fd283f601 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 1 SET STATISTICS 1000;', + regexp =3D> qr/^ + \QALTER INDEX dump_test.index_with_stats ALTER COLUMN 1 SET STATISTICS = 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 --Xssso5lpTBgMxDfe-- --mGCtrYeZ202LI9ZG Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlwXQRkACgkQnvQgOdby QH04lQ/9GGGNU/RYOW6IfwKHKxlInRMl4XmraHzPW+UajpBDyea7Y8xpC2ikIpVU FcTvYoRnUvy2Dx644Z+2gYB8r6FIYpaEV0/ovvIg0pOKY5plQn6UlqQwPL3OrjR7 TepXW7otatYLJa7MrCg9bgTE2fkGZHuVDQmgahc7u83SsOTW1P3YN+4qBFQfq1AZ P89mdVeh9BrcuBU6e+o7nrAyc4xnicL9mh11rG/3tGln2pjnbef0e5gv8VD87qT5 dZ+Rgg4TNCuu7AA5J/VaYdAfffFGZ7+mq/8vQfV2ypB+1DgrcxAWAJ2jseRJyEZf +TPa03W6uk5fc7RwvPhlKi6jp9dDxF42fV8dqYdR4/jE7xav8S6WGmNSs0k6WnEk z+TlKcG9epzeSTFWJRnpEcTJvL4LiqvLiRN6FKBWNL5SIoLrxytbrhWVGYW+8jeo qfKYWRc7rUERQatqurm0ljoYowP/BQu2LroB0EZxVC4XYU0IbQFmGlOK9/L80xqy eZK3CiIzm0JN03iD82B+caHBDwDbMdbFkocuYIXUdljWuPjzy57Igy92yvW/dYE7 OuL95pB/+pp7QZu4fRgbtxbS+TI39jtx6t0r/LWDbB6j8PI8GN4TSYrJnMHSH58b 6pbqk9H2DDci3o7j7rZB/xGuarPo2jVDBowR6fN1Hd+T8AgKdMA= =qE5R -----END PGP SIGNATURE----- --mGCtrYeZ202LI9ZG--