Re: Composite types as parameters - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Composite types as parameters
Date
Msg-id 8732.1624807614@sss.pgh.pa.us
Whole thread Raw
In response to Re: Composite types as parameters  (Elijah Stone <elronnd@elronnd.net>)
Responses Re: Composite types as parameters
List pgsql-hackers
Elijah Stone <elronnd@elronnd.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



pgsql-hackers by date:

Previous
From: Julien Rouhaud
Date:
Subject: Re: Deparsing rewritten query
Next
From: Peter Geoghegan
Date:
Subject: Re: Farewell greeting