Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
pg_dump: Refactor getIndexes()
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 20 Dec 2021 10:08:05 +0000 (11:08 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 20 Dec 2021 10:18:01 +0000 (11:18 +0100)
Rearrange the version-dependent pieces in the new more modular style.

Discussion: https://www.postgresql.org/message-id/flat/67a28a3f-7b79-a5a9-fcc7-947b170e66f0%40enterprisedb.com

src/bin/pg_dump/pg_dump.c

index ac291fbef21ac0edb530e76421b5cab1a56af1e3..b52f3ccda251309f3d9defdf3e4f6a8055750e21 100644 (file)
@@ -6508,6 +6508,50 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
    }
    appendPQExpBufferChar(tbloids, '}');
 
+   resetPQExpBuffer(query);
+
+   appendPQExpBuffer(query,
+                     "SELECT t.tableoid, t.oid, i.indrelid, "
+                     "t.relname AS indexname, "
+                     "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
+                     "i.indkey, i.indisclustered, "
+                     "c.contype, c.conname, "
+                     "c.condeferrable, c.condeferred, "
+                     "c.tableoid AS contableoid, "
+                     "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, ");
+
+
+   if (fout->remoteVersion >= 90400)
+       appendPQExpBuffer(query,
+                         "i.indisreplident, ");
+   else
+       appendPQExpBuffer(query,
+                         "false AS indisreplident, ");
+
+   if (fout->remoteVersion >= 110000)
+       appendPQExpBuffer(query,
+                         "inh.inhparent AS parentidx, "
+                         "i.indnkeyatts AS indnkeyatts, "
+                         "i.indnatts AS indnatts, "
+                         "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) "
+                         "  FROM pg_catalog.pg_attribute "
+                         "  WHERE attrelid = i.indexrelid AND "
+                         "    attstattarget >= 0) AS indstatcols, "
+                         "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) "
+                         "  FROM pg_catalog.pg_attribute "
+                         "  WHERE attrelid = i.indexrelid AND "
+                         "    attstattarget >= 0) AS indstatvals ");
+   else
+       appendPQExpBuffer(query,
+                         "0 AS parentidx, "
+                         "i.indnatts AS indnkeyatts, "
+                         "i.indnatts AS indnatts, "
+                         "'' AS indstatcols, "
+                         "'' AS indstatvals ");
+
    /*
     * The point of the messy-looking outer join is to find a constraint that
     * is related by an internal dependency link to the index. If we find one,
@@ -6520,29 +6564,6 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
    if (fout->remoteVersion >= 110000)
    {
        appendPQExpBuffer(query,
-                         "SELECT t.tableoid, t.oid, i.indrelid, "
-                         "t.relname AS indexname, "
-                         "inh.inhparent AS parentidx, "
-                         "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
-                         "i.indnkeyatts AS indnkeyatts, "
-                         "i.indnatts AS indnatts, "
-                         "i.indkey, i.indisclustered, "
-                         "i.indisreplident, "
-                         "c.contype, c.conname, "
-                         "c.condeferrable, c.condeferred, "
-                         "c.tableoid AS contableoid, "
-                         "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, "
-                         "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) "
-                         "  FROM pg_catalog.pg_attribute "
-                         "  WHERE attrelid = i.indexrelid AND "
-                         "    attstattarget >= 0) AS indstatcols,"
-                         "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) "
-                         "  FROM pg_catalog.pg_attribute "
-                         "  WHERE attrelid = i.indexrelid AND "
-                         "    attstattarget >= 0) AS indstatvals "
                          "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
                          "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
                          "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
@@ -6558,41 +6579,6 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
                          "ORDER BY i.indrelid, indexname",
                          tbloids->data);
    }
-   else if (fout->remoteVersion >= 90400)
-   {
-       /*
-        * the test on indisready is necessary in 9.2, and harmless in
-        * earlier/later versions
-        */
-       appendPQExpBuffer(query,
-                         "SELECT t.tableoid, t.oid, i.indrelid, "
-                         "t.relname AS indexname, "
-                         "0 AS parentidx, "
-                         "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
-                         "i.indnatts AS indnkeyatts, "
-                         "i.indnatts AS indnatts, "
-                         "i.indkey, i.indisclustered, "
-                         "i.indisreplident, "
-                         "c.contype, c.conname, "
-                         "c.condeferrable, c.condeferred, "
-                         "c.tableoid AS contableoid, "
-                         "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, "
-                         "'' AS indstatcols, "
-                         "'' AS indstatvals "
-                         "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
-                         "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
-                         "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
-                         "LEFT JOIN pg_catalog.pg_constraint c "
-                         "ON (i.indrelid = c.conrelid AND "
-                         "i.indexrelid = c.conindid AND "
-                         "c.contype IN ('p','u','x')) "
-                         "WHERE i.indisvalid AND i.indisready "
-                         "ORDER BY i.indrelid, indexname",
-                         tbloids->data);
-   }
    else
    {
        /*
@@ -6600,23 +6586,6 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
         * earlier/later versions
         */
        appendPQExpBuffer(query,
-                         "SELECT t.tableoid, t.oid, i.indrelid, "
-                         "t.relname AS indexname, "
-                         "0 AS parentidx, "
-                         "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
-                         "i.indnatts AS indnkeyatts, "
-                         "i.indnatts AS indnatts, "
-                         "i.indkey, i.indisclustered, "
-                         "false AS indisreplident, "
-                         "c.contype, c.conname, "
-                         "c.condeferrable, c.condeferred, "
-                         "c.tableoid AS contableoid, "
-                         "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, "
-                         "'' AS indstatcols, "
-                         "'' AS indstatvals "
                          "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
                          "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) "
                          "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "