@@ -1550,6 +1550,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
15501550TableInfo * tbinfo = tdinfo -> tdtable ;
15511551const char * classname = tbinfo -> dobj .name ;
15521552PQExpBuffer q = createPQExpBuffer ();
1553+ PQExpBuffer insertStmt = NULL ;
15531554PGresult * res ;
15541555int tuple ;
15551556int nfields ;
@@ -1591,34 +1592,57 @@ dumpTableData_insert(Archive *fout, void *dcontext)
15911592nfields = PQnfields (res );
15921593for (tuple = 0 ;tuple < PQntuples (res );tuple ++ )
15931594{
1594- archprintf (fout ,"INSERT INTO %s " ,fmtId (classname ));
1595- if (nfields == 0 )
1595+ /*
1596+ * First time through, we build as much of the INSERT statement as
1597+ * possible in "insertStmt", which we can then just print for each
1598+ * line. If the table happens to have zero columns then this will
1599+ * be a complete statement, otherwise it will end in "VALUES(" and
1600+ * be ready to have the row's column values appended.
1601+ */
1602+ if (insertStmt == NULL )
15961603{
1604+ insertStmt = createPQExpBuffer ();
1605+ appendPQExpBuffer (insertStmt ,"INSERT INTO %s " ,
1606+ fmtId (classname ));
1607+
15971608/* corner case for zero-column table */
1598- archprintf (fout ,"DEFAULT VALUES;\n" );
1599- continue ;
1600- }
1601- if (column_inserts )
1602- {
1603- resetPQExpBuffer (q );
1604- appendPQExpBuffer (q ,"(" );
1605- for (field = 0 ;field < nfields ;field ++ )
1609+ if (nfields == 0 )
16061610{
1607- if (field > 0 )
1608- appendPQExpBuffer (q ,", " );
1609- appendPQExpBufferStr (q ,fmtId (PQfname (res ,field )));
1611+ appendPQExpBufferStr (insertStmt ,"DEFAULT VALUES;\n" );
1612+ }
1613+ else
1614+ {
1615+ /* append the list of column names if required */
1616+ if (column_inserts )
1617+ {
1618+ appendPQExpBufferStr (insertStmt ,"(" );
1619+ for (field = 0 ;field < nfields ;field ++ )
1620+ {
1621+ if (field > 0 )
1622+ appendPQExpBufferStr (insertStmt ,", " );
1623+ appendPQExpBufferStr (insertStmt ,
1624+ fmtId (PQfname (res ,field )));
1625+ }
1626+ appendPQExpBufferStr (insertStmt ,") " );
1627+ }
1628+
1629+ appendPQExpBufferStr (insertStmt ,"VALUES (" );
16101630}
1611- appendPQExpBuffer (q ,") " );
1612- archputs (q -> data ,fout );
16131631}
1614- archprintf (fout ,"VALUES (" );
1632+
1633+ archputs (insertStmt -> data ,fout );
1634+
1635+ /* if it is zero-column table then we're done */
1636+ if (nfields == 0 )
1637+ continue ;
1638+
16151639for (field = 0 ;field < nfields ;field ++ )
16161640{
16171641if (field > 0 )
1618- archprintf ( fout ,"," );
1642+ archputs ( " , " ,fout );
16191643if (PQgetisnull (res ,tuple ,field ))
16201644{
1621- archprintf ( fout , "NULL" );
1645+ archputs ( "NULL" , fout );
16221646continue ;
16231647}
16241648
@@ -1647,7 +1671,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16471671const char * s = PQgetvalue (res ,tuple ,field );
16481672
16491673if (strspn (s ,"0123456789 +-eE." )== strlen (s ))
1650- archprintf ( fout , "%s" , s );
1674+ archputs ( s , fout );
16511675else
16521676archprintf (fout ,"'%s'" ,s );
16531677}
@@ -1661,9 +1685,9 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16611685
16621686case BOOLOID :
16631687if (strcmp (PQgetvalue (res ,tuple ,field ),"t" )== 0 )
1664- archprintf ( fout , "true" );
1688+ archputs ( "true" , fout );
16651689else
1666- archprintf ( fout , "false" );
1690+ archputs ( "false" , fout );
16671691break ;
16681692
16691693default :
@@ -1676,7 +1700,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16761700break ;
16771701}
16781702}
1679- archprintf ( fout , ");\n" );
1703+ archputs ( ");\n" , fout );
16801704}
16811705
16821706if (PQntuples (res ) <=0 )
@@ -1687,11 +1711,14 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16871711PQclear (res );
16881712}
16891713
1690- archprintf ( fout , "\n\n" );
1714+ archputs ( "\n\n" , fout );
16911715
16921716ExecuteSqlStatement (fout ,"CLOSE _pg_dump_cursor" );
16931717
16941718destroyPQExpBuffer (q );
1719+ if (insertStmt != NULL )
1720+ destroyPQExpBuffer (insertStmt );
1721+
16951722return 1 ;
16961723}
16971724