跳转到主要内容

pg_try_cast

文本转换失败时返回 NULL 或指定类型的回退值。

pg_try_cast · 文本转换失败时返回 NULL 或指定类型的回退值。

概览

ID扩展软件包版本类别许可证编程语言
4855pg_try_castpg_try_cast1.0FUNCPostgreSQLC
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-dtr否是否是是是
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.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
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
软件包版本平台仓库大小下载
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
软件包版本平台仓库大小下载
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
软件包版本平台仓库大小下载
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
软件包版本平台仓库大小下载
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

源码

pig build pkg pg_try_cast;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

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

使用 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 EXTENSION pg_try_cast;

用法

来源:

pg_try_cast 提供 try_cast 与 try_convert,在受支持的输入转换出错时返回回退值,避免整个查询失败。发布版本 1.0.0 安装的 SQL 版本为 1.0。要求 PostgreSQL 16 或更新版本,上游 CI 覆盖 16–18。扩展可信、可迁移,无需预加载。

核心用法

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

运行边界

try_cast(value text, fallback anyelement) 的第二个参数同时决定结果类型和出错回退值;try_convert(fallback anyelement, value text) 的参数顺序相反。NULL 输入仍返回 NULL,应为回退值指定明确类型。扩展会检查类型修饰符与域约束。它处理文本输入转换,不涵盖任意原生类型转换。尚未支持软错误机制的输入函数仍可能抛出错误,包括向 regtype 传入格式错误的类型名称。安装文件就绪后,拥有数据库 CREATE 权限的角色可安装此可信扩展。

表格阅读器