Commit 8dabef83 authored by Tom Lane's avatar Tom Lane

Code review for patch to dump primary and unique constraints as

constraints, rather than as CREATE INDEX commands.
parent ad7d3bdd
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.282 2002/08/15 16:36:06 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.283 2002/08/16 21:03:42 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -5286,7 +5286,7 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo) ...@@ -5286,7 +5286,7 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
if (tbinfo->attstattarget[j] >= 0 && if (tbinfo->attstattarget[j] >= 0 &&
!tbinfo->attisdropped[j]) !tbinfo->attisdropped[j])
{ {
appendPQExpBuffer(q, "ALTER TABLE %s ", appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
fmtId(tbinfo->relname, force_quotes)); fmtId(tbinfo->relname, force_quotes));
appendPQExpBuffer(q, "ALTER COLUMN %s ", appendPQExpBuffer(q, "ALTER COLUMN %s ",
fmtId(tbinfo->attnames[j], force_quotes)); fmtId(tbinfo->attnames[j], force_quotes));
...@@ -5388,6 +5388,13 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -5388,6 +5388,13 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
/* Make sure we are in proper schema so indexdef is right */ /* Make sure we are in proper schema so indexdef is right */
selectSourceSchema(tbinfo->relnamespace->nspname); selectSourceSchema(tbinfo->relnamespace->nspname);
/*
* The point of the messy-looking outer join is to find a constraint
* that is related by an internal dependency link to the index.
* If we find one, we emit an ADD CONSTRAINT command instead of
* a CREATE INDEX command. We assume an index won't have more than
* one internal dependency.
*/
resetPQExpBuffer(query); resetPQExpBuffer(query);
if (g_fout->remoteVersion >= 70300) if (g_fout->remoteVersion >= 70300)
appendPQExpBuffer(query, appendPQExpBuffer(query,
...@@ -5399,8 +5406,13 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -5399,8 +5406,13 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
"coalesce(c.contype, '0') as contype " "coalesce(c.contype, '0') as contype "
"FROM pg_catalog.pg_index i " "FROM pg_catalog.pg_index i "
"JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
"LEFT OUTER JOIN pg_catalog.pg_constraint c " "LEFT JOIN pg_catalog.pg_depend d "
" ON (c.conrelid = i.indrelid AND c.conname = t.relname) " "ON (d.classid = t.tableoid "
"AND d.objid = t.oid "
"AND d.deptype = 'i') "
"LEFT JOIN pg_catalog.pg_constraint c "
"ON (d.refclassid = c.tableoid "
"AND d.refobjid = c.oid) "
"WHERE i.indrelid = '%s'::pg_catalog.oid " "WHERE i.indrelid = '%s'::pg_catalog.oid "
"ORDER BY indexrelname", "ORDER BY indexrelname",
tbinfo->oid); tbinfo->oid);
...@@ -5411,8 +5423,7 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -5411,8 +5423,7 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
"pg_get_indexdef(i.indexrelid) as indexdef, " "pg_get_indexdef(i.indexrelid) as indexdef, "
"i.indkey, " "i.indkey, "
"t.relnatts as indnkeys, " "t.relnatts as indnkeys, "
"CASE WHEN i.indisprimary IS TRUE THEN " "CASE WHEN i.indisprimary THEN 'p'::char "
" 'p'::char "
"ELSE '0'::char END as contype " "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 "
...@@ -5442,17 +5453,19 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -5442,17 +5453,19 @@ 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 *contype = PQgetvalue(res, j, i_contype); char contype = *(PQgetvalue(res, j, i_contype));
resetPQExpBuffer(q); resetPQExpBuffer(q);
resetPQExpBuffer(delq); resetPQExpBuffer(delq);
if (strcmp(contype, "p") == 0 || strcmp(contype, "u") == 0) if (contype == 'p' || contype == 'u')
{ {
/* /*
* Constraints which are marked as so (created with a * If we found a constraint matching the index, emit
* constraint creation utility statement, not an index * ADD CONSTRAINT not CREATE INDEX.
* creation statment) should be regenerated as such. *
* In a pre-7.3 database, we take this path iff the index
* was marked indisprimary.
*/ */
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 *));
...@@ -5461,11 +5474,11 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -5461,11 +5474,11 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
parseNumericArray(PQgetvalue(res, j, i_indkey), parseNumericArray(PQgetvalue(res, j, i_indkey),
indkeys, indnkeys); indkeys, indnkeys);
appendPQExpBuffer(q, "ALTER TABLE %s ADD ", appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
fmtId(tbinfo->relname, force_quotes)); fmtId(tbinfo->relname, force_quotes));
appendPQExpBuffer(q, "CONSTRAINT %s %s (", appendPQExpBuffer(q, "ADD CONSTRAINT %s %s (",
fmtId(indexrelname, force_quotes), fmtId(indexrelname, force_quotes),
strcmp(contype, "p") == 0 ? "PRIMARY KEY" : "UNIQUE"); contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
for (k = 0; k < indnkeys; k++) for (k = 0; k < indnkeys; k++)
{ {
...@@ -5483,12 +5496,20 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables) ...@@ -5483,12 +5496,20 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
appendPQExpBuffer(q, ");\n"); appendPQExpBuffer(q, ");\n");
/* DROP must be fully qualified in case same name appears in pg_catalog */
appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
fmtId(tbinfo->relnamespace->nspname, force_quotes));
appendPQExpBuffer(delq, "%s ",
fmtId(tbinfo->relname, force_quotes));
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
fmtId(indexrelname, force_quotes));
ArchiveEntry(fout, indexreloid, ArchiveEntry(fout, indexreloid,
indexrelname, indexrelname,
tbinfo->relnamespace->nspname, tbinfo->relnamespace->nspname,
tbinfo->usename, tbinfo->usename,
"CONSTRAINT", NULL, "CONSTRAINT", NULL,
q->data, "", q->data, delq->data,
NULL, NULL, NULL); NULL, NULL, NULL);
for (k = 0; k < indnkeys; k++) for (k = 0; k < indnkeys; k++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment