Recent patch to dump nondefault attstorage settings broke pg_dump for
authorTom Lane
Fri, 25 Jul 2003 21:02:52 +0000 (21:02 +0000)
committerTom Lane
Fri, 25 Jul 2003 21:02:52 +0000 (21:02 +0000)
dropped columns.  Fix by using LEFT JOIN rather than straight join
between pg_attribute and pg_type.  Also, use pg_type.oid as input to
format_type, so that we don't get a failure on deleted types of deleted
columns (this may be a change we ought to backpatch to 7.3....).

src/bin/pg_dump/pg_dump.c

index a695485b5c9edb09f70353e0c9c9648dc0e8eeda..66b10c2aa77c60df051d2d23fa363a36938736c3 100644 (file)
@@ -12,7 +12,7 @@
  * by PostgreSQL
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.337 2003/07/25 19:37:21 momjian Exp $
+ *   $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.338 2003/07/25 21:02:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2371,6 +2371,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
                j,
                k;
    PQExpBuffer q = createPQExpBuffer();
+   int         i_attnum;
    int         i_attname;
    int         i_atttypname;
    int         i_atttypmod;
@@ -2421,12 +2422,13 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
 
        if (g_fout->remoteVersion >= 70300)
        {
+           /* need left join here to not fail on dropped columns ... */
            appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, a.attstattarget, a.attstorage, t.typstorage, "
-                     "a.attnotnull, a.atthasdef, a.attisdropped, a.attislocal, "
-             "pg_catalog.format_type(a.atttypid,a.atttypmod) as atttypname "
-                             "from pg_catalog.pg_attribute a, pg_catalog.pg_type t "
-                             "where a.atttypid = t.oid "
-                             "and a.attrelid = '%s'::pg_catalog.oid "
+                             "a.attnotnull, a.atthasdef, a.attisdropped, a.attislocal, "
+                             "pg_catalog.format_type(t.oid,a.atttypmod) as atttypname "
+                             "from pg_catalog.pg_attribute a left join pg_catalog.pg_type t "
+                             "on a.atttypid = t.oid "
+                             "where a.attrelid = '%s'::pg_catalog.oid "
                              "and a.attnum > 0::pg_catalog.int2 "
                              "order by a.attrelid, a.attnum",
                              tbinfo->oid);
@@ -2439,11 +2441,11 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
             * been explicitly set or was just a default.
             */
            appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, -1 as attstattarget, a.attstorage, t.typstorage, "
-                        "a.attnotnull, a.atthasdef, false as attisdropped, null as attislocal, "
-                        "format_type(a.atttypid,a.atttypmod) as atttypname "
-                             "from pg_attribute a, pg_type t "
-                             "where a.atttypid = t.oid "
-                             "and a.attrelid = '%s'::oid "
+                             "a.attnotnull, a.atthasdef, false as attisdropped, null as attislocal, "
+                             "format_type(t.oid,a.atttypmod) as atttypname "
+                             "from pg_attribute a left join pg_type t "
+                             "on a.atttypid = t.oid "
+                             "where a.attrelid = '%s'::oid "
                              "and a.attnum > 0::int2 "
                              "order by a.attrelid, a.attnum",
                              tbinfo->oid);
@@ -2451,7 +2453,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
        else
        {
            /* format_type not available before 7.1 */
-           appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, -1 as attstattarget, attstorage, 'p' as typstorage, "
+           appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, -1 as attstattarget, attstorage, attstorage as typstorage, "
                         "attnotnull, atthasdef, false as attisdropped, null as attislocal, "
                              "(select typname from pg_type where oid = atttypid) as atttypname "
                              "from pg_attribute a "
@@ -2471,6 +2473,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
 
        ntups = PQntuples(res);
 
+       i_attnum = PQfnumber(res, "attnum");
        i_attname = PQfnumber(res, "attname");
        i_atttypname = PQfnumber(res, "atttypname");
        i_atttypmod = PQfnumber(res, "atttypmod");
@@ -2501,6 +2504,12 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
 
        for (j = 0; j < ntups; j++)
        {
+           if (j+1 != atoi(PQgetvalue(res, j, i_attnum)))
+           {
+               write_msg(NULL, "invalid attribute numbering in table \"%s\"\n",
+                         tbinfo->relname);
+                   exit_nicely();
+           }
            tbinfo->attnames[j] = strdup(PQgetvalue(res, j, i_attname));
            tbinfo->atttypnames[j] = strdup(PQgetvalue(res, j, i_atttypname));
            tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
@@ -5410,7 +5419,7 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
 
            /*
             * Dump per-column storage information.  The statement is only dumped if
-            * the storage has been changed.
+            * the storage has been changed from the type's default.
             */
            if(!tbinfo->attisdropped[j] && tbinfo->attstorage[j] != tbinfo->typstorage[j])
            {