Re: Composite types as parameters

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Elijah Stone <elronnd(at)elronnd(dot)net>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Composite types as parameters
Date: 2021-06-27 15:26:54
Message-ID: 8732.1624807614@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Elijah Stone <elronnd(at)elronnd(dot)net> writes:
> On Sat, 26 Jun 2021, Tom Lane wrote:
>> If it still doesn't work, please provide a more concrete example.

> Thanks, unfortunately adding the explicit cast doesn't help. I've
> attached a minimal runnable example.

So your problem is that you're explicitly saying that the input is
of generic-RECORD type. You should let the server infer its type,
instead, which it can easily do from context in this example.
That is, pass zero as the type OID, or leave out the paramTypes
array altogether. The example works for me with this change:

@@ -30,13 +30,13 @@

// error:
check(PQexecParams(c, "INSERT INTO tab VALUES($1, 8);",
- 1, &(Oid){RECORDOID}, &(const char*){recbuf},
+ 1, &(Oid){0}, &(const char*){recbuf},
&(int){rec - recbuf}, &(int){1/*binary*/},
1/*binary result*/));

// error as well:
check(PQexecParams(c, "INSERT INTO tab VALUES($1::some_record, 8);",
- 1, &(Oid){RECORDOID}, &(const char*){recbuf},
+ 1, &(Oid){0}, &(const char*){recbuf},
&(int){rec - recbuf}, &(int){1},
1));

In more complicated cases you might need to fetch the composite
type's actual OID and pass that. But I'd go with the lazy approach
until forced to do differently.

> Is there a
> way to discover the OID of a composite type? And is the wire format the
> same as for a generic record?

Same as for any other type: SELECT 'mytypename'::regtype::oid.
And yes.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Geoghegan 2021-06-27 16:51:28 Re: Farewell greeting
Previous Message Julien Rouhaud 2021-06-27 15:21:37 Re: Deparsing rewritten query