Search code examples
sqlpostgresqlplpgsqlidentifier

How to represent double quotes in dynamic PL/pgsql ALTER TABLE statement


I find myself in a position that I'm sure many others have before. I generated my PostgreSQL schema using a generator and now I'm in production(!), I realise I should really try to remove the double quotes from the table and column names that the generator added i.e.

CREATE TABLE "myschema"."mytable" (....

should have been

CREATE TABLE myschema.mytable (....

I'm writing a PL/pgsql function to loop through information_schema.tables and information_schema.columns and then to execute ALTER TABLE statements to drop the doube quotes.

What I can't grasp from the documentation and searhcing is how to issue that statement.

I know it's an EXECUTE I need but can someone help me with the syntax to rename "mytable" to mytable using that. I'm lost with $$ and format() and quote_ident() and think I'm overcomplicating how to indicate that my original table names will have double quotes.


Solution

  • The double-quotes in your examples are not part of the names, just decorators to preserve original spelling of identifiers. See:

    While working with lower-case, legal identifiers (like your examples suggest), those double-quotes are purely optional. Add them or leave them.

    If you've been unwise enough to create objects with illegal names or with mixed case, those double-quotes are required at all times. The cure is to stick to legal, lower-case identifiers.

    Start by reading the manual about identifiers.