0

Is there a way in PostgreSQL to check whether the current context is during CREATE EXTENSION or ALTER EXTENSION … UPDATE?

I'm switching over some of my schemas that I managed with my home-brewed PostgreSQL migration framework to be based on extensions instead. In my migration framework, I had various way to see if I was running within a migration, and I would like to see something similar during extension installation/upgrade.

Maybe I must throw and catch an exception and inspect the stack? I hope not…

Context [added Nov. 21, '22]: I would like to create an event trigger that ignores events that are part of extension installation and upgrade. To be honest, I don't really need this. (I realized that, within my use case, extension installation is handled from within migrations anyway.) But, as a theoretical exercise, I still would like to know if this is possible.

1 Answer 1

2

Extension scripts have a magic token, @extschema@, that gets replaced with the target schema name. You can check if the replacement occurs. I'm not sure it can be in a string literal and still get replaced; its designed to be used in an identifier. Details in the PostgreSQL manual.

As extension scripts have to live in a special place and only get called with CREATE EXTENSION / ALTER EXTENSION UPDATE and can have protection against being included via psql you can be fairly certain about how your code got called. You could put a wrapper on your code that passes a source identifier to an inner function through an argument.

1
  • I like your idea, which I hadn't thought of myself. However, it doesn't solve my precise problem: I want to create an event trigger that ignores events that take place during CREATE EXTENSION and ALTER EXTENSION … UPDATE. I will update my question with that bit of context.
    – BigSmoke
    Nov 21 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.