Re: Access to current database from C-language function - Mailing list pgsql-hackers

From Florian Pflug
Subject Re: Access to current database from C-language function
Date
Msg-id 8661F8C6-C9BA-4B83-A68C-302B07B7CAD6@phlo.org
Whole thread Raw
In response to Access to current database from C-language function  (Achim Domma <domma@procoders.net>)
Responses Re: Access to current database from C-language function
List pgsql-hackers
On Jul25, 2011, at 13:40 , Achim Domma wrote:
> I have read http://www.postgresql.org/docs/9.1/static/xfunc-c.html and my idea is, to write a C function which
returnsa set of rows. To generate the result set, I would like to access indexes directly using the information I found
athttp://www.postgresql.org/docs/9.1/static/indexam.html. But I don't get the idea how to glue both parts together!?
Couldsomebody give me a starting point? How do I get a handle to the current database inside a C function? 

If you want to access the database using SQL from C-language functions, there the SPI API for that.
(http://www.postgresql.org/docs/9.0/interactive/spi.html).The API provided by SPI is conceptually similar to the one
providedby the client library libpq, i.e. it works in terms of statements, cursors, ...). SPI takes care of handling
allthe low-level details like making sure you're using a valid snapshot, are inside a transaction, correctly handle
lockedrows, ... 

A more low-level API is provided by {heap,index}_{beginscan,endscan}, heap_{insert,update,delete} and index_insert.
However,correct handling of transactions using this API isn't easy - for example, to update a row you'd first have to
findthe latest version of that row, then decide if you're allowed to update it, and finally create a new version. 

best regards,
Florian Pflug



pgsql-hackers by date:

Previous
From: Achim Domma
Date:
Subject: Access to current database from C-language function
Next
From: Robert Haas
Date:
Subject: Re: pgbench --unlogged-tables