From de9801fc62ba9ed905c2f20686459a206d453453 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 20 Aug 2002 04:46:00 +0000
Subject: [PATCH] Add current_database().

> Quick system function to pull out the current database.
>
> I've used this a number of times to allow stored procedures to find out
> where they are.  Especially useful for those that do logging or hit a
> remote server.
>
> It's called current_database() to match with current_user().

It's also a necessity for an informational schema.  The catalog
(database) name is required in a number of places.

Rod Taylor
---
 doc/src/sgml/func.sgml        |  7 ++++++-
 src/backend/utils/adt/misc.c  | 19 ++++++++++++++++++-
 src/include/catalog/pg_proc.h |  5 ++++-
 src/include/utils/builtins.h  |  3 ++-
 4 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index bf2ce4667e2..fddc65e84d3 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -1,5 +1,5 @@
 <!--
-$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.112 2002/08/16 23:01:18 tgl Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.113 2002/08/20 04:45:59 momjian Exp $
 PostgreSQL documentation
 -->
 
@@ -5082,6 +5082,11 @@ SELECT NULLIF(value, '(none)') ...
        <entry><type>name[]</type></entry>
        <entry>names of schemas in search path optionally including implicit schemas</entry>
       </row>
+      <row>
+       <entry><function>current_database()</function></entry>
+       <entry><type>name</type></entry>
+       <entry>name of current database</entry>
+      </row>
      </tbody>
     </tgroup>
    </table>
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index 57640ef08d0..24eb6821843 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.24 2002/06/20 20:29:37 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.25 2002/08/20 04:45:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,6 +17,7 @@
 #include <time.h>
 
 #include "postgres.h"
+#include "miscadmin.h"
 
 #include "utils/builtins.h"
 
@@ -111,3 +112,19 @@ userfntest(PG_FUNCTION_ARGS)
 
 	PG_RETURN_INT32(i);
 }
+
+/*
+ * current_database()
+ *	Expose the current database to the user
+ */
+Datum
+current_database(PG_FUNCTION_ARGS)
+{
+	Name   db;
+
+	db = (Name) palloc(NAMEDATALEN);
+
+	namestrcpy(db, DatabaseName);
+
+	PG_RETURN_NAME(db);
+}
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index 2213fe7228c..a186cb08256 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_proc.h,v 1.256 2002/08/17 13:04:15 momjian Exp $
+ * $Id: pg_proc.h,v 1.257 2002/08/20 04:45:59 momjian Exp $
  *
  * NOTES
  *	  The script catalog/genbki.sh reads this file and generates .bki
@@ -1106,6 +1106,9 @@ DESCR("does not match LIKE expression");
 DATA(insert OID =  860 (  bpchar		   PGNSP PGUID 12 f f t f i 1 1042 "18"  char_bpchar - _null_ ));
 DESCR("convert char to char()");
 
+DATA(insert OID = 861 ( current_database       PGNSP PGUID 12 f f t f i 0 19 "0" current_database - _null_ ));
+DESCR("returns the current database");
+
 DATA(insert OID =  862 (  int4_mul_cash		   PGNSP PGUID 12 f f t f i 2 790 "23 790"  int4_mul_cash - _null_ ));
 DESCR("multiply");
 DATA(insert OID =  863 (  int2_mul_cash		   PGNSP PGUID 12 f f t f i 2 790 "21 790"  int2_mul_cash - _null_ ));
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index 3588673c2ae..e9937ad751e 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: builtins.h,v 1.192 2002/08/16 23:01:21 tgl Exp $
+ * $Id: builtins.h,v 1.193 2002/08/20 04:46:00 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -315,6 +315,7 @@ extern Datum nonnullvalue(PG_FUNCTION_ARGS);
 extern Datum oidrand(PG_FUNCTION_ARGS);
 extern Datum oidsrand(PG_FUNCTION_ARGS);
 extern Datum userfntest(PG_FUNCTION_ARGS);
+extern Datum current_database(PG_FUNCTION_ARGS);
 
 /* not_in.c */
 extern Datum int4notin(PG_FUNCTION_ARGS);
-- 
GitLab