// SQL snippet

Track COPY and import progress in PostgreSQL

A long COPY prints nothing until it finishes. pg_stat_progress_copy shows how far it has got while it runs.

Main statement
PostgreSQL

The task

pg_stat_progress_copy (PostgreSQL 14 and later) has one row per backend running a COPY, in either direction. For COPY FROM a server-side file it knows the file size, so bytes_processed against bytes_total gives a real percentage; for every COPY it counts tuples processed. The pid joins to pg_stat_activity for the user and start time. Client-side imports such as psql's \copy run COPY FROM STDIN on the server, so they appear here too, just without a total.

The snippet

Written for PostgreSQL.

PostgreSQL
SELECT p.pid,
       p.datname,
       a.usename,
       p.command,
       p.type,
       p.bytes_processed, p.bytes_total,
       ROUND(100.0 * p.bytes_processed / NULLIF(p.bytes_total, 0), 1) AS pct_done,
       p.tuples_processed,
       now() - a.query_start AS running_for
FROM pg_stat_progress_copy p
LEFT JOIN pg_stat_activity a ON a.pid = p.pid
ORDER BY p.bytes_processed DESC;

工作原理

command is COPY FROM or COPY TO, and type is FILE, PROGRAM, PIPE (STDIN or STDOUT) or CALLBACK (logical replication's initial table sync). bytes_total is known only for COPY FROM a file; it is 0 otherwise, and NULLIF turns the percentage into NULL instead of a division error. tuples_processed is then the progress signal. The LEFT JOIN to pg_stat_activity adds who is running it and for how long. Imports done with batched INSERT statements do not appear here at all. Index builds afterwards have their own view, pg_stat_progress_create_index, shown in the variant.

Variations

Index builds after the load: pg_stat_progress_create_index

SELECT pid, datname, relid::regclass AS table_name,
       index_relid::regclass AS index_name, command, phase,
       blocks_done, blocks_total,
       ROUND(100.0 * blocks_done / NULLIF(blocks_total, 0), 1) AS pct_of_phase
FROM pg_stat_progress_create_index
ORDER BY pid;

Questions that come up

bytes_total is 0. How do I estimate progress?

STDIN and pipe sources stream without a declared size. Compare tuples_processed with the row count you expect (wc -l on the source file is close enough for CSV without embedded newlines), or sample tuples_processed twice a minute apart and divide the remaining rows by that rate.

The COPY is slow but has a high percentage. Kill it or let it finish?

Usually let it finish, unless it is blocking something more important. A COPY is all or nothing: cancelling it rolls back every row loaded so far, and those rows stay behind as dead space until vacuum removes them. PostgreSQL 17 added ON_ERROR ignore for COPY FROM, which skips rows with data type errors instead of failing the whole load; the skipped count appears as tuples_skipped in this view.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.