Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 97e1ec4

Browse files
committed
Speed up printing of INSERT statements in pg_dump.
In --inserts and especially --column-inserts mode, we can get a useful speedup by generating the common prefix of all a table's INSERT commands just once, and then printing the prebuilt string for each row. This avoids multiple invocations of fmtId() and other minor fooling around. David Rowley
1 parent 3172eea commit 97e1ec4

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
15501550
TableInfo *tbinfo = tdinfo->tdtable;
15511551
const char *classname = tbinfo->dobj.name;
15521552
PQExpBuffer q = createPQExpBuffer();
1553+
PQExpBuffer insertStmt = NULL;
15531554
PGresult *res;
15541555
int tuple;
15551556
int nfields;
@@ -1591,34 +1592,57 @@ dumpTableData_insert(Archive *fout, void *dcontext)
15911592
nfields = PQnfields(res);
15921593
for (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+
16151639
for (field = 0; field < nfields; field++)
16161640
{
16171641
if (field > 0)
1618-
archprintf(fout, ", ");
1642+
archputs(", ", fout);
16191643
if (PQgetisnull(res, tuple, field))
16201644
{
1621-
archprintf(fout, "NULL");
1645+
archputs("NULL", fout);
16221646
continue;
16231647
}
16241648

@@ -1647,7 +1671,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16471671
const char *s = PQgetvalue(res, tuple, field);
16481672

16491673
if (strspn(s, "0123456789 +-eE.") == strlen(s))
1650-
archprintf(fout, "%s", s);
1674+
archputs(s, fout);
16511675
else
16521676
archprintf(fout, "'%s'", s);
16531677
}
@@ -1661,9 +1685,9 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16611685

16621686
case BOOLOID:
16631687
if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
1664-
archprintf(fout, "true");
1688+
archputs("true", fout);
16651689
else
1666-
archprintf(fout, "false");
1690+
archputs("false", fout);
16671691
break;
16681692

16691693
default:
@@ -1676,7 +1700,7 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16761700
break;
16771701
}
16781702
}
1679-
archprintf(fout, ");\n");
1703+
archputs(");\n", fout);
16801704
}
16811705

16821706
if (PQntuples(res) <= 0)
@@ -1687,11 +1711,14 @@ dumpTableData_insert(Archive *fout, void *dcontext)
16871711
PQclear(res);
16881712
}
16891713

1690-
archprintf(fout, "\n\n");
1714+
archputs("\n\n", fout);
16911715

16921716
ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
16931717

16941718
destroyPQExpBuffer(q);
1719+
if (insertStmt != NULL)
1720+
destroyPQExpBuffer(insertStmt);
1721+
16951722
return 1;
16961723
}
16971724

0 commit comments

Comments
 (0)