Other Functions
Changelog (last updated v2.2)
- v2.2:
current_databaserequires parentheses -
current_databaseis now a function —current_database()— rather than a bare keyword.Previously
SELECT current_databaseparsed as a reference to a reserved keyword. Now it’s a regular function call, which lets tools like Metabase useSELECT current_database() AS current_database(same name as keyword and column alias) without a parse error.Upgrade: rewrite any bare
current_databasereferences ascurrent_database().
CARDINALITY(list)- returns the number of elements in the list.
ARRAY_LENGTH(array, dimension)(v2.2+)- returns the number of elements in
arrayat the given dimension.- XTDB arrays are 1-dimensional, so
dimensionmust be1; any other value throws. - PostgreSQL-compatible.
- XTDB arrays are 1-dimensional, so
ARRAY_LOWER(array, dimension)(v2.2+)- returns the lower bound of
arrayat the given dimension.- Always returns
1— XTDB arrays are 1-indexed with no custom lower bounds. dimensionmust be1; any other value throws.- PostgreSQL-compatible.
- Always returns
LENGTH(expr)- returns the length of the value in
expr, where<expr>is one of the following:- A string: returns the number of utf8 characters in the string (alias for
CHAR_LENGTH) - A byte-array: returns the number of bytes in the array (alias for
OCTET_LENGTH) - A list: returns the number of elements in the list (alias for
CARDINALITY) - A set: returns the number of elements in the set
- A struct: returns the number of non-absent fields in the struct
- A string: returns the number of utf8 characters in the string (alias for
TRIM_ARRAY(array, n)- returns a copy of
arraywith the lastnelements removed. obj->field- PostgreSQL-compatible JSON field access operator. Extracts a field from a struct by key (preserving the original type).
fieldmust be a string literal (field name) or integer literal (for array index access)- Returns the value at the specified field/index
- Returns NULL if the field does not exist
- Example:
data->'age'returns theagefield from thedatastruct - Supports chaining:
data->'nested'->'inner'accesses nested fields
obj->>field- PostgreSQL-compatible JSON field access operator. Extracts a field from a struct by key as text.
- Same as
->but casts the result to text (string) fieldmust be a string literal (field name) or integer literal (for array index access)- Returns the value at the specified field/index as a string
- Returns NULL if the field does not exist
- Example:
data->>'age'returns theagefield from thedatastruct as text - Supports chaining:
data->'nested'->>'inner'accesses nested fields and returns as text
- Same as
obj#>path- PostgreSQL-compatible JSON path access operator. Extracts a nested field by following a path (preserving the original type).
pathmust be a literal array of string/integer elements (e.g.,ARRAY['nested', 'inner'])- Returns the value at the specified path
- Returns NULL if any step in the path does not exist
- Example:
data #> ARRAY['nested', 'inner']accessesdata.nested.inner - Equivalent to chaining
->operators but more concise for deep paths
obj#>>path- PostgreSQL-compatible JSON path access operator. Extracts a nested field by following a path as text.
- Same as
#>but casts the result to text (string) pathmust be a literal array of string/integer elements (e.g.,ARRAY['nested', 'inner'])- Returns the value at the specified path as a string
- Returns NULL if any step in the path does not exist
- Example:
data #>> ARRAY['nested', 'inner']accessesdata.nested.inneras text - Equivalent to chaining
->operators and ending with->>
- Same as
PostgreSQL built-in functions
Section titled “PostgreSQL built-in functions”current_database()(v2.2+)- returns the name of the current database.
current_setting(name)(v2.2+)- returns the value of a GUC parameter.
XTDB recognises a fixed set of parameter names — e.g.
'search_path'returns'"$user", public','server_version_num'returns the reported PostgreSQL version.