Question

I cannot find any documentation regarding HSTORE data access using the C library. Currently I'm considering to just convert the HSTORE columns into arrays in my queries but is there a way to avoid such conversions?

Was it helpful?

Solution

libpqtypes appears to have some support for hstore.

Another option is to avoid directly interacting with hstore in your code. You can still benefit from it in the database without dealing with its text representation on the client side. Say you want to fetch a hstore field; you just use:

SELECT t.id, k, v FROM thetable t, LATERAL each(t.hstorefield);

or on old PostgreSQL versions you can use the quirky and nonstandard set-returning-function-in-SELECT form:

SELECT t.id, each(t.hstorefield) FROM thetable t;

(but watch out if selecting multiple records from t this way, you'll get weird results wheras LATERAL will be fine).

Another option is to use hstore_to_array or hstore_to_matrix when querying, if you're comfortable dealing with PostgreSQL array representation.

To create hstore values you can use the hstore constructors that take arrays. Those arrays can in turn be created with array_agg over a VALUES clause if you don't want to deal with PostgreSQL's array representation in your code.

All this mess should go away in future, as PostgreSQL 9.4 is likely to have much better interoperation between hstore and json types, allowing you to just use the json representation when interacting with hstore.

OTHER TIPS

The binary protocol for hstore is not complicated.
See the _send and _recv functions from its IO code.
Of course, that means requesting (or binding) it in binary format in libpq.
(see the paramFormats[] and resultFormat arguments to PQexecParams)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top