Skip to content
pg_background

pg_background

pg_background : Run SQL queries in the background

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1110
pg_background
pg_background
2.0.2
TIME
GPL-3.0
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
See Also
pg_cron
pg_task
pg_later
pgq
timescaledb
timescaledb_toolkit
timeseries
periods

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PGDG
2.0.2
18
17
16
15
14
pg_background-
RPM
PGDG
2.0.2
18
17
16
15
14
pg_background_$v-
DEB
PGDG
2.0.2
18
17
16
15
14
postgresql-$v-pg-background-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
el8.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
el9.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
el9.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
el10.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
el10.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
d12.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
d12.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
d13.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
d13.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
u22.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
u22.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
u24.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
u24.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
u26.x86_64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
u26.aarch64
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PGDG 2.0.2
PackageVersionOSORGSIZEFile URL
pg_background_182.0.2el8.x86_64pgdg65.0 KiBpg_background_18-2.0.2-1PGDG.rhel8.10.x86_64.rpm
pg_background_182.0el8.x86_64pigsty63.7 KiBpg_background_18-2.0-1PIGSTY.el8.x86_64.rpm
pg_background_182.0el8.x86_64pgdg63.1 KiBpg_background_18-2.0-1PGDG.rhel8.10.x86_64.rpm
pg_background_181.9.3el8.x86_64pgdg58.5 KiBpg_background_18-1.9.3-1PGDG.rhel8.10.x86_64.rpm
pg_background_181.9.2el8.x86_64pgdg55.4 KiBpg_background_18-1.9.2-1PGDG.rhel8.10.x86_64.rpm
pg_background_181.9.1el8.x86_64pgdg55.1 KiBpg_background_18-1.9.1-1PGDG.rhel8.10.x86_64.rpm
pg_background_181.6el8.x86_64pgdg41.8 KiBpg_background_18-1.6-1PGDG.rhel8.10.x86_64.rpm
pg_background_181.5el8.x86_64pgdg22.5 KiBpg_background_18-1.5-1PGDG.rhel8.x86_64.rpm
pg_background_182.0.2el8.aarch64pgdg63.8 KiBpg_background_18-2.0.2-1PGDG.rhel8.10.aarch64.rpm
pg_background_182.0el8.aarch64pigsty62.5 KiBpg_background_18-2.0-1PIGSTY.el8.aarch64.rpm
pg_background_182.0el8.aarch64pgdg61.8 KiBpg_background_18-2.0-1PGDG.rhel8.10.aarch64.rpm
pg_background_181.9.3el8.aarch64pgdg57.3 KiBpg_background_18-1.9.3-1PGDG.rhel8.10.aarch64.rpm
pg_background_181.9.2el8.aarch64pgdg54.1 KiBpg_background_18-1.9.2-1PGDG.rhel8.10.aarch64.rpm
pg_background_181.9.1el8.aarch64pgdg53.9 KiBpg_background_18-1.9.1-1PGDG.rhel8.10.aarch64.rpm
pg_background_181.8el8.aarch64pgdg45.8 KiBpg_background_18-1.8-1PGDG.rhel8.10.aarch64.rpm
pg_background_181.6el8.aarch64pgdg40.7 KiBpg_background_18-1.6-1PGDG.rhel8.10.aarch64.rpm
pg_background_181.5el8.aarch64pgdg22.0 KiBpg_background_18-1.5-1PGDG.rhel8.aarch64.rpm
pg_background_182.0.2el9.x86_64pgdg63.1 KiBpg_background_18-2.0.2-1PGDG.rhel9.8.x86_64.rpm
pg_background_182.0.2el9.x86_64pgdg63.1 KiBpg_background_18-2.0.2-1PGDG.rhel9.7.x86_64.rpm
pg_background_182.0.2el9.x86_64pgdg63.2 KiBpg_background_18-2.0.2-1PGDG.rhel9.6.x86_64.rpm
pg_background_182.0el9.x86_64pigsty62.0 KiBpg_background_18-2.0-1PIGSTY.el9.x86_64.rpm
pg_background_182.0el9.x86_64pgdg61.4 KiBpg_background_18-2.0-1PGDG.rhel9.8.x86_64.rpm
pg_background_182.0el9.x86_64pgdg61.4 KiBpg_background_18-2.0-1PGDG.rhel9.7.x86_64.rpm
pg_background_182.0el9.x86_64pgdg61.5 KiBpg_background_18-2.0-1PGDG.rhel9.6.x86_64.rpm
pg_background_181.9.3el9.x86_64pgdg57.4 KiBpg_background_18-1.9.3-1PGDG.rhel9.8.x86_64.rpm
pg_background_181.9.3el9.x86_64pgdg57.4 KiBpg_background_18-1.9.3-1PGDG.rhel9.7.x86_64.rpm
pg_background_181.9.3el9.x86_64pgdg57.5 KiBpg_background_18-1.9.3-1PGDG.rhel9.6.x86_64.rpm
pg_background_181.9.2el9.x86_64pgdg54.3 KiBpg_background_18-1.9.2-1PGDG.rhel9.7.x86_64.rpm
pg_background_181.9.2el9.x86_64pgdg54.5 KiBpg_background_18-1.9.2-1PGDG.rhel9.6.x86_64.rpm
pg_background_181.9.1el9.x86_64pgdg54.0 KiBpg_background_18-1.9.1-1PGDG.rhel9.7.x86_64.rpm
pg_background_181.9.1el9.x86_64pgdg54.0 KiBpg_background_18-1.9.1-1PGDG.rhel9.6.x86_64.rpm
pg_background_181.8el9.x86_64pgdg46.5 KiBpg_background_18-1.8-1PGDG.rhel9.7.x86_64.rpm
pg_background_181.8el9.x86_64pgdg46.6 KiBpg_background_18-1.8-1PGDG.rhel9.6.x86_64.rpm
pg_background_181.6el9.x86_64pgdg41.3 KiBpg_background_18-1.6-1PGDG.rhel9.7.x86_64.rpm
pg_background_181.6el9.x86_64pgdg41.4 KiBpg_background_18-1.6-1PGDG.rhel9.6.x86_64.rpm
pg_background_181.5el9.x86_64pgdg22.7 KiBpg_background_18-1.5-1PGDG.rhel9.x86_64.rpm
pg_background_182.0.2el9.aarch64pgdg62.6 KiBpg_background_18-2.0.2-1PGDG.rhel9.8.aarch64.rpm
pg_background_182.0.2el9.aarch64pgdg62.6 KiBpg_background_18-2.0.2-1PGDG.rhel9.7.aarch64.rpm
pg_background_182.0.2el9.aarch64pgdg62.7 KiBpg_background_18-2.0.2-1PGDG.rhel9.6.aarch64.rpm
pg_background_182.0el9.aarch64pigsty61.2 KiBpg_background_18-2.0-1PIGSTY.el9.aarch64.rpm
pg_background_182.0el9.aarch64pgdg60.8 KiBpg_background_18-2.0-1PGDG.rhel9.8.aarch64.rpm
pg_background_182.0el9.aarch64pgdg60.8 KiBpg_background_18-2.0-1PGDG.rhel9.7.aarch64.rpm
pg_background_182.0el9.aarch64pgdg60.9 KiBpg_background_18-2.0-1PGDG.rhel9.6.aarch64.rpm
pg_background_181.9.3el9.aarch64pgdg56.4 KiBpg_background_18-1.9.3-1PGDG.rhel9.8.aarch64.rpm
pg_background_181.9.3el9.aarch64pgdg56.4 KiBpg_background_18-1.9.3-1PGDG.rhel9.7.aarch64.rpm
pg_background_181.9.3el9.aarch64pgdg56.5 KiBpg_background_18-1.9.3-1PGDG.rhel9.6.aarch64.rpm
pg_background_181.9.2el9.aarch64pgdg53.4 KiBpg_background_18-1.9.2-1PGDG.rhel9.7.aarch64.rpm
pg_background_181.9.2el9.aarch64pgdg53.5 KiBpg_background_18-1.9.2-1PGDG.rhel9.6.aarch64.rpm
pg_background_181.9.1el9.aarch64pgdg53.1 KiBpg_background_18-1.9.1-1PGDG.rhel9.7.aarch64.rpm
pg_background_181.9.1el9.aarch64pgdg53.2 KiBpg_background_18-1.9.1-1PGDG.rhel9.6.aarch64.rpm
pg_background_181.8el9.aarch64pgdg45.4 KiBpg_background_18-1.8-1PGDG.rhel9.7.aarch64.rpm
pg_background_181.8el9.aarch64pgdg45.5 KiBpg_background_18-1.8-1PGDG.rhel9.6.aarch64.rpm
pg_background_181.6el9.aarch64pgdg40.3 KiBpg_background_18-1.6-1PGDG.rhel9.7.aarch64.rpm
pg_background_181.6el9.aarch64pgdg40.5 KiBpg_background_18-1.6-1PGDG.rhel9.6.aarch64.rpm
pg_background_181.5el9.aarch64pgdg22.0 KiBpg_background_18-1.5-1PGDG.rhel9.aarch64.rpm
pg_background_182.0.2el10.x86_64pgdg63.7 KiBpg_background_18-2.0.2-1PGDG.rhel10.2.x86_64.rpm
pg_background_182.0.2el10.x86_64pgdg63.7 KiBpg_background_18-2.0.2-1PGDG.rhel10.1.x86_64.rpm
pg_background_182.0.2el10.x86_64pgdg64.0 KiBpg_background_18-2.0.2-1PGDG.rhel10.0.x86_64.rpm
pg_background_182.0el10.x86_64pigsty62.2 KiBpg_background_18-2.0-1PIGSTY.el10.x86_64.rpm
pg_background_182.0el10.x86_64pgdg61.9 KiBpg_background_18-2.0-1PGDG.rhel10.2.x86_64.rpm
pg_background_182.0el10.x86_64pgdg61.9 KiBpg_background_18-2.0-1PGDG.rhel10.1.x86_64.rpm
pg_background_182.0el10.x86_64pgdg62.3 KiBpg_background_18-2.0-1PGDG.rhel10.0.x86_64.rpm
pg_background_181.9.3el10.x86_64pgdg57.6 KiBpg_background_18-1.9.3-1PGDG.rhel10.2.x86_64.rpm
pg_background_181.9.3el10.x86_64pgdg57.6 KiBpg_background_18-1.9.3-1PGDG.rhel10.1.x86_64.rpm
pg_background_181.9.3el10.x86_64pgdg57.9 KiBpg_background_18-1.9.3-1PGDG.rhel10.0.x86_64.rpm
pg_background_181.9.2el10.x86_64pgdg54.5 KiBpg_background_18-1.9.2-1PGDG.rhel10.1.x86_64.rpm
pg_background_181.9.2el10.x86_64pgdg54.9 KiBpg_background_18-1.9.2-1PGDG.rhel10.0.x86_64.rpm
pg_background_181.9.1el10.x86_64pgdg54.2 KiBpg_background_18-1.9.1-1PGDG.rhel10.1.x86_64.rpm
pg_background_181.9.1el10.x86_64pgdg54.7 KiBpg_background_18-1.9.1-1PGDG.rhel10.0.x86_64.rpm
pg_background_181.8el10.x86_64pgdg46.8 KiBpg_background_18-1.8-1PGDG.rhel10.1.x86_64.rpm
pg_background_181.8el10.x86_64pgdg47.2 KiBpg_background_18-1.8-1PGDG.rhel10.0.x86_64.rpm
pg_background_181.6el10.x86_64pgdg41.6 KiBpg_background_18-1.6-1PGDG.rhel10.1.x86_64.rpm
pg_background_181.6el10.x86_64pgdg42.0 KiBpg_background_18-1.6-1PGDG.rhel10.0.x86_64.rpm
pg_background_181.5el10.x86_64pgdg23.1 KiBpg_background_18-1.5-1PGDG.rhel10.x86_64.rpm
pg_background_182.0.2el10.aarch64pgdg63.2 KiBpg_background_18-2.0.2-1PGDG.rhel10.2.aarch64.rpm
pg_background_182.0.2el10.aarch64pgdg63.2 KiBpg_background_18-2.0.2-1PGDG.rhel10.1.aarch64.rpm
pg_background_182.0.2el10.aarch64pgdg63.2 KiBpg_background_18-2.0.2-1PGDG.rhel10.0.aarch64.rpm
pg_background_182.0el10.aarch64pigsty61.6 KiBpg_background_18-2.0-1PIGSTY.el10.aarch64.rpm
pg_background_182.0el10.aarch64pgdg61.3 KiBpg_background_18-2.0-1PGDG.rhel10.2.aarch64.rpm
pg_background_182.0el10.aarch64pgdg61.3 KiBpg_background_18-2.0-1PGDG.rhel10.1.aarch64.rpm
pg_background_182.0el10.aarch64pgdg61.3 KiBpg_background_18-2.0-1PGDG.rhel10.0.aarch64.rpm
pg_background_181.9.3el10.aarch64pgdg56.8 KiBpg_background_18-1.9.3-1PGDG.rhel10.2.aarch64.rpm
pg_background_181.9.3el10.aarch64pgdg56.8 KiBpg_background_18-1.9.3-1PGDG.rhel10.1.aarch64.rpm
pg_background_181.9.3el10.aarch64pgdg56.8 KiBpg_background_18-1.9.3-1PGDG.rhel10.0.aarch64.rpm
pg_background_181.9.2el10.aarch64pgdg53.8 KiBpg_background_18-1.9.2-1PGDG.rhel10.1.aarch64.rpm
pg_background_181.9.2el10.aarch64pgdg53.8 KiBpg_background_18-1.9.2-1PGDG.rhel10.0.aarch64.rpm
pg_background_181.9.1el10.aarch64pgdg53.5 KiBpg_background_18-1.9.1-1PGDG.rhel10.1.aarch64.rpm
pg_background_181.9.1el10.aarch64pgdg53.5 KiBpg_background_18-1.9.1-1PGDG.rhel10.0.aarch64.rpm
pg_background_181.8el10.aarch64pgdg45.9 KiBpg_background_18-1.8-1PGDG.rhel10.1.aarch64.rpm
pg_background_181.8el10.aarch64pgdg45.9 KiBpg_background_18-1.8-1PGDG.rhel10.0.aarch64.rpm
pg_background_181.6el10.aarch64pgdg40.7 KiBpg_background_18-1.6-1PGDG.rhel10.1.aarch64.rpm
pg_background_181.6el10.aarch64pgdg40.7 KiBpg_background_18-1.6-1PGDG.rhel10.0.aarch64.rpm
pg_background_181.5el10.aarch64pgdg22.6 KiBpg_background_18-1.5-1PGDG.rhel10.aarch64.rpm
postgresql-18-pg-background2.0.2d12.x86_64pgdg67.6 KiBpostgresql-18-pg-background_2.0.2-1.pgdg12+1_amd64.deb
postgresql-18-pg-background2.0d12.x86_64pgdg67.9 KiBpostgresql-18-pg-background_2.0-2.pgdg12+1_amd64.deb
postgresql-18-pg-background2.0d12.x86_64pigsty100.9 KiBpostgresql-18-pg-background_2.0-1PIGSTY~bookworm_amd64.deb
postgresql-18-pg-background1.9.2d12.x86_64pgdg58.9 KiBpostgresql-18-pg-background_1.9.2-1.pgdg12+1_amd64.deb
postgresql-18-pg-background2.0.2d12.aarch64pgdg66.3 KiBpostgresql-18-pg-background_2.0.2-1.pgdg12+1_arm64.deb
postgresql-18-pg-background2.0d12.aarch64pgdg66.3 KiBpostgresql-18-pg-background_2.0-2.pgdg12+1_arm64.deb
postgresql-18-pg-background2.0d12.aarch64pigsty99.4 KiBpostgresql-18-pg-background_2.0-1PIGSTY~bookworm_arm64.deb
postgresql-18-pg-background1.9.2d12.aarch64pgdg57.0 KiBpostgresql-18-pg-background_1.9.2-1.pgdg12+1_arm64.deb
postgresql-18-pg-background2.0.2d13.x86_64pgdg68.0 KiBpostgresql-18-pg-background_2.0.2-1.pgdg13+1_amd64.deb
postgresql-18-pg-background2.0d13.x86_64pgdg68.0 KiBpostgresql-18-pg-background_2.0-2.pgdg13+1_amd64.deb
postgresql-18-pg-background2.0d13.x86_64pigsty101.3 KiBpostgresql-18-pg-background_2.0-1PIGSTY~trixie_amd64.deb
postgresql-18-pg-background1.9.2d13.x86_64pgdg58.7 KiBpostgresql-18-pg-background_1.9.2-1.pgdg13+1_amd64.deb
postgresql-18-pg-background2.0.2d13.aarch64pgdg66.5 KiBpostgresql-18-pg-background_2.0.2-1.pgdg13+1_arm64.deb
postgresql-18-pg-background2.0d13.aarch64pgdg66.5 KiBpostgresql-18-pg-background_2.0-2.pgdg13+1_arm64.deb
postgresql-18-pg-background2.0d13.aarch64pigsty99.7 KiBpostgresql-18-pg-background_2.0-1PIGSTY~trixie_arm64.deb
postgresql-18-pg-background1.9.2d13.aarch64pgdg57.3 KiBpostgresql-18-pg-background_1.9.2-1.pgdg13+1_arm64.deb
postgresql-18-pg-background2.0.2u22.x86_64pgdg69.1 KiBpostgresql-18-pg-background_2.0.2-1.pgdg22.04+1_amd64.deb
postgresql-18-pg-background2.0u22.x86_64pgdg69.2 KiBpostgresql-18-pg-background_2.0-2.pgdg22.04+1_amd64.deb
postgresql-18-pg-background2.0u22.x86_64pigsty107.7 KiBpostgresql-18-pg-background_2.0-1PIGSTY~jammy_amd64.deb
postgresql-18-pg-background1.9.2u22.x86_64pgdg59.7 KiBpostgresql-18-pg-background_1.9.2-1.pgdg22.04+1_amd64.deb
postgresql-18-pg-background2.0.2u22.aarch64pgdg67.5 KiBpostgresql-18-pg-background_2.0.2-1.pgdg22.04+1_arm64.deb
postgresql-18-pg-background2.0u22.aarch64pgdg67.4 KiBpostgresql-18-pg-background_2.0-2.pgdg22.04+1_arm64.deb
postgresql-18-pg-background2.0u22.aarch64pigsty105.9 KiBpostgresql-18-pg-background_2.0-1PIGSTY~jammy_arm64.deb
postgresql-18-pg-background1.9.2u22.aarch64pgdg58.0 KiBpostgresql-18-pg-background_1.9.2-1.pgdg22.04+1_arm64.deb
postgresql-18-pg-background2.0.2u24.x86_64pgdg68.2 KiBpostgresql-18-pg-background_2.0.2-1.pgdg24.04+1_amd64.deb
postgresql-18-pg-background2.0u24.x86_64pgdg68.1 KiBpostgresql-18-pg-background_2.0-2.pgdg24.04+1_amd64.deb
postgresql-18-pg-background2.0u24.x86_64pigsty104.3 KiBpostgresql-18-pg-background_2.0-1PIGSTY~noble_amd64.deb
postgresql-18-pg-background1.9.2u24.x86_64pgdg58.8 KiBpostgresql-18-pg-background_1.9.2-1.pgdg24.04+1_amd64.deb
postgresql-18-pg-background2.0.2u24.aarch64pgdg66.2 KiBpostgresql-18-pg-background_2.0.2-1.pgdg24.04+1_arm64.deb
postgresql-18-pg-background2.0u24.aarch64pgdg66.2 KiBpostgresql-18-pg-background_2.0-2.pgdg24.04+1_arm64.deb
postgresql-18-pg-background2.0u24.aarch64pigsty103.1 KiBpostgresql-18-pg-background_2.0-1PIGSTY~noble_arm64.deb
postgresql-18-pg-background1.9.2u24.aarch64pgdg57.2 KiBpostgresql-18-pg-background_1.9.2-1.pgdg24.04+1_arm64.deb
postgresql-18-pg-background2.0.2u26.x86_64pgdg67.4 KiBpostgresql-18-pg-background_2.0.2-1.pgdg26.04+1_amd64.deb
postgresql-18-pg-background2.0u26.x86_64pgdg67.5 KiBpostgresql-18-pg-background_2.0-2.pgdg26.04+1_amd64.deb
postgresql-18-pg-background2.0u26.x86_64pigsty103.4 KiBpostgresql-18-pg-background_2.0-1PIGSTY~resolute_amd64.deb
postgresql-18-pg-background1.9.2u26.x86_64pgdg58.4 KiBpostgresql-18-pg-background_1.9.2-1.pgdg26.04+1_amd64.deb
postgresql-18-pg-background2.0.2u26.aarch64pgdg65.8 KiBpostgresql-18-pg-background_2.0.2-1.pgdg26.04+1_arm64.deb
postgresql-18-pg-background2.0u26.aarch64pgdg65.7 KiBpostgresql-18-pg-background_2.0-2.pgdg26.04+1_arm64.deb
postgresql-18-pg-background2.0u26.aarch64pigsty102.4 KiBpostgresql-18-pg-background_2.0-1PIGSTY~resolute_arm64.deb
postgresql-18-pg-background1.9.2u26.aarch64pgdg56.3 KiBpostgresql-18-pg-background_1.9.2-1.pgdg26.04+1_arm64.deb

Source

pig build pkg pg_background;		# build rpm/deb

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install pg_background;		# install via package name, for the active PG version

pig install pg_background -v 18;   # install for PG 18
pig install pg_background -v 17;   # install for PG 17
pig install pg_background -v 16;   # install for PG 16
pig install pg_background -v 15;   # install for PG 15
pig install pg_background -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION pg_background;

Usage

Sources: official README, v2.0 release notes, migration guide.

pg_background executes SQL inside PostgreSQL background worker processes. Workers run independent transactions inside the server, which is useful for asynchronous maintenance, autonomous side effects, bounded long-running tasks, and progress-tracked jobs.

Version 2.0 makes the unsuffixed API canonical. The old _v2 names remain deprecated aliases through the 2.x line, but new code should use names such as pg_background_launch, pg_background_result, and pg_background_run.

One-Shot Execution

Use pg_background_run when the SQL has side effects and you only need execution metadata:

CREATE EXTENSION pg_background;

SELECT completed, has_error, sqlstate, error_message,
       row_count, command_tag, elapsed_ms, timed_out
FROM pg_background_run(
  'INSERT INTO audit_log(ts, who) VALUES (clock_timestamp(), current_user)',
  queue_size := 0,
  timeout_ms := 30000,
  label := 'audit-login'
);

Launch And Fetch Results

Use the launch/result pattern when the background SQL returns rows:

SELECT * FROM pg_background_launch(
  'SELECT count(*) FROM large_table',
  queue_size := 65536,
  label := 'count-large-table'
) AS h;

SELECT * FROM pg_background_result(h.pid, h.cookie) AS (count bigint);

Results can be consumed once. Keep both pid and cookie; the cookie protects later calls from PID reuse.

Fire And Forget

For side effects where no result rows need to be consumed:

SELECT * FROM pg_background_submit(
  $$VACUUM (ANALYZE) public.events$$,
  queue_size := 65536,
  label := 'vacuum-events'
);

Core API

  • pg_background_launch(sql, queue_size, label) launches a worker and returns pg_background_handle(pid, cookie).
  • pg_background_submit(sql, queue_size, label) launches fire-and-forget work and returns a handle.
  • pg_background_result(pid, cookie) consumes result rows once.
  • pg_background_result_info(pid, cookie) returns completion and row-count metadata without consuming rows.
  • pg_background_error_info(pid, cookie) returns structured SQL error details.
  • pg_background_wait(pid, cookie, timeout_ms DEFAULT 0) waits for completion; timeout_ms <= 0 blocks indefinitely.
  • pg_background_cancel(pid, cookie, grace_ms DEFAULT 0) requests cooperative cancellation.
  • pg_background_detach(pid, cookie) stops tracking a worker while letting it continue.
  • pg_background_outcome(pid, cookie) returns a combined status snapshot without raising on missing state.
  • pg_background_list and pg_background_activity are monitoring views; pg_background_stats() returns session counters.

Convenience helpers include pg_background_run_query, pg_background_drain, pg_background_wait_any, pg_background_cancel_by_label, and pg_background_purge.

Progress Reporting

Report progress from inside the worker SQL, then poll it from the launcher:

SELECT * FROM pg_background_launch($$
  SELECT pg_background_report_progress(0, 'starting');
  SELECT pg_sleep(1);
  SELECT pg_background_report_progress(100, 'done');
$$) AS h;

SELECT * FROM pg_background_get_progress(h.pid, h.cookie);

pg_background_report_progress is the 2.0 name; the earlier pg_background_progress name was hard-renamed.

GUCs And Loading

pg_background does not require shared_preload_libraries. Preloading is optional and mainly useful when you want its GUCs available before the extension is first loaded in a session.

SET pg_background.max_workers = 10;
SET pg_background.default_queue_size = '256KB';
SET pg_background.worker_timeout = '5min';
  • pg_background.max_workers defaults to 16.
  • pg_background.default_queue_size defaults to 65536 bytes.
  • pg_background.worker_timeout defaults to 0, meaning no execution timeout.

Caveats

  • Pigsty packages pg_background 2.0 for PostgreSQL 14-18; upstream 2.0 also validates PostgreSQL 19 beta.
  • Upgrades from pre-1.8 installs must first reach the 1.8/1.10 release line before updating to 2.0.
  • The original v1 PID-only API was removed. Unsuffixed names now have cookie-protected semantics and return/use (pid, cookie).
  • pg_background_cancel_v2_grace and pg_background_wait_v2_timeout are folded into pg_background_cancel(..., grace_ms) and pg_background_wait(..., timeout_ms).
  • pg_background_status_v2 was removed; use pg_background_outcome(pid, cookie).
Last updated on