Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f11e6d

Browse files
committed
This patch allow pg_dump save name of primary key constraint (if primary
key exist).awn@bcs.zp.ua
1 parent579f8f0 commit6f11e6d

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

‎src/bin/pg_dump/pg_dump.c

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.181 2000/11/24 22:32:26 petere Exp $
25+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.182 2000/11/27 20:51:40 momjian Exp $
2626
*
2727
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2828
*
@@ -1544,6 +1544,8 @@ clearTableInfo(TableInfo *tblinfo, int numTables)
15441544
free(tblinfo[i].notnull);
15451545
if (tblinfo[i].primary_key)
15461546
free(tblinfo[i].primary_key);
1547+
if (tblinfo[i].primary_key_name)
1548+
free(tblinfo[i].primary_key_name);
15471549
}
15481550
free(tblinfo);
15491551
}
@@ -2144,6 +2146,49 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
21442146
else
21452147
tblinfo[i].primary_key=NULL;
21462148

2149+
/* Get primary key name (if primary key exist) */
2150+
if (tblinfo[i].primary_key)
2151+
{
2152+
PGresult*res2;
2153+
intn;
2154+
2155+
resetPQExpBuffer(query);
2156+
appendPQExpBuffer(query,
2157+
"SELECT c.relname "
2158+
"FROM pg_index i, pg_class c "
2159+
"WHERE i.indrelid = %s"
2160+
"AND i.indisprimary "
2161+
"AND c.oid = i.indexrelid ",
2162+
tblinfo[i].oid);
2163+
res2=PQexec(g_conn,query->data);
2164+
if (!res2||PQresultStatus(res2)!=PGRES_TUPLES_OK)
2165+
{
2166+
fprintf(stderr,"getTables(): SELECT (for PRIMARY KEY NAME) failed. Explanation from backend: %s",
2167+
PQerrorMessage(g_conn));
2168+
exit_nicely(g_conn);
2169+
}
2170+
2171+
n=PQntuples(res2);
2172+
if (n!=1)
2173+
{
2174+
fprintf(stderr,
2175+
"getTables(): SELECT (for PRIMARY KEY NAME) failed. This is impossible but object with OID == %s have %d primary keys.\n",
2176+
tblinfo[i].oid,
2177+
n);
2178+
exit_nicely(g_conn);
2179+
}
2180+
2181+
tblinfo[i].primary_key_name=
2182+
strdup(fmtId(PQgetvalue(res2,0,0),force_quotes));
2183+
if (tblinfo[i].primary_key_name==NULL)
2184+
{
2185+
perror("strdup");
2186+
exit(1);
2187+
}
2188+
}
2189+
else
2190+
tblinfo[i].primary_key_name=NULL;
2191+
21472192
/* Get Triggers */
21482193
if (tblinfo[i].ntrig>0)
21492194
{
@@ -3558,7 +3603,10 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
35583603
{
35593604
if (actual_atts+tblinfo[i].ncheck>0)
35603605
appendPQExpBuffer(q,",\n\t");
3561-
appendPQExpBuffer(q,"PRIMARY KEY (%s)",tblinfo[i].primary_key);
3606+
appendPQExpBuffer(q,
3607+
"CONSTRAINT %s PRIMARY KEY (%s)",
3608+
tblinfo[i].primary_key_name,
3609+
tblinfo[i].primary_key);
35623610
}
35633611

35643612
appendPQExpBuffer(q,"\n)");

‎src/bin/pg_dump/pg_dump.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_dump.h,v 1.54 2000/11/14 18:37:46 tgl Exp $
9+
* $Id: pg_dump.h,v 1.55 2000/11/27 20:51:40 momjian Exp $
1010
*
1111
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
1212
*
@@ -117,6 +117,7 @@ typedef struct _tableInfo
117117
intntrig;/* # of triggers */
118118
TrigInfo*triggers;/* Triggers on the table */
119119
char*primary_key;/* PRIMARY KEY of the table, if any */
120+
char*primary_key_name;/* PRIMARY KEY name, if any */
120121
}TableInfo;
121122

122123
typedefstruct_inhInfo

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp