Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from Ccode first { simboll is lost) - Mailing list pgsql-bugs

From Andres Freund
Subject Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from Ccode first { simboll is lost)
Date
Msg-id 20170626223536.r3nkil6cfzqwlw2r@alap3.anarazel.de
Whole thread Raw
In response to [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)  (Арсен Арутюнян <arutar@bk.ru>)
List pgsql-bugs
Hi,

On 2017-06-27 01:21:04 +0300, Арсен Арутюнян wrote:
> Oid * Types = (Oid *)palloc(2 * sizeof(Oid));
> Types[0] = 23; //Int32
> Types[1] = 114; //JSON
> m_JsonInsertPlan = SPI_saveplan(SPI_prepare("insert into json_test(Num,Obj) values ($1,$2)\0", 2, Types));

So the type of what you're inserting is int and json. The latter is a
variable length type.  But then you do:

> Values[0] = Int32GetDatum(227);
> Values[1] = CStringGetDatum(JsonObject->data);
> int ret = SPI_execute_plan(m_JsonInsertPlan, Values, NULL, false, 0);

inserting a cstring, rather than a json value.  I.e. the issue is that
you're "lying" about the types, and their validity. You should either
use text as the plan input, and cast the result of the statement, or you
need to actually construct a proper datum, using OidInputFunctionCall or
json_in directly.

> m_JsonInsertPlan = SPI_saveplan(SPI_prepare("insert into json_test(Num,Obj) values ($1,$2)\0", 2, Types));

Not sure what that \0 is supposed to do here?

> Types[1] = 114; //JSON

You should probably include catalog/pg_type.h and use JSONOID etc.

Andres


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

pgsql-bugs by date:

Previous
From: Арсен Арутюнян
Date:
Subject: [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Next
From: Tom Lane
Date:
Subject: Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)