55 *Implements the basic DB functions used by the archiver.
66 *
77 * IDENTIFICATION
8- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.66 2005/10/15 02:49:38 momjian Exp $
8+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.67 2006/02/05 20:58:47 tgl Exp $
99 *
1010 *-------------------------------------------------------------------------
1111 */
@@ -302,7 +302,7 @@ ExecuteSqlCommand(ArchiveHandle *AH, PQExpBuffer qry, char *desc)
302302{
303303if (PQresultStatus (res )== PGRES_COPY_IN )
304304{
305- AH -> pgCopyIn = 1 ;
305+ AH -> pgCopyIn = true ;
306306}
307307else
308308{
@@ -383,13 +383,12 @@ _sendCopyLine(ArchiveHandle *AH, char *qry, char *eos)
383383appendPQExpBuffer (AH -> pgCopyBuf ,"%s\n" ,qry );
384384isEnd = (strcmp (AH -> pgCopyBuf -> data ,"\\.\n" )== 0 );
385385
386- /*---------
387- *fprintf(stderr, "Sending '%s' via
388- * COPY(at end = %d)\n\n", AH->pgCopyBuf->data, isEnd);
389- *---------
386+ /*
387+ *Note that we drop the data on the floor if libpq has failed to
388+ * enter COPYmode; this allows us to behave reasonably when trying
389+ * to continue after an error in a COPY command.
390390 */
391-
392- if (PQputline (AH -> connection ,AH -> pgCopyBuf -> data )!= 0 )
391+ if (AH -> pgCopyIn && PQputline (AH -> connection ,AH -> pgCopyBuf -> data )!= 0 )
393392die_horribly (AH ,modulename ,"error returned by PQputline\n" );
394393
395394resetPQExpBuffer (AH -> pgCopyBuf );
@@ -400,10 +399,10 @@ _sendCopyLine(ArchiveHandle *AH, char *qry, char *eos)
400399
401400if (isEnd )
402401{
403- if (PQendcopy (AH -> connection )!= 0 )
402+ if (AH -> pgCopyIn && PQendcopy (AH -> connection )!= 0 )
404403die_horribly (AH ,modulename ,"error returned by PQendcopy\n" );
405404
406- AH -> pgCopyIn = 0 ;
405+ AH -> pgCopyIn = false ;
407406}
408407
409408return qry + loc + 1 ;
@@ -615,7 +614,18 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, size_t bufLen)
615614/* Could switch between command and COPY IN mode at each line */
616615while (qry < eos )
617616{
618- if (AH -> pgCopyIn )
617+ /*
618+ * If libpq is in CopyIn mode *or* if the archive structure shows we
619+ * are sending COPY data, treat the data as COPY data. The pgCopyIn
620+ * check is only needed for backwards compatibility with ancient
621+ * archive files that might just issue a COPY command without marking
622+ * it properly. Note that in an archive entry that has a copyStmt,
623+ * all data up to the end of the entry will go to _sendCopyLine, and
624+ * therefore will be dropped if libpq has failed to enter COPY mode.
625+ * Also, if a "\." data terminator is found, anything remaining in the
626+ * archive entry will be dropped.
627+ */
628+ if (AH -> pgCopyIn || AH -> writingCopyData )
619629qry = _sendCopyLine (AH ,qry ,eos );
620630else
621631qry = _sendSQLLine (AH ,qry ,eos );