Commit 814f40cf authored by Tom Lane's avatar Tom Lane

Use a cursor for fetching data in -d or -D mode, so that pg_dump doesn't

run out of memory with large tables in these modes.  Patch from
Martijn van Oosterhout.
parent 545c6696
......@@ -22,7 +22,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.226 2001/08/27 01:09:59 tgl Exp $
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.227 2001/08/27 20:33:07 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -400,20 +400,34 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv)
if (fout->remoteVersion >= 70100)
{
appendPQExpBuffer(q, "SELECT * FROM ONLY %s", fmtId(classname, force_quotes));
appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT * FROM ONLY %s", fmtId(classname, force_quotes));
} else {
appendPQExpBuffer(q, "SELECT * FROM %s", fmtId(classname, force_quotes));
appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT * FROM %s", fmtId(classname, force_quotes));
}
res = PQexec(g_conn, q->data);
if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK)
PQresultStatus(res) != PGRES_COMMAND_OK)
{
write_msg(NULL, "dumpClasses(): SQL command failed\n");
write_msg(NULL, "Error message from server: %s", PQerrorMessage(g_conn));
write_msg(NULL, "The command was: %s\n", q->data);
exit_nicely();
}
do {
PQclear(res);
res = PQexec(g_conn, "FETCH 100 FROM _pg_dump_cursor");
if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK)
{
write_msg(NULL, "dumpClasses(): SQL command failed\n");
write_msg(NULL, "Error message from server: %s", PQerrorMessage(g_conn));
write_msg(NULL, "The command was: FETCH 100 FROM _pg_dump_cursor\n");
exit_nicely();
}
for (tuple = 0; tuple < PQntuples(res); tuple++)
{
archprintf(fout, "INSERT INTO %s ", fmtId(classname, force_quotes));
......@@ -470,7 +484,21 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv)
}
archprintf(fout, ");\n");
}
} while( PQntuples(res) > 0 );
PQclear(res);
res = PQexec(g_conn, "CLOSE _pg_dump_cursor");
if (!res ||
PQresultStatus(res) != PGRES_COMMAND_OK)
{
write_msg(NULL, "dumpClasses(): SQL command failed\n");
write_msg(NULL, "Error message from server: %s", PQerrorMessage(g_conn));
write_msg(NULL, "The command was: CLOSE _pg_dump_cursor\n");
exit_nicely();
}
PQclear(res);
destroyPQExpBuffer(q);
return 1;
}
......
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