From deaec7cb45f1819ca95893916e104eb030772317 Mon Sep 17 00:00:00 2001 From: jian he Date: Tue, 14 Oct 2025 15:16:04 +0800 Subject: [PATCH 1/2] add function argument name to function substr with patch applied \df substr List of functions Schema | Name | Result data type | Argument data types | Type ------------+--------+------------------+--------------------------------------------+------ pg_catalog | substr | bytea | source bytea, start integer | func pg_catalog | substr | bytea | source bytea, start integer, count integer | func pg_catalog | substr | text | source text, start integer | func pg_catalog | substr | text | source text, start integer, count integer | func (4 rows) discussion: https://postgr.es/m/CACJufxHTBkymh06D4mGKNe1YfRNFN+gFBybmygWk=PtMqu00LQ@mail.gmail.com --- doc/src/sgml/func/func-binarystring.sgml | 4 ++-- doc/src/sgml/func/func-string.sgml | 4 ++-- src/include/catalog/pg_proc.dat | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/doc/src/sgml/func/func-binarystring.sgml b/doc/src/sgml/func/func-binarystring.sgml index b256381e01f0..580515951268 100644 --- a/doc/src/sgml/func/func-binarystring.sgml +++ b/doc/src/sgml/func/func-binarystring.sgml @@ -571,11 +571,11 @@ substr - substr ( bytes bytea, start integer , count integer ) + substr ( source bytea, start integer , count integer ) bytea - Extracts the substring of bytes starting at + Extracts the substring of source starting at the start'th byte, and extending for count bytes if that is specified. (Same diff --git a/doc/src/sgml/func/func-string.sgml b/doc/src/sgml/func/func-string.sgml index 7ad1436e5f82..3325ade065a1 100644 --- a/doc/src/sgml/func/func-string.sgml +++ b/doc/src/sgml/func/func-string.sgml @@ -1390,11 +1390,11 @@ substr - substr ( string text, start integer , count integer ) + substr ( source text, start integer , count integer ) text - Extracts the substring of string starting at + Extracts the substring of source starting at the start'th character, and extending for count characters if that is specified. (Same diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 5cf9e12fcb9a..aa92f72b2997 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3718,6 +3718,7 @@ prosrc => 'rtrim' }, { oid => '877', descr => 'extract portion of string', proname => 'substr', prorettype => 'text', proargtypes => 'text int4 int4', + proargnames => '{source, start, count}', prosrc => 'text_substr' }, { oid => '878', descr => 'map a set of characters appearing in string', proname => 'translate', prorettype => 'text', proargtypes => 'text text text', @@ -3736,6 +3737,7 @@ prosrc => 'rtrim1' }, { oid => '883', descr => 'extract portion of string', proname => 'substr', prorettype => 'text', proargtypes => 'text int4', + proargnames => '{source, start}', prosrc => 'text_substr_no_len' }, { oid => '884', descr => 'trim selected characters from both ends of string', proname => 'btrim', prorettype => 'text', proargtypes => 'text text', @@ -6306,9 +6308,11 @@ prosrc => 'bytea_substr_no_len' }, { oid => '2085', descr => 'extract portion of string', proname => 'substr', prorettype => 'bytea', proargtypes => 'bytea int4 int4', + proargnames => '{source, start, count}', prosrc => 'bytea_substr' }, { oid => '2086', descr => 'extract portion of string', proname => 'substr', prorettype => 'bytea', proargtypes => 'bytea int4', + proargnames => '{source, start}', prosrc => 'bytea_substr_no_len' }, { oid => '2014', descr => 'position of substring', proname => 'position', prorettype => 'int4', proargtypes => 'bytea bytea', From 010bc6ec781a8f003da81066ca7438041b2e67ec Mon Sep 17 00:00:00 2001 From: jian he Date: Mon, 10 Nov 2025 10:17:55 +0800 Subject: [PATCH 2/2] add function argument name to function substring with patch applied \df substring List of functions Schema | Name | Result data type | Argument data types | Type ------------+-----------+------------------+--------------------------------------------+------ pg_catalog | substring | bit | source bit, start integer | func pg_catalog | substring | bit | source bit, start integer, count integer | func pg_catalog | substring | bytea | source bytea, start integer | func pg_catalog | substring | bytea | source bytea, start integer, count integer | func pg_catalog | substring | text | source text, pattern text | func pg_catalog | substring | text | source text, pattern text, escape text | func pg_catalog | substring | text | source text, start integer | func pg_catalog | substring | text | source text, start integer, count integer | func (8 rows) discussion: https://postgr.es/m/CACJufxHTBkymh06D4mGKNe1YfRNFN+gFBybmygWk=PtMqu00LQ@mail.gmail.com --- doc/src/sgml/func/func-binarystring.sgml | 26 ++++++++-- doc/src/sgml/func/func-bitstring.sgml | 24 ++++++++- doc/src/sgml/func/func-matching.sgml | 28 ++++++----- doc/src/sgml/func/func-string.sgml | 62 +++++++++++++++++++++--- src/backend/catalog/system_functions.sql | 2 +- src/include/catalog/pg_proc.dat | 8 +++ 6 files changed, 127 insertions(+), 23 deletions(-) diff --git a/doc/src/sgml/func/func-binarystring.sgml b/doc/src/sgml/func/func-binarystring.sgml index 580515951268..24eb1ecf6e4e 100644 --- a/doc/src/sgml/func/func-binarystring.sgml +++ b/doc/src/sgml/func/func-binarystring.sgml @@ -200,11 +200,11 @@ substring - substring ( bytes bytea FROM start integer FOR count integer ) + substring ( source bytea FROM start integer FOR count integer ) bytea - Extracts the substring of bytes starting at + Extracts the substring of source starting at the start'th byte if that is specified, and stopping after count bytes if that is specified. Provide at least one of start @@ -216,6 +216,26 @@ + + + + substring + + substring ( source bytea, start integer , count integer ) + bytea + + + Extracts the substring of source starting at + the start'th byte, + and stopping after count bytes if that is + specified. + + + substring(source=>'\x1234567890'::bytea, start=>3, count=>2) + \x5678 + + + @@ -579,7 +599,7 @@ the start'th byte, and extending for count bytes if that is specified. (Same - as substring(bytes + as substring(source from start for count).) diff --git a/doc/src/sgml/func/func-bitstring.sgml b/doc/src/sgml/func/func-bitstring.sgml index 3f59de464a44..2dd828df4a4b 100644 --- a/doc/src/sgml/func/func-bitstring.sgml +++ b/doc/src/sgml/func/func-bitstring.sgml @@ -279,11 +279,11 @@ substring - substring ( bits bit FROM start integer FOR count integer ) + substring ( source bit FROM start integer FOR count integer ) bit - Extracts the substring of bits starting at + Extracts the substring of source starting at the start'th bit if that is specified, and stopping after count bits if that is specified. Provide at least one of start @@ -295,6 +295,26 @@ + + + + substring + + substring ( source bit, start integer , count integer ) + bit + + + Extracts the substring of source starting at + the start'th bit, + and stopping after count bits if that is + specified. + + + substring(source=>B'110010111111', start=>3, count=>2) + 00 + + + diff --git a/doc/src/sgml/func/func-matching.sgml b/doc/src/sgml/func/func-matching.sgml index 91a0b7ca0de3..228684397d02 100644 --- a/doc/src/sgml/func/func-matching.sgml +++ b/doc/src/sgml/func/func-matching.sgml @@ -234,13 +234,13 @@ -string SIMILAR TO pattern ESCAPE escape-character -string NOT SIMILAR TO pattern ESCAPE escape-character +source SIMILAR TO pattern ESCAPE escape-character +source NOT SIMILAR TO pattern ESCAPE escape-character The SIMILAR TO operator returns true or - false depending on whether its pattern matches the given string. + false depending on whether its pattern matches the given string (the source). It is similar to LIKE, except that it interprets the pattern using the SQL standard's definition of a regular expression. SQL regular expressions are a curious cross @@ -369,15 +369,15 @@ regular expression pattern. The function can be written according to standard SQL syntax: -substring(string similar pattern escape escape-character) +substring(source SIMILAR pattern ESCAPE escape-character) or using the now obsolete SQL:1999 syntax: -substring(string from pattern for escape-character) +substring(source FROM pattern FOR escape-character) or as a plain three-argument function: -substring(string, pattern, escape-character) +substring(source, pattern, escape) As with SIMILAR TO, the specified pattern must match the entire data string, or else the @@ -581,11 +581,17 @@ substring('foobar' SIMILAR '#"o_b#"%' ESCAPE '#') NULL - The substring function with two parameters, - substring(string from - pattern), provides extraction of a - substring - that matches a POSIX regular expression pattern. It returns null if + The substring function with two parameters provides extraction of a + substring that matches a POSIX regular expression pattern. + The function can be written according to standard SQL syntax: + +substring(source FROM pattern) + + It can also written as a plain two-argument function: + +substring(source, pattern) + + It returns null if there is no match, otherwise the first portion of the text that matched the pattern. But if the pattern contains any parentheses, the portion of the text that matched the first parenthesized subexpression (the diff --git a/doc/src/sgml/func/func-string.sgml b/doc/src/sgml/func/func-string.sgml index 3325ade065a1..e87bbb0dfe54 100644 --- a/doc/src/sgml/func/func-string.sgml +++ b/doc/src/sgml/func/func-string.sgml @@ -400,11 +400,11 @@ substring - substring ( string text FROM start integer FOR count integer ) + substring ( source text FROM start integer FOR count integer ) text - Extracts the substring of string starting at + Extracts the substring of source starting at the start'th character if that is specified, and stopping after count characters if that is specified. Provide at least one of start @@ -426,7 +426,7 @@ - substring ( string text FROM pattern text ) + substring ( source text FROM pattern text ) text @@ -441,11 +441,11 @@ - substring ( string text SIMILAR pattern text ESCAPE escape text ) + substring ( source text SIMILAR pattern text ESCAPE escape text ) text - substring ( string text FROM pattern text FOR escape text ) + substring ( source text FROM pattern text FOR escape text ) text @@ -1398,7 +1398,7 @@ the start'th character, and extending for count characters if that is specified. (Same - as substring(string + as substring(source from start for count).) @@ -1412,6 +1412,56 @@ + + + + substring + + substring ( source text, start integer , count integer ) + text + + + Extracts the substring of source starting at + the start'th character, + and stopping after count characters if that is + specified. + + + substring(source=>'Thomas', start=>2, count=>3) + hom + + + + + + substring ( source text, pattern text, escape text) + text + + + Extracts the first substring matching SQL regular expression; + see . + + + substring('Thomas', '%#"o_a#"_', '#') + oma + + + + + + substring ( source text, pattern text ) + text + + + Extracts the first substring matching POSIX regular expression; see + . + + + substring('Thomas', '...$') + mas + + + diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql index 2d946d6d9e9b..f0850f9ef647 100644 --- a/src/backend/catalog/system_functions.sql +++ b/src/backend/catalog/system_functions.sql @@ -42,7 +42,7 @@ CREATE OR REPLACE FUNCTION rpad(text, integer) IMMUTABLE PARALLEL SAFE STRICT COST 1 RETURN rpad($1, $2, ' '); -CREATE OR REPLACE FUNCTION "substring"(text, text, text) +CREATE OR REPLACE FUNCTION "substring"(source text, pattern text, escape text) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT COST 1 diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index aa92f72b2997..19700ebd9e6f 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3748,9 +3748,11 @@ { oid => '936', descr => 'extract portion of string', proname => 'substring', prorettype => 'text', proargtypes => 'text int4 int4', + proargnames => '{source, start, count}', prosrc => 'text_substr' }, { oid => '937', descr => 'extract portion of string', proname => 'substring', prorettype => 'text', proargtypes => 'text int4', + proargnames => '{source, start}', prosrc => 'text_substr_no_len' }, { oid => '2087', descr => 'replace all occurrences in string of old_substr with new_substr', @@ -4168,6 +4170,7 @@ prosrc => 'bitcat' }, { oid => '1680', descr => 'extract portion of bitstring', proname => 'substring', prorettype => 'bit', proargtypes => 'bit int4 int4', + proargnames => '{source, start, count}', prosrc => 'bitsubstr' }, { oid => '1681', descr => 'bitstring length', proname => 'length', prorettype => 'int4', proargtypes => 'bit', @@ -4197,6 +4200,7 @@ prosrc => 'bitposition' }, { oid => '1699', descr => 'extract portion of bitstring', proname => 'substring', prorettype => 'bit', proargtypes => 'bit int4', + proargnames => '{source, start}', prosrc => 'bitsubstr_no_len' }, { oid => '3030', descr => 'substitute portion of bitstring', @@ -6302,9 +6306,11 @@ prosrc => 'byteacat' }, { oid => '2012', descr => 'extract portion of string', proname => 'substring', prorettype => 'bytea', + proargnames => '{source, start, count}', proargtypes => 'bytea int4 int4', prosrc => 'bytea_substr' }, { oid => '2013', descr => 'extract portion of string', proname => 'substring', prorettype => 'bytea', proargtypes => 'bytea int4', + proargnames => '{source, start}', prosrc => 'bytea_substr_no_len' }, { oid => '2085', descr => 'extract portion of string', proname => 'substr', prorettype => 'bytea', proargtypes => 'bytea int4 int4', @@ -6504,9 +6510,11 @@ { oid => '2073', descr => 'extract text matching regular expression', proname => 'substring', prorettype => 'text', proargtypes => 'text text', + proargnames => '{source, pattern}', prosrc => 'textregexsubstr' }, { oid => '2074', descr => 'extract text matching SQL regular expression', proname => 'substring', prolang => 'sql', prorettype => 'text', + proargnames => '{source, pattern, escape}', proargtypes => 'text text text', prosrc => 'see system_functions.sql' }, { oid => '2075', descr => 'convert int8 to bitstring',