Sub topic 11
Inspecting `pg_cast`
Assume you know basic SELECT syntax and common PostgreSQL types. This chapter shows how to inspect PostgreSQL’s catalog of registered casts and use it to predict whether a conversion is implicit, assignment-only, or explicit.
What `pg_cast` records
pg_cast is a system catalog. A system catalog is a table that stores metadata about objects in the current database. Each row in pg_cast describes a conversion from one data type to another.
The important columns are:
| Column | Meaning |
|---|---|
| `castsource` | OID of the source type |
| `casttarget` | OID of the target type |
| `castmethod` | How PostgreSQL performs the conversion |
| `castcontext` | Where PostgreSQL may use the cast |
| `castfunc` | Function used by the cast, when applicable |
An OID is PostgreSQL’s internal identifier for an object. The ::regtype output conversion turns a type OID into a readable type name.
SELECT castsource::regtype AS source_type,
casttarget::regtype AS target_type,
castmethod,
castcontext,
castfunc::regprocedure AS cast_function
FROM pg_cast
WHERE castsource = 'int4'::regtype
AND casttarget IN ('int8'::regtype, 'int2'::regtype);A typical result shows that integer to bigint has an implicit cast, while integer to smallint is assignment-only.
Reading `castcontext`
The castcontext column uses one letter:
| Value | Meaning |
|---|---|
| `i` | Implicit. PostgreSQL may insert the cast during expression resolution. |
| `a` | Assignment. PostgreSQL may use it when storing a value in a target column. |
| `e` | Explicit only. You must request the conversion. |
For example, PostgreSQL can usually widen an integer automatically:
SELECT 1::integer + 2::bigint AS total;The integer value can become a bigint because the conversion is implicit. Narrowing from integer to smallint is less safe because the value might be outside the smaller type’s range. PostgreSQL therefore does not normally use that conversion to resolve an expression automatically.
Assignment casts have a narrower purpose:
CREATE TABLE measurements (
value smallint
);
INSERT INTO measurements (value)
VALUES (12);The integer literal can be converted while assigning it to the smallint column. This does not mean PostgreSQL will use the same conversion for every operator or function call.
Reading `castmethod`
The castmethod column describes the implementation:
fmeans PostgreSQL calls the function named bycastfunc.bmeans the source and target have compatible internal representations, so no conversion function is needed.imeans PostgreSQL uses input/output functions to convert through text.
You can inspect all casts involving a type:
SELECT castsource::regtype AS source_type,
casttarget::regtype AS target_type,
castmethod,
castcontext
FROM pg_cast
WHERE castsource = 'text'::regtype
OR casttarget = 'text'::regtype
ORDER BY source_type::text, target_type::text;How to use the catalog safely
pg_cast helps explain parser behavior, but it is not a substitute for testing a complete expression. Operator overloads, function resolution, preferred types, and literal handling also affect the final choice.
Treat pg_cast as read-only metadata. Do not update its rows directly. PostgreSQL maintains this catalog through CREATE CAST, extension installation, and system upgrades.
Exercise
What does a castcontext value of a mean, and why might PostgreSQL use that cast for an INSERT but not for an arithmetic expression?
Show answer
It means assignment-only. PostgreSQL may use the cast when storing a value in a target column, but it will not automatically use it during general expression or operator resolution.