Posts

Showing posts with the label explicit cast

Casting

Image
Introduction In PostgreSQL, whether two Data Type s can be cast to each other and how type casting precedence is handled follows a system catalog table named pg_cast ( System Catalog ), which defines three levels of permission including: Implicit Cast Converted automatically without requiring any extra action The condition is that the two types must be truly compatible and not result in data loss For example: SMALLINT -> INTEGER -> BIGINT -> NUMERIC Assignment Cast Only automatically casts when executing an INSERT or UPDATE statement into a target column's data type For example: INSERTing VARCHAR data into a TEXT column Explicit Cast (Mandatory manual casting) When using syntax to manually cast types such as :: or CAST(x AS type) For example: Casting the string '123' or '2026-07-21' to INTEGER or DATE ( '123'::integer ) If there is no cast definition between Type A and Type B in pg_cast , casting cannot be performed without an intermediate co...