pg_dump.save_pkey_name.patch - Mailing list pgsql-patches

From awn@bcs.zp.ua
Subject pg_dump.save_pkey_name.patch
Date
Msg-id 20001127034621.A6643@bcs.zp.ua
Whole thread Raw
Responses Re: pg_dump.save_pkey_name.patch
List pgsql-patches
This patch allow pg_dump save name of primary key constraint (if primary
key exist).

Patch was created around PostgreSQL v7.0.3 sources.

--- src/bin/pg_dump/pg_dump.c.O    Sun Nov  5 06:03:44 2000
+++ src/bin/pg_dump/pg_dump.c    Mon Nov 27 03:23:17 2000
@@ -1216,6 +1216,8 @@ clearTableInfo(TableInfo *tblinfo, int n
             free(tblinfo[i].notnull);
         if (tblinfo[i].primary_key)
             free(tblinfo[i].primary_key);
+        if (tblinfo[i].primary_key_name)
+            free(tblinfo[i].primary_key_name);
     }
     free(tblinfo);
 }
@@ -1760,6 +1762,49 @@ getTables(int *numTables, FuncInfo *finf
         else
             tblinfo[i].primary_key = NULL;

+        /* Get primary key name (if primary key exist) */
+        if (tblinfo[i].primary_key)
+        {
+            PGresult   *res2;
+            int           n;
+
+            resetPQExpBuffer(query);
+            appendPQExpBuffer(query,
+                              "SELECT c.relname "
+                              "FROM pg_index i, pg_class c "
+                              "WHERE i.indrelid = %s"
+                              "AND   i.indisprimary "
+                              "AND   c.oid = i.indexrelid ",
+                              tblinfo[i].oid);
+            res2 = PQexec(g_conn, query->data);
+            if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK)
+            {
+                fprintf(stderr, "getTables(): SELECT (for PRIMARY KEY NAME) failed.  Explanation from backend: %s",
+                        PQerrorMessage(g_conn));
+                exit_nicely(g_conn);
+            }
+
+            n = PQntuples(res2);
+            if (n != 1)
+            {
+                fprintf(stderr,
+                        "getTables(): SELECT (for PRIMARY KEY NAME) failed. This is impossible but object with OID ==
%shave %d primary keys.\n", 
+                        tblinfo[i].oid,
+                        n);
+                exit_nicely(g_conn);
+            }
+
+            tblinfo[i].primary_key_name =
+                strdup(fmtId(PQgetvalue(res2, 0, 0), force_quotes));
+            if (tblinfo[i].primary_key_name == NULL)
+            {
+                perror("strdup");
+                exit(1);
+            }
+        }
+        else
+            tblinfo[i].primary_key_name = NULL;
+
         /* Get Triggers */
         if (tblinfo[i].ntrig > 0)
         {
@@ -3206,7 +3251,10 @@ dumpTables(FILE *fout, TableInfo *tblinf
             {
                 if (actual_atts + tblinfo[i].ncheck > 0)
                     appendPQExpBuffer(q, ",\n\t");
-                appendPQExpBuffer(q, "PRIMARY KEY (%s)", tblinfo[i].primary_key);
+                appendPQExpBuffer(q,
+                                  "CONSTRAINT %s PRIMARY KEY (%s)",
+                                  tblinfo[i].primary_key_name,
+                                  tblinfo[i].primary_key);
             }

             appendPQExpBuffer(q, "\n)");
--- src/bin/pg_dump/pg_dump.h.O    Sun Nov  5 06:03:44 2000
+++ src/bin/pg_dump/pg_dump.h    Mon Nov 27 03:23:16 2000
@@ -99,6 +99,7 @@ typedef struct _tableInfo
     char      **trcomments;        /* COMMENT ON TRIGGER ... */
     char      **troids;            /* TRIGGER oids */
     char       *primary_key;    /* PRIMARY KEY of the table, if any */
+    char       *primary_key_name;    /* PRIMARY KEY name, if any */
 } TableInfo;

 typedef struct _inhInfo

--
Andrew W. Nosenko    (awn@bcs.zp.ua)

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: 7.1: to_char(): Y,YY,YYY,YYYY fix
Next
From: Bruce Momjian
Date:
Subject: Re: pg_dump.save_pkey_name.patch