1212 *by PostgreSQL
1313 *
1414 * IDENTIFICATION
15- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.310 2002/12/01 18:44:00 tgl Exp $
15+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.311 2002/12/12 21:03:24 tgl Exp $
1616 *
1717 *-------------------------------------------------------------------------
1818 */
@@ -827,6 +827,9 @@ dumpClasses_nodumpData(Archive *fout, char *oid, void *dctxv)
827827 * possibility of retrieving data in the wrong column order. (The
828828 * default column ordering of COPY will not be what we want in certain
829829 * corner cases involving ADD COLUMN and inheritance.)
830+ *
831+ * NB: caller should have already determined that there are dumpable
832+ * columns, so that fmtCopyColumnList will return something.
830833 */
831834if (g_fout -> remoteVersion >=70300 )
832835column_list = fmtCopyColumnList (tbinfo );
@@ -1140,40 +1143,45 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout,
11401143
11411144if (tblinfo [i ].dump )
11421145{
1146+ const char * column_list ;
1147+
11431148if (g_verbose )
11441149write_msg (NULL ,"preparing to dump the contents of table %s\n" ,
11451150classname );
11461151
1147- dumpCtx = (DumpContext * )malloc (sizeof (DumpContext ));
1148- dumpCtx -> tblinfo = (TableInfo * )tblinfo ;
1149- dumpCtx -> tblidx = i ;
1150- dumpCtx -> oids = oids ;
1151-
1152- if (!dumpData )
1152+ /* Get column list first to check for zero-column table */
1153+ column_list = fmtCopyColumnList (& (tblinfo [i ]));
1154+ if (column_list )
11531155{
1154- /* Dump/restore using COPY */
1155- const char * column_list ;
1156-
1157- dumpFn = dumpClasses_nodumpData ;
1158- column_list = fmtCopyColumnList (& (tblinfo [i ]));
1159- resetPQExpBuffer (copyBuf );
1160- appendPQExpBuffer (copyBuf ,"COPY %s %s %sFROM stdin;\n" ,
1161- fmtId (tblinfo [i ].relname ),
1162- column_list ,
1156+ dumpCtx = (DumpContext * )malloc (sizeof (DumpContext ));
1157+ dumpCtx -> tblinfo = (TableInfo * )tblinfo ;
1158+ dumpCtx -> tblidx = i ;
1159+ dumpCtx -> oids = oids ;
1160+
1161+ if (!dumpData )
1162+ {
1163+ /* Dump/restore using COPY */
1164+ dumpFn = dumpClasses_nodumpData ;
1165+ resetPQExpBuffer (copyBuf );
1166+ appendPQExpBuffer (copyBuf ,"COPY %s %s %sFROM stdin;\n" ,
1167+ fmtId (tblinfo [i ].relname ),
1168+ column_list ,
11631169 (oids && tblinfo [i ].hasoids ) ?"WITH OIDS " :"" );
1164- copyStmt = copyBuf -> data ;
1165- }
1166- else
1167- {
1168- /* Restore using INSERT */
1169- dumpFn = dumpClasses_dumpData ;
1170- copyStmt = NULL ;
1171- }
1170+ copyStmt = copyBuf -> data ;
1171+ }
1172+ else
1173+ {
1174+ /* Restore using INSERT */
1175+ dumpFn = dumpClasses_dumpData ;
1176+ copyStmt = NULL ;
1177+ }
11721178
1173- ArchiveEntry (fout ,tblinfo [i ].oid ,tblinfo [i ].relname ,
1174- tblinfo [i ].relnamespace -> nspname ,tblinfo [i ].usename ,
1175- "TABLE DATA" ,NULL ,"" ,"" ,copyStmt ,
1176- dumpFn ,dumpCtx );
1179+ ArchiveEntry (fout ,tblinfo [i ].oid ,tblinfo [i ].relname ,
1180+ tblinfo [i ].relnamespace -> nspname ,
1181+ tblinfo [i ].usename ,
1182+ "TABLE DATA" ,NULL ,"" ,"" ,copyStmt ,
1183+ dumpFn ,dumpCtx );
1184+ }
11771185}
11781186}
11791187
@@ -3220,13 +3228,16 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
32203228if (typdefault )
32213229appendPQExpBuffer (q ," DEFAULT %s" ,typdefault );
32223230
3223- /* Fetch and process CHECK Constraints */
3231+ PQclear (res );
3232+
3233+ /*
3234+ * Fetch and process CHECK constraints for the domain
3235+ */
32243236appendPQExpBuffer (chkquery ,"SELECT conname, consrc "
32253237"FROM pg_catalog.pg_constraint "
32263238"WHERE contypid = '%s'::pg_catalog.oid" ,
32273239tinfo -> oid );
32283240
3229- PQclear (res );
32303241res = PQexec (g_conn ,chkquery -> data );
32313242if (!res ||
32323243PQresultStatus (res )!= PGRES_TUPLES_OK )
@@ -3236,7 +3247,6 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
32363247exit_nicely ();
32373248}
32383249
3239- /* Expecting a single result only */
32403250ntups = PQntuples (res );
32413251for (i = 0 ;i < ntups ;i ++ )
32423252{
@@ -3246,7 +3256,8 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
32463256conname = PQgetvalue (res ,i ,PQfnumber (res ,"conname" ));
32473257consrc = PQgetvalue (res ,i ,PQfnumber (res ,"consrc" ));
32483258
3249- appendPQExpBuffer (q ," CONSTRAINT %s CHECK %s" ,fmtId (conname ),consrc );
3259+ appendPQExpBuffer (q ,"\n\tCONSTRAINT %s CHECK %s" ,
3260+ fmtId (conname ),consrc );
32503261}
32513262
32523263appendPQExpBuffer (q ,";\n" );
@@ -6744,7 +6755,10 @@ fmtQualifiedId(const char *schema, const char *id)
67446755}
67456756
67466757/*
6747- * return a column list clause for the given relation.
6758+ * Return a column list clause for the given relation.
6759+ *
6760+ * Special case: if there are no undropped columns in the relation, return
6761+ * NULL, not an invalid "()" column list.
67486762 */
67496763static const char *
67506764fmtCopyColumnList (const TableInfo * ti )
@@ -6772,6 +6786,10 @@ fmtCopyColumnList(const TableInfo *ti)
67726786appendPQExpBuffer (q ,"%s" ,fmtId (attnames [i ]));
67736787needComma = true;
67746788}
6789+
6790+ if (!needComma )
6791+ return NULL ;/* no undropped columns */
6792+
67756793appendPQExpBuffer (q ,")" );
67766794return q -> data ;
67776795}