Thread: How to get the name of the current database in C function/extention inside a trigger?

I'm writting a function/extention in C for a trigger. Inside a trigger, in C, I want to get the name of the current database. However, not via SPI_exec(), SPI_prepare() and the like, but more directly, in a more faster way.

I'm aware of "current_database()" but it'll require calling it via SPI_exec()

I've found this code which is  implementation of 'conninfo' command:

https://github.com/LuberZheng/FAS_Sniffer/blob/84872cb2360ce12219970b828e4377e5d3a91bb8/postgres/postgresql-9.5.4/src/bin/psql/command.c#L343

but it's still unclear how to do the same thing from a C function inside for a trigger.


How to do it?


P.S.

While SPI_exec(), SPI_prepare() won't work for me, other SPI functions such SPI_get* will be ok.

Versions: 11 or 12



For the versions 11 or 12



On Mon, 3 Aug 2020 at 21:26, alex m <al-gen@serendipia.email> wrote:
> I'm writting a function/extention in C for a trigger. Inside a trigger, in C, I want to get the name of the current
database.However, not via SPI_exec(), SPI_prepare() and the like, but more directly, in a more faster way.
 

You can use MyDatabaseId in miscadmin.h

If you git grep MyDatabaseId in src/contrib you can see some examples.

A good tip for the future would be to just look at the source code of
the built-in function and see how it does it.

David



Thanks.


El 03/08/2020 a las 16:04, David Rowley escribió:

> On Mon, 3 Aug 2020 at 21:26, alex m <al-gen@serendipia.email> wrote:
>> I'm writting a function/extention in C for a trigger. Inside a trigger, in C, I want to get the name of the current
database.However, not via SPI_exec(), SPI_prepare() and the like, but more directly, in a more faster way.
 
> You can use MyDatabaseId in miscadmin.h
>
> If you git grep MyDatabaseId in src/contrib you can see some examples.
>
> A good tip for the future would be to just look at the source code of
> the built-in function and see how it does it.
>
> David