2

Postgres extension development

I am working with C API for postgres-9.4 installed from ubuntu trusty main repo. This might be a silly question, but please bear with me.

I would like to use a function that converts a cstring to Jsonb* structure defined in

http://doxygen.postgresql.org/jsonb_8h.html

There are functions doing exactly this already defined in

http://doxygen.postgresql.org/jsonb_8c.html

Namely, the function Datum jsonb_in ( PG_FUNCTION_ARGS ), however I am not sure if I can call this function from C API in a portable and safe manner. As it seems it is intended for being called by postgres from first glance.

I could also use the function jsonb_from_cstring

http://doxygen.postgresql.org/jsonb_8c.html#ab23eca28d5880f86a0943d71c90d6654

but it is declared and defined in jsonb.c and not declared in json.h, and hence linking with this function is not a very clean solution. I tried finding the symbols for jsonb_from_cstring in libpq.so, however there are none. I am guessing I need a non-standard build of postgres?

So the question is, what is the best way to convert a cstring to a Jsonb* structure from within C API?

Edit:

The extension gets json data as a string from external source and is supposed to be able to store this string in a Jsonb type

iggy
  • 1,613
  • 1
  • 18
  • 35
  • Are you developing a Postgres extension? Or user-defined function to run on server? These functions are not from public interface and you're not supposed to use them otherwise. – Tometzky Feb 11 '15 at 21:36
  • @Tometzky I am developing a postgres extension – iggy Feb 11 '15 at 21:49

1 Answers1

2

This was answered in postgres mailing list

http://www.postgresql.org/message-id/CAFj8pRCeGL7q_EGTz2=FyQZ2Qrtn1x_76mz3fuR=b7beEug7Wg@mail.gmail.com

Quote: you can call "input function" - jsonb_in

Jsonb *targetjsonbvar = DatumGetJsonb(DirectFunctionCall1(jsonb_in, CStringGetDatum(cstrvalue)));

Catija
  • 359
  • 6
  • 21
iggy
  • 1,613
  • 1
  • 18
  • 35