Comments on: Writing PostgreSQL Extensions is Fun – C Language https://www.percona.com/blog/writing-postgresql-extensions-is-fun-c-language/ Fri, 05 Apr 2019 14:56:13 +0000 hourly 1 https://wordpress.org/?v=6.5.2 By: Ibrar Ahmed https://www.percona.com/blog/writing-postgresql-extensions-is-fun-c-language/#comment-10970580 Fri, 05 Apr 2019 14:56:13 +0000 https://www.percona.com/blog/?p=56111#comment-10970580 Yes, thats right, I am not disagreeing that.

]]>
By: RhodiumToad (@RhodiumToad) https://www.percona.com/blog/writing-postgresql-extensions-is-fun-c-language/#comment-10970579 Fri, 05 Apr 2019 14:53:00 +0000 https://www.percona.com/blog/?p=56111#comment-10970579 As I said above, defining a function that’s in some module does also load the module (in that session, only). That’s why CREATE EXTENSION ends up loading it, which calls _PG_init. If you disconnect and reconnect after doing the CREATE, you’ll find that the extension exists but that the module is not loaded until you call the function.

]]>
By: Ibrar Ahmed https://www.percona.com/blog/writing-postgresql-extensions-is-fun-c-language/#comment-10970573 Fri, 05 Apr 2019 13:10:40 +0000 https://www.percona.com/blog/?p=56111#comment-10970573 I have not explained the _preload_libraries too. I have also not explained the complete loading concept of the module. As this is the basic blog about extesnion, so lets keep that simple.

]]>
By: Ibrar Ahmed https://www.percona.com/blog/writing-postgresql-extensions-is-fun-c-language/#comment-10970572 Fri, 05 Apr 2019 13:06:42 +0000 https://www.percona.com/blog/?p=56111#comment-10970572 I think you are right about _PG_fini, but it is still there in the code. But _PG_init called in case of CREATE EXTENSION too.

Example:
postgres=# create extension log;
2019-04-05 13:05:42.572 UTC [17456] WARNING: _PG_init
psql: WARNING: _PG_init
CREATE EXTENSION

]]>
By: RhodiumToad (@RhodiumToad) https://www.percona.com/blog/writing-postgresql-extensions-is-fun-c-language/#comment-10970571 Fri, 05 Apr 2019 12:51:06 +0000 https://www.percona.com/blog/?p=56111#comment-10970571 Note that _PG_fini is never actually called; there is no provision for unloading modules. (Way back in the day there used to be, but it was removed as unsafe – the code is still there in the backend but #ifdef’d out.) So it’s best not to mention it in examples.

If a module has a _PG_init, then also bear in mind that it won’t normally be loaded until the first use of (or definition of) some function defined in the module, unless you also add it to an appropriate _preload_libraries config setting.

]]>