From 0e1e899955dbedbadc909c6ca54c9ddcfb545b73 Mon Sep 17 00:00:00 2001 From: Haibo Yan Date: Sun, 28 Jun 2026 13:28:19 -0700 Subject: [PATCH 4/4] Add built-in format casts for CAST ... FORMAT Add built-in format cast functions and pg_format_cast bootstrap rows for the initial datetime/string formatted casts: text/varchar/bpchar <-> date text/varchar/bpchar <-> timestamp text/varchar/bpchar <-> timestamptz The wrappers reuse PostgreSQL's existing formatting code and are reached through pg_format_cast, not by parser or analyzer special cases. They are STRICT, so NULL input or NULL FORMAT returns NULL. This provides SQL-standard-style CAST ... FORMAT support for the common datetime/string cases, while keeping PostgreSQL extensions from the generic infrastructure: FORMAT may be any expression coercible to text, and format cast lookup remains catalog-driven. The patch does not claim complete SQL-standard datetime-template conformance; time, timetz, numeric, interval, and fuller template coverage are left for future work. Built-in format cast rows are treated as system objects and are not dumped by pg_dump. --- src/backend/utils/adt/formatting.c | 317 +++++++++++++++++++++ src/bin/pg_dump/pg_dump.c | 25 +- src/include/catalog/pg_format_cast.dat | 71 +++++ src/include/catalog/pg_proc.dat | 75 +++++ src/test/regress/expected/expressions.out | 25 +- src/test/regress/expected/format_casts.out | 279 +++++++++++++++++- src/test/regress/sql/expressions.sql | 17 +- src/test/regress/sql/format_casts.sql | 128 ++++++++- 8 files changed, 914 insertions(+), 23 deletions(-) create mode 100644 src/include/catalog/pg_format_cast.dat diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index d52d71b0a8c..d42dea166b6 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -4138,6 +4138,323 @@ to_date(PG_FUNCTION_ARGS) PG_RETURN_DATEADT(result); } +/* ---------- + * Built-in format casts for CAST ( AS FORMAT