2121 *
2222 *
2323 * IDENTIFICATION
24- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.124 1999/11/22 17:56:36 momjian Exp $
24+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.125 1999/12/11 00:31:05 momjian Exp $
2525 *
2626 * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2727 *
@@ -299,14 +299,13 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids)
299299
300300
301301static void
302- dumpClasses_dumpData (FILE * fout ,const char * classname ,
303- const TableInfo tblinfo ,bool oids )
302+ dumpClasses_dumpData (FILE * fout ,const char * classname )
304303{
305304PGresult * res ;
306305char q [MAX_QUERY_SIZE ];
307306int tuple ;
308307int field ;
309- char * expsrc ;
308+ const char * expsrc ;
310309
311310sprintf (q ,"SELECT * FROM %s" ,fmtId (classname ,force_quotes ));
312311res = PQexec (g_conn ,q );
@@ -456,7 +455,7 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, FILE *fout,
456455if (!dumpData )
457456dumpClasses_nodumpData (fout ,classname ,oids );
458457else
459- dumpClasses_dumpData (fout ,classname , tblinfo [ i ], oids );
458+ dumpClasses_dumpData (fout ,classname );
460459}
461460}
462461}
@@ -1082,7 +1081,8 @@ clearTableInfo(TableInfo *tblinfo, int numTables)
10821081free (tblinfo [i ].typnames );
10831082if (tblinfo [i ].notnull )
10841083free (tblinfo [i ].notnull );
1085-
1084+ if (tblinfo [i ].primary_key )
1085+ free (tblinfo [i ].primary_key );
10861086}
10871087free (tblinfo );
10881088}
@@ -1408,6 +1408,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
14081408int i_usename ;
14091409int i_relchecks ;
14101410int i_reltriggers ;
1411+ int i_relhasindex ;
14111412
14121413/*
14131414 * find all the user-defined tables (no indices and no catalogs),
@@ -1421,7 +1422,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
14211422
14221423sprintf (query ,
14231424"SELECT pg_class.oid, relname, relkind, relacl, usename, "
1424- "relchecks, reltriggers "
1425+ "relchecks, reltriggers, relhasindex "
14251426"from pg_class, pg_user "
14261427"where relowner = usesysid and "
14271428"(relkind = 'r' or relkind = 'S') and relname !~ '^pg_' "
@@ -1448,6 +1449,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
14481449i_usename = PQfnumber (res ,"usename" );
14491450i_relchecks = PQfnumber (res ,"relchecks" );
14501451i_reltriggers = PQfnumber (res ,"reltriggers" );
1452+ i_relhasindex = PQfnumber (res ,"relhasindex" );
14511453
14521454for (i = 0 ;i < ntups ;i ++ )
14531455{
@@ -1547,8 +1549,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
15471549tblinfo [i ].check_expr = (char * * )malloc (ntups2 * sizeof (char * ));
15481550for (i2 = 0 ;i2 < ntups2 ;i2 ++ )
15491551{
1550- char * name = PQgetvalue (res2 ,i2 ,i_rcname );
1551- char * expr = PQgetvalue (res2 ,i2 ,i_rcsrc );
1552+ const char * name = PQgetvalue (res2 ,i2 ,i_rcname );
1553+ const char * expr = PQgetvalue (res2 ,i2 ,i_rcsrc );
15521554
15531555query [0 ]= '\0' ;
15541556if (name [0 ]!= '$' )
@@ -1561,6 +1563,48 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
15611563else
15621564tblinfo [i ].check_expr = NULL ;
15631565
1566+ /* Get primary key */
1567+ if (strcmp (PQgetvalue (res ,i ,i_relhasindex ),"t" )== 0 )
1568+ {
1569+ PGresult * res2 ;
1570+ char str [INDEX_MAX_KEYS * NAMEDATALEN + 3 ]= "" ;
1571+ int j ;
1572+
1573+ sprintf (query ,
1574+ "SELECT a.attname "
1575+ "FROM pg_index i, pg_class c, pg_attribute a "
1576+ "WHERE i.indisprimary AND i.indrelid = %s "
1577+ " AND i.indexrelid = c.oid AND a.attnum > 0 AND a.attrelid = c.oid "
1578+ "ORDER BY a.attnum " ,
1579+ tblinfo [i ].oid );
1580+ res2 = PQexec (g_conn ,query );
1581+ if (!res2 || PQresultStatus (res2 )!= PGRES_TUPLES_OK )
1582+ {
1583+ fprintf (stderr ,"getTables(): SELECT (for PRIMARY KEY) failed. Explanation from backend: %s" ,
1584+ PQerrorMessage (g_conn ));
1585+ exit_nicely (g_conn );
1586+ }
1587+
1588+ for (j = 0 ;j < PQntuples (res2 );j ++ )
1589+ {
1590+ if (strlen (str )> 0 )
1591+ strcat (str ,", " );
1592+ strcat (str ,fmtId (PQgetvalue (res2 ,j ,0 ),force_quotes ));
1593+ }
1594+
1595+ if (strlen (str )> 0 ) {
1596+ tblinfo [i ].primary_key = strdup (str );
1597+ if (tblinfo [i ].primary_key == NULL ) {
1598+ perror ("strdup" );
1599+ exit (1 );
1600+ }
1601+ }
1602+ else
1603+ tblinfo [i ].primary_key = NULL ;
1604+ }
1605+ else
1606+ tblinfo [i ].primary_key = NULL ;
1607+
15641608/* Get Triggers */
15651609if (tblinfo [i ].ntrig > 0 )
15661610{
@@ -1605,11 +1649,11 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16051649tblinfo [i ].triggers = (char * * )malloc (ntups2 * sizeof (char * ));
16061650for (i2 = 0 ,query [0 ]= 0 ;i2 < ntups2 ;i2 ++ )
16071651{
1608- char * tgfunc = PQgetvalue (res2 ,i2 ,i_tgfoid );
1652+ const char * tgfunc = PQgetvalue (res2 ,i2 ,i_tgfoid );
16091653int2 tgtype = atoi (PQgetvalue (res2 ,i2 ,i_tgtype ));
16101654int tgnargs = atoi (PQgetvalue (res2 ,i2 ,i_tgnargs ));
1611- char * tgargs = PQgetvalue (res2 ,i2 ,i_tgargs );
1612- char * p ;
1655+ const char * tgargs = PQgetvalue (res2 ,i2 ,i_tgargs );
1656+ const char * p ;
16131657char farg [MAX_QUERY_SIZE ];
16141658int findx ;
16151659
@@ -1670,8 +1714,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16701714query ,fmtId (tblinfo [i ].relname ,force_quotes ),tgfunc );
16711715for (findx = 0 ;findx < tgnargs ;findx ++ )
16721716{
1673- char * s ,
1674- * d ;
1717+ const char * s ;
1718+ char * d ;
16751719
16761720for (p = tgargs ;;)
16771721{
@@ -1921,13 +1965,13 @@ getIndices(int *numIndices)
19211965 */
19221966
19231967sprintf (query ,
1924- "SELECT t1.relname as indexrelname, t2.relname as indrelname, "
1968+ "SELECT t1.relname as indexrelname, t2.relname as indrelname, "
19251969"i.indproc, i.indkey, i.indclass, "
19261970"a.amname as indamname, i.indisunique "
19271971"from pg_index i, pg_class t1, pg_class t2, pg_am a "
1928- "where t1.oid = i.indexrelid and t2.oid = i.indrelid "
1972+ "WHERE t1.oid = i.indexrelid and t2.oid = i.indrelid "
19291973"and t1.relam = a.oid and i.indexrelid > '%u'::oid "
1930- "and t2.relname !~ '^pg_' and t2.relkind != 'l'" ,
1974+ "and t2.relname !~ '^pg_' and t2.relkind != 'l' and not i.indisprimary " ,
19311975g_last_builtin_oid );
19321976
19331977res = PQexec (g_conn ,query );
@@ -2066,7 +2110,7 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
20662110int i_lancompiler ;
20672111char * lanname ;
20682112char * lancompiler ;
2069- char * lanplcallfoid ;
2113+ const char * lanplcallfoid ;
20702114int i ,
20712115fidx ;
20722116
@@ -2747,7 +2791,14 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
27472791tblinfo [i ].check_expr [k ]);
27482792}
27492793
2750- strcat (q ,")" );
2794+ /* PRIMARY KEY */
2795+ if (tblinfo [i ].primary_key ) {
2796+ if (actual_atts + tblinfo [i ].ncheck > 0 )
2797+ strcat (q ,",\n\t" );
2798+ sprintf (q + strlen (q ),"PRIMARY KEY (%s)" ,tblinfo [i ].primary_key );
2799+ }
2800+
2801+ strcat (q ,"\n)" );
27512802
27522803if (numParents > 0 )
27532804{
@@ -3134,8 +3185,8 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
31343185minv ,
31353186cache ;
31363187char cycled ,
3137- called ,
3138- * t ;
3188+ called ;
3189+ const char * t ;
31393190char query [MAX_QUERY_SIZE ];
31403191
31413192sprintf (query ,