Skip to content

Commit a9f6c5b

Browse files
committed
Unique and primary key constraints are both dumped using ALTER TABLE
statements. Unique indexes with CREATE INDEX. Basically, pg_constraint left outer'd to pg_index. Rod Taylor
1 parent e6f02c8 commit a9f6c5b

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 27 additions & 17 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.272 2002/07/18 04:43:50 momjian Exp $
25+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.273 2002/07/18 04:50:51 momjian Exp $
2626
*
2727
*-------------------------------------------------------------------------
2828
*/
@@ -4850,7 +4850,7 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
48504850
int i_indexreloid;
48514851
int i_indexrelname;
48524852
int i_indexdef;
4853-
int i_indisprimary;
4853+
int i_contype;
48544854
int i_indkey;
48554855
int i_indnkeys;
48564856

@@ -4872,23 +4872,28 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
48724872
if (g_fout->remoteVersion >= 70300)
48734873
appendPQExpBuffer(query,
48744874
"SELECT i.indexrelid as indexreloid, "
4875-
"t.relname as indexrelname, "
4875+
"coalesce(c.conname, t.relname) as indexrelname, "
48764876
"pg_catalog.pg_get_indexdef(i.indexrelid) as indexdef, "
4877-
"i.indisprimary, i.indkey, "
4878-
"t.relnatts as indnkeys "
4879-
"FROM pg_catalog.pg_index i, "
4880-
"pg_catalog.pg_class t "
4881-
"WHERE t.oid = i.indexrelid "
4882-
"AND i.indrelid = '%s'::pg_catalog.oid "
4877+
"i.indkey, "
4878+
"t.relnatts as indnkeys, "
4879+
"coalesce(c.contype, '0') as contype "
4880+
"FROM pg_catalog.pg_index i "
4881+
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
4882+
"LEFT OUTER JOIN pg_catalog.pg_constraint c "
4883+
" ON (c.conrelid = i.indrelid AND c.conname = t.relname) "
4884+
"WHERE i.indrelid = '%s'::pg_catalog.oid "
48834885
"ORDER BY indexrelname",
48844886
tbinfo->oid);
48854887
else
48864888
appendPQExpBuffer(query,
48874889
"SELECT i.indexrelid as indexreloid, "
48884890
"t.relname as indexrelname, "
48894891
"pg_get_indexdef(i.indexrelid) as indexdef, "
4890-
"i.indisprimary, i.indkey, "
4891-
"t.relnatts as indnkeys "
4892+
"i.indkey, "
4893+
"t.relnatts as indnkeys, "
4894+
"CASE WHEN i.indisprimary IS TRUE THEN "
4895+
" 'p'::char "
4896+
"ELSE '0'::char END as contype "
48924897
"FROM pg_index i, pg_class t "
48934898
"WHERE t.oid = i.indexrelid "
48944899
"AND i.indrelid = '%s'::oid "
@@ -4908,7 +4913,7 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
49084913
i_indexreloid = PQfnumber(res, "indexreloid");
49094914
i_indexrelname = PQfnumber(res, "indexrelname");
49104915
i_indexdef = PQfnumber(res, "indexdef");
4911-
i_indisprimary = PQfnumber(res, "indisprimary");
4916+
i_contype = PQfnumber(res, "contype");
49124917
i_indkey = PQfnumber(res, "indkey");
49134918
i_indnkeys = PQfnumber(res, "indnkeys");
49144919

@@ -4917,14 +4922,18 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
49174922
const char *indexreloid = PQgetvalue(res, j, i_indexreloid);
49184923
const char *indexrelname = PQgetvalue(res, j, i_indexrelname);
49194924
const char *indexdef = PQgetvalue(res, j, i_indexdef);
4920-
const char *indisprimary = PQgetvalue(res, j, i_indisprimary);
4925+
const char *contype = PQgetvalue(res, j, i_contype);
49214926

49224927
resetPQExpBuffer(q);
49234928
resetPQExpBuffer(delq);
49244929

4925-
if (strcmp(indisprimary, "t") == 0)
4930+
if (strcmp(contype, "p") == 0 || strcmp(contype, "u") == 0)
49264931
{
4927-
/* Handle PK indexes specially */
4932+
/*
4933+
* Constraints which are marked as so (created with a
4934+
* constraint creation utility statement, not an index
4935+
* creation statment) should be regenerated as such.
4936+
*/
49284937
int indnkeys = atoi(PQgetvalue(res, j, i_indnkeys));
49294938
char **indkeys = (char **) malloc(indnkeys * sizeof(char *));
49304939
int k;
@@ -4934,8 +4943,9 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
49344943

49354944
appendPQExpBuffer(q, "ALTER TABLE %s ADD ",
49364945
fmtId(tbinfo->relname, force_quotes));
4937-
appendPQExpBuffer(q, "CONSTRAINT %s PRIMARY KEY (",
4938-
fmtId(indexrelname, force_quotes));
4946+
appendPQExpBuffer(q, "CONSTRAINT %s %s (",
4947+
fmtId(indexrelname, force_quotes),
4948+
strcmp(contype, "p") == 0 ? "PRIMARY KEY" : "UNIQUE");
49394949

49404950
for (k = 0; k < indnkeys; k++)
49414951
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy