Re: Access to current database from C-language function

From: Florian Pflug <fgp(at)phlo(dot)org>
To: Achim Domma <domma(at)procoders(dot)net>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Access to current database from C-language function
Date: 2011-07-25 12:48:16
Message-ID: 8661F8C6-C9BA-4B83-A68C-302B07B7CAD6@phlo.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2011-07-25 13:23:01 Re: pgbench --unlogged-tables
Previous Message Achim Domma 2011-07-25 11:40:45 Access to current database from C-language function