跳转到主要内容

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

ID Extension Package Version Category License Language
4855 pg_try_cast pg_try_cast 1.0 FUNC PostgreSQL C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-dtr No Yes No Yes yes yes
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

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT PIGSTY 1.0 18 17 16 15 14 pg_try_cast -
RPM PIGSTY 1.0.0 18 17 16 15 14 pg_try_cast_$v -
DEB PIGSTY 1.0.0 18 17 16 15 14 postgresql-$v-pg-try-cast -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
el8.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
el9.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
el9.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
el10.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
el10.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
d12.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
d12.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
d13.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
d13.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
u22.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
u22.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
u24.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
u24.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
u26.x86_64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
u26.aarch64 PIGSTY 1.0.0 PIGSTY 1.0.0 PIGSTY 1.0.0 N/A N/A
Package Version OS ORG SIZE File URL
pg_try_cast_18 1.0.0 el8.x86_64 pigsty 21.4 KiB pg_try_cast_18-1.0.0-1PGSTY.el8.x86_64.rpm
pg_try_cast_18 1.0.0 el8.aarch64 pigsty 21.4 KiB pg_try_cast_18-1.0.0-1PGSTY.el8.aarch64.rpm
pg_try_cast_18 1.0.0 el9.x86_64 pigsty 21.7 KiB pg_try_cast_18-1.0.0-1PGSTY.el9.x86_64.rpm
pg_try_cast_18 1.0.0 el9.aarch64 pigsty 21.5 KiB pg_try_cast_18-1.0.0-1PGSTY.el9.aarch64.rpm
pg_try_cast_18 1.0.0 el10.x86_64 pigsty 21.8 KiB pg_try_cast_18-1.0.0-1PGSTY.el10.x86_64.rpm
pg_try_cast_18 1.0.0 el10.aarch64 pigsty 21.7 KiB pg_try_cast_18-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-try-cast 1.0.0 d12.x86_64 pigsty 13.7 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-try-cast 1.0.0 d12.aarch64 pigsty 13.7 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-try-cast 1.0.0 d13.x86_64 pigsty 13.7 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-18-pg-try-cast 1.0.0 d13.aarch64 pigsty 13.7 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-18-pg-try-cast 1.0.0 u22.x86_64 pigsty 13.5 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-18-pg-try-cast 1.0.0 u22.aarch64 pigsty 13.6 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-18-pg-try-cast 1.0.0 u24.x86_64 pigsty 13.8 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~noble_amd64.deb
postgresql-18-pg-try-cast 1.0.0 u24.aarch64 pigsty 13.7 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~noble_arm64.deb
postgresql-18-pg-try-cast 1.0.0 u26.x86_64 pigsty 13.9 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-18-pg-try-cast 1.0.0 u26.aarch64 pigsty 13.8 KiB postgresql-18-pg-try-cast_1.0.0-1PGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
pg_try_cast_17 1.0.0 el8.x86_64 pigsty 21.4 KiB pg_try_cast_17-1.0.0-1PGSTY.el8.x86_64.rpm
pg_try_cast_17 1.0.0 el8.aarch64 pigsty 21.4 KiB pg_try_cast_17-1.0.0-1PGSTY.el8.aarch64.rpm
pg_try_cast_17 1.0.0 el9.x86_64 pigsty 21.7 KiB pg_try_cast_17-1.0.0-1PGSTY.el9.x86_64.rpm
pg_try_cast_17 1.0.0 el9.aarch64 pigsty 21.4 KiB pg_try_cast_17-1.0.0-1PGSTY.el9.aarch64.rpm
pg_try_cast_17 1.0.0 el10.x86_64 pigsty 21.8 KiB pg_try_cast_17-1.0.0-1PGSTY.el10.x86_64.rpm
pg_try_cast_17 1.0.0 el10.aarch64 pigsty 21.7 KiB pg_try_cast_17-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-try-cast 1.0.0 d12.x86_64 pigsty 13.6 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-try-cast 1.0.0 d12.aarch64 pigsty 13.7 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-try-cast 1.0.0 d13.x86_64 pigsty 13.6 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-17-pg-try-cast 1.0.0 d13.aarch64 pigsty 13.7 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-17-pg-try-cast 1.0.0 u22.x86_64 pigsty 13.7 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-17-pg-try-cast 1.0.0 u22.aarch64 pigsty 13.9 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-17-pg-try-cast 1.0.0 u24.x86_64 pigsty 13.8 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~noble_amd64.deb
postgresql-17-pg-try-cast 1.0.0 u24.aarch64 pigsty 13.7 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~noble_arm64.deb
postgresql-17-pg-try-cast 1.0.0 u26.x86_64 pigsty 13.9 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-17-pg-try-cast 1.0.0 u26.aarch64 pigsty 13.8 KiB postgresql-17-pg-try-cast_1.0.0-1PGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
pg_try_cast_16 1.0.0 el8.x86_64 pigsty 21.4 KiB pg_try_cast_16-1.0.0-1PGSTY.el8.x86_64.rpm
pg_try_cast_16 1.0.0 el8.aarch64 pigsty 21.4 KiB pg_try_cast_16-1.0.0-1PGSTY.el8.aarch64.rpm
pg_try_cast_16 1.0.0 el9.x86_64 pigsty 21.7 KiB pg_try_cast_16-1.0.0-1PGSTY.el9.x86_64.rpm
pg_try_cast_16 1.0.0 el9.aarch64 pigsty 21.4 KiB pg_try_cast_16-1.0.0-1PGSTY.el9.aarch64.rpm
pg_try_cast_16 1.0.0 el10.x86_64 pigsty 21.8 KiB pg_try_cast_16-1.0.0-1PGSTY.el10.x86_64.rpm
pg_try_cast_16 1.0.0 el10.aarch64 pigsty 21.7 KiB pg_try_cast_16-1.0.0-1PGSTY.el10.aarch64.rpm
postgresql-16-pg-try-cast 1.0.0 d12.x86_64 pigsty 13.6 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~bookworm_amd64.deb
postgresql-16-pg-try-cast 1.0.0 d12.aarch64 pigsty 13.7 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~bookworm_arm64.deb
postgresql-16-pg-try-cast 1.0.0 d13.x86_64 pigsty 13.6 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~trixie_amd64.deb
postgresql-16-pg-try-cast 1.0.0 d13.aarch64 pigsty 13.8 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~trixie_arm64.deb
postgresql-16-pg-try-cast 1.0.0 u22.x86_64 pigsty 13.7 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~jammy_amd64.deb
postgresql-16-pg-try-cast 1.0.0 u22.aarch64 pigsty 13.9 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~jammy_arm64.deb
postgresql-16-pg-try-cast 1.0.0 u24.x86_64 pigsty 13.8 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~noble_amd64.deb
postgresql-16-pg-try-cast 1.0.0 u24.aarch64 pigsty 13.7 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~noble_arm64.deb
postgresql-16-pg-try-cast 1.0.0 u26.x86_64 pigsty 13.9 KiB postgresql-16-pg-try-cast_1.0.0-1PGSTY~resolute_amd64.deb
postgresql-16-pg-try-cast 1.0.0 u26.aarch64 pigsty 13.8 KiB postgresql-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 via package name, for the active PG version

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.