Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgSVX-0003zD-1e for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Mar 2023 15:42:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pgSUX-0000G6-P1 for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Mar 2023 15:41:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgSUX-0000Fh-Gb for pgsql-bugs@lists.postgresql.org; Sun, 26 Mar 2023 15:41:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgSUV-0007ie-3m for pgsql-bugs@lists.postgresql.org; Sun, 26 Mar 2023 15:41:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 32QFf5nu2912451; Sun, 26 Mar 2023 11:41:05 -0400 From: Tom Lane 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 In-reply-to: <17869-f049fe97d6a4491b@postgresql.org> References: <17869-f049fe97d6a4491b@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sun, 26 Mar 2023 13:21:37 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2912449.1679845265.1@sss.pgh.pa.us> Date: Sun, 26 Mar 2023 11:41:05 -0400 Message-ID: <2912450.1679845265@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form 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