From: Tom Lane <tgl@sss.pgh.pa.us>
To: jiangshan.liu@tju.edu.cn
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17869: Inconsistency between PL/pgSQL Function Parameter Handling and SQL Query Results
Date: Sun, 26 Mar 2023 11:41:05 -0400
Message-ID: <2912450.1679845265@sss.pgh.pa.us> (raw)
In-Reply-To: <17869-f049fe97d6a4491b@postgresql.org>
References: <17869-f049fe97d6a4491b@postgresql.org>
PG Bug reporting form <noreply@postgresql.org> writes:
> However, when passing fixed-length character types as parameters in PL/pgSQL
> functions, the behavior seems to be different. The documentation states that
> parenthesized type modifiers are discarded by CREATE FUNCTION, meaning that
> CREATE FUNCTION foo (varchar(10)) is the same as CREATE FUNCTION foo
> (varchar) [2].
I think you've misunderstood that. Type modifiers are not applied
to function parameters. Thus, this function declaration:
> CREATE OR REPLACE FUNCTION test(param CHAR) RETURNS TEXT AS $$
avers that the function takes any CHAR-type value regardless of length.
Had you written, say,
regression=# SELECT * FROM test('abc'::char);
NOTICE: a
test
------
a
(1 row)
the cast operation would enforce the "defaults to length 1" business;
but the function itself does not.
Generally speaking this is desirable because you wouldn't want to have
to write a different copy of test() for each string length you might
want to use it with. If you are really intent on getting the other
behavior you could use a domain:
regression=# create domain c1 as char(1);
CREATE DOMAIN
regression=# CREATE OR REPLACE FUNCTION test(param c1) RETURNS TEXT AS $$
BEGIN
RAISE NOTICE '%', param;
RETURN param;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION
regression=# SELECT * FROM test('abc');
ERROR: value too long for type character(1)
regression=# SELECT * FROM test('abc'::char);
NOTICE: a
test
------
a
(1 row)
This happens because there is no concept of a non-typmod-enforcing
cast to a domain.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, jiangshan.liu@tju.edu.cn, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17869: Inconsistency between PL/pgSQL Function Parameter Handling and SQL Query Results
In-Reply-To: <2912450.1679845265@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox