2222 *
2323 *
2424 * IDENTIFICATION
25- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.289 2002/08/2200:01:45 tgl Exp $
25+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.290 2002/08/2221:29:34 tgl Exp $
2626 *
2727 *-------------------------------------------------------------------------
2828 */
@@ -124,7 +124,7 @@ static char *GetPrivileges(Archive *AH, const char *s, const char *type);
124124static int dumpBlobs (Archive * AH ,char * ,void * );
125125static int dumpDatabase (Archive * AH );
126126static const char * getAttrName (int attrnum ,TableInfo * tblInfo );
127- static const char * fmtCopyColumnList (const TableInfo * ti );
127+ static const char * fmtCopyColumnList (const TableInfo * ti );
128128
129129extern char * optarg ;
130130extern int optind ,
@@ -828,7 +828,16 @@ dumpClasses_nodumpData(Archive *fout, char *oid, void *dctxv)
828828 */
829829selectSourceSchema (tbinfo -> relnamespace -> nspname );
830830
831- column_list = fmtCopyColumnList (tbinfo );
831+ /*
832+ * If possible, specify the column list explicitly so that we have no
833+ * possibility of retrieving data in the wrong column order. (The
834+ * default column ordering of COPY will not be what we want in certain
835+ * corner cases involving ADD COLUMN and inheritance.)
836+ */
837+ if (g_fout -> remoteVersion >=70300 )
838+ column_list = fmtCopyColumnList (tbinfo );
839+ else
840+ column_list = "" ;/* can't select columns in COPY */
832841
833842if (oids && hasoids )
834843{
@@ -1123,11 +1132,11 @@ static void
11231132dumpClasses (const TableInfo * tblinfo ,const int numTables ,Archive * fout ,
11241133const bool oids )
11251134{
1126- int i ;
1135+ PQExpBuffer copyBuf = createPQExpBuffer () ;
11271136DataDumperPtr dumpFn ;
11281137DumpContext * dumpCtx ;
1129- char copyBuf [512 ];
11301138char * copyStmt ;
1139+ int i ;
11311140
11321141for (i = 0 ;i < numTables ;i ++ )
11331142{
@@ -1162,11 +1171,12 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout,
11621171
11631172dumpFn = dumpClasses_nodumpData ;
11641173column_list = fmtCopyColumnList (& (tblinfo [i ]));
1165- sprintf (copyBuf ,"COPY %s %s %sFROM stdin;\n" ,
1166- fmtId (tblinfo [i ].relname ),
1167- column_list ,
1168- (oids && tblinfo [i ].hasoids ) ?"WITH OIDS " :"" );
1169- copyStmt = copyBuf ;
1174+ resetPQExpBuffer (copyBuf );
1175+ appendPQExpBuffer (copyBuf ,"COPY %s %s %sFROM stdin;\n" ,
1176+ fmtId (tblinfo [i ].relname ),
1177+ column_list ,
1178+ (oids && tblinfo [i ].hasoids ) ?"WITH OIDS " :"" );
1179+ copyStmt = copyBuf -> data ;
11701180}
11711181else
11721182{
@@ -1181,6 +1191,8 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout,
11811191dumpFn ,dumpCtx );
11821192}
11831193}
1194+
1195+ destroyPQExpBuffer (copyBuf );
11841196}
11851197
11861198
@@ -6652,11 +6664,9 @@ fmtQualifiedId(const char *schema, const char *id)
66526664
66536665/*
66546666 * return a column list clause for the given relation.
6655- * returns an empty string if the remote server is older than
6656- * 7.3.
66576667 */
6658- static const char *
6659- fmtCopyColumnList (const TableInfo * ti )
6668+ static const char *
6669+ fmtCopyColumnList (const TableInfo * ti )
66606670{
66616671static PQExpBuffer q = NULL ;
66626672int numatts = ti -> numatts ;
@@ -6665,16 +6675,11 @@ fmtCopyColumnList(const TableInfo* ti)
66656675bool needComma ;
66666676int i ;
66676677
6668- if (g_fout -> remoteVersion < 70300 )
6669- return "" ;
6670-
66716678if (q )/* first time through? */
66726679resetPQExpBuffer (q );
66736680else
66746681q = createPQExpBuffer ();
66756682
6676- resetPQExpBuffer (q );
6677-
66786683appendPQExpBuffer (q ,"(" );
66796684needComma = false;
66806685for (i = 0 ;i < numatts ;i ++ )