Re: get_database_name() from background worker

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Koichi Suzuki <koichi(at)2ndquadrant(dot)com>
Cc: ROS Didier <didier(dot)ros(at)edf(dot)fr>, "tsunakawa(dot)takay(at)fujitsu(dot)com" <tsunakawa(dot)takay(at)fujitsu(dot)com>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: get_database_name() from background worker
Date: 2019-12-11 11:06:45
Message-ID: 20191211110645.GM72921@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2019-12-11 11:13:06 Re: Start Walreceiver completely before shut down it on standby server.
Previous Message Amit Khandekar 2019-12-11 10:46:43 Re: logical decoding : exceeded maxAllocatedDescs for .spill files