On 1/3/16 9:23 PM, Tom Lane wrote:
Jim Nasby <jim.na...@bluetreble.com> writes:
regrole and regnamespace don't run their output through quote_ident().
That's contrary to all the other reg* operators.
Worse, they also don't *allow* quoted input. Not only is that different
from reg*, it's the *opposite*:


BTW, there's a concrete reason why this is broken, which is that although
regrole and regnamespace didn't bother with copying quoting/dequoting from
the other types, they *did* copy the special case logic about allowing
and emitting numeric OIDs.  This means that an output like "1234" from
regroleout is formally inconsistent: there is no way to tell if it's an
numeric OID or a role name that happens to be all digits.  (With proper
quoting logic, you could tell because an all-digits role name would have
gotten quoted.)  Conversely, if you create an all-digits role name, there
is no way to get regrolein to interpret it as such, whereas a dequoting
rule would have disambiguated.

I'm inclined to leave to_regrole and to_regnamespace alone, though, since
they have no numeric-OID path, and they will provide an "out" for anyone
who wants to handle nonquoted names.  (Though at least in HEAD we ought to
fix them to take type text as input.  Using cstring for ordinary functions
is just sloppy.)

None of the other to_reg* casts do that though, so this would be inconsistent.

Another potential problem for regnamespace is that it doesn't allow an entry for the catalog. I'm not sure what the spec says about that, but every other function allows dbname.schema.blah (dbname == catalog).

I started working on a fix, but it's currently blowing up in bootstrap and I haven't been able to figure out why yet:

running bootstrap script ... FATAL: improper qualified name (too many dotted names): oid_ops
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index 8b105fe..a555a1f 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -2613,6 +2613,9 @@ TSConfigIsVisible(Oid cfgid)
  *             extract the schema name and object name.
  *
  * *nspname_p is set to NULL if there is no explicit schema name.
+ * 
+ * If *objname_p is not set then treat names as a schema name, possibly with a
+ * catalog name.
  */
 void
 DeconstructQualifiedName(List *names,
@@ -2623,19 +2626,21 @@ DeconstructQualifiedName(List *names,
        char       *schemaname = NULL;
        char       *objname = NULL;
 
-       switch (list_length(names))
+       switch (list_length(names) + objname_p ? 0 : 1)
        {
                case 1:
                        objname = strVal(linitial(names));
                        break;
                case 2:
                        schemaname = strVal(linitial(names));
-                       objname = strVal(lsecond(names));
+                       if (objname_p)
+                               objname = strVal(lsecond(names));
                        break;
                case 3:
                        catalogname = strVal(linitial(names));
                        schemaname = strVal(lsecond(names));
-                       objname = strVal(lthird(names));
+                       if (objname_p)
+                               objname = strVal(lthird(names));
 
                        /*
                         * We check the catalog name and then ignore it.
@@ -2655,7 +2660,8 @@ DeconstructQualifiedName(List *names,
        }
 
        *nspname_p = schemaname;
-       *objname_p = objname;
+       if (objname_p)
+               *objname_p = objname;
 }
 
 /*
diff --git a/src/backend/utils/adt/regproc.c b/src/backend/utils/adt/regproc.c
index 59e5dc8..8c862cf 100644
--- a/src/backend/utils/adt/regproc.c
+++ b/src/backend/utils/adt/regproc.c
@@ -1569,6 +1569,7 @@ Datum
 regrolein(PG_FUNCTION_ARGS)
 {
        char       *role_name_or_oid = PG_GETARG_CSTRING(0);
+       List       *names;
        Oid                     result;
 
        /* '-' ? */
@@ -1586,7 +1587,15 @@ regrolein(PG_FUNCTION_ARGS)
        }
 
        /* Normal case: see if the name matches any pg_authid entry. */
-       result = get_role_oid(role_name_or_oid, false);
+       names = stringToQualifiedNameList(role_name_or_oid);
+
+       if (list_length(names) > 1)
+               ereport(ERROR,
+                               (errcode(ERRCODE_SYNTAX_ERROR),
+                       errmsg("improper qualified name (too many dotted 
names): %s",
+                                  NameListToString(names))));
+
+       result = get_role_oid(strVal(linitial(names)), false);
 
        PG_RETURN_OID(result);
 }
@@ -1668,7 +1677,9 @@ Datum
 regnamespacein(PG_FUNCTION_ARGS)
 {
        char       *nsp_name_or_oid = PG_GETARG_CSTRING(0);
+       char       *nsp_name;
        Oid                     result = InvalidOid;
+       List       *names;
 
        /* '-' ? */
        if (strcmp(nsp_name_or_oid, "-") == 0)
@@ -1685,7 +1696,9 @@ regnamespacein(PG_FUNCTION_ARGS)
        }
 
        /* Normal case: see if the name matches any pg_namespace entry. */
-       result = get_namespace_oid(nsp_name_or_oid, false);
+       names = stringToQualifiedNameList(nsp_name_or_oid);
+       DeconstructQualifiedName(names, &nsp_name, 0);
+       result = get_namespace_oid(nsp_name, false);
 
        PG_RETURN_OID(result);
 }
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to