Skip to content

Commit e639a1a

Browse files
committed
This takes care of TODO item
* pg_dump should preserve primary key information Also a couple of warnings removed. -- Peter Eisentraut Sernanders väg 10:115
1 parent 3c75d64 commit e639a1a

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
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

301301
static void
302-
dumpClasses_dumpData(FILE *fout, const char *classname,
303-
const TableInfo tblinfo, bool oids)
302+
dumpClasses_dumpData(FILE *fout, const char *classname)
304303
{
305304
PGresult *res;
306305
char q[MAX_QUERY_SIZE];
307306
int tuple;
308307
int field;
309-
char *expsrc;
308+
const char *expsrc;
310309

311310
sprintf(q, "SELECT * FROM %s", fmtId(classname, force_quotes));
312311
res = PQexec(g_conn, q);
@@ -456,7 +455,7 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, FILE *fout,
456455
if (!dumpData)
457456
dumpClasses_nodumpData(fout, classname, oids);
458457
else
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)
10821081
free(tblinfo[i].typnames);
10831082
if (tblinfo[i].notnull)
10841083
free(tblinfo[i].notnull);
1085-
1084+
if (tblinfo[i].primary_key)
1085+
free(tblinfo[i].primary_key);
10861086
}
10871087
free(tblinfo);
10881088
}
@@ -1408,6 +1408,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
14081408
int i_usename;
14091409
int i_relchecks;
14101410
int 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

14221423
sprintf(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)
14481449
i_usename = PQfnumber(res, "usename");
14491450
i_relchecks = PQfnumber(res, "relchecks");
14501451
i_reltriggers = PQfnumber(res, "reltriggers");
1452+
i_relhasindex = PQfnumber(res, "relhasindex");
14511453

14521454
for (i = 0; i < ntups; i++)
14531455
{
@@ -1547,8 +1549,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
15471549
tblinfo[i].check_expr = (char **) malloc(ntups2 * sizeof(char *));
15481550
for (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

15531555
query[0] = '\0';
15541556
if (name[0] != '$')
@@ -1561,6 +1563,48 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
15611563
else
15621564
tblinfo[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 */
15651609
if (tblinfo[i].ntrig > 0)
15661610
{
@@ -1605,11 +1649,11 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16051649
tblinfo[i].triggers = (char **) malloc(ntups2 * sizeof(char *));
16061650
for (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);
16091653
int2 tgtype = atoi(PQgetvalue(res2, i2, i_tgtype));
16101654
int 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;
16131657
char farg[MAX_QUERY_SIZE];
16141658
int findx;
16151659

@@ -1670,8 +1714,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16701714
query, fmtId(tblinfo[i].relname, force_quotes), tgfunc);
16711715
for (findx = 0; findx < tgnargs; findx++)
16721716
{
1673-
char *s,
1674-
*d;
1717+
const char *s;
1718+
char *d;
16751719

16761720
for (p = tgargs;;)
16771721
{
@@ -1921,13 +1965,13 @@ getIndices(int *numIndices)
19211965
*/
19221966

19231967
sprintf(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",
19311975
g_last_builtin_oid);
19321976

19331977
res = PQexec(g_conn, query);
@@ -2066,7 +2110,7 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
20662110
int i_lancompiler;
20672111
char *lanname;
20682112
char *lancompiler;
2069-
char *lanplcallfoid;
2113+
const char *lanplcallfoid;
20702114
int i,
20712115
fidx;
20722116

@@ -2747,7 +2791,14 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
27472791
tblinfo[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

27522803
if (numParents > 0)
27532804
{
@@ -3134,8 +3185,8 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
31343185
minv,
31353186
cache;
31363187
char cycled,
3137-
called,
3138-
*t;
3188+
called;
3189+
const char *t;
31393190
char query[MAX_QUERY_SIZE];
31403191

31413192
sprintf(query,

src/bin/pg_dump/pg_dump.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: pg_dump.h,v 1.41 1999/11/22 17:56:36 momjian Exp $
8+
* $Id: pg_dump.h,v 1.42 1999/12/11 00:31:05 momjian Exp $
99
*
1010
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
1111
*
@@ -101,6 +101,7 @@ typedef struct _tableInfo
101101
char **check_expr; /* [CONSTRAINT name] CHECK expressions */
102102
int ntrig; /* # of triggers */
103103
char **triggers; /* CREATE TRIGGER ... */
104+
char *primary_key; /* PRIMARY KEY of the table, if any */
104105
} TableInfo;
105106

106107
typedef struct _inhInfo

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