Re: Is there a way to check if objects are created with quotes or not? - Mailing list pgsql-admin

From shammat@gmx.net
Subject Re: Is there a way to check if objects are created with quotes or not?
Date
Msg-id bec068b6-6e4d-4774-8f52-4de635862ae5@gmx.net
Whole thread Raw
In response to Is there a way to check if objects are created with quotes or not?  (Edwin UY <edwin.uy@gmail.com>)
List pgsql-admin
Edwin UY schrieb am 22.01.2025 um 00:28:
> I am just now finding out that I sometimes I have to enclosed tables
> in quotes, that is a name named table1 need to be quoted as 'table1'
> otherwise it gives error. Is there any way to find out what are
> those such tables so we can rename it to be without the quotes? Or
> maybe it is not a good idea to rename as it might break the
> indexes / foreign keys / sequences?

The function quote_ident() will return a quoted string if needed
(e.g. when using mixed case or other special characters).

To find all tables that would need double quotes (table1 is not one of them) you
can check if the name stored in the system catalog is the same as the quoted name:

    select *
    from pg_tables
    where schemaname = 'public'
      and tablename <> quote_ident(tablename);


If you only have tables that use mixed case, another option is
to check if the name is all lower case:

    select *
    from pg_tables
    where schemaname = 'public'
      and tablename <> lower(tablename);


The recommendation is indeed to avoid quoted identifiers.

Using the above query you could generate ALTER statements to rename those using
mixed case (and only that) to lower case.

Something along the lines:

    select format('alter table %I.%I rename to %I;', schemaname, tablename, lower(tablename))
    from pg_tables
    where schemaname = 'public'
      and tablename <> lower(tablename)
      and lower(tablename) = quote_ident(lower(tablename))




pgsql-admin by date:

Previous
From: Jeff Janes
Date:
Subject: Re: Commit with wait event on advisory lock!
Next
From: Laurenz Albe
Date:
Subject: Re: High Disk Utilization Due to Query on Large Table