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)
48504850int i_indexreloid ;
48514851int i_indexrelname ;
48524852int i_indexdef ;
4853- int i_indisprimary ;
4853+ int i_contype ;
48544854int i_indkey ;
48554855int i_indnkeys ;
48564856
@@ -4872,23 +4872,28 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
48724872if (g_fout -> remoteVersion >=70300 )
48734873appendPQExpBuffer (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" ,
48844886tbinfo -> oid );
48854887else
48864888appendPQExpBuffer (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)
49084913i_indexreloid = PQfnumber (res ,"indexreloid" );
49094914i_indexrelname = PQfnumber (res ,"indexrelname" );
49104915i_indexdef = PQfnumber (res ,"indexdef" );
4911- i_indisprimary = PQfnumber (res ,"indisprimary " );
4916+ i_contype = PQfnumber (res ,"contype " );
49124917i_indkey = PQfnumber (res ,"indkey" );
49134918i_indnkeys = PQfnumber (res ,"indnkeys" );
49144919
@@ -4917,14 +4922,18 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
49174922const char * indexreloid = PQgetvalue (res ,j ,i_indexreloid );
49184923const char * indexrelname = PQgetvalue (res ,j ,i_indexrelname );
49194924const 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
49224927resetPQExpBuffer (q );
49234928resetPQExpBuffer (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+ */
49284937int indnkeys = atoi (PQgetvalue (res ,j ,i_indnkeys ));
49294938char * * indkeys = (char * * )malloc (indnkeys * sizeof (char * ));
49304939int k ;
@@ -4934,8 +4943,9 @@ dumpIndexes(Archive *fout, TableInfo *tblinfo, int numTables)
49344943
49354944appendPQExpBuffer (q ,"ALTER TABLE %s ADD " ,
49364945fmtId (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
49404950for (k = 0 ;k < indnkeys ;k ++ )
49414951{