Commit b7325168 authored by Tom Lane's avatar Tom Lane

Don't produce bogus COPY command when there are no undropped columns

in a table.
parent 40c4472e
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* by PostgreSQL * by PostgreSQL
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.310 2002/12/01 18:44:00 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.311 2002/12/12 21:03:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -827,6 +827,9 @@ dumpClasses_nodumpData(Archive *fout, char *oid, void *dctxv) ...@@ -827,6 +827,9 @@ dumpClasses_nodumpData(Archive *fout, char *oid, void *dctxv)
* possibility of retrieving data in the wrong column order. (The * possibility of retrieving data in the wrong column order. (The
* default column ordering of COPY will not be what we want in certain * default column ordering of COPY will not be what we want in certain
* corner cases involving ADD COLUMN and inheritance.) * corner cases involving ADD COLUMN and inheritance.)
*
* NB: caller should have already determined that there are dumpable
* columns, so that fmtCopyColumnList will return something.
*/ */
if (g_fout->remoteVersion >= 70300) if (g_fout->remoteVersion >= 70300)
column_list = fmtCopyColumnList(tbinfo); column_list = fmtCopyColumnList(tbinfo);
...@@ -1140,40 +1143,45 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout, ...@@ -1140,40 +1143,45 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout,
if (tblinfo[i].dump) if (tblinfo[i].dump)
{ {
const char *column_list;
if (g_verbose) if (g_verbose)
write_msg(NULL, "preparing to dump the contents of table %s\n", write_msg(NULL, "preparing to dump the contents of table %s\n",
classname); classname);
dumpCtx = (DumpContext *) malloc(sizeof(DumpContext)); /* Get column list first to check for zero-column table */
dumpCtx->tblinfo = (TableInfo *) tblinfo; column_list = fmtCopyColumnList(&(tblinfo[i]));
dumpCtx->tblidx = i; if (column_list)
dumpCtx->oids = oids;
if (!dumpData)
{ {
/* Dump/restore using COPY */ dumpCtx = (DumpContext *) malloc(sizeof(DumpContext));
const char *column_list; dumpCtx->tblinfo = (TableInfo *) tblinfo;
dumpCtx->tblidx = i;
dumpFn = dumpClasses_nodumpData; dumpCtx->oids = oids;
column_list = fmtCopyColumnList(&(tblinfo[i]));
resetPQExpBuffer(copyBuf); if (!dumpData)
appendPQExpBuffer(copyBuf, "COPY %s %s %sFROM stdin;\n", {
fmtId(tblinfo[i].relname), /* Dump/restore using COPY */
column_list, dumpFn = dumpClasses_nodumpData;
resetPQExpBuffer(copyBuf);
appendPQExpBuffer(copyBuf, "COPY %s %s %sFROM stdin;\n",
fmtId(tblinfo[i].relname),
column_list,
(oids && tblinfo[i].hasoids) ? "WITH OIDS " : ""); (oids && tblinfo[i].hasoids) ? "WITH OIDS " : "");
copyStmt = copyBuf->data; copyStmt = copyBuf->data;
} }
else else
{ {
/* Restore using INSERT */ /* Restore using INSERT */
dumpFn = dumpClasses_dumpData; dumpFn = dumpClasses_dumpData;
copyStmt = NULL; copyStmt = NULL;
} }
ArchiveEntry(fout, tblinfo[i].oid, tblinfo[i].relname, ArchiveEntry(fout, tblinfo[i].oid, tblinfo[i].relname,
tblinfo[i].relnamespace->nspname, tblinfo[i].usename, tblinfo[i].relnamespace->nspname,
"TABLE DATA", NULL, "", "", copyStmt, tblinfo[i].usename,
dumpFn, dumpCtx); "TABLE DATA", NULL, "", "", copyStmt,
dumpFn, dumpCtx);
}
} }
} }
...@@ -3220,13 +3228,16 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo) ...@@ -3220,13 +3228,16 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
if (typdefault) if (typdefault)
appendPQExpBuffer(q, " DEFAULT %s", typdefault); appendPQExpBuffer(q, " DEFAULT %s", typdefault);
/* Fetch and process CHECK Constraints */ PQclear(res);
/*
* Fetch and process CHECK constraints for the domain
*/
appendPQExpBuffer(chkquery, "SELECT conname, consrc " appendPQExpBuffer(chkquery, "SELECT conname, consrc "
"FROM pg_catalog.pg_constraint " "FROM pg_catalog.pg_constraint "
"WHERE contypid = '%s'::pg_catalog.oid", "WHERE contypid = '%s'::pg_catalog.oid",
tinfo->oid); tinfo->oid);
PQclear(res);
res = PQexec(g_conn, chkquery->data); res = PQexec(g_conn, chkquery->data);
if (!res || if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK) PQresultStatus(res) != PGRES_TUPLES_OK)
...@@ -3236,7 +3247,6 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo) ...@@ -3236,7 +3247,6 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
exit_nicely(); exit_nicely();
} }
/* Expecting a single result only */
ntups = PQntuples(res); ntups = PQntuples(res);
for (i = 0; i < ntups; i++) for (i = 0; i < ntups; i++)
{ {
...@@ -3246,7 +3256,8 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo) ...@@ -3246,7 +3256,8 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
conname = PQgetvalue(res, i, PQfnumber(res, "conname")); conname = PQgetvalue(res, i, PQfnumber(res, "conname"));
consrc = PQgetvalue(res, i, PQfnumber(res, "consrc")); consrc = PQgetvalue(res, i, PQfnumber(res, "consrc"));
appendPQExpBuffer(q, " CONSTRAINT %s CHECK %s", fmtId(conname), consrc); appendPQExpBuffer(q, "\n\tCONSTRAINT %s CHECK %s",
fmtId(conname), consrc);
} }
appendPQExpBuffer(q, ";\n"); appendPQExpBuffer(q, ";\n");
...@@ -6744,7 +6755,10 @@ fmtQualifiedId(const char *schema, const char *id) ...@@ -6744,7 +6755,10 @@ fmtQualifiedId(const char *schema, const char *id)
} }
/* /*
* return a column list clause for the given relation. * Return a column list clause for the given relation.
*
* Special case: if there are no undropped columns in the relation, return
* NULL, not an invalid "()" column list.
*/ */
static const char * static const char *
fmtCopyColumnList(const TableInfo *ti) fmtCopyColumnList(const TableInfo *ti)
...@@ -6772,6 +6786,10 @@ fmtCopyColumnList(const TableInfo *ti) ...@@ -6772,6 +6786,10 @@ fmtCopyColumnList(const TableInfo *ti)
appendPQExpBuffer(q, "%s", fmtId(attnames[i])); appendPQExpBuffer(q, "%s", fmtId(attnames[i]));
needComma = true; needComma = true;
} }
if (!needComma)
return NULL; /* no undropped columns */
appendPQExpBuffer(q, ")"); appendPQExpBuffer(q, ")");
return q->data; return q->data;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment