Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)

From: Andres Freund <andres(at)anarazel(dot)de>
To: Арсен Арутюнян <arutar(at)bk(dot)ru>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Date: 2017-06-26 22:35:36
Message-ID: 20170626223536.r3nkil6cfzqwlw2r@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2017-06-26 22:59:27 Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Previous Message Арсен Арутюнян 2017-06-26 22:21:04 Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)