Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistencies Casting to Double-Quoted Types

Using postgres 12.2:

select '46ee2794-ddd1-4c4b-be04-82908ff1885d'::"uuid" /*works, uuid is a built-in type, not an alias*/
select '1'::"int4"    /*works ... int4 is alias for integer*/
select '1'::"integer" /*fails ... integer is built-in type*/

I have since abandoned the double quotes, but am still curious as to why this behaves the way it does!

like image 215
Avocado Avatar asked Jan 25 '26 00:01

Avocado


1 Answers

It may seem strange but actually int4 is a base type while integer is a kind of alias, if we assume that base types are stored in the system catalog pg_type.

select oid, typname
from pg_type
where typname like 'int%'
and typcategory = 'N'

 oid | typname
-----+---------
  20 | int8
  21 | int2
  23 | int4
(3 rows)
like image 107
klin Avatar answered Jan 27 '26 14:01

klin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!