On Tue, Apr 28, 2015 at 9:38 AM, Robert Haas <[email protected]> wrote:
>
> On Sat, Apr 25, 2015 at 8:05 AM, Fabrízio de Royes Mello
> <[email protected]> wrote:
> >> >> > If we ever implement something like
> >> >> >
> >> >> > COMMENT ON CURRENT_DATABASE IS ...
> >> >> >
> >> >> > it will be useful, because you will be able to restore a dump into
> >> >> > another database and have the comment apply to the target
database.
> >> >
> >> > I think it's simple to implement, but how about pg_dump... we need to
> >> > add
> >> > new option (like --use-current-database) or am I missing something ?
> >>
> >> I think we'd just change it to use the new syntax, full stop. I see
> >> no need for an option.
> >
> > I'm returning on this...
> >
> > What's the reasonable syntaxes?
> >
> > COMMENT ON CURRENT DATABASE IS 'text';
> >
> > or
> >
> > COMMENT ON DATABASE { CURRENT_DATABASE | object_name } IS 'text';
>
> The second one would require making CURRENT_DATABASE a reserved
> keyword, and I'm not keen to create any more of those. I like the
> first one. The other alternative that may be worth considering is:
>
> COMMENT ON CURRENT_DATABASE IS 'text';
>
> That doesn't require making CURRENT_DATABASE a reserved keyword, but
> it does require making it a keyword, and it doesn't look very SQL-ish.
> Still, we have a bunch of other CURRENT_FOO keywords.
>
> But I'm inclined to stick with your first proposal.
>
Attached the patch to support "COMMENT ON CURRENT DATABASE IS ..."
(including pg_dump).
On my next spare time I'll send the "ALTER ROLE ... IN CURRENT DATABASE"
patch.
Regards,
--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Timbira: http://www.timbira.com.br
>> Blog: http://fabriziomello.github.io
>> Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello
>> Github: http://github.com/fabriziomello
diff --git a/doc/src/sgml/ref/comment.sgml b/doc/src/sgml/ref/comment.sgml
index 656f5aa..b080106 100644
--- a/doc/src/sgml/ref/comment.sgml
+++ b/doc/src/sgml/ref/comment.sgml
@@ -30,6 +30,7 @@ COMMENT ON
CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ON <replaceable class="PARAMETER">table_name</replaceable> |
CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ON DOMAIN <replaceable class="PARAMETER">domain_name</replaceable> |
CONVERSION <replaceable class="PARAMETER">object_name</replaceable> |
+ CURRENT DATABASE |
DATABASE <replaceable class="PARAMETER">object_name</replaceable> |
DOMAIN <replaceable class="PARAMETER">object_name</replaceable> |
EXTENSION <replaceable class="PARAMETER">object_name</replaceable> |
@@ -92,6 +93,11 @@ COMMENT ON
</para>
<para>
+ The CURRENT DATABASE means the comment will be applied to the database
+ where the command is executed.
+ </para>
+
+ <para>
Comments can be viewed using <application>psql</application>'s
<command>\d</command> family of commands.
Other user interfaces to retrieve comments can be built atop
@@ -301,6 +307,7 @@ COMMENT ON COLUMN my_table.my_column IS 'Employee ID number';
COMMENT ON CONVERSION my_conv IS 'Conversion to UTF8';
COMMENT ON CONSTRAINT bar_col_cons ON bar IS 'Constrains column col';
COMMENT ON CONSTRAINT dom_col_constr ON DOMAIN dom IS 'Constrains col of domain';
+COMMENT ON CURRENT DATABASE IS 'Current Database Comment';
COMMENT ON DATABASE my_database IS 'Development Database';
COMMENT ON DOMAIN my_domain IS 'Email Address Domain';
COMMENT ON EXTENSION hstore IS 'implements the hstore data type';
diff --git a/src/backend/commands/comment.c b/src/backend/commands/comment.c
index 6d8c006..db9b3c5 100644
--- a/src/backend/commands/comment.c
+++ b/src/backend/commands/comment.c
@@ -43,25 +43,29 @@ CommentObject(CommentStmt *stmt)
ObjectAddress address = InvalidObjectAddress;
/*
- * When loading a dump, we may see a COMMENT ON DATABASE for the old name
- * of the database. Erroring out would prevent pg_restore from completing
- * (which is really pg_restore's fault, but for now we will work around
- * the problem here). Consensus is that the best fix is to treat wrong
- * database name as a WARNING not an ERROR; hence, the following special
- * case. (If the length of stmt->objname is not 1, get_object_address
+ * If the length of stmt->objname is 1 then the COMMENT ON DATABASE command
+ * was used, else COMMENT ON CURRENT DATABASE was used instead.
+ * (If the length of stmt->objname is not 1, get_object_address
* will throw an error below; that's OK.)
*/
- if (stmt->objtype == OBJECT_DATABASE && list_length(stmt->objname) == 1)
+ if (stmt->objtype == OBJECT_DATABASE)
{
- char *database = strVal(linitial(stmt->objname));
-
- if (!OidIsValid(get_database_oid(database, true)))
+ /* COMMENT ON DATABASE name */
+ if (list_length(stmt->objname) == 1)
{
- ereport(WARNING,
- (errcode(ERRCODE_UNDEFINED_DATABASE),
- errmsg("database \"%s\" does not exist", database)));
- return address;
+ char *database = strVal(linitial(stmt->objname));
+
+ if (!OidIsValid(get_database_oid(database, true)))
+ {
+ ereport(WARNING,
+ (errcode(ERRCODE_UNDEFINED_DATABASE),
+ errmsg("database \"%s\" does not exist", database)));
+ return address;
+ }
}
+ /* COMMENT ON CURRENT DATABASE */
+ else
+ stmt->objname = list_make1(makeString(get_database_name(MyDatabaseId)));
}
/*
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 962a69d..fa111c3 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -5654,6 +5654,15 @@ CommentStmt:
n->comment = $6;
$$ = (Node *) n;
}
+ | COMMENT ON CURRENT_P DATABASE IS comment_text
+ {
+ CommentStmt *n = makeNode(CommentStmt);
+ n->objtype = OBJECT_DATABASE;
+ n->objname = NIL;
+ n->objargs = NIL;
+ n->comment = $6;
+ $$ = (Node *) n;
+ }
| COMMENT ON TYPE_P Typename IS comment_text
{
CommentStmt *n = makeNode(CommentStmt);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 977b72e..36d6151 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2488,7 +2488,7 @@ dumpDatabase(Archive *fout, DumpOptions *dopt)
* Generates warning when loaded into a differently-named
* database.
*/
- appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", fmtId(datname));
+ appendPQExpBuffer(dbQry, "COMMENT ON CURRENT DATABASE IS ");
appendStringLiteralAH(dbQry, comment, fout);
appendPQExpBufferStr(dbQry, ";\n");
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers