Re: [PATCH] pg_input_is_valid() reuses the first type name inside a PL/pgSQL loop

From: Andreas Karlsson <andreas(at)proxel(dot)se>
To: xnervwang(at)gmail(dot)com
Cc: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, John Naylor <johncnaylorls(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] pg_input_is_valid() reuses the first type name inside a PL/pgSQL loop
Date: 2026-11-25 04:38:49
Message-ID: 58683ada-4f10-48b8-afde-a9752e38a29d@proxel.se
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 11/14/26 02:29, xnervwang(at)gmail(dot)com wrote:
> Compared with master:
>
>                     no name cache          cache + check
>                     per call   query       per call   query
> 'int4'              +277 ns    2.9x        +3.5 ns    +2.4%
> 'varchar(10)'       +509 ns    4.4x        +4.2 ns    +2.7%
> 'numeric(7,4)'      +649 ns    4.9x        +4.3 ns    +2.6%
> domain in public    +302 ns    3.0x        +3.5 ns    +2.2%
> PL/pgSQL variable   +270 ns    3.8x        +7.7 ns    +8.4%
>
> On master, pg_input_is_valid() costs 23 to 62 ns per call in these
> queries.
>
> With "cache + check", the function gives the same answers as if
> nothing were cached when a type is dropped and recreated under the
> same name, or search_path changes between calls, including between
> two FETCHes from the same cursor. If a type is dropped without being
> recreated, it now reports "type ... does not exist" instead of
> "cache lookup failed for type".
>
> Keeping only the parse tree and looking up the name on every call
> costs 57 to 91 ns more per call than master, making the queries
> 1.4 to 1.6 times as long.

1.5 times slower does not sound too horrible, how complex is that code
compared to cache + check?

> > If this function had been added now I
> > personally would have preferred the signature to be:
> >
> > pg_input_is_valid(text, regtype)
>
> regtypein() parses the typmod but returns only the OID, so a regtype
> argument loses the typmod. The documentation example
> pg_input_is_valid('1234.567', 'numeric(7,4)') returns false, but with
> a regtype argument it would lose the (7,4) and return true.

Good point.

> > Do we have similar optimizations for other functions which take a
> type name?
>
> No. Other functions that take a type name as text, such as
> to_regtype() and regtypein(), parse it on every call. I did not find
> another function that caches it.

Yeah, I did not find one either.

--
Andreas Karlsson
Percona

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ayush Tiwari 2026-11-25 04:54:02 Re: Small cleanup of leftover pre-1.1.0 OpenSSL code
Previous Message Richard Guo 2026-11-25 04:37:19 Re: Simplify the uniqueness proof for self-join elimination