How to Compare PostgreSQL 18 and PostgreSQL 19 COPY Performance

We often use the COPY command in PostgreSQL to move amounts of data quickly between tables and external files. Every major release of PostgreSQL brings improvements to parts of the database engine, and one part that is especially important to measure is data loading performance.

PostgreSQL 19 brings several changes when compared to PostgreSQL 18. Therefore, this test looks at a comparison of the COPY command performance between PostgreSQL 18.6 and PostgreSQL 19.0.

In this comparison, we will generate one million rows. Export them into CSV and text formats. Then, we will import those generated files back into PostgreSQL tables using the COPY command. The results give a picture of how PostgreSQL 19 performs compared to PostgreSQL 18 when it comes to common bulk data transfer operations.

Now, let’s benchmark the COPY command performance in more detail.

Execute the queries below in PostgreSQL version 18.

Check the version like this.

postgres=# select version();

Result:

                                                               version                                                               
-------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 18.6 (Ubuntu 18.6-1.pgdg22.04+2) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, 64-bit
(1 row)

Enable timing in psql like this.

postgres=# \timing on

Drop if there is any table named customers that already exists in the database.

postgres=# DROP TABLE IF EXISTS customers;

Result:

Time: 10.926 ms

Now, create a table named customers like this.

postgres=# CREATE TABLE customers (
    id          int,
    name        text,
    email       text,
    description text
);

Result:

CREATE TABLE
Time: 4.277 ms

Now, insert nearly 1 million values into the table named customers.

postgres=# INSERT INTO customers
SELECT i,
       'customer_' || i,
       'user' || i || '@example.com',
       repeat(md5(i::text), 8)          -- long text (256 chars)
FROM generate_series(1, 1000000) AS i;

Result:

INSERT 0 1000000
Time: 4318.526 ms (00:04.319)

Now, copy the data from this table to the file named customers in .csv format.

postgres=# COPY customers TO '/tmp/customers.csv' WITH (FORMAT csv, HEADER);

Result:

COPY 1000000
Time: 1154.067 ms (00:01.154)

Now, copy the table data into the .txt file.

postgres=# COPY customers TO '/tmp/customers.txt';

Result:

COPY 1000000
Time: 928.601 ms

Check if there is any table named customers_load that already exists, and if it exists, then drop that table.

postgres=# DROP TABLE IF EXISTS customers_load;

Result:

NOTICE:  table "customers_load" does not exist, skipping
Time: 0.594 ms

Now, create a new table named customers_load with the same structure as the customers table like this.

postgres=# CREATE TABLE customers_load (LIKE customers);

Result:

CREATE TABLE
Time: 3.205 ms

Now, execute the COPY command and check the execution time.

postgres=# COPY customers_load FROM '/tmp/customers.csv' WITH (FORMAT csv, HEADER);

Result:

COPY 1000000
Time: 2746.697 ms (00:02.747)

Now, truncate again.

postgres=# TRUNCATE customers_load;

Result:

TRUNCATE TABLE
Time: 91.759 ms

Now, load the data from the .txt file into this table named customers_load like this.

postgres=# COPY customers_load FROM '/tmp/customers.txt';

Result:

COPY 1000000
Time: 2256.998 ms (00:02.257)

Now, check the total count of records from the table named customers_load like this.

postgres=# SELECT count(*) FROM customers_load;  

Result:

  count  
---------
 1000000
(1 row)

Time: 147.215 ms

Now, test the same queries in PostgreSQL version 19.

postgres=# select version();

Result:

                                               version                                               
-----------------------------------------------------------------------------------------------------
 PostgreSQL 19.0 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0, 64-bit
(1 row)

Enable the timing.

postgres=# \timing on

Result:

Timing is on.

Create the same table and insert the same values again.

postgres=# CREATE TABLE customers (
    id          int,
    name        text,
    email       text,
    description text
);
Time: 9.995 ms
postgres=# INSERT INTO customers
SELECT i,
       'customer_' || i,
       'user' || i || '@example.com',
       repeat(md5(i::text), 8)          -- long text (256 chars)
FROM generate_series(1, 1000000) AS i;
Time: 3360.989 ms (00:03.361)

Now, copy the data from the table named customers to the file named customers_19 in .csv format.

postgres=# COPY customers TO '/tmp/customers_19.csv' WITH (FORMAT csv, HEADER);

Result:

COPY 1000000
Time: 1132.117 ms (00:01.132)

Now, copy the data to the normal.txt file like this.

postgres=# COPY customers TO '/tmp/customers_19.txt';

Result:

COPY 1000000
Time: 2073.641 ms (00:02.074)

Drop the table named customers_load, if it already exists.

postgres=# DROP TABLE IF EXISTS customers_load;

Result:

NOTICE:  table "customers_load" does not exist, skipping
Time: 0.233 ms

Create a new table like this.

postgres=# CREATE TABLE customers_load (LIKE customers);

Result:

CREATE TABLE
Time: 17.321 ms

Now, copy the data from the file to the table like this.

postgres=# COPY customers_load FROM '/tmp/customers_19.csv' WITH (FORMAT csv, HEADER);

Result:

COPY 1000000
Time: 2593.580 ms (00:02.594)

Clear all data from the table named customers_load.

postgres=# TRUNCATE customers_load;

Result:

TRUNCATE TABLE
Time: 84.670 ms

Now, import the data from the normal .txt file into the customers_load table like this.

postgres=# COPY customers_load FROM '/tmp/customers_19.txt';

Result:

COPY 1000000
Time: 1653.076 ms (00:01.653)

Now, let’s compare the copy command execution results from both postgres versions.

Operation PostgreSQL 18.6 PostgreSQL 19.0 Performance Difference
Insert 1 million rows into the customers table 4318.526 ms 3360.989 ms PostgreSQL 19 is 957.537 ms faster
COPY table data to CSV file 1154.067 ms 1132.117 ms PostgreSQL 19 is 21.950 ms faster
COPY table data to text file 928.601 ms 2073.641 ms PostgreSQL 18 is 1145.040 ms faster
COPY CSV file into table 2746.697 ms 2593.580 ms PostgreSQL 19 is 153.117 ms faster
COPY text file into table 2256.998 ms 1653.076 ms PostgreSQL 19 is 603.922 ms faster
TRUNCATE customers_load table 91.759 ms 84.670 ms PostgreSQL 19 is 7.089 ms faster

The COPY performance test between PostgreSQL 18.6 and PostgreSQL 19.0 shows that PostgreSQL 19 does better in most of the data loading cases we tried. The CSV import and the text file import both finished faster on PostgreSQL 19. So, it is a pick if you often load big datasets into tables.

Still, the test also shows that PostgreSQL 18 did better when exporting table data into a text file. That tells us the gains are not the same for every COPY operation. They can change based on the data format and the kind of workload.

From this test with one million rows, PostgreSQL 19 shows gains for bulk data loading, and its export performance is still competitive. Your own results may be different. It depends on your hardware, your storage setup, your database settings, and the size and structure of the data you work with.

WhatsApp