Cast to a type by its OID?

Viewed 59

I would like to cast a value to a type using not the type name, but the OID of the type. Is this possible? So instead of:

SELECT '{{a}}'::char[]

do

SELECT '{{a}}'::1002

(although the above results in a syntax error)

Context: I'm writing a library in Python to query PostgreSQL, which includes conversions from PostgreSQL types to Python types, and I want to make sure I test all the types, and it seems easiest to go through all the OIDs listed at https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat

1 Answers

I dont know about the possibility to call by oid, but why not just in your library in python use the table pg_type , if you have the postgresql database? i guess you want to build the automatic test for you mappings between types of Postgresql and Python, so you before checks can query from that table relevant information about build-in types in Postgresql

select *  from pg_type 

you can filter by typtype (base type or others) and by typcategory (numeric, arrays , etcs)

The doc about pg_type

p.s. Hope this will help =)

Related