Skip to content

Commit 06ba4a6

Browse files
committed
Use COPY FREEZE in pgbench for faster benchmark table population.
While populating the pgbench_accounts table, plain COPY was unconditionally used. By changing it to COPY FREEZE, the time for VACUUM is significantly reduced, thus the total time of "pgbench -i" is also reduced. This only happens if pgbench runs against PostgreSQL 14 or later because COPY FREEZE in previous versions of PostgreSQL does not bring the benefit. Also if partitioning is used, COPY FREEZE cannot be used. In this case plain COPY will be used too. Author: Tatsuo Ishii Discussion: https://postgr.es/m/20210308.143907.2014279678657453983.t-ishii@gmail.com Reviewed-by: Fabien COELHO, Laurenz Albe, Peter Geoghegan, Dean Rasheed
1 parent 469150a commit 06ba4a6

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

doc/src/sgml/ref/pgbench.sgml

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
220220
data is generated in <command>pgbench</command> client and then
221221
sent to the server. This uses the client/server bandwidth
222222
extensively through a <command>COPY</command>.
223+
<command>pgbench</command> uses the FREEZE option with 14 or later
224+
versions of <productname>PostgreSQL</productname> to speed up
225+
subsequent <command>VACUUM</command>, unless partitions are enabled.
223226
Using <literal>g</literal> causes logging to print one message
224227
every 100,000 rows while generating data for the
225228
<structname>pgbench_accounts</structname> table.

src/bin/pgbench/pgbench.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -4153,6 +4153,7 @@ initGenerateDataClientSide(PGconn *con)
41534153
PGresult *res;
41544154
int i;
41554155
int64 k;
4156+
char *copy_statement;
41564157

41574158
/* used to track elapsed time and estimate of the remaining time */
41584159
pg_time_usec_t start;
@@ -4199,7 +4200,15 @@ initGenerateDataClientSide(PGconn *con)
41994200
/*
42004201
* accounts is big enough to be worth using COPY and tracking runtime
42014202
*/
4202-
res = PQexec(con, "copy pgbench_accounts from stdin");
4203+
4204+
/* use COPY with FREEZE on v14 and later without partioning */
4205+
if (partitions == 0 && PQserverVersion(con) >= 140000)
4206+
copy_statement = "copy pgbench_accounts from stdin with (freeze on)";
4207+
else
4208+
copy_statement = "copy pgbench_accounts from stdin";
4209+
4210+
res = PQexec(con, copy_statement);
4211+
42034212
if (PQresultStatus(res) != PGRES_COPY_IN)
42044213
{
42054214
pg_log_fatal("unexpected copy in result: %s", PQerrorMessage(con));

0 commit comments

Comments
 (0)