OSDN Git Service

Add options to force quoting of all identifiers.
authorRobert Haas <rhaas@postgresql.org>
Thu, 22 Jul 2010 01:22:35 +0000 (01:22 +0000)
committerRobert Haas <rhaas@postgresql.org>
Thu, 22 Jul 2010 01:22:35 +0000 (01:22 +0000)
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
doc/src/sgml/ref/pg_dump.sgml
doc/src/sgml/ref/pg_dumpall.sgml
src/backend/utils/adt/ruleutils.c
src/backend/utils/misc/guc.c
src/backend/utils/misc/postgresql.conf.sample
src/bin/pg_dump/dumputils.c
src/bin/pg_dump/dumputils.h
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dumpall.c
src/include/utils/builtins.h

index c343bee..0f20e75 100644 (file)
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.298 2010/07/20 00:47:52 rhaas Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.299 2010/07/22 01:22:32 rhaas Exp $ -->
 
 <chapter Id="runtime-config">
   <title>Server Configuration</title>
@@ -5209,6 +5209,23 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
       </listitem>
      </varlistentry>
 
+    <varlistentry id="guc-quote-all-identifiers" xreflabel="quote-all-identifiers">
+      <term><varname>quote_all_identifiers</varname> (<type>boolean</type>)</term>
+      <indexterm>
+       <primary><varname>quote_all_identifiers</> configuration parameter</primary>
+      </indexterm>
+      <listitem>
+       <para>
+        When the database generates SQL, force all identifiers to be quoted,
+        even if they are not (currently) keywords.  This will affect the
+        output of <command>EXPLAIN</> as well as the results of functions
+        like <function>pg_get_viewdef</>.  See also the
+           <option>--quote-all-identifiers</option> to
+        <xref linkend="app-pgdump"> and <xref linkend="app-pg-dumpall">.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-sql-inheritance" xreflabel="sql_inheritance">
       <term><varname>sql_inheritance</varname> (<type>boolean</type>)</term>
       <indexterm>
index 0f254d4..f0498df 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.117 2010/02/23 17:28:09 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.118 2010/07/22 01:22:33 rhaas Exp $
 PostgreSQL documentation
 -->
 
@@ -661,6 +661,17 @@ PostgreSQL documentation
      </varlistentry>
 
      <varlistentry>
+      <term><option>--quote-all-identifiers</></term>
+      <listitem>
+       <para>
+        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.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
        <term><option>-?</></term>
        <term><option>--help</></term>
        <listitem>
index 95a3751..7509730 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.82 2010/04/03 07:23:01 petere Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.83 2010/07/22 01:22:33 rhaas Exp $
 PostgreSQL documentation
 -->
 
@@ -357,6 +357,17 @@ PostgreSQL documentation
      </varlistentry>
 
      <varlistentry>
+      <term><option>--quote-all-identifiers</></term>
+      <listitem>
+       <para>
+        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.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
        <term><option>-?</></term>
        <term><option>--help</></term>
        <listitem>
index 7f1ec87..e06ac56 100644 (file)
@@ -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)
        {
                /*
index 9cc2423..463b563 100644 (file)
@@ -10,7 +10,7 @@
  * Written by Peter Eisentraut <peter_e@gmx.net>.
  *
  * 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,
+               },
+               &quote_all_identifiers,
+               false, NULL, NULL
+       },
+
        /* End-of-list marker */
        {
                {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL
index 21469d3..d3c07bd 100644 (file)
 #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
index 124a458..64e5b1d 100644 (file)
@@ -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
        {
index 298b687..3d22a46 100644 (file)
@@ -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 */
index 63b1da0..666312f 100644 (file)
@@ -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, &quote_all_identifiers, 1},
                {"role", required_argument, NULL, 3},
                {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
 
@@ -635,6 +636,12 @@ main(int argc, char **argv)
                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.
         */
        do_sql_command(g_conn, "BEGIN");
index 275a22a..3f9b777 100644 (file)
@@ -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, &quote_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");
index 9f05868..92b3c64 100644 (file)
@@ -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);