I have a C extension (code below) in which I’m trying to get access to a database but I’m getting the following error:
FATAL: cannot read pg_class without having selected a database at character 15
How to get around this issue? I couldn’t find out how to specify the database in the SPI API.
Similar code if run through a trigger (calling a function that accesses the same database) works just fine – obviously the database is implicit in this case.
Code follows:
static void my_function(XactEvent event, void *arg) {
char sql[512];
switch(event) {
case XACT_EVENT_COMMIT:
if(SPI_connect() == SPI_OK_CONNECT)
{
snprintf(sql, sizeof(sql), "SELECT * FROM myschema.mytable;");
if (SPI_OK_SELECT == SPI_execute(sql, true, 0))
{
// Rest of code...
}
}
Thanks,
D.
The database should always be implicit, since SPI is only meant to be used by C functions called from a database session. See the example.
How do you execute this code?