pg_retry
pg_retry
pg_retry : Retry SQL statements on transient errors with exponential backoff
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4100 | pg_retry
|
pg_retry
|
1.0.0 |
UTIL
|
PostgreSQL
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r
|
No
|
Yes
|
No
|
Yes
|
yes
|
no
|
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY
|
1.0.0 |
18
17
16
15
14
|
pg_retry |
- |
| RPM | PIGSTY
|
1.0.0 |
18
17
16
15
14
|
pg_retry_$v |
- |
| DEB | PIGSTY
|
1.0.0 |
18
17
16
15
14
|
postgresql-$v-retry |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
el8.aarch64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
el9.x86_64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
el9.aarch64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
el10.x86_64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
el10.aarch64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
d12.x86_64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
d12.aarch64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
d13.x86_64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
d13.aarch64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
u22.x86_64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
u22.aarch64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
u24.x86_64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
u24.aarch64
|
PIGSTY 1.0.0
|
PIGSTY 1.0.0
|
MISS
|
MISS
|
MISS
|
Source
pig build pkg pg_retry; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pg_retry; # install via package name, for the active PG version
pig install pg_retry -v 18; # install for PG 18
pig install pg_retry -v 17; # install for PG 17Create this extension with:
CREATE EXTENSION pg_retry;Usage
pg_retry: Retry SQL statements on transient errors with exponential backoff
Function Signature
retry.retry(
sql TEXT, -- SQL statement to run (exactly one)
max_tries INT DEFAULT 3, -- total attempts (1 + retries), >= 1
base_delay_ms INT DEFAULT 50, -- initial backoff delay in ms
max_delay_ms INT DEFAULT 1000, -- cap for exponential backoff
retry_sqlstates TEXT[] DEFAULT ARRAY['40001','40P01','55P03','57014']
) RETURNS INT -- number of rows processedDefault retryable SQLSTATEs: 40001 (serialization_failure), 40P01 (deadlock_detected), 55P03 (lock_not_available), 57014 (query_canceled).
Examples
Basic retry with defaults:
SELECT retry.retry('UPDATE accounts SET balance = balance - 100 WHERE id = 1');Custom retry parameters:
SELECT retry.retry(
'INSERT INTO audit_log (event) VALUES (''test'')',
5, -- max_tries
100, -- base_delay_ms
5000 -- max_delay_ms
);GUC Configuration
ALTER SYSTEM SET pg_retry.default_max_tries = 5;
ALTER SYSTEM SET pg_retry.default_base_delay_ms = 100;
ALTER SYSTEM SET pg_retry.default_max_delay_ms = 5000;
ALTER SYSTEM SET pg_retry.default_sqlstates = '40001,40P01,55P03,57014';
SELECT pg_reload_conf();Safety Rules
- Only one SQL statement per call (multi-statement fails)
- Transaction control statements (BEGIN, COMMIT, ROLLBACK) are prohibited
- Parameters are validated (max_tries >= 1, non-negative delays, base <= max delay)
Last updated on