← ClaudeAtlas

bgworker-and-extensionslisted

Register a PostgreSQL background worker or layer extension hooks on _PG_init — covers RegisterBackgroundWorker (static at shared_preload_libraries time) vs RegisterDynamicBackgroundWorker (runtime), the BackgroundWorker struct (bgw_flags BGWORKER_SHMEM_ACCESS / BGWORKER_BACKEND_DATABASE_CONNECTION, bgw_start_time, bgw_restart_time, bgw_main_arg, bgw_notify_pid), BackgroundWorkerInitializeConnection, the signal handler skeleton (SignalHandlerForConfigReload + die), the WaitLatch idiom with WL_EXIT_ON_PM_DEATH, and the GetBackgroundWorkerPid / WaitForBackgroundWorkerStartup / TerminateBackgroundWorker lifecycle calls. Use whenever a PG patch or extension registers a bgworker, writes a worker_main function, debugs a worker that fails to start or restart, layers ProcessUtility_hook / planner_hook / ExecutorStart_hook on _PG_init, or coordinates bgw_notify_pid signaling. Skip for Celery / Sidekiq / RQ / Resque / Kafka-consumer worker pools, AWS Lambda / Cloud Run background jobs, Cron / systemd timers, OS-level da
matejformanek/postgres-claude · ★ 0 · AI & Automation · score 70
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