1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

Add test scaffolding for soft error reporting from input functions.

pg_input_is_valid() returns boolean, while pg_input_error_message()
returns the primary error message if the input is bad, or NULL
if the input is OK.  The main reason for having two functions is
so that we can test both the details-wanted and the no-details-wanted
code paths.

Although these are primarily designed with testing in mind,
it could well be that they'll be useful to end users as well.

This patch is mostly by me, but it owes very substantial debt to
earlier work by Nikita Glukhov, Andrew Dunstan, and Amul Sul.
Thanks to Andres Freund for review.

Discussion: https://postgr.es/m/3bbbb0df-7382-bf87-9737-340ba096e034@postgrespro.ru
This commit is contained in:
Tom Lane
2022-12-09 10:08:44 -05:00
parent d9f7f5d32f
commit 1939d26282
7 changed files with 277 additions and 1 deletions

View File

@ -24683,6 +24683,107 @@ SELECT collation for ('foo' COLLATE "de_DE");
</sect2>
<sect2 id="functions-info-validity">
<title>Data Validity Checking Functions</title>
<para>
The functions shown in <xref linkend="functions-info-validity-table"/>
can be helpful for checking validity of proposed input data.
</para>
<table id="functions-info-validity-table">
<title>Data Validity Checking Functions</title>
<tgroup cols="1">
<thead>
<row>
<entry role="func_table_entry"><para role="func_signature">
Function
</para>
<para>
Description
</para>
<para>
Example(s)
</para></entry>
</row>
</thead>
<tbody>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_input_is_valid</primary>
</indexterm>
<function>pg_input_is_valid</function> (
<parameter>string</parameter> <type>text</type>,
<parameter>type</parameter> <type>text</type>
)
<returnvalue>boolean</returnvalue>
</para>
<para>
Tests whether the given <parameter>string</parameter> is valid
input for the specified data type, returning true or false.
</para>
<para>
This function will only work as desired if the data type's input
function has been updated to report invalid input as
a <quote>soft</quote> error. Otherwise, invalid input will abort
the transaction, just as if the string had been cast to the type
directly.
</para>
<para>
<literal>pg_input_is_valid('42', 'integer')</literal>
<returnvalue>t</returnvalue>
</para>
<para>
<literal>pg_input_is_valid('42000000000', 'integer')</literal>
<returnvalue>f</returnvalue>
</para>
<para>
<literal>pg_input_is_valid('1234.567', 'numeric(7,4)')</literal>
<returnvalue>f</returnvalue>
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_input_error_message</primary>
</indexterm>
<function>pg_input_error_message</function> (
<parameter>string</parameter> <type>text</type>,
<parameter>type</parameter> <type>text</type>
)
<returnvalue>text</returnvalue>
</para>
<para>
Tests whether the given <parameter>string</parameter> is valid
input for the specified data type; if not, return the error
message that would have been thrown. If the input is valid, the
result is NULL. The inputs are the same as
for <function>pg_input_is_valid</function>.
</para>
<para>
This function will only work as desired if the data type's input
function has been updated to report invalid input as
a <quote>soft</quote> error. Otherwise, invalid input will abort
the transaction, just as if the string had been cast to the type
directly.
</para>
<para>
<literal>pg_input_error_message('42000000000', 'integer')</literal>
<returnvalue>value "42000000000" is out of range for type integer</returnvalue>
</para>
<para>
<literal>pg_input_error_message('1234.567', 'numeric(7,4)')</literal>
<returnvalue>numeric field overflow</returnvalue>
</para></entry>
</row>
</tbody>
</tgroup>
</table>
</sect2>
<sect2 id="functions-info-snapshot">
<title>Transaction ID and Snapshot Information Functions</title>