From 6f11e6dffae8413121f78fac7db41dc9a98e1295 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Mon, 27 Nov 2000 20:51:40 +0000
Subject: [PATCH] This patch allow pg_dump save name of primary key constraint
 (if primary key exist).

awn@bcs.zp.ua
---
 src/bin/pg_dump/pg_dump.c | 52 +++++++++++++++++++++++++++++++++++++--
 src/bin/pg_dump/pg_dump.h |  3 ++-
 2 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 7c55378dee9..f0d6cd2e08a 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -22,7 +22,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.181 2000/11/24 22:32:26 petere Exp $
+ *	  $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.182 2000/11/27 20:51:40 momjian Exp $
  *
  * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
  *
@@ -1544,6 +1544,8 @@ clearTableInfo(TableInfo *tblinfo, int numTables)
 			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);
 }
@@ -2144,6 +2146,49 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
 		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 == %s have %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)
 		{
@@ -3558,7 +3603,10 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
 				{
 					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)");
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 19bfa85c595..1871ed3e2f6 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_dump.h,v 1.54 2000/11/14 18:37:46 tgl Exp $
+ * $Id: pg_dump.h,v 1.55 2000/11/27 20:51:40 momjian Exp $
  *
  * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
  *
@@ -117,6 +117,7 @@ typedef struct _tableInfo
 	int			ntrig;			/* # of triggers */
 	TrigInfo	*triggers;		/* Triggers on the table */
 	char	   *primary_key;	/* PRIMARY KEY of the table, if any */
+	char	   *primary_key_name;	/* PRIMARY KEY name, if any */
 } TableInfo;
 
 typedef struct _inhInfo
-- 
GitLab