default permissible datatype conversion matrix
-
19-02-2021 - |
Question
Today I wanted to define a uuid of value 00000000-0000-0000-0000-000000000000
. Being a SQL Server man, I'd usually...
select cast(0x0 as uniqueidentifier);
...but I'm in postgres world now so I whipped out a sensible...
select cast('\x00'::bytea as uuid);
ERROR: cannot cast type bytea to uuid
LINE 1: select cast('\x00'::bytea as uuid);
^
Damn! So I head over to the postgres docs on Type Conversion hoping to see a doc like this one to review which datatypes I can cast between by default without needing to create an explicit cast.
If such a chart does exist in the docs, it's well hidden. Google is likewise not super helpful in this regard.
Is there a good documentation reference for default permissible type casts in postgresql?
To be clear, I don't actually care about the uuid
Solution
You can get a list using the psql
client with
\dCS
This will show all casts defined between system data types.
In addition, all types can be cast to and from text
using the type input and output functions.
OTHER TIPS
As a supplement to Laurenz' post...
From the docs:
\dC[+] [ pattern ]
Lists type casts. If pattern is specified, only casts whose source or target types match the pattern are listed. If + is appended to the command name, each object is listed with its associated description.
\set ECHO_HIDDEN on
reveals \dCS+
to be executing...
SELECT pg_catalog.format_type(castsource, NULL) AS "Source type",
pg_catalog.format_type(casttarget, NULL) AS "Target type",
CASE WHEN c.castmethod = 'b' THEN '(binary coercible)'
WHEN c.castmethod = 'i' THEN '(with inout)'
ELSE p.proname
END AS "Function",
CASE WHEN c.castcontext = 'e' THEN 'no'
WHEN c.castcontext = 'a' THEN 'in assignment'
ELSE 'yes'
END AS "Implicit?",
d.description AS "Description"
FROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p
ON c.castfunc = p.oid
LEFT JOIN pg_catalog.pg_type ts
ON c.castsource = ts.oid
LEFT JOIN pg_catalog.pg_namespace ns
ON ns.oid = ts.typnamespace
LEFT JOIN pg_catalog.pg_type tt
ON c.casttarget = tt.oid
LEFT JOIN pg_catalog.pg_namespace nt
ON nt.oid = tt.typnamespace
LEFT JOIN pg_catalog.pg_description d
ON d.classoid = c.tableoid AND d.objoid = c.oid AND d.objsubid = 0
WHERE ( (true AND pg_catalog.pg_type_is_visible(ts.oid)
) OR (true AND pg_catalog.pg_type_is_visible(tt.oid)
) )
ORDER BY 1, 2;
...which points us to a few more interesting docs pages.
pg_cast
- the base system table listing permissible castspg_type
- the base system table reference for datatypesformat_type()
- a prettifier function forpg_type
. Helpful in mapping proper names to aliases as seen on the datatypes reference table