spi in bgworker

Started by James Harperabout 12 years ago3 messagesgeneral
Jump to latest
#1James Harper
james.harper@bendigoit.com.au

The docs say I'm supposed to call BackgroundWorkerInitializeConnection in a bgworker and then I can start using spi functions, but they aren't clear on if I should call SPI_connect too.

If I call BackgroundWorkerInitializeConnection and then SPI_execute I get a return code of SPI_ERROR_UNCONNECTED. If I call BackgroundWorkerInitializeConnection then SPI_connect I get a segfault.

I'm initialising my worker as:

strncpy(worker.bgw_name, "tds handler", sizeof(worker.bgw_name));
worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
worker.bgw_restart_time = BGW_NEVER_RESTART;
//worker.bgw_main_arg = 1433;
worker.bgw_main = tdsworker;
RegisterDynamicBackgroundWorker(&worker, &handle);

What am I missing? (this is 9.4 obviously, as I'm using a dynamic bgworker)

Thanks

James

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#2Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: James Harper (#1)
Re: spi in bgworker

James Harper wrote:

The docs say I'm supposed to call BackgroundWorkerInitializeConnection in a bgworker and then I can start using spi functions, but they aren't clear on if I should call SPI_connect too.

If I call BackgroundWorkerInitializeConnection and then SPI_execute I get a return code of SPI_ERROR_UNCONNECTED. If I call BackgroundWorkerInitializeConnection then SPI_connect I get a segfault.

You need to start a transaction after initializing the database
connection. Did you see the sample worker_spi code in contrib?

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3James Harper
james.harper@bendigoit.com.au
In reply to: Alvaro Herrera (#2)
Re: spi in bgworker

James Harper wrote:

The docs say I'm supposed to call BackgroundWorkerInitializeConnection in

a bgworker and then I can start using spi functions, but they aren't clear on if
I should call SPI_connect too.

If I call BackgroundWorkerInitializeConnection and then SPI_execute I get a

return code of SPI_ERROR_UNCONNECTED. If I call
BackgroundWorkerInitializeConnection then SPI_connect I get a segfault.

You need to start a transaction after initializing the database
connection. Did you see the sample worker_spi code in contrib?

I have now. Thanks!

Is the documentation here a bit light because background workers are a new feature? Or is this general postgres knowledge I just haven't picked up on yet?

Could SPI_connect return an error rather than crashing?

Thanks again

James

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general