pg_stat_backtrace
pg_stat_backtrace : Capture or log C-level stack backtraces of PostgreSQL processes
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 6030 | pg_stat_backtrace | pg_stat_backtrace | 1.0.0 |
STAT | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | pg_stat_log pg_tracing pgfr_record logerrors pgelog |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.0.0 |
18 17 16 15 14 | pg_stat_backtrace |
- |
| RPM | PIGSTY | 1.0.0 |
18 17 16 15 14 | pg_stat_backtrace_$v |
libunwind |
| DEB | PIGSTY | 1.0.0 |
18 17 16 15 14 | postgresql-$v-pg-stat-backtrace |
libunwind8 |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| el8.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| el9.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| el9.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| el10.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| el10.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| d12.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| d12.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| d13.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| d13.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| u22.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| u22.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| u24.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| u24.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| u26.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
| u26.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
Source
github.com/Nickyoung0/pg_stat_backtrace
pg_stat_backtrace-1.0.0.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Create this extension with:
Usage
Sources: pg_stat_backtrace upstream README, upstream changelog, local source tarball
pg_stat_backtrace-1.0.0.tar.gz.
pg_stat_backtrace captures or logs the C-level stack backtrace of a PostgreSQL backend or auxiliary process on the same Linux host. It uses ptrace(PTRACE_SEIZE) plus libunwind; it does not use shared_preload_libraries and does not send SIGSTOP to the target.
Capture A Backtrace
Find a target process from PostgreSQL views, then call pg_get_backtrace(pid):
The returned text uses a pstack(1)-style format:
Write To The Server Log
Use pg_log_backtrace(pid) when the result should go through the normal PostgreSQL log pipeline:
The function returns true on success.
Permissions
By default, execute privilege is revoked from PUBLIC for both functions. Grant access only to trusted monitoring roles:
The C code still enforces target checks:
- Superusers may target any PostgreSQL process in the instance, including auxiliary processes such as
walwriter,checkpointer,walsender, autovacuum workers, startup, and archiver processes. - Non-superusers may target only regular backends owned by roles they are members of.
- Auxiliary processes have no role ownership and are rejected for non-superusers.
- A non-superuser may not target a superuser-owned backend, even with role membership.
Input And Error Behavior
Both functions are VOLATILE STRICT PARALLEL RESTRICTED.
Self-targeting is rejected because a Linux process cannot ptrace itself:
Operational Caveats
- Version 1.0.0 supports PostgreSQL 14-18. Upstream 1.0.0 also advertises PostgreSQL 19 compatibility.
- The extension is Linux-only and depends on
libunwind/libunwind-ptraceat build and runtime. - On hosts with Yama ptrace restrictions, backend-to-backend capture may require
kernel.yama.ptrace_scope = 0. - The target process is briefly paused while the stack is unwound. Avoid tight loops against critical processes such as
walwriter,checkpointer, or synchronous-replicationwalsenderon busy primaries. - Linux permits only one tracer per target process. Concurrent calls against the same PID can fail with
EPERM; retry after the in-flight call finishes.