skip to content

In PostgreSQL, how do you follow a long COPY FROM load with pg_stat_progress_copy, and what do the pg_stat_progress views not tell you?

level: middleimportance: nice to knowfreq 24%

answer

  1. one row per running command
  2. join on pid
  3. bytes_total is zero from STDIN
  4. counters, not an ETA
  5. row vanishes when it ends

basics

~10 s

While a COPY runs, pg_stat_progress_copy has one row for its backend with bytes_processed, bytes_total and tuples_processed; join it to pg_stat_activity on pid. Progress views show live counters only: no time estimate and no history.

solid answer

~40 s

PostgreSQL reports progress for `ANALYZE`, `CLUSTER`/`VACUUM FULL`, `CREATE INDEX`, `VACUUM`, `COPY` and base backups, each in its own `pg_stat_progress_*` view with one row per backend running the command. For a load, `pg_stat_progress_copy` gives `command`, `type` (`FILE`, `PROGRAM`, `PIPE`, `CALLBACK`), `bytes_processed`, `bytes_total`, `tuples_processed` and `tuples_excluded`. A percentage needs `bytes_total`, which is 0 when the size is unknown, as with psql's `\copy`, which sends `COPY FROM STDIN`. Joining on `pid` to `pg_stat_activity` adds the wait columns, so flat counters plus a `Lock` wait mean the load is blocked. The views give no ETA, keep no history once the command ends, and say nothing about visibility: the copied rows are invisible to others until the `COPY` commits.

code

sql · 10 lines
sql
SELECT p.pid, p.command, p.type,
       pg_size_pretty(p.bytes_processed) AS done,
       pg_size_pretty(p.bytes_total)     AS total,
       round(100.0 * p.bytes_processed
             / nullif(p.bytes_total, 0), 1) AS pct,
       p.tuples_processed,
       a.wait_event_type, a.wait_event,
       now() - a.query_start AS running_for
FROM pg_stat_progress_copy p
JOIN pg_stat_activity a USING (pid);

go deeper

for a junior

Recall that long commands such as COPY, ANALYZE and VACUUM have pg_stat_progress views with one row per running backend.

for a middle

Explain how to compute COPY progress from bytes_processed and bytes_total, why bytes_total is 0 for a \copy load, and why you join on pid to pg_stat_activity.

for a senior

Show that you read stalled counters together with wait events to tell a blocked load from a slow one, and estimate time left by sampling a rate.

for a principal

Discuss monitoring batch-load windows end to end, combining progress sampling with alerting and logs, given that progress views keep no history.

## Which commands report progress PostgreSQL reports live progress for a handful of long-running commands, each in its own view named `pg_stat_progress_<command>`. Every view has **one row per backend currently running that command**, keyed by `pid`, and the row disappears when the command ends. | View | Reports on | Covered in | |---|---|---| | `pg_stat_progress_copy` | `COPY` | here | | `pg_stat_progress_analyze` | `ANALYZE` | here | | `pg_stat_progress_basebackup` | `BASE_BACKUP`, which `pg_basebackup` runs | here | | `pg_stat_progress_vacuum` | `VACUUM` | the autovacuum topic | | `pg_stat_progress_cluster` | `CLUSTER`, `VACUUM FULL` | the bloat topic | | `pg_stat_progress_create_index` | `CREATE INDEX`, `REINDEX` | the index-upkeep topic | They share a pattern: identity columns (`pid`, `datid`, `datname`, usually `relid`), often a `phase` text column, and counters that grow as the command works. For `ANALYZE`, for example, `phase` moves through values such as `acquiring sample rows`, `computing statistics` and `finalizing analyze`, and `sample_blks_scanned` against `sample_blks_total` shows how much of the sample has been read; on an inheritance or partitioned parent, `child_tables_done` against `child_tables_total` counts the children. A base backup's row compares `backup_streamed` with `backup_total`, which is only an estimate and is NULL when `pg_basebackup` skips the size estimate. ## Reading pg_stat_progress_copy Consider the retailer's nightly load of its product catalogue into a staging table: a multi-gigabyte `COPY FROM` that sometimes overruns its window. Its row reports: | Column | Meaning | |---|---| | `command` | `COPY FROM` or `COPY TO` | | `type` | `FILE`, `PROGRAM`, `PIPE` (for `COPY FROM STDIN` and `COPY TO STDOUT`) or `CALLBACK` (for example, logical replication's initial table copy) | | `relid` | The table being copied, or 0 when copying from a `SELECT` query | | `bytes_processed` | Bytes handled so far | | `bytes_total` | Size of the source file for `COPY FROM`, or 0 if not available | | `tuples_processed` | Rows handled so far | | `tuples_excluded` | Rows dropped by the `COPY`'s `WHERE` clause | A **percentage** is `bytes_processed / bytes_total`, and it exists only when the server knows the size, which in practice means `COPY FROM` a server-side file. A load run with psql's `\copy` sends the data over the connection as `COPY FROM STDIN` (`type = PIPE`), so `bytes_total` is 0 and progress has to be judged from `tuples_processed` against the row count you expect. ## Join it to pg_stat_activity The progress view says how far the command has got; `pg_stat_activity` says what it is doing right now. Joining the two on `pid` adds `wait_event_type`, `wait_event`, `query_start` and the `query` text: - counters rising and no wait: the load is running; - counters flat and `wait_event_type = 'Lock'`: it is blocked, and `pg_blocking_pids()` names by whom; - counters flat and `Client` / `ClientRead` on a `COPY FROM STDIN`: the server is waiting for the client to send more data; - counters rising slowly with `IO` waits or `LWLock` waits such as `WALWrite`: storage or WAL is the limit. ## What progress views do not tell you - **No estimate of completion time.** They expose counters and phases, not an ETA; you derive a rate by sampling. - **No history.** The row vanishes when the command ends, whether it succeeded or failed, so last night's run cannot be inspected afterwards; the server log is where that lives. - **Nothing about visibility.** `tuples_processed` counts rows written so far, but the `COPY` runs in one transaction and none of those rows are visible to other sessions until it commits. - **Not every command.** Only the commands listed above report progress. A long `SELECT` or `UPDATE` has no progress view; `pg_stat_activity` and its wait events are all you get. - **Nothing when tracking is off.** Progress rows are written only when `track_activities` is on, which is the default. ## Estimating time left 1. Read `bytes_processed` (or `tuples_processed`) together with `now()`. 2. Read both again a minute later. 3. Divide the remaining work by the observed rate, remembering that the rate can change as the table's indexes grow or WAL flushing backs up.

  • A PostgreSQL COPY's tuples_processed stops rising for minutes; how do you tell what it is waiting for?
    Join `pg_stat_progress_copy` to `pg_stat_activity` on `pid` and read the wait columns. `Lock` means it is blocked by another session, which `pg_blocking_pids()` names. `Client` / `ClientRead` on a `COPY FROM STDIN` means the client has stopped sending data. `IO` or `LWLock` waits such as `WALWrite` point at storage or WAL throughput.
  • Why can't you look up how long last night's catalogue COPY took in pg_stat_progress_copy?
    Progress rows exist only while the command runs and disappear when it ends, whether it succeeded or failed. Durations of finished statements come from the server log (for example `log_min_duration_statement`) or from `pg_stat_statements`, both covered by the query-statistics topic.

  • pg_stat_progress_copy keeps a row after the COPY ends, so last night's load is visible.
  • Rows counted in tuples_processed are already visible to other sessions.
  • bytes_total is always filled, so a percentage is always available.
  • The progress views report an estimated time to completion.