11/*-------------------------------------------------------------------------
22 *
33 * pg_dump.c
4- * pg_dump is an utility for dumping out a postgres database
4+ * pg_dump is a utility for dumping out a postgres database
55 * into a script file.
66 *
77 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
2222 *
2323 *
2424 * IDENTIFICATION
25- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.202 2001/04/14 13:11:03 pjw Exp $
25+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.203 2001/04/22 21:34:13 tgl Exp $
2626 *
2727 * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2828 *
@@ -194,6 +194,7 @@ static char *GetPrivileges(const char *s);
194194static int dumpBlobs (Archive * AH , char * , void * );
195195static int dumpDatabase (Archive * AH );
196196static PQExpBuffer getPKconstraint (TableInfo * tblInfo , IndInfo * indInfo );
197+ static const char * getAttrName (int attrnum , TableInfo * tblInfo );
197198
198199extern char * optarg ;
199200extern int optind ,
@@ -3932,26 +3933,19 @@ getPKconstraint(TableInfo *tblInfo, IndInfo *indInfo)
39323933{
39333934 PQExpBuffer pkBuf = createPQExpBuffer ();
39343935 int k ;
3935- int indkey ;
3936-
3937- resetPQExpBuffer (pkBuf );
39383936
39393937 appendPQExpBuffer (pkBuf , "Constraint %s Primary Key (" ,
39403938 tblInfo -> primary_key_name );
39413939
3942-
39433940 for (k = 0 ; k < INDEX_MAX_KEYS ; k ++ )
39443941 {
3945- char * attname ;
3942+ int indkey ;
3943+ const char * attname ;
39463944
39473945 indkey = atoi (indInfo -> indkey [k ]);
39483946 if (indkey == InvalidAttrNumber )
39493947 break ;
3950- indkey -- ;
3951- if (indkey == ObjectIdAttributeNumber - 1 )
3952- attname = "oid" ;
3953- else
3954- attname = tblInfo -> attnames [indkey ];
3948+ attname = getAttrName (indkey , tblInfo );
39553949
39563950 appendPQExpBuffer (pkBuf , "%s%s" ,
39573951 (k == 0 ) ? "" : ", " ,
@@ -3963,6 +3957,41 @@ getPKconstraint(TableInfo *tblInfo, IndInfo *indInfo)
39633957 return pkBuf ;
39643958}
39653959
3960+ /*
3961+ * getAttrName: extract the correct name for an attribute
3962+ *
3963+ * The array tblInfo->attnames[] only provides names of user attributes;
3964+ * if a system attribute number is supplied, we have to fake it.
3965+ * We also do a little bit of bounds checking for safety's sake.
3966+ */
3967+ static const char *
3968+ getAttrName (int attrnum , TableInfo * tblInfo )
3969+ {
3970+ if (attrnum > 0 && attrnum <= tblInfo -> numatts )
3971+ return tblInfo -> attnames [attrnum - 1 ];
3972+ switch (attrnum )
3973+ {
3974+ case SelfItemPointerAttributeNumber :
3975+ return "ctid" ;
3976+ case ObjectIdAttributeNumber :
3977+ return "oid" ;
3978+ case MinTransactionIdAttributeNumber :
3979+ return "xmin" ;
3980+ case MinCommandIdAttributeNumber :
3981+ return "cmin" ;
3982+ case MaxTransactionIdAttributeNumber :
3983+ return "xmax" ;
3984+ case MaxCommandIdAttributeNumber :
3985+ return "cmax" ;
3986+ case TableOidAttributeNumber :
3987+ return "tableoid" ;
3988+ }
3989+ fprintf (stderr , "getAttrName(): Invalid attribute number %d for table %s\n" ,
3990+ attrnum , tblInfo -> relname );
3991+ exit_nicely (g_conn );
3992+ return NULL ; /* keep compiler quiet */
3993+ }
3994+
39663995/*
39673996 * dumpIndices:
39683997 * write out to fout all the user-define indices
@@ -3978,8 +4007,7 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
39784007 char * classname [INDEX_MAX_KEYS ];
39794008 char * funcname ; /* the name of the function to comput the
39804009 * index key from */
3981- int indkey ,
3982- indclass ;
4010+ int indclass ;
39834011 int nclass ;
39844012
39854013 PQExpBuffer q = createPQExpBuffer (),
@@ -4111,19 +4139,17 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
41114139 resetPQExpBuffer (attlist );
41124140 for (k = 0 ; k < INDEX_MAX_KEYS ; k ++ )
41134141 {
4114- char * attname ;
4142+ int indkey ;
4143+ const char * attname ;
41154144
41164145 indkey = atoi (indinfo [i ].indkey [k ]);
41174146 if (indkey == InvalidAttrNumber )
41184147 break ;
4119- indkey -- ;
4120- if (indkey == ObjectIdAttributeNumber - 1 )
4121- attname = "oid" ;
4122- else
4123- attname = tblinfo [tableInd ].attnames [indkey ];
4148+ attname = getAttrName (indkey , & tblinfo [tableInd ]);
41244149 if (funcname )
41254150 appendPQExpBuffer (attlist , "%s%s" ,
4126- (k == 0 ) ? "" : ", " , fmtId (attname , force_quotes ));
4151+ (k == 0 ) ? "" : ", " ,
4152+ fmtId (attname , force_quotes ));
41274153 else
41284154 {
41294155 if (k >= nclass )
@@ -4138,20 +4164,14 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
41384164 appendPQExpBuffer (id1 , fmtId (attname , force_quotes ));
41394165 appendPQExpBuffer (id2 , fmtId (classname [k ], force_quotes ));
41404166 appendPQExpBuffer (attlist , "%s%s %s" ,
4141- (k == 0 ) ? "" : ", " , id1 -> data , id2 -> data );
4167+ (k == 0 ) ? "" : ", " ,
4168+ id1 -> data , id2 -> data );
41424169 free (classname [k ]);
41434170 }
41444171 }
41454172
41464173 if (!tablename || (strcmp (indinfo [i ].indrelname , tablename ) == 0 ) || (strlen (tablename ) == 0 ))
41474174 {
4148-
4149- /*
4150- * We make the index belong to the owner of its table, which
4151- * is not necessarily right but should answer 99% of the time.
4152- * Would have to add owner name to IndInfo to do it right.
4153- */
4154-
41554175 resetPQExpBuffer (id1 );
41564176 resetPQExpBuffer (id2 );
41574177 appendPQExpBuffer (id1 , fmtId (indinfo [i ].indexrelname , force_quotes ));
@@ -4178,11 +4198,15 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
41784198 else
41794199 appendPQExpBuffer (q , " %s );\n" , attlist -> data );
41804200
4181- /* Dump Index Comments */
4182-
4201+ /*
4202+ * We make the index belong to the owner of its table, which
4203+ * is not necessarily right but should answer 99% of the time.
4204+ * Would have to add owner name to IndInfo to do it right.
4205+ */
41834206 ArchiveEntry (fout , tblinfo [tableInd ].oid , id1 -> data , "INDEX" , NULL , q -> data , delq -> data ,
41844207 "" , tblinfo [tableInd ].usename , NULL , NULL );
41854208
4209+ /* Dump Index Comments */
41864210 resetPQExpBuffer (q );
41874211 appendPQExpBuffer (q , "INDEX %s" , id1 -> data );
41884212 dumpComment (fout , q -> data , indinfo [i ].indoid );
0 commit comments