Hook
A way of extending functionality at source code level
A hook
provides a way of enabling custom routines which are called at particular points in the PostgreSQL operational lifecycle.
Hooks of various kinds have been present in PostgreSQL since PostgreSQL 8.2. However, documentation is sparse.
Available hooks
- General hooks
- Security hooks
- Function manager hooks
- Planner hooks
- Executor hooks
- PL/pgSQL hooks
Change history
- PostgreSQL 13
openssl_tls_init_hook
added (commit 896fcdb2)
- PostgreSQL 9.6
create_upper_paths_hook
added (commit 5864d6a4)
- PostgreSQL 9.5
row_security_policy_hook_permissive
added (commit 0bf22e0c)row_security_policy_hook_restrictive
added (commit 0bf22e0c)set_join_pathlist_hook
added (commit e7cb7ee1)set_rel_pathlist_hook
added (commit c2ea2285)
- PostgreSQL 9.2
emit_log_hook
added (commit 19dbc346)post_parse_analyze_hook
added (commit a40fa613)
- PostgreSQL 9.1
ClientAuthentication_hook
added (commit 20709f81)ExecutorCheckPerms_hook
added (commit f4122a8d)ExecutorFinish_hook
added (commit a874fe7b)fmgr_hook
added (commit d368e1a2)needs_fmgr_hook
added (commit d368e1a2)object_access_hook
added (commit cc1ed40d)
- PostgreSQL 9.0
check_password_hook
added (commit c742b795)ProcessUtility_hook
added (commit a5495cd8)
- PostgreSQL 8.4
ExecutorEnd_hook
added (commit cd35e9d7)ExecutorRun_hook
added (commit 6cc88f0a)ExecutorStart_hook
added (commit cd35e9d7)get_attavgwidth_hook
added (commit 7b7df9f0)get_index_stats_hook
added (commit 7b7df9f0)get_relation_stats_hook
added (commit 7b7df9f0)shmem_startup_hook
added (commit dad75a62)
- PostgreSQL 8.3
ExplainOneQuery_hook
added (commit 604ffd28)explain_get_index_name_hook
added (commit 604ffd28)get_relation_info_hook
added (commit 604ffd28)join_search_hook
added (commit cdf0231c)planner_hook
added (commit 604ffd28)
- PostgreSQL 8.2
Proposed hooks
Session start/end hooks
The following hooks:
session_start_hook
session_end_hook
were added during the PostgreSQL 13 development cycle in commit e788bd92, but subsequently withdrawn in commit 9555cc8d; see thread "Add hooks for session start and session end, take two".
Examples
Shared memory startup hook in an extension:
static shmem_startup_hook_type prev_shmem_startup_hook = NULL; static void our_shmem_startup(void); void _PG_init(void) { prev_shmem_startup_hook = shmem_startup_hook; shmem_startup_hook = our_shmem_startup; } /* * shmem_startup hook: allocate or attach to shared memory, */ static void our_shmem_startup(void) { bool found; if (prev_shmem_startup_hook) prev_shmem_startup_hook(); /* * Create or attach to the shared memory state, including hash table */ LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE); /* do stuff... */ LWLockRelease(AddinShmemInitLock); }