Skip to content

format_x

Polymorphic variadic string formatting with named lookups into composite, JSONB, and HSTORE values.

format_x · Polymorphic variadic string formatting with named lookups into composite, JSONB, and HSTORE values.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100546format_xformat_x0.0.1FUNCMITC

PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18

Usage

Sources:

format_x extends PostgreSQL’s format() with named and chained lookups into composite values, json, jsonb, and hstore. Use it to format structured values without extracting every field first.

Named Lookups

CREATE EXTENSION format_x;

SELECT format_x(
  'Hello %(name)s from %(address.city)s',
  '{"name":"Ada","address":{"city":"London"}}'::jsonb
);

A lookup has the form %(key)s; dots traverse nested values. Positional argument selection can be combined with a lookup, such as %2(name)s. The standard %s, %I, and %L conversions, position fields, widths, and precision are also supported.

SELECT format_x(
  'SELECT * FROM %1(name)I WHERE code = %1(code)L',
  '{"name":"places","code":"GB"}'::jsonb
);

Inputs and Caveats

The installed overloads are format_x(text) and format_x(text, VARIADIC "any"). Composite, JSON, and JSONB lookups work directly. hstore is only needed when an hstore value is passed; it is not a declared extension dependency.

Missing keys, unsupported input types, and some NULL lookups raise errors. %I quotes an identifier and rejects NULL; %L quotes a literal using quote_nullable behavior. Quoting fields does not make an arbitrary format template safe: keep the template trusted and use parameters when executing generated SQL. This version uses PostgreSQL internal formatting and type APIs, so verify the build on the exact PostgreSQL release before deployment.

Table reader