mirror of
https://github.com/postgres/postgres.git
synced 2025-06-06 00:02:36 -04:00
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
This commit is contained in:
parent
e6f02c8231
commit
a9f6c5b5c7
@ -22,7 +22,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.272 2002/07/18 04:43:50 momjian Exp $
|
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.273 2002/07/18 04:50:51 momjian Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -4850,7 +4850,7 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
int i_indexreloid;
|
int i_indexreloid;
|
||||||
int i_indexrelname;
|
int i_indexrelname;
|
||||||
int i_indexdef;
|
int i_indexdef;
|
||||||
int i_indisprimary;
|
int i_contype;
|
||||||
int i_indkey;
|
int i_indkey;
|
||||||
int i_indnkeys;
|
int i_indnkeys;
|
||||||
|
|
||||||
@ -4872,14 +4872,16 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
if (g_fout->remoteVersion >= 70300)
|
if (g_fout->remoteVersion >= 70300)
|
||||||
appendPQExpBuffer(query,
|
appendPQExpBuffer(query,
|
||||||
"SELECT i.indexrelid as indexreloid, "
|
"SELECT i.indexrelid as indexreloid, "
|
||||||
"t.relname as indexrelname, "
|
"coalesce(c.conname, t.relname) as indexrelname, "
|
||||||
"pg_catalog.pg_get_indexdef(i.indexrelid) as indexdef, "
|
"pg_catalog.pg_get_indexdef(i.indexrelid) as indexdef, "
|
||||||
"i.indisprimary, i.indkey, "
|
"i.indkey, "
|
||||||
"t.relnatts as indnkeys "
|
"t.relnatts as indnkeys, "
|
||||||
"FROM pg_catalog.pg_index i, "
|
"coalesce(c.contype, '0') as contype "
|
||||||
"pg_catalog.pg_class t "
|
"FROM pg_catalog.pg_index i "
|
||||||
"WHERE t.oid = i.indexrelid "
|
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
|
||||||
"AND i.indrelid = '%s'::pg_catalog.oid "
|
"LEFT OUTER JOIN pg_catalog.pg_constraint c "
|
||||||
|
" ON (c.conrelid = i.indrelid AND c.conname = t.relname) "
|
||||||
|
"WHERE i.indrelid = '%s'::pg_catalog.oid "
|
||||||
"ORDER BY indexrelname",
|
"ORDER BY indexrelname",
|
||||||
tbinfo->oid);
|
tbinfo->oid);
|
||||||
else
|
else
|
||||||
@ -4887,8 +4889,11 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
"SELECT i.indexrelid as indexreloid, "
|
"SELECT i.indexrelid as indexreloid, "
|
||||||
"t.relname as indexrelname, "
|
"t.relname as indexrelname, "
|
||||||
"pg_get_indexdef(i.indexrelid) as indexdef, "
|
"pg_get_indexdef(i.indexrelid) as indexdef, "
|
||||||
"i.indisprimary, i.indkey, "
|
"i.indkey, "
|
||||||
"t.relnatts as indnkeys "
|
"t.relnatts as indnkeys, "
|
||||||
|
"CASE WHEN i.indisprimary IS TRUE THEN "
|
||||||
|
" 'p'::char "
|
||||||
|
"ELSE '0'::char END as contype "
|
||||||
"FROM pg_index i, pg_class t "
|
"FROM pg_index i, pg_class t "
|
||||||
"WHERE t.oid = i.indexrelid "
|
"WHERE t.oid = i.indexrelid "
|
||||||
"AND i.indrelid = '%s'::oid "
|
"AND i.indrelid = '%s'::oid "
|
||||||
@ -4908,7 +4913,7 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
i_indexreloid = PQfnumber(res, "indexreloid");
|
i_indexreloid = PQfnumber(res, "indexreloid");
|
||||||
i_indexrelname = PQfnumber(res, "indexrelname");
|
i_indexrelname = PQfnumber(res, "indexrelname");
|
||||||
i_indexdef = PQfnumber(res, "indexdef");
|
i_indexdef = PQfnumber(res, "indexdef");
|
||||||
i_indisprimary = PQfnumber(res, "indisprimary");
|
i_contype = PQfnumber(res, "contype");
|
||||||
i_indkey = PQfnumber(res, "indkey");
|
i_indkey = PQfnumber(res, "indkey");
|
||||||
i_indnkeys = PQfnumber(res, "indnkeys");
|
i_indnkeys = PQfnumber(res, "indnkeys");
|
||||||
|
|
||||||
@ -4917,14 +4922,18 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
const char *indexreloid = PQgetvalue(res, j, i_indexreloid);
|
const char *indexreloid = PQgetvalue(res, j, i_indexreloid);
|
||||||
const char *indexrelname = PQgetvalue(res, j, i_indexrelname);
|
const char *indexrelname = PQgetvalue(res, j, i_indexrelname);
|
||||||
const char *indexdef = PQgetvalue(res, j, i_indexdef);
|
const char *indexdef = PQgetvalue(res, j, i_indexdef);
|
||||||
const char *indisprimary = PQgetvalue(res, j, i_indisprimary);
|
const char *contype = PQgetvalue(res, j, i_contype);
|
||||||
|
|
||||||
resetPQExpBuffer(q);
|
resetPQExpBuffer(q);
|
||||||
resetPQExpBuffer(delq);
|
resetPQExpBuffer(delq);
|
||||||
|
|
||||||
if (strcmp(indisprimary, "t") == 0)
|
if (strcmp(contype, "p") == 0 || strcmp(contype, "u") == 0)
|
||||||
{
|
{
|
||||||
/* Handle PK indexes specially */
|
/*
|
||||||
|
* Constraints which are marked as so (created with a
|
||||||
|
* constraint creation utility statement, not an index
|
||||||
|
* creation statment) should be regenerated as such.
|
||||||
|
*/
|
||||||
int indnkeys = atoi(PQgetvalue(res, j, i_indnkeys));
|
int indnkeys = atoi(PQgetvalue(res, j, i_indnkeys));
|
||||||
char **indkeys = (char **) malloc(indnkeys * sizeof(char *));
|
char **indkeys = (char **) malloc(indnkeys * sizeof(char *));
|
||||||
int k;
|
int k;
|
||||||
@ -4934,8 +4943,9 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
|
|||||||
|
|
||||||
appendPQExpBuffer(q, "ALTER TABLE %s ADD ",
|
appendPQExpBuffer(q, "ALTER TABLE %s ADD ",
|
||||||
fmtId(tbinfo->relname, force_quotes));
|
fmtId(tbinfo->relname, force_quotes));
|
||||||
appendPQExpBuffer(q, "CONSTRAINT %s PRIMARY KEY (",
|
appendPQExpBuffer(q, "CONSTRAINT %s %s (",
|
||||||
fmtId(indexrelname, force_quotes));
|
fmtId(indexrelname, force_quotes),
|
||||||
|
strcmp(contype, "p") == 0 ? "PRIMARY KEY" : "UNIQUE");
|
||||||
|
|
||||||
for (k = 0; k < indnkeys; k++)
|
for (k = 0; k < indnkeys; k++)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user