Thread

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

    Rahila Syed <rahilasyed90@gmail.com> — 2025-11-21T04:28:23Z

    Hi,
    
    
    >
    > CREATE extension does not automatically load or ensure that _PG_init() is
    >> run.
    >> It mainly runs the .sql script in your extension.
    >>
    > Thanks for the clarification. However, in my testing, _PG_init() did run
    > when I executed
    > CREATE EXTENSION. I suspect this might be happening because the SQL
    > script defines
    > C functions using MODULE_PATHNAME, which triggers the library load.
    > In a new session, _PG_init() seems to run again when any of those C
    > functions are executed.
    >
    >
    You are right, if the create extension creates a C function it triggers the
    load of the library, which
    in turn causes _PG_init() to execute.
    Sorry for the confusion.
    
    FWIW, the call stack looks like the following:
    _PG_init ()
    internal_load_library()
    load_external_function ()
    fmgr_c_validator ()
    FunctionCall1Coll ()
    OidFunctionCall1Coll ()
    ProcedureCreate ()
    CreateFunction ()
    ProcessUtilitySlow ()
    standard_ProcessUtility ()
    ProcessUtility ()
    execute_sql_string ()
    execute_extension_script ()
    CreateExtensionInternal ()
    CreateExtension ()
    
    Thank you,
    Rahila Syed
    
    >