Re: to_regtype() Raises Error - Mailing list pgsql-hackers

From Erik Wienhold
Subject Re: to_regtype() Raises Error
Date
Msg-id 910762019.132918.1694993587823@office.mailbox.org
Whole thread Raw
In response to Re: to_regtype() Raises Error  (Vik Fearing <vik@postgresfriends.org>)
Responses Re: to_regtype() Raises Error
List pgsql-hackers
On 18/09/2023 00:57 CEST Vik Fearing <vik@postgresfriends.org> wrote:

> On 9/18/23 00:41, Erik Wienhold wrote:
> > On 18/09/2023 00:13 CEST David E. Wheeler <david@justatheory.com> wrote:
> >
> >> david=# select to_regtype('inteval second');
> >> ERROR:  syntax error at or near "second"
> >> LINE 1: select to_regtype('inteval second');
> >>                  ^
> >> CONTEXT:  invalid type name "inteval second”
> >
> > Probably a typo and you meant 'interval second' which works.
>
> No, that is precisely the point.  The result should be null instead of
> an error.

Well, the docs say "return NULL rather than throwing an error if the name is
not found".  To me "name is not found" implies that it has to be valid syntax
first to even have a name that can be looked up.

String 'inteval second' is a syntax error when interpreted as a type name.
The same when I want to create a table with that typo:

    test=# create table t (a inteval second);
    ERROR:  syntax error at or near "second"
    LINE 1: create table t (a inteval second);

And a custom function is always an option:

    create function to_regtype_lax(name text)
      returns regtype
      language plpgsql
      as $$
    begin
      return to_regtype(name);
    exception
      when others then
        return null;
    end
    $$;

    test=# select to_regtype_lax('inteval second');
     to_regtype_lax
    ----------------
     <NULL>
    (1 row)

--
Erik



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: to_regtype() Raises Error
Next
From: "David E. Wheeler"
Date:
Subject: Re: to_regtype() Raises Error