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