1
1
/*-------------------------------------------------------------------------
2
2
*
3
3
* 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
5
5
* into a script file.
6
6
*
7
7
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
22
22
*
23
23
*
24
24
* 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 $
26
26
*
27
27
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
28
28
*
@@ -194,6 +194,7 @@ static char *GetPrivileges(const char *s);
194
194
static int dumpBlobs (Archive * AH , char * , void * );
195
195
static int dumpDatabase (Archive * AH );
196
196
static PQExpBuffer getPKconstraint (TableInfo * tblInfo , IndInfo * indInfo );
197
+ static const char * getAttrName (int attrnum , TableInfo * tblInfo );
197
198
198
199
extern char * optarg ;
199
200
extern int optind ,
@@ -3932,26 +3933,19 @@ getPKconstraint(TableInfo *tblInfo, IndInfo *indInfo)
3932
3933
{
3933
3934
PQExpBuffer pkBuf = createPQExpBuffer ();
3934
3935
int k ;
3935
- int indkey ;
3936
-
3937
- resetPQExpBuffer (pkBuf );
3938
3936
3939
3937
appendPQExpBuffer (pkBuf , "Constraint %s Primary Key (" ,
3940
3938
tblInfo -> primary_key_name );
3941
3939
3942
-
3943
3940
for (k = 0 ; k < INDEX_MAX_KEYS ; k ++ )
3944
3941
{
3945
- char * attname ;
3942
+ int indkey ;
3943
+ const char * attname ;
3946
3944
3947
3945
indkey = atoi (indInfo -> indkey [k ]);
3948
3946
if (indkey == InvalidAttrNumber )
3949
3947
break ;
3950
- indkey -- ;
3951
- if (indkey == ObjectIdAttributeNumber - 1 )
3952
- attname = "oid" ;
3953
- else
3954
- attname = tblInfo -> attnames [indkey ];
3948
+ attname = getAttrName (indkey , tblInfo );
3955
3949
3956
3950
appendPQExpBuffer (pkBuf , "%s%s" ,
3957
3951
(k == 0 ) ? "" : ", " ,
@@ -3963,6 +3957,41 @@ getPKconstraint(TableInfo *tblInfo, IndInfo *indInfo)
3963
3957
return pkBuf ;
3964
3958
}
3965
3959
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
+
3966
3995
/*
3967
3996
* dumpIndices:
3968
3997
* write out to fout all the user-define indices
@@ -3978,8 +4007,7 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
3978
4007
char * classname [INDEX_MAX_KEYS ];
3979
4008
char * funcname ; /* the name of the function to comput the
3980
4009
* index key from */
3981
- int indkey ,
3982
- indclass ;
4010
+ int indclass ;
3983
4011
int nclass ;
3984
4012
3985
4013
PQExpBuffer q = createPQExpBuffer (),
@@ -4111,19 +4139,17 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
4111
4139
resetPQExpBuffer (attlist );
4112
4140
for (k = 0 ; k < INDEX_MAX_KEYS ; k ++ )
4113
4141
{
4114
- char * attname ;
4142
+ int indkey ;
4143
+ const char * attname ;
4115
4144
4116
4145
indkey = atoi (indinfo [i ].indkey [k ]);
4117
4146
if (indkey == InvalidAttrNumber )
4118
4147
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 ]);
4124
4149
if (funcname )
4125
4150
appendPQExpBuffer (attlist , "%s%s" ,
4126
- (k == 0 ) ? "" : ", " , fmtId (attname , force_quotes ));
4151
+ (k == 0 ) ? "" : ", " ,
4152
+ fmtId (attname , force_quotes ));
4127
4153
else
4128
4154
{
4129
4155
if (k >= nclass )
@@ -4138,20 +4164,14 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
4138
4164
appendPQExpBuffer (id1 , fmtId (attname , force_quotes ));
4139
4165
appendPQExpBuffer (id2 , fmtId (classname [k ], force_quotes ));
4140
4166
appendPQExpBuffer (attlist , "%s%s %s" ,
4141
- (k == 0 ) ? "" : ", " , id1 -> data , id2 -> data );
4167
+ (k == 0 ) ? "" : ", " ,
4168
+ id1 -> data , id2 -> data );
4142
4169
free (classname [k ]);
4143
4170
}
4144
4171
}
4145
4172
4146
4173
if (!tablename || (strcmp (indinfo [i ].indrelname , tablename ) == 0 ) || (strlen (tablename ) == 0 ))
4147
4174
{
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
-
4155
4175
resetPQExpBuffer (id1 );
4156
4176
resetPQExpBuffer (id2 );
4157
4177
appendPQExpBuffer (id1 , fmtId (indinfo [i ].indexrelname , force_quotes ));
@@ -4178,11 +4198,15 @@ dumpIndices(Archive *fout, IndInfo *indinfo, int numIndices,
4178
4198
else
4179
4199
appendPQExpBuffer (q , " %s );\n" , attlist -> data );
4180
4200
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
+ */
4183
4206
ArchiveEntry (fout , tblinfo [tableInd ].oid , id1 -> data , "INDEX" , NULL , q -> data , delq -> data ,
4184
4207
"" , tblinfo [tableInd ].usename , NULL , NULL );
4185
4208
4209
+ /* Dump Index Comments */
4186
4210
resetPQExpBuffer (q );
4187
4211
appendPQExpBuffer (q , "INDEX %s" , id1 -> data );
4188
4212
dumpComment (fout , q -> data , indinfo [i ].indoid );
0 commit comments