pgrx icon indicating copy to clipboard operation
pgrx copied to clipboard

Startup hook

Open ccleve opened this issue 2 years ago • 3 comments

For performance reasons, I need to cache the oid of a custom Postgres type that I create with pgrx. I'm having a bit of trouble.

If I fetch the oid on demand and cache it in a static mut, it works -- the first time through. The difficulty is that if in the same pgrx session I DROP EXTENSION then CREATE EXTENSION, which you do frequently during development, the system re-runs all of the extension's startup SQL, which means that the custom type gets recreated with a new oid. The old oid sitting in the static mut does not get cleared. Also, it appears that _PG_init() does not get called on DROP/CREATE EXTENSION, so I can't clear it there.

What I need is a startup hook that runs after all of the extension's SQL has run. I suppose I could create a #[pg_extern] function and call it in a SELECT statement in the startup SQL itself, but that seems awkward and I'm not sure how to guarantee that this statement will execute after my custom type has been created.

Does such a hook exist? Or is there a better way to do this that I haven't thought of?

ccleve avatar Mar 16 '23 14:03 ccleve

Never mind. I solved the problem using a completely different approach.

I'll leave this open just in case you want a reminder to document the startup sequence.

ccleve avatar Mar 17 '23 01:03 ccleve

This problem has reared its head again. Any thoughts on how to solve it?

Summary: I need a hook to run a bit of code after the extension's startup SQL (schema) has run.

Edit: And any changes made to the catalog are visible.

ccleve avatar Sep 11 '23 21:09 ccleve

If I fetch the oid on demand and cache it in a static mut, it works -- the first time through.

If you don't need this to be visible to other processes/threads, you can use a thread_local! and not need static mut.

I know of nothing to run at precisely this time. If I had to, I would probably try to use a static DATA: Mutex<_> of some kind.

workingjubilee avatar Feb 20 '24 22:02 workingjubilee