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 returns a set of rows. To generate the 
> result set, I would like to access indexes directly using the information I 
> found at http://www.postgresql.org/docs/9.1/static/indexam.html. But I don't 
> get the idea how to glue both parts together!? Could somebody 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 provided by the client library libpq, 
i.e. it works in terms of statements, cursors, ...). SPI takes care of handling 
all the low-level details like making sure you're using a valid snapshot, are 
inside a transaction, correctly handle locked rows, ...

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 find the latest version of that row, then decide if you're 
allowed to update it, and finally create a new version.

best regards,
Florian Pflug


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

Reply via email to