Available functions and operators

This page lists the PostgreSQL functions and operators available in this library. Each category below links to its own page with the details.

See also: Query results for the PHP value a function result arrives as

Operator conflicts and usage notes

Some PostgreSQL operators have multiple meanings depending on the data types involved. This library provides specific DQL function names to avoid conflicts:

Operator Array/JSON usage Spatial usage Text/pattern usage
@> CONTAINS (arrays contain elements) N/A (PostGIS defines no @>; see ~) N/A
<@ IS_CONTAINED_BY (element in array) N/A (PostGIS defines no <@; see @) N/A
@ N/A SPATIAL_CONTAINED_BY (bounding box contained) N/A
~ N/A SPATIAL_CONTAINS (bounding box contains) REGEXP (text pattern matching)
&& OVERLAPS (arrays/ranges overlap) Works automatically with geometry/geography N/A

Usage Guidelines:

  • Arrays/JSON: Use CONTAINS, IS_CONTAINED_BY, OVERLAPS for array and JSON operations → Array and JSON functions
  • Spatial: Use SPATIAL_CONTAINS, SPATIAL_CONTAINED_BY for explicit spatial bounding box operations → PostGIS spatial functions
  • Text: Use REGEXP, IREGEXP for pattern matching → Text and pattern functions
  • Boolean operators: the containment, overlap and bounding box operators return booleans and must be compared with = TRUE or = FALSE in DQL
  • The rules behind these names, and the ones every function follows (= TRUE, FILTER and OVER, literals and parameters): Writing DQL

Function and operator categories

Each category has its own page:

Array and JSON functions

Array and JSON functions and operators: the array operators (@>, <@, &&), the JSON operators (->, ->>, #>, #>>), and the array, JSON, JSONB and JSON path functions.

PostGIS spatial functions

PostGIS spatial functions and operators: the bounding box and distance operators, and the accessor, constructor, relationship, measurement, overlay, processing, editor and linear referencing functions.

Text and pattern functions

Text and pattern functions and operators: the text operators (~, ilike, @@), regular expressions, text processing, full-text search, fuzzy string matching, and hashing and checksums.

Date and range functions

Date, time, and range functions: date and time arithmetic and extraction, range constructors, and the range operators.

Mathematical functions

Mathematical and statistical functions: arithmetic, rounding, trigonometry, random numbers, the bitwise and boolean aggregates (BIT_AND, BOOL_OR, EVERY, …) and the statistical aggregates (CORR, STDDEV, PERCENTILE_CONT, MODE, …).

Utility functions

Utility functions: type casting (CAST), formatting (TO_CHAR, TO_NUMBER), NULL checks (ERROR_ON_NULL), and UUID generation and inspection (GEN_RANDOM_UUID, UUIDV4, UUIDV7, UUID_EXTRACT_TIMESTAMP, UUID_EXTRACT_VERSION).

XML functions

XML functions: aggregation (XMLAGG), well-formedness checks (XML_IS_WELL_FORMED, XML_IS_WELL_FORMED_DOCUMENT, XML_IS_WELL_FORMED_CONTENT), construction (XMLTEXT, XMLCOMMENT, XMLCONCAT, XMLPI), serialization (XMLSERIALIZE) and XPath (XPATH, XPATH_EXISTS, XMLEXISTS).

Window functions

Window functions: OVER for running any aggregate over a window, with frame clauses, the ranking functions ROW_NUMBER, RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST and NTILE, and the value functions LAG, LEAD, FIRST_VALUE, LAST_VALUE and NTH_VALUE.

Network address functions

Network address functions: taking an inet or cidr value apart (HOST, BROADCAST, NETWORK, NETMASK, HOSTMASK), reading it (FAMILY, MASKLEN, ABBREV), combining two (INET_MERGE, INET_SAME_FAMILY) and changing the mask (SET_MASKLEN).

Hstore functions (requires hstore extension)

Hstore functions: keys and values as arrays or sets (HSTORE_AKEYS, HSTORE_SKEYS, HSTORE_AVALS, HSTORE_SVALS), HSTORE_DEFINED, HSTORE_DELETE, and conversion to JSON (HSTORE_TO_JSON, HSTORE_TO_JSON_LOOSE).

Ltree functions

PostgreSQL ltree types: extracting subpaths, path length and position, the longest common ancestor, conversion to and from text, and the lquery and ltxtquery match operators.

Vector distance functions (requires pgvector extension)

Distance functions for fixed-dimension float vectors stored with the vector type:

  • L2_DISTANCE - Euclidean (L2) distance between two vectors
  • COSINE_DISTANCE - Cosine distance between two vectors
  • INNER_PRODUCT - Inner (dot) product of two vectors

Quick reference

Most commonly used functions

Array Operations: (full list)

  • CONTAINS (@>) - Test if array/range contains elements
  • OVERLAPS (&&) - Test if arrays/ranges overlap
  • ARRAY_AGG - Aggregate values into arrays
  • TRIM_ARRAY - Remove the last elements of an array
  • FILTER - Restrict the rows an aggregate reads (FILTER (WHERE ...))

JSON Operations: (full list)

  • JSON_GET_FIELD_AS_TEXT (->>) - Extract JSON field as text
  • JSON_BUILD_OBJECT - Build JSON objects
  • JSON - Parse text as JSON, optionally rejecting repeated keys (WITH UNIQUE KEYS)
  • JSONB_PATH_EXISTS - Test JSON path existence

Spatial Operations: (full list)

  • ST_INTERSECTS - Test if geometries intersect
  • ST_DISTANCE - Calculate distance between geometries
  • ST_CONTAINS - Test spatial containment

Text Operations: (full list)

  • ASCII - Get numeric code of first character
  • BTRIM/LTRIM/RTRIM - Trim characters from string ends
  • CASEFOLD - Fold case for a case-insensitive comparison that follows Unicode (PostgreSQL 18)
  • CHR - Get character from code point
  • ILIKE - Case-insensitive pattern matching
  • INITCAP - Capitalize first letter of each word
  • LPAD/RPAD - Left/right pad a string to a given length
  • QUOTE_IDENT/QUOTE_LITERAL/QUOTE_NULLABLE - Quote SQL identifiers and literals
  • REGEXP (~) - Regular expression matching
  • REGEXP_SPLIT_TO_ARRAY - Split text into an array at each regular expression match
  • STARTS_WITH - Test if text starts with a substring
  • STRPOS - Find position of substring
  • TRANSLATE - Replace characters in a string
  • LEVENSHTEIN - Calculate edit distance between strings (fuzzy matching)
  • SOUNDEX - Phonetic encoding for similarity matching
  • MD5/SHA256/SHA512 (and other SHA variants) - Cryptographic hashing
  • CRC32/CRC32C - CRC checksum computation
  • BIT_COUNT - Count the bits set in a binary or bit string value
  • REVERSE_BYTES - Reverse byte order for bytea values

Date/Range Operations: (full list)

  • CLOCK_TIMESTAMP - Current timestamp at call time
  • DATE_ADD - Add interval to date
  • DATE_EXTRACT - Extract date components
  • DATERANGE - Create date ranges
  • ISFINITE - Test if date/timestamp/interval is finite
  • JUSTIFY_DAYS/JUSTIFY_HOURS/JUSTIFY_INTERVAL - Adjust interval representations
  • MAKE_INTERVAL - Build an interval from years, months, weeks, days, hours, minutes and seconds
  • RANGE_MINUS_MULTI/MULTIRANGE_MINUS_MULTI - Subtract ranges or multiranges without failing on a gap
  • STATEMENT_TIMESTAMP - Timestamp of current SQL statement
  • TRANSACTION_TIMESTAMP - Timestamp of current transaction

Mathematical Operations: (full list)

  • DIV/GCD/LCM/FACTORIAL - Integer arithmetic functions
  • ERF/ERFC - Error and complementary error functions
  • GAMMA/LGAMMA - Gamma function for statistical calculations
  • GREATEST/LEAST - Find maximum/minimum values
  • ROUND/SCALE/MIN_SCALE/TRIM_SCALE - Numeric precision functions
  • RANDOM/RANDOM_NORMAL - Generate random numbers, optionally between bounds
  • Bitwise/Boolean Aggregates: BIT_AND, BIT_OR, BIT_XOR, BOOL_AND, BOOL_OR, EVERY
  • Statistical Aggregates: CORR, COVAR_POP, COVAR_SAMP, MODE, PERCENTILE_CONT, PERCENTILE_DISC, STDDEV, STDDEV_POP, VAR_POP, VARIANCE

Utility Functions: (full list)

  • CAST - General type conversion
  • PG_INPUT_IS_VALID - Test whether a string would convert to a type, without failing the query
  • TO_CHAR - Convert numbers and dates to formatted strings
  • TO_NUMBER - Parse formatted text as numbers
  • TO_HEX/TO_BIN/TO_OCT - Write an integer in hexadecimal, binary or octal
  • ERROR_ON_NULL - Return a value, or fail the query when it is NULL
  • GEN_RANDOM_UUID - Generate a random UUID (version 4)
  • UUIDV4 - Explicit UUID version 4 generation
  • UUIDV7 - Generate a UUID (version 7) that sorts by creation time
  • UUID_EXTRACT_TIMESTAMP - Extract timestamp from UUID v1 or v7
  • UUID_EXTRACT_VERSION - Extract version number from UUID

Network Address Operations: (full list)

  • HOST/NETWORK/BROADCAST - Extract address parts
  • MASKLEN/NETMASK/HOSTMASK - Mask information
  • INET_MERGE - Smallest network containing two addresses

Hstore Operations: (full list)

  • HSTORE_AKEYS - Return hstore keys as an array
  • HSTORE_AVALS - Return hstore values as an array
  • HSTORE_DEFINED - Check if key exists and is not NULL
  • HSTORE_DELETE - Delete key from hstore

Ltree Operations: (full list)

  • SUBLTREE - Extract subpath from ltree
  • SUBPATH - Extract subpath with offset and length
  • NLEVEL - Get number of labels in path
  • INDEX - Find position of ltree in another ltree
  • LCA - Find longest common ancestor
  • TEXT2LTREE - Cast text to ltree
  • LTREE2TEXT - Cast ltree to text
  • MATCHES_LQUERY - Check whether a path matches an lquery pattern (~)
  • MATCHES_ANY_LQUERY - Check whether a path matches any lquery pattern in an array (?)
  • MATCHES_LTXTQUERY - Check whether a path matches an ltxtquery label query (@)

Vector Distance Operations:

  • L2_DISTANCE - Euclidean distance between vectors
  • COSINE_DISTANCE - Cosine distance between vectors
  • INNER_PRODUCT - Inner product of two vectors

Composite Types:

  • COMPOSITE_FIELD - Access a field from a PostgreSQL composite type column → Examples

XML Functions: (full list)

  • XML_IS_WELL_FORMED - Check whether a text string is well-formed XML
  • XML_IS_WELL_FORMED_CONTENT - Check whether a text string is well-formed XML content
  • XML_IS_WELL_FORMED_DOCUMENT - Check whether a text string is a well-formed XML document
  • XMLAGG - Aggregate XML values (supports ORDER BY)
  • XMLCOMMENT - Create an XML comment
  • XMLCONCAT - Concatenate multiple XML values into a single XML value
  • XMLEXISTS - Test if an XPath expression matches any nodes in an XML value
  • XMLPI - Create an XML processing instruction
  • XMLSERIALIZE - Serialize an XML value into a character string, optionally indented
  • XMLTEXT - Create an XML text node (with entity escaping)
  • XPATH - Evaluate an XPath expression against an XML value, returning matched nodes
  • XPATH_EXISTS - Test if an XPath expression matches any node in an XML value

Window Functions: (full list)

  • OVER - Run an aggregate or a window function over a window, such as running totals and moving averages
  • ROW_NUMBER/RANK/DENSE_RANK - Number or rank rows within their partition
  • PERCENT_RANK/CUME_DIST - Relative rank and cumulative distribution within the partition
  • NTILE - Split the partition into a number of buckets
  • LAG/LEAD - Value from a row before or after the current one, optionally skipping NULLs with IGNORE NULLS
  • FIRST_VALUE/LAST_VALUE/NTH_VALUE - Value from the first, last or n-th row of the window frame

Table of contents