bgworker-and-extensionslisted
Install: claude install-skill matejformanek/postgres-claude
# bgworker-and-extensions — background workers + extension integration
This is the procedural cookbook for registering and writing PostgreSQL
background workers and for layering extension hooks on the postmaster
lifecycle. For the conceptual model see
`knowledge/idioms/bgworker-and-parallel.md`.
This skill is one of three siblings that share the `_PG_init` /
postmaster-lifecycle boundary:
- `gucs-config` — custom GUC variables.
- **bgworker-and-extensions** (this skill) — background workers + hooks.
- `parallel-query` — ParallelContext + parallel-safe markings.
## 1. Static vs dynamic registration
| API | Where to call | Restart? |
|---|---|---|
| `RegisterBackgroundWorker(&w)` | `_PG_init`, only when `process_shared_preload_libraries_in_progress` | Yes per `bgw_restart_time` |
| `RegisterDynamicBackgroundWorker(&w, &handle)` | Any backend at runtime | Yes per `bgw_restart_time`, but worker is forgotten once `handle` goes away unless re-registered |
[verified-by-code `source/src/include/postmaster/bgworker.h:122-133`]
`RegisterBackgroundWorker` errors out unless called from `_PG_init` during
shared-library preload (i.e. `process_shared_preload_libraries_in_progress`
is true). From any other site — including a regular backend at runtime —
use `RegisterDynamicBackgroundWorker` instead.
## 2. Filling the `BackgroundWorker` struct
```c
BackgroundWorker worker;
memset(&worker, 0, sizeof(worker));
snprintf(worker.bgw_name, BGW_MAXLEN, "my_ext worker %d", i);
snprintf(worke