Search code examples
postgresqlpostgresql-extensions

How to make an extension not relocatable?


I have an extension: https://github.com/CraigTyle/Mathexp

My task is to make the extension not relocatable: it should be possible to install the extension in any schema, but it should be impossible to change that schema.

I was told that this is how to do it: Do not use operators and use the @extschema@ prefix for locally defined types and objects. well don't declare relocatable extension.

What exactly do I have to do?


Solution

  • First, you have to change relocatable to false in the extension's control file.

    Other than that, the recommendation you got is partly reasonable and partly nonsense.

    You should define your functions like this:

    CREATE FUNCTION .... AS
    $$ /* function body */ $$
    SET search_path = @extschema@;
    

    Then the search_path is fixed to pg_catalog, pg_temp and your extension schema for the duration of the function call. That means that all access to objects without an explicit schema will only search in these schemas.

    Then you don't have to worry about explicitly qualifying everything in the function with @extschema@, and you can use operators without having to worry, because search_path applies to operators as well. (You can also schema-qualify operators: OPERATOR(schema.+), but that's obviously painful and harms readability.)