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

Change history

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".


Shared memory startup hook in an extension:

static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
static void our_shmem_startup(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
	bool		found;

	if (prev_shmem_startup_hook)

	 * Create or attach to the shared memory state, including hash table
	LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);

 	/* do stuff... */



Application interface, Hooks, PostgreSQL concept, PostgreSQL internals