Thread: interperting type oid in C code
I'm working on a foreign data wrapper and I want to switch based on the column type. Specifically, if the column type in the external table is the same as in the (locally defined) foreign table then I can get some speedup for some types.
Through the ForeignScanState object I can get TupleDesc and AttInMetadata opjects, and through there I can get the Oid of the column type, but now I'm stumped. How can I programmatically check whether Oid X refers to type int4, or whatever?SELECT typname
FROM pg_type
WHERE oid = Oid_x;
FROM pg_type
WHERE oid = Oid_x;
On Mon, Oct 19, 2015 at 2:36 PM, Ken Been <kbbeen@gmail.com> wrote:
Thanks.I'm working on a foreign data wrapper and I want to switch based on the column type. Specifically, if the column type in the external table is the same as in the (locally defined) foreign table then I can get some speedup for some types.Through the ForeignScanState object I can get TupleDesc and AttInMetadata opjects, and through there I can get the Oid of the column type, but now I'm stumped. How can I programmatically check whether Oid X refers to type int4, or whatever?
--
Melvin Davidson
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.

Thanks, but I actually wanted to do it from C code. But anyway I think I found the answer: use the symbolic constants in catalog/pg_type.h, such as INT4OID.
On Mon, Oct 19, 2015 at 6:44 PM, Melvin Davidson <melvin6925@gmail.com> wrote:
SELECT typname
FROM pg_type
WHERE oid = Oid_x;--On Mon, Oct 19, 2015 at 2:36 PM, Ken Been <kbbeen@gmail.com> wrote:Thanks.I'm working on a foreign data wrapper and I want to switch based on the column type. Specifically, if the column type in the external table is the same as in the (locally defined) foreign table then I can get some speedup for some types.Through the ForeignScanState object I can get TupleDesc and AttInMetadata opjects, and through there I can get the Oid of the column type, but now I'm stumped. How can I programmatically check whether Oid X refers to type int4, or whatever?Melvin Davidson
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.
Ken Been wrote: > Thanks, but I actually wanted to do it from C code. But anyway I think I > found the answer: use the symbolic constants in catalog/pg_type.h, such as > INT4OID. You can probably use SearchSysCache1(TYPEOID, ObjectIdGetDatum(your_oid)) or perhaps lookup_type_cache(your_oid). -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Those are more complicated, and it's not obvious to me how to use them. I really think that all I need is something as simple as "if (my_oid == INT4OID) {...}". Is there any reason why I shouldn't just do that?
On Mon, Oct 19, 2015 at 7:08 PM, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:
Ken Been wrote:
> Thanks, but I actually wanted to do it from C code. But anyway I think I
> found the answer: use the symbolic constants in catalog/pg_type.h, such as
> INT4OID.
You can probably use
SearchSysCache1(TYPEOID, ObjectIdGetDatum(your_oid))
or perhaps
lookup_type_cache(your_oid).
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Ken Been wrote: > Those are more complicated, and it's not obvious to me how to use them. I > really think that all I need is something as simple as "if (my_oid == > INT4OID) {...}". Is there any reason why I shouldn't just do that? I don't know. I was thinking that you might want to handle a larger set of type OIDs. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Well, it's a few types, but the logic is different for each one, so they have to be handled independently anyway.
On Mon, Oct 19, 2015 at 7:40 PM, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:
Ken Been wrote:
> Those are more complicated, and it's not obvious to me how to use them. I
> really think that all I need is something as simple as "if (my_oid ==
> INT4OID) {...}". Is there any reason why I shouldn't just do that?
I don't know. I was thinking that you might want to handle a larger set
of type OIDs.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Ken Been wrote: > I'm working on a foreign data wrapper and I want to switch based on the column type. > Specifically, if the column type in the external table is the same as in the (locally defined) foreign > table then I can get some speedup for some types. > > Through the ForeignScanState object I can get TupleDesc and AttInMetadata opjects, and > through there I can get the Oid of the column type, but now I'm stumped. How can I programmatically > check whether Oid X refers to type int4, or whatever? > Thanks, but I actually wanted to do it from C code. But anyway I think I found the answer: use the > symbolic constants in catalog/pg_type.h, such as INT4OID. That's fine, there is no problem with doing that. Yours, Laurenz Albe