Re: Clarification on when _PG_init() is invoked for extensions

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ayush Vatsa <ayushvatsa1810(at)gmail(dot)com>
Cc: Rahila Syed <rahilasyed90(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Clarification on when _PG_init() is invoked for extensions
Date: 2025-11-20 21:58:13
Message-ID: 2076422.1763675893@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Ayush Vatsa <ayushvatsa1810(at)gmail(dot)com> writes:
> My use case is that I’m building an experimental extension that will be
> installed only in a few
> databases. I want the hooks initialized by this extension to apply only to
> those specific
> databases, while all other databases should continue with the default
> PostgreSQL behavior.

If you don't name the extension in shared_preload_libraries, it
will not be loaded into the postmaster, but only into individual
sessions. The ways the latter can happen are

* session_preload_libraries or local_preload_libraries GUC
* explicit LOAD command
* creation of a C function that references that library
* calling of a C function that references that library

Extensions per se don't have anything to do with this,
except to the extent that an extension's creation script
might do some of the above actions.

You might be able to get what you want by doing

ALTER DATABASE db SET session_preload_libraries = 'your-library'

for each DB you want it to be active in. If it exposes any SQL
objects you'd also need to issue CREATE EXTENSION in each DB.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jacob Champion 2025-11-20 21:59:22 Re: RFC 9266: Channel Bindings for TLS 1.3 support
Previous Message Jacob Champion 2025-11-20 21:52:52 Re: RFC 9266: Channel Bindings for TLS 1.3 support