Re: get_database_name() from background worker - Mailing list pgsql-hackers

From Michael Paquier
Subject Re: get_database_name() from background worker
Date
Msg-id 20191211110645.GM72921@paquier.xyz
Whole thread Raw
In response to Re: get_database_name() from background worker  (Koichi Suzuki <koichi@2ndquadrant.com>)
List pgsql-hackers
On Wed, Dec 11, 2019 at 05:17:00PM +0900, Koichi Suzuki wrote:
> Not using this extension, sorry.

I have no idea what you are trying to do, but get_database_name()
accesses the system cache, which means two things:
- The access needs to be done in the context of a transaction.  That's
a trick we use in a couple of places in core, see for example
IdentifySystem() in walsender.c which looks for the database name.  In
this case, you need to do the call in-between StartTransactionCommand
and CommitTransactionCommand, and you should make sure that the memory
context does not point to the one of the transaction to have an access
to the syscache data after committing the inner transaction used for
the lookup.
- Your background worker needs a database access, so bgw_flags needs
to be BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION,
and also make sure to use BackgroundWorkerInitializeConnection at the
beginning of the main loop of your worker.

Hope that helps.
--
Michael

Attachment

pgsql-hackers by date:

Previous
From: Amit Khandekar
Date:
Subject: Re: logical decoding : exceeded maxAllocatedDescs for .spill files
Next
From: Michael Paquier
Date:
Subject: Re: Start Walreceiver completely before shut down it on standbyserver.