From ce68df468a41d8dbb992184aad490c07d02ca721 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Thu, 22 Jul 2010 01:22:35 +0000 Subject: [PATCH] Add options to force quoting of all identifiers. I've added a quote_all_identifiers GUC which affects the behavior of the backend, and a --quote-all-identifiers argument to pg_dump and pg_dumpall which sets the GUC and also affects the quoting done internally by those applications. Design by Tom Lane; review by Alex Hunsaker; in response to bug #5488 filed by Hartmut Goebel. --- doc/src/sgml/config.sgml | 19 ++++++++++++++++++- doc/src/sgml/ref/pg_dump.sgml | 13 ++++++++++++- doc/src/sgml/ref/pg_dumpall.sgml | 13 ++++++++++++- src/backend/utils/adt/ruleutils.c | 6 +++++- src/backend/utils/misc/guc.c | 11 ++++++++++- src/backend/utils/misc/postgresql.conf.sample | 1 + src/bin/pg_dump/dumputils.c | 8 ++++++-- src/bin/pg_dump/dumputils.h | 4 +++- src/bin/pg_dump/pg_dump.c | 9 ++++++++- src/bin/pg_dump/pg_dumpall.c | 9 ++++++++- src/include/utils/builtins.h | 3 ++- 11 files changed, 85 insertions(+), 11 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index c343bee9d0..0f20e75fbc 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1,4 +1,4 @@ - + Server Configuration @@ -5209,6 +5209,23 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir' + + quote_all_identifiers (boolean) + + quote_all_identifiers configuration parameter + + + + When the database generates SQL, force all identifiers to be quoted, + even if they are not (currently) keywords. This will affect the + output of EXPLAIN as well as the results of functions + like pg_get_viewdef. See also the + to + and . + + + + sql_inheritance (boolean) diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index 0f254d45ad..f0498df50c 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -1,5 +1,5 @@ @@ -660,6 +660,17 @@ PostgreSQL documentation + + + + + Force quoting of all identifiers. This may be useful when dumping a + database for migration to a future version that may have introduced + additional keywords. + + + + diff --git a/doc/src/sgml/ref/pg_dumpall.sgml b/doc/src/sgml/ref/pg_dumpall.sgml index 95a37512ee..7509730885 100644 --- a/doc/src/sgml/ref/pg_dumpall.sgml +++ b/doc/src/sgml/ref/pg_dumpall.sgml @@ -1,5 +1,5 @@ @@ -356,6 +356,17 @@ PostgreSQL documentation + + + + + Force quoting of all identifiers. This may be useful when dumping a + database for migration to a future version that may have introduced + additional keywords. + + + + diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 7f1ec8766c..e06ac5697b 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.328 2010/07/13 20:57:19 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.329 2010/07/22 01:22:33 rhaas Exp $ * *------------------------------------------------------------------------- */ @@ -132,6 +132,7 @@ static SPIPlanPtr plan_getrulebyoid = NULL; static const char *query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1"; static SPIPlanPtr plan_getviewrule = NULL; static const char *query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2"; +bool quote_all_identifiers; /* ---------- @@ -6727,6 +6728,9 @@ quote_identifier(const char *ident) } } + if (quote_all_identifiers) + safe = false; + if (safe) { /* diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 9cc24237f8..463b563b4b 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -10,7 +10,7 @@ * Written by Peter Eisentraut . * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.564 2010/07/20 00:47:53 rhaas Exp $ + * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.565 2010/07/22 01:22:33 rhaas Exp $ * *-------------------------------------------------------------------- */ @@ -1282,6 +1282,15 @@ static struct config_bool ConfigureNamesBool[] = false, NULL, NULL }, + { + {"quote_all_identifiers", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS, + gettext_noop("When generating SQL fragments, quote all identifiers."), + NULL, + }, + "e_all_identifiers, + false, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 21469d3f4a..d3c07bd502 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -510,6 +510,7 @@ #default_with_oids = off #escape_string_warning = on #lo_compat_privileges = off +#quote_all_identifiers = off #sql_inheritance = on #standard_conforming_strings = on #synchronize_seqscans = on diff --git a/src/bin/pg_dump/dumputils.c b/src/bin/pg_dump/dumputils.c index 124a458666..64e5b1df10 100644 --- a/src/bin/pg_dump/dumputils.c +++ b/src/bin/pg_dump/dumputils.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.56 2010/03/03 20:10:48 heikki Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.57 2010/07/22 01:22:34 rhaas Exp $ * *------------------------------------------------------------------------- */ @@ -36,6 +36,8 @@ static bool parallel_init_done = false; static DWORD tls_index; #endif +int quote_all_identifiers; + void init_parallel_dump_utils(void) { @@ -102,8 +104,10 @@ fmtId(const char *rawid) * These checks need to match the identifier production in scan.l. Don't * use islower() etc. */ + if (quote_all_identifiers) + need_quotes = true; /* slightly different rules for first character */ - if (!((rawid[0] >= 'a' && rawid[0] <= 'z') || rawid[0] == '_')) + else if (!((rawid[0] >= 'a' && rawid[0] <= 'z') || rawid[0] == '_')) need_quotes = true; else { diff --git a/src/bin/pg_dump/dumputils.h b/src/bin/pg_dump/dumputils.h index 298b687cd1..3d22a46323 100644 --- a/src/bin/pg_dump/dumputils.h +++ b/src/bin/pg_dump/dumputils.h @@ -8,7 +8,7 @@ * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.29 2010/02/26 02:01:16 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.30 2010/07/22 01:22:34 rhaas Exp $ * *------------------------------------------------------------------------- */ @@ -46,4 +46,6 @@ extern bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule); +extern int quote_all_identifiers; + #endif /* DUMPUTILS_H */ diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 63b1da09ab..666312fb56 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -25,7 +25,7 @@ * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.582 2010/07/14 21:21:08 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.583 2010/07/22 01:22:34 rhaas Exp $ * *------------------------------------------------------------------------- */ @@ -297,6 +297,7 @@ main(int argc, char **argv) {"inserts", no_argument, &dump_inserts, 1}, {"lock-wait-timeout", required_argument, NULL, 2}, {"no-tablespaces", no_argument, &outputNoTablespaces, 1}, + {"quote-all-identifiers", no_argument, "e_all_identifiers, 1}, {"role", required_argument, NULL, 3}, {"use-set-session-authorization", no_argument, &use_setsessauth, 1}, @@ -634,6 +635,12 @@ main(int argc, char **argv) if (g_fout->remoteVersion >= 70300) do_sql_command(g_conn, "SET statement_timeout = 0"); + /* + * Quote all identifiers, if requested. + */ + if (quote_all_identifiers && g_fout->remoteVersion >= 90100) + do_sql_command(g_conn, "SET quote_all_identifiers = true"); + /* * Start serializable transaction to dump consistent data. */ diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 275a22ab88..3f9b77739f 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.134 2010/02/26 02:01:17 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.135 2010/07/22 01:22:34 rhaas Exp $ * *------------------------------------------------------------------------- */ @@ -130,6 +130,7 @@ main(int argc, char *argv[]) {"inserts", no_argument, &inserts, 1}, {"lock-wait-timeout", required_argument, NULL, 2}, {"no-tablespaces", no_argument, &no_tablespaces, 1}, + {"quote-all-identifiers", no_argument, "e_all_identifiers, 1}, {"role", required_argument, NULL, 3}, {"use-set-session-authorization", no_argument, &use_setsessauth, 1}, @@ -328,6 +329,8 @@ main(int argc, char *argv[]) appendPQExpBuffer(pgdumpopts, " --inserts"); if (no_tablespaces) appendPQExpBuffer(pgdumpopts, " --no-tablespaces"); + if (quote_all_identifiers) + appendPQExpBuffer(pgdumpopts, " --quote-all-identifiers"); if (use_setsessauth) appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization"); @@ -440,6 +443,10 @@ main(int argc, char *argv[]) destroyPQExpBuffer(query); } + /* Force quoting of all identifiers if requested. */ + if (quote_all_identifiers && server_version >= 90000) + executeCommand(conn, "SET quote_all_identifiers = true"); + fprintf(OPF, "--\n-- PostgreSQL database cluster dump\n--\n\n"); if (verbose) dumpTimestamp("Started on"); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index 9f0586855c..92b3c64aaa 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.351 2010/07/13 20:57:19 tgl Exp $ + * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.352 2010/07/22 01:22:35 rhaas Exp $ * *------------------------------------------------------------------------- */ @@ -583,6 +583,7 @@ extern Datum record_ge(PG_FUNCTION_ARGS); extern Datum btrecordcmp(PG_FUNCTION_ARGS); /* ruleutils.c */ +extern bool quote_all_identifiers; extern Datum pg_get_ruledef(PG_FUNCTION_ARGS); extern Datum pg_get_ruledef_ext(PG_FUNCTION_ARGS); extern Datum pg_get_viewdef(PG_FUNCTION_ARGS);