Skip to content

fix: report worker activity to pg_stat_activity#255

Merged
utkarash2991 merged 2 commits into
masterfrom
fix/pgstat-report-activity-in-worker
May 18, 2026
Merged

fix: report worker activity to pg_stat_activity#255
utkarash2991 merged 2 commits into
masterfrom
fix/pgstat-report-activity-in-worker

Conversation

@utkarash2991
Copy link
Copy Markdown
Contributor

@utkarash2991 utkarash2991 commented May 18, 2026

Problem

The pg_net background worker doesn't call pgstat_report_activity(), so its row in pg_stat_activity shows up with blank state, query, query_start, xact_start, and state_change columns. This makes it impossible to tell from monitoring whether the worker is idle, processing a tick, or stuck in a transaction.

Regular user backends get this for free via tcop/postgres.c. Background workers must do it themselves.

Fix

Add pgstat_report_activity transitions around the inner work loop:

  • STATE_IDLE initially (worker starts by waiting for a wake)
  • STATE_RUNNING when the worker enters the inner work loop
  • STATE_IDLE when the inner loop drains and the worker goes back to waiting

cmd_str = NULL, matching what other background workers do (logical replication apply worker, walsender on idle). backend_type already identifies the row as "pg_net <ver> worker", so a query-column label would be redundant.

Verification

Sample output before/after on a local repro:

Before (backend_type ilike '%pg_net%'):

 pid | backend_type         | state | wait_event
-----+----------------------+-------+-----------
 218 | pg_net 0.20.2 worker |       | Extension

After, at rest:

 pid | backend_type         | state | wait_event
-----+----------------------+-------+-----------
 218 | pg_net 0.20.2 worker | idle  | Extension

After, while processing a tick:

 pid | backend_type         | state  | wait_event
-----+----------------------+--------+-----------
 218 | pg_net 0.20.2 worker | active | Extension

Refs PSQL-1221, follow-up to #254.

Background workers must call pgstat_report_activity() themselves to
update state/state_change in pg_stat_activity; regular user backends
get this for free via tcop/postgres.c. Without it the pg_net worker
shows up with a blank state column, making it impossible to tell
from monitoring whether the worker is idle, processing a tick, or
stuck in a transaction. Adds STATE_IDLE / STATE_RUNNING transitions
around the inner work loop.

cmd_str is NULL, matching what other background workers (e.g.
logical replication apply worker) do; backend_type already
identifies it as "pg_net <ver> worker" so a query-column label
would be redundant.
@steve-chavez
Copy link
Copy Markdown
Member

@utkarash2991 Can we test this somehow?

@utkarash2991
Copy link
Copy Markdown
Contributor Author

@utkarash2991 Can we test this somehow?

@steve-chavez Added a regression test in 8ec2990. It asserts pg_stat_activity.state shows 'idle' at rest and 'active' during a slow request (using delay=2 so the active window is observable). Please let me know how this looks .

@utkarash2991
Copy link
Copy Markdown
Contributor Author

@utkarash2991 Can we test this somehow?

@steve-chavez Added a regression test in 8ec2990. It asserts pg_stat_activity.state shows 'idle' at rest and 'active' during a slow request (using delay=2 so the active window is observable). Please let me know how this looks .

@steve-chavez does the changes looks good to you ? ok to merge ?

Copy link
Copy Markdown
Member

@steve-chavez steve-chavez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@utkarash2991 utkarash2991 merged commit 8347504 into master May 18, 2026
32 checks passed
@utkarash2991 utkarash2991 deleted the fix/pgstat-report-activity-in-worker branch May 18, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants