Skip to content

pg_try_cast

Text conversion with NULL or a typed fallback on conversion errors.

pg_try_cast · Text conversion with NULL or a typed fallback on conversion errors.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4855pg_try_castpg_try_cast1.0FUNCPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-dtrNoYesNoYesyesyes
Release 1.0.0 installs control/SQL 1.0; META provides.version says 1.0.0. PG16+ required, CI covers 16-18; input types without soft-error support may still raise errors.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY1.019 18 17 16 15pg_try_cast-
RPMPIGSTY1.0.019 18 17 16 15pg_try_cast_$v-
DEBPIGSTY1.0.019 18 17 16 15postgresql-$v-pg-try-cast-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
el8.aarch64N/APIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
el9.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
el9.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
el10.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
el10.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
d12.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
d12.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
d13.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
d13.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
u22.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
u22.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
u24.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
u24.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
u26.x86_64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
u26.aarch64PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0PIGSTY 1.0.0N/A
PackageVersionOSORGSIZEFile URL
pg_try_cast_191.0.0el9.x86_64pigsty21.8 KiBpg_try_cast_19-1.0.0-1PGSTY.el9.x86_64.rpm
pg_try_cast_191.0.0el9.aarch64pigsty21.7 KiBpg_try_cast_19-1.0.0-1PGSTY.el9.aarch64.rpm
pg_try_cast_191.0.0el10.x86_64pigsty21.8 KiBpg_try_cast_19-1.0.0-1PGSTY.el10.x86_64.rpm
pg_try_cast_191.0.0el10.aarch64pigsty21.7 KiBpg_try_cast_19-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-try-cast1.0.0d12.x86_64pigsty13.7 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-try-cast1.0.0d12.aarch64pigsty13.7 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-try-cast1.0.0d13.x86_64pigsty13.7 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-19-pg-try-cast1.0.0d13.aarch64pigsty13.7 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-19-pg-try-cast1.0.0u22.x86_64pigsty13.5 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-19-pg-try-cast1.0.0u22.aarch64pigsty13.6 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-19-pg-try-cast1.0.0u24.x86_64pigsty13.8 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~noble_amd64.deb
postgresql-19-pg-try-cast1.0.0u24.aarch64pigsty13.7 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~noble_arm64.deb
postgresql-19-pg-try-cast1.0.0u26.x86_64pigsty13.9 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-19-pg-try-cast1.0.0u26.aarch64pigsty13.8 KiBpostgresql-19-pg-try-cast_1.0.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_try_cast_181.0.0el8.x86_64pigsty21.5 KiBpg_try_cast_18-1.0.0-1PGSTY.el8.x86_64.rpm
pg_try_cast_181.0.0el8.aarch64pigsty21.7 KiBpg_try_cast_18-1.0.0-1PGSTY.el8.aarch64.rpm
pg_try_cast_181.0.0el9.x86_64pigsty21.8 KiBpg_try_cast_18-1.0.0-1PGSTY.el9.x86_64.rpm
pg_try_cast_181.0.0el9.aarch64pigsty21.7 KiBpg_try_cast_18-1.0.0-1PGSTY.el9.aarch64.rpm
pg_try_cast_181.0.0el10.x86_64pigsty21.8 KiBpg_try_cast_18-1.0.0-1PGSTY.el10.x86_64.rpm
pg_try_cast_181.0.0el10.aarch64pigsty21.7 KiBpg_try_cast_18-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-try-cast1.0.0d12.x86_64pigsty13.7 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-try-cast1.0.0d12.aarch64pigsty13.7 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-try-cast1.0.0d13.x86_64pigsty13.7 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-18-pg-try-cast1.0.0d13.aarch64pigsty13.7 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-18-pg-try-cast1.0.0u22.x86_64pigsty13.4 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-18-pg-try-cast1.0.0u22.aarch64pigsty13.6 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-18-pg-try-cast1.0.0u24.x86_64pigsty13.8 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~noble_amd64.deb
postgresql-18-pg-try-cast1.0.0u24.aarch64pigsty13.7 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~noble_arm64.deb
postgresql-18-pg-try-cast1.0.0u26.x86_64pigsty13.9 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-18-pg-try-cast1.0.0u26.aarch64pigsty13.8 KiBpostgresql-18-pg-try-cast_1.0.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_try_cast_171.0.0el8.x86_64pigsty21.5 KiBpg_try_cast_17-1.0.0-1PGSTY.el8.x86_64.rpm
pg_try_cast_171.0.0el8.aarch64pigsty21.6 KiBpg_try_cast_17-1.0.0-1PGSTY.el8.aarch64.rpm
pg_try_cast_171.0.0el9.x86_64pigsty21.8 KiBpg_try_cast_17-1.0.0-1PGSTY.el9.x86_64.rpm
pg_try_cast_171.0.0el9.aarch64pigsty21.6 KiBpg_try_cast_17-1.0.0-1PGSTY.el9.aarch64.rpm
pg_try_cast_171.0.0el10.x86_64pigsty21.8 KiBpg_try_cast_17-1.0.0-1PGSTY.el10.x86_64.rpm
pg_try_cast_171.0.0el10.aarch64pigsty21.7 KiBpg_try_cast_17-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-try-cast1.0.0d12.x86_64pigsty13.6 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-try-cast1.0.0d12.aarch64pigsty13.7 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-try-cast1.0.0d13.x86_64pigsty13.6 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-17-pg-try-cast1.0.0d13.aarch64pigsty13.7 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-17-pg-try-cast1.0.0u22.x86_64pigsty13.7 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-17-pg-try-cast1.0.0u22.aarch64pigsty13.9 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-17-pg-try-cast1.0.0u24.x86_64pigsty13.8 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~noble_amd64.deb
postgresql-17-pg-try-cast1.0.0u24.aarch64pigsty13.7 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~noble_arm64.deb
postgresql-17-pg-try-cast1.0.0u26.x86_64pigsty13.9 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-17-pg-try-cast1.0.0u26.aarch64pigsty13.8 KiBpostgresql-17-pg-try-cast_1.0.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_try_cast_161.0.0el8.x86_64pigsty21.5 KiBpg_try_cast_16-1.0.0-1PGSTY.el8.x86_64.rpm
pg_try_cast_161.0.0el8.aarch64pigsty21.6 KiBpg_try_cast_16-1.0.0-1PGSTY.el8.aarch64.rpm
pg_try_cast_161.0.0el9.x86_64pigsty21.8 KiBpg_try_cast_16-1.0.0-1PGSTY.el9.x86_64.rpm
pg_try_cast_161.0.0el9.aarch64pigsty21.7 KiBpg_try_cast_16-1.0.0-1PGSTY.el9.aarch64.rpm
pg_try_cast_161.0.0el10.x86_64pigsty21.8 KiBpg_try_cast_16-1.0.0-1PGSTY.el10.x86_64.rpm
pg_try_cast_161.0.0el10.aarch64pigsty21.7 KiBpg_try_cast_16-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-16-pg-try-cast1.0.0d12.x86_64pigsty13.6 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-16-pg-try-cast1.0.0d12.aarch64pigsty13.7 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-16-pg-try-cast1.0.0d13.x86_64pigsty13.6 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-16-pg-try-cast1.0.0d13.aarch64pigsty13.7 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-16-pg-try-cast1.0.0u22.x86_64pigsty13.7 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-16-pg-try-cast1.0.0u22.aarch64pigsty13.9 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-16-pg-try-cast1.0.0u24.x86_64pigsty13.8 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~noble_amd64.deb
postgresql-16-pg-try-cast1.0.0u24.aarch64pigsty13.7 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~noble_arm64.deb
postgresql-16-pg-try-cast1.0.0u26.x86_64pigsty13.9 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-16-pg-try-cast1.0.0u26.aarch64pigsty13.8 KiBpostgresql-16-pg-try-cast_1.0.0-1PGSTY~resolute_arm64.deb

Source

pig build pkg pg_try_cast;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_try_cast;		# install by package name

pig install pg_try_cast -v 19;   # install for PG 19
pig install pg_try_cast -v 18;   # install for PG 18
pig install pg_try_cast -v 17;   # install for PG 17
pig install pg_try_cast -v 16;   # install for PG 16

Create this extension with:

CREATE EXTENSION pg_try_cast;

Usage

Sources:

pg_try_cast provides try_cast and try_convert for converting text without aborting a query on supported input errors. Release 1.0.0 installs SQL version 1.0. PostgreSQL 16 or later is required; upstream CI covers 16–18. It is trusted and relocatable, with no preload requirement.

Core Workflow

CREATE EXTENSION pg_try_cast;
SELECT try_cast('42', NULL::integer);
SELECT try_cast('invalid', 0);
SELECT try_convert(NULL::date, '2026-02-30');

Operational Boundaries

try_cast(value text, fallback anyelement) takes its result type and error fallback from the second argument; try_convert(fallback anyelement, value text) reverses the order. NULL input returns NULL. Give the fallback an explicit type. Type modifiers and domain constraints are checked. This covers text input, not arbitrary native casts. Input functions without soft-error support can still raise an error, including malformed type names passed to regtype. A role with CREATE privilege on the database can install this trusted extension after its files are installed.

Table reader