Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 84a3634

Browse files
committed
Avoid assuming that pg_index table entries have unique OIDs, or even
that they have OIDs at all (the primary key for this table is indexrelid, not OID). Simplify overly complex query to get name of primary key.
1 parent ed5c4e4 commit 84a3634

File tree

2 files changed

+42
-64
lines changed

2 files changed

+42
-64
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.214 2001/07/16 05:06:59 tgl Exp $
25+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.215 2001/07/17 00:30:35 tgl Exp $
2626
*
2727
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2828
*
@@ -1744,8 +1744,10 @@ clearIndInfo(IndInfo *ind, int numIndexes)
17441744
return;
17451745
for (i = 0; i < numIndexes; ++i)
17461746
{
1747-
if (ind[i].indoid)
1748-
free(ind[i].indoid);
1747+
if (ind[i].indexreloid)
1748+
free(ind[i].indexreloid);
1749+
if (ind[i].indreloid)
1750+
free(ind[i].indreloid);
17491751
if (ind[i].indexrelname)
17501752
free(ind[i].indexrelname);
17511753
if (ind[i].indrelname)
@@ -1758,8 +1760,8 @@ clearIndInfo(IndInfo *ind, int numIndexes)
17581760
free(ind[i].indisunique);
17591761
if (ind[i].indisprimary)
17601762
free(ind[i].indisprimary);
1761-
if (ind[i].indpred)
1762-
free(ind[i].indpred);
1763+
if (ind[i].indhaspred)
1764+
free(ind[i].indhaspred);
17631765
for (a = 0; a < INDEX_MAX_KEYS; ++a)
17641766
{
17651767
if (ind[i].indkey[a])
@@ -2264,7 +2266,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
22642266

22652267
resetPQExpBuffer(query);
22662268
appendPQExpBuffer(query,
2267-
"SELECT Oid FROM pg_index i WHERE i.indisprimary AND i.indrelid = %s ",
2269+
"SELECT indexrelid FROM pg_index i WHERE i.indisprimary AND i.indrelid = %s ",
22682270
tblinfo[i].oid);
22692271
res2 = PQexec(g_conn, query->data);
22702272
if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK)
@@ -2297,31 +2299,10 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
22972299
int n;
22982300

22992301
resetPQExpBuffer(query);
2300-
if (g_fout->remoteVersion < 70100)
2301-
{
2302-
/* Fake the LOJ from below */
2303-
appendPQExpBuffer(query,
2304-
" SELECT c.relname "
2305-
" FROM pg_index i, pg_class c "
2306-
" WHERE i.indrelid = %s"
2307-
" AND i.indisprimary "
2308-
" AND c.oid = i.indexrelid"
2309-
" UNION ALL "
2310-
" SELECT NULL "
2311-
" FROM pg_index i "
2312-
" WHERE i.indrelid = %s"
2313-
" AND i.indisprimary "
2314-
" And NOT Exists(Select * From pg_class c Where c.oid = i.indexrelid)",
2315-
tblinfo[i].oid, tblinfo[i].oid);
2316-
2317-
} else {
2318-
appendPQExpBuffer(query,
2319-
"SELECT c.relname "
2320-
"FROM pg_index i LEFT OUTER JOIN pg_class c ON c.oid = i.indexrelid "
2321-
"WHERE i.indrelid = %s"
2322-
"AND i.indisprimary ",
2323-
tblinfo[i].oid);
2324-
}
2302+
appendPQExpBuffer(query,
2303+
"SELECT relname FROM pg_class "
2304+
"WHERE oid = %s",
2305+
tblinfo[i].pkIndexOid);
23252306

23262307
res2 = PQexec(g_conn, query->data);
23272308
if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK)
@@ -2339,14 +2320,6 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
23392320
exit_nicely();
23402321
}
23412322

2342-
/* Sanity check on LOJ */
2343-
if (PQgetisnull(res2, 0, 0))
2344-
{
2345-
write_msg(NULL, "name of primary key of table \"%s\" returned NULL value\n",
2346-
tblinfo[i].relname);
2347-
exit_nicely();
2348-
}
2349-
23502323
tblinfo[i].primary_key_name =
23512324
strdup(fmtId(PQgetvalue(res2, 0, 0), force_quotes));
23522325
if (tblinfo[i].primary_key_name == NULL)
@@ -2879,17 +2852,17 @@ getIndexes(int *numIndexes)
28792852
int ntups;
28802853
IndInfo *indinfo;
28812854

2855+
int i_indexreloid;
2856+
int i_indreloid;
28822857
int i_indexrelname;
28832858
int i_indrelname;
28842859
int i_indamname;
28852860
int i_indproc;
28862861
int i_indkey;
28872862
int i_indclass;
28882863
int i_indisunique;
2889-
int i_indoid;
2890-
int i_oid;
28912864
int i_indisprimary;
2892-
int i_indpred;
2865+
int i_indhaspred;
28932866

28942867
/*
28952868
* find all the user-defined indexes.
@@ -2902,11 +2875,14 @@ getIndexes(int *numIndexes)
29022875
*/
29032876

29042877
appendPQExpBuffer(query,
2905-
"SELECT i.oid, t1.oid as indoid, t1.relname as indexrelname, t2.relname as indrelname, "
2878+
"SELECT i.indexrelid as indexreloid, "
2879+
"i.indrelid as indreloid, "
2880+
"t1.relname as indexrelname, t2.relname as indrelname, "
29062881
"i.indproc, i.indkey, i.indclass, "
2907-
"a.amname as indamname, i.indisunique, i.indisprimary, i.indpred "
2908-
"from pg_index i, pg_class t1, pg_class t2, pg_am a "
2909-
"WHERE t1.oid = i.indexrelid and t2.oid = i.indrelid "
2882+
"a.amname as indamname, i.indisunique, i.indisprimary, "
2883+
"length(i.indpred) > 0 as indhaspred "
2884+
"from pg_index i, pg_class t1, pg_class t2, pg_am a "
2885+
"WHERE t1.oid = i.indexrelid and t2.oid = i.indrelid "
29102886
"and t1.relam = a.oid and i.indexrelid > '%u'::oid "
29112887
"and t2.relname !~ '^pg_' ",
29122888
g_last_builtin_oid);
@@ -2930,8 +2906,8 @@ getIndexes(int *numIndexes)
29302906

29312907
memset((char *) indinfo, 0, ntups * sizeof(IndInfo));
29322908

2933-
i_oid = PQfnumber(res, "oid");
2934-
i_indoid = PQfnumber(res, "indoid");
2909+
i_indexreloid = PQfnumber(res, "indexreloid");
2910+
i_indreloid = PQfnumber(res, "indreloid");
29352911
i_indexrelname = PQfnumber(res, "indexrelname");
29362912
i_indrelname = PQfnumber(res, "indrelname");
29372913
i_indamname = PQfnumber(res, "indamname");
@@ -2940,12 +2916,12 @@ getIndexes(int *numIndexes)
29402916
i_indclass = PQfnumber(res, "indclass");
29412917
i_indisunique = PQfnumber(res, "indisunique");
29422918
i_indisprimary = PQfnumber(res, "indisprimary");
2943-
i_indpred = PQfnumber(res, "indpred");
2919+
i_indhaspred = PQfnumber(res, "indhaspred");
29442920

29452921
for (i = 0; i < ntups; i++)
29462922
{
2947-
indinfo[i].oid = strdup(PQgetvalue(res, i, i_oid));
2948-
indinfo[i].indoid = strdup(PQgetvalue(res, i, i_indoid));
2923+
indinfo[i].indexreloid = strdup(PQgetvalue(res, i, i_indexreloid));
2924+
indinfo[i].indreloid = strdup(PQgetvalue(res, i, i_indreloid));
29492925
indinfo[i].indexrelname = strdup(PQgetvalue(res, i, i_indexrelname));
29502926
indinfo[i].indrelname = strdup(PQgetvalue(res, i, i_indrelname));
29512927
indinfo[i].indamname = strdup(PQgetvalue(res, i, i_indamname));
@@ -2958,7 +2934,7 @@ getIndexes(int *numIndexes)
29582934
INDEX_MAX_KEYS);
29592935
indinfo[i].indisunique = strdup(PQgetvalue(res, i, i_indisunique));
29602936
indinfo[i].indisprimary = strdup(PQgetvalue(res, i, i_indisprimary));
2961-
indinfo[i].indpred = strdup(PQgetvalue(res, i, i_indpred));
2937+
indinfo[i].indhaspred = strdup(PQgetvalue(res, i, i_indhaspred));
29622938
}
29632939
PQclear(res);
29642940
return indinfo;
@@ -4106,7 +4082,8 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
41064082
/* Find the corresponding index */
41074083
for (k = 0; k < numIndexes; k++)
41084084
{
4109-
if (strcmp(indinfo[k].oid, tblinfo[i].pkIndexOid) == 0)
4085+
if (strcmp(indinfo[k].indexreloid,
4086+
tblinfo[i].pkIndexOid) == 0)
41104087
break;
41114088
}
41124089

@@ -4244,7 +4221,7 @@ getAttrName(int attrnum, TableInfo *tblInfo)
42444221

42454222
/*
42464223
* dumpIndexes:
4247-
* write out to fout all the user-define indexes
4224+
* write out to fout all the user-defined indexes
42484225
*/
42494226
void
42504227
dumpIndexes(Archive *fout, IndInfo *indinfo, int numIndexes,
@@ -4447,13 +4424,14 @@ dumpIndexes(Archive *fout, IndInfo *indinfo, int numIndexes,
44474424
else
44484425
appendPQExpBuffer(q, " %s )", attlist->data);
44494426

4450-
if (*indinfo[i].indpred) /* If there is an index predicate */
4427+
if (strcmp(indinfo[i].indhaspred, "t") == 0)
44514428
{
4429+
/* There is an index predicate, so fetch and dump it */
44524430
int numRows;
44534431
PQExpBuffer pred = createPQExpBuffer();
44544432

4455-
appendPQExpBuffer(pred, "SELECT pg_get_expr(indpred,indrelid) as pred FROM pg_index WHERE oid = %s",
4456-
indinfo[i].oid);
4433+
appendPQExpBuffer(pred, "SELECT pg_get_expr(indpred,indrelid) as pred FROM pg_index WHERE indexrelid = %s",
4434+
indinfo[i].indexreloid);
44574435
res = PQexec(g_conn, pred->data);
44584436
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
44594437
{
@@ -4491,7 +4469,7 @@ dumpIndexes(Archive *fout, IndInfo *indinfo, int numIndexes,
44914469
/* Dump Index Comments */
44924470
resetPQExpBuffer(q);
44934471
appendPQExpBuffer(q, "INDEX %s", id1->data);
4494-
dumpComment(fout, q->data, indinfo[i].indoid);
4472+
dumpComment(fout, q->data, indinfo[i].indexreloid);
44954473

44964474
}
44974475
}

src/bin/pg_dump/pg_dump.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_dump.h,v 1.66 2001/07/16 05:06:59 tgl Exp $
9+
* $Id: pg_dump.h,v 1.67 2001/07/17 00:30:35 tgl Exp $
1010
*
1111
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
1212
*
@@ -134,10 +134,10 @@ typedef struct _inhInfo
134134

135135
typedef struct _indInfo
136136
{
137-
char *oid; /* Oid of the pg_index entry */
138-
char *indoid; /* oid of the pg_class entry for the index */
139-
char *indexrelname; /* name of the secondary index class */
140-
char *indrelname; /* name of the indexed heap class */
137+
char *indexreloid; /* oid of the index itself */
138+
char *indreloid; /* oid of the table the index is on */
139+
char *indexrelname; /* name of the index itself */
140+
char *indrelname; /* name of the indexed table */
141141
char *indamname; /* name of the access method (e.g. btree,
142142
* rtree, etc.) */
143143
char *indproc; /* oid of the function to compute the
@@ -147,7 +147,7 @@ typedef struct _indInfo
147147
char *indclass[INDEX_MAX_KEYS]; /* opclass of the keys */
148148
char *indisunique; /* is this index unique? */
149149
char *indisprimary; /* is this a PK index? */
150-
char *indpred; /* index predicate */
150+
char *indhaspred; /* does this index have a predicate? */
151151
} IndInfo;
152152

153153
typedef struct _aggInfo

0 commit comments

Comments
 (0)