Обсуждение: Passing current_database to BackgroundWorkerInitializeConnection

Поиск
Список
Период
Сортировка

Passing current_database to BackgroundWorkerInitializeConnection

От
Jeremy Finzel
Дата:
I am having trouble figuring out the right way to do this, clearly missing something obvious.  I am simply trying to pass the current database to BackgroundWorkerInitializeConnection, but MyDatabaseId is showing as 0, and I am getting this error in the running of function get_database_name here at the top:

ERROR:  invalid cache ID: 21

char       *database_name = get_database_name(MyDatabaseId);

/*
...
*/

    /* Connect to our database */
    BackgroundWorkerInitializeConnection(database_name, NULL);


Any direction would be much appreciated.  I have tried to look at example uses of MyDatabaseId and not sure what I'm missing here.

Thanks,
Jeremy

Re: Passing current_database to BackgroundWorkerInitializeConnection

От
Andres Freund
Дата:
On 2018-03-30 16:36:59 -0500, Jeremy Finzel wrote:
> I am having trouble figuring out the right way to do this, clearly missing
> something obvious.  I am simply trying to pass the current database
> to BackgroundWorkerInitializeConnection, but MyDatabaseId is showing as 0,
> and I am getting this error in the running of function get_database_name
> here at the top:

What do you mean with "current database"? Before you
BackgroundWorkerInitializeConnection() there is no such thing?

Greetings,

Andres Freund


Re: Passing current_database to BackgroundWorkerInitializeConnection

От
Jeremy Finzel
Дата:

What do you mean with "current database"? Before you
BackgroundWorkerInitializeConnection() there is no such thing?

My module is based directly off the worker_spi example. The worker is dynamically launched via SQL command. But in the worker_spi example, the database postgres is just hardcoded as the database in which to start the background worker process. Instead, I want to start it in the database in which I run the SQL command.

Thank you!

Jeremy 

Re: Passing current_database to BackgroundWorkerInitializeConnection

От
Andres Freund
Дата:

On March 30, 2018 3:16:31 PM PDT, Jeremy Finzel <finzelj@gmail.com> wrote:
>> What do you mean with "current database"? Before you
>> BackgroundWorkerInitializeConnection() there is no such thing?
>
>
>My module is based directly off the worker_spi example. The worker is
>dynamically launched via SQL command. But in the worker_spi example,
>the
>database postgres is just hardcoded as the database in which to start
>the
>background worker process. Instead, I want to start it in the database
>in
>which I run the SQL command.

The started worker isn't associated with the original database. You can pass the database oid as an argument to the
launchedbgworker. 

Andres

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.


Re: Passing current_database to BackgroundWorkerInitializeConnection

От
Jeremy Finzel
Дата:


On Fri, Mar 30, 2018 at 5:37 PM, Andres Freund <andres@anarazel.de> wrote:


On March 30, 2018 3:16:31 PM PDT, Jeremy Finzel <finzelj@gmail.com> wrote:
>> What do you mean with "current database"? Before you
>> BackgroundWorkerInitializeConnection() there is no such thing?
>
>
>My module is based directly off the worker_spi example. The worker is
>dynamically launched via SQL command. But in the worker_spi example,
>the
>database postgres is just hardcoded as the database in which to start
>the
>background worker process. Instead, I want to start it in the database
>in
>which I run the SQL command.

The started worker isn't associated with the original database. You can pass the database oid as an argument to the launched bgworker.


Thank you, this makes sense.  However, how can this be done since I can only pass one argument to bgw_main?  Is there any way to do this without having to store the value in shared memory?  I was going to try passing an array instead of an int, but I'm not liking that much.  I am trying to pass naptime and database_name (or oid).

Thanks,
Jeremy

Re: Passing current_database to BackgroundWorkerInitializeConnection

От
Andres Freund
Дата:
Hi,

On 2018-04-02 14:24:53 -0500, Jeremy Finzel wrote:
> Thank you, this makes sense.  However, how can this be done since I can
> only pass one argument to bgw_main?  Is there any way to do this without
> having to store the value in shared memory?

No (I mean you can store it in the filesystem or such as well, but
...). Pretty fundamentally sharing data between concurrently running
processes needs a medium to share the data over. The bgw infrastructure
allocates just enough so you can put an index to it into
shmem. Allocating more would be wasteful and/or not enough for some
users.

Greetings,

Andres Freund


Re: Passing current_database to BackgroundWorkerInitializeConnection

От
Jeremy Finzel
Дата:


On Mon, Apr 2, 2018 at 2:27 PM, Andres Freund <andres@anarazel.de> wrote:
Hi,

On 2018-04-02 14:24:53 -0500, Jeremy Finzel wrote:
> Thank you, this makes sense.  However, how can this be done since I can
> only pass one argument to bgw_main?  Is there any way to do this without
> having to store the value in shared memory?

No (I mean you can store it in the filesystem or such as well, but
...). Pretty fundamentally sharing data between concurrently running
processes needs a medium to share the data over. The bgw infrastructure
allocates just enough so you can put an index to it into
shmem. Allocating more would be wasteful and/or not enough for some
users.

Greetings,

Andres Freund

Hmmm... not sure if I follow.  My goal is to run a SQL statement every 10 seconds (or what value is chosen) in a particular database, using a background worker.  Those are the two arguments.  Am I missing some way to implement this apart from passing those 2 arguments into the launcher function?  Is the way to do this properly then to allocate shared memory for it, as opposed to trying to pass args into the main function?

Thanks,
Jeremy

Re: Passing current_database to BackgroundWorkerInitializeConnection

От
Andres Freund
Дата:
Hi,

On 2018-04-02 14:33:54 -0500, Jeremy Finzel wrote:
> Hmmm... not sure if I follow.  My goal is to run a SQL statement every 10
> seconds (or what value is chosen) in a particular database, using a
> background worker.  Those are the two arguments.  Am I missing some way to
> implement this apart from passing those 2 arguments into the launcher
> function?  Is the way to do this properly then to allocate shared memory
> for it, as opposed to trying to pass args into the main function?

Yes, that's the proper way. Allocate shared memory and pass a pointer to
that as the argument.

Greetings,

Andres Freund