Implement current_query(), that shows the currently executing query.
authorBruce Momjian <bruce@momjian.us>
Fri, 4 Apr 2008 16:57:21 +0000 (16:57 +0000)
committerBruce Momjian <bruce@momjian.us>
Fri, 4 Apr 2008 16:57:21 +0000 (16:57 +0000)
At the same time remove dblink/dblink_current_query() as it is no longer
necessary
*BACKWARD COMPATIBILITY ISSUE* for dblink

Tomas Doran

contrib/dblink/dblink.c
contrib/dblink/dblink.h
contrib/dblink/dblink.sql.in
contrib/dblink/expected/dblink.out
contrib/dblink/sql/dblink.sql
contrib/dblink/uninstall_dblink.sql
doc/src/sgml/dblink.sgml
doc/src/sgml/func.sgml
src/backend/utils/adt/misc.c
src/include/catalog/pg_proc.h
src/include/utils/builtins.h

index 863dd976d9befade39eb3c043c5e578d015e561d..51d5a126911643c95cda91b5c240712070f3e987 100644 (file)
@@ -1631,23 +1631,6 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
        PG_RETURN_TEXT_P(cstring_to_text(sql));
 }
 
-/*
- * dblink_current_query
- * return the current query string
- * to allow its use in (among other things)
- * rewrite rules
- */
-PG_FUNCTION_INFO_V1(dblink_current_query);
-Datum
-dblink_current_query(PG_FUNCTION_ARGS)
-{
-       if (debug_query_string)
-               PG_RETURN_TEXT_P(cstring_to_text(debug_query_string));
-       else
-               PG_RETURN_NULL();
-}
-
-
 /*************************************************************
  * internal functions
  */
index 706faf4c7535fec800840d9cfec3e61f527d8a92..81be7e45fcfd168b95bae1ea8d799ece78d52fef 100644 (file)
@@ -56,6 +56,5 @@ extern Datum dblink_get_pkey(PG_FUNCTION_ARGS);
 extern Datum dblink_build_sql_insert(PG_FUNCTION_ARGS);
 extern Datum dblink_build_sql_delete(PG_FUNCTION_ARGS);
 extern Datum dblink_build_sql_update(PG_FUNCTION_ARGS);
-extern Datum dblink_current_query(PG_FUNCTION_ARGS);
 
 #endif   /* DBLINK_H */
index c72a87e4581c90a90dccb178f84be85f2a6473a8..cb4ab8cae3b77a3e77e5edf66dd36cd7235d2807 100644 (file)
@@ -163,11 +163,6 @@ RETURNS text
 AS 'MODULE_PATHNAME','dblink_build_sql_update'
 LANGUAGE C STRICT;
 
-CREATE OR REPLACE FUNCTION dblink_current_query ()
-RETURNS text
-AS 'MODULE_PATHNAME','dblink_current_query'
-LANGUAGE C;
-
 CREATE OR REPLACE FUNCTION dblink_send_query(text, text)
 RETURNS int4
 AS 'MODULE_PATHNAME', 'dblink_send_query'
index fd35d76af9672cf22db450c34e551064d6b8b109..ec5284daf4856ea522fbb203f88e0188ca44f0b1 100644 (file)
@@ -22,13 +22,6 @@ INSERT INTO foo VALUES (7,'h','{"a7","b7","c7"}');
 INSERT INTO foo VALUES (8,'i','{"a8","b8","c8"}');
 INSERT INTO foo VALUES (9,'j','{"a9","b9","c9"}');
 -- misc utilities
--- show the currently executing query
-SELECT 'hello' AS hello, dblink_current_query() AS query;
- hello |                           query                           
--------+-----------------------------------------------------------
- hello | SELECT 'hello' AS hello, dblink_current_query() AS query;
-(1 row)
-
 -- list the primary key fields
 SELECT *
 FROM dblink_get_pkey('foo');
index 48e1daca54d2b800ba97dd8dbc637319b89b9d6d..1d5f962db8421b0001c72c67264bffe65f95357a 100644 (file)
@@ -27,9 +27,6 @@ INSERT INTO foo VALUES (9,'j','{"a9","b9","c9"}');
 
 -- misc utilities
 
--- show the currently executing query
-SELECT 'hello' AS hello, dblink_current_query() AS query;
-
 -- list the primary key fields
 SELECT *
 FROM dblink_get_pkey('foo');
index fa16e3dbd6d1b0c2d6fa8d84317080a0f62db84b..162b484a34462e89a57dcafe4bee947fb14eb9d0 100644 (file)
@@ -3,8 +3,6 @@
 -- Adjust this setting to control where the objects get dropped.
 SET search_path = public;
 
-DROP FUNCTION dblink_current_query ();
-
 DROP FUNCTION dblink_build_sql_update (text, int2vector, int4, _text, _text);
 
 DROP FUNCTION dblink_build_sql_delete (text, int2vector, int4, _text);
index 5fdc6a729ee1cdfff23e468582a64f5ac7081d4b..9bd5b0445d18ed219526b9900f7649bcde708cb7 100644 (file)
@@ -1346,49 +1346,6 @@ SELECT *
   </refsect1>
  </refentry>
 
- <refentry id="CONTRIB-DBLINK-CURRENT-QUERY">
-  <refnamediv>
-   <refname>dblink_current_query</refname>
-   <refpurpose>returns the current query string</refpurpose>
-  </refnamediv>
-
-  <refsynopsisdiv>
-   <synopsis>
-    dblink_current_query() returns text
-   </synopsis>
-  </refsynopsisdiv>
-
-  <refsect1>
-   <title>Description</title>
-
-   <para>
-    Returns the currently executing interactive command string of the
-    local database session, or NULL if it can't be determined.  Note
-    that this function is not really related to <filename>dblink</>'s
-    other functionality.  It is provided since it is sometimes useful
-    in generating queries to be forwarded to remote databases.
-   </para>
-  </refsect1>
-
-  <refsect1>
-   <title>Return Value</title>
-
-   <para>Returns a copy of the currently executing query string.</para>
-  </refsect1>
-
-  <refsect1>
-   <title>Example</title>
-
-   <programlisting>
-test=# select dblink_current_query();
-      dblink_current_query
---------------------------------
- select dblink_current_query();
-(1 row)
-   </programlisting>
-  </refsect1>
- </refentry>
-
  <refentry id="CONTRIB-DBLINK-GET-PKEY">
   <refnamediv>
    <refname>dblink_get_pkey</refname>
index f0f3ee84cd8de0a0a1a9b27dfdab20e1139d9607..8326e882921de3599afe2d24fc5ffc12447b0c39 100644 (file)
@@ -10724,6 +10724,12 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a);
        <entry>user name of current execution context</entry>
       </row>
 
+      <row>
+       <entry><literal><function>current_query</function></literal></entry>
+       <entry><type>text</type></entry>
+       <entry>text of the currently executing query (might contain more than one statement)</entry>
+      </row>  
+
       <row>
        <entry><literal><function>inet_client_addr</function>()</literal></entry>
        <entry><type>inet</type></entry>
index 82bc8308cfc8af1479385f5232eb7787a133dd3b..0d580539695b2241e514b0e3fd56a525ea242357 100644 (file)
@@ -29,6 +29,7 @@
 #include "storage/pmsignal.h"
 #include "storage/procarray.h"
 #include "utils/builtins.h"
+#include "tcop/tcopprot.h"
 
 #define atooid(x)  ((Oid) strtoul((x), NULL, 10))
 
@@ -71,6 +72,19 @@ current_database(PG_FUNCTION_ARGS)
 }
 
 
+/*
+ * current_query()
+ *  Expose the current query to the user (useful in stored procedures)
+ */
+Datum
+current_query(PG_FUNCTION_ARGS)
+{
+       if (debug_query_string)
+               PG_RETURN_TEXT_P(cstring_to_text(debug_query_string));
+       else
+               PG_RETURN_NULL();
+}
+
 /*
  * Functions to send signals to other backends.
  */
index 23484149bb62d2de8d0e3109bc62191591173d47..8354fd7d6bb37f52c8decdd269b922636bfe0a75 100644 (file)
@@ -1117,6 +1117,8 @@ DESCR("convert char to char()");
 
 DATA(insert OID = 861 ( current_database          PGNSP PGUID 12 1 0 f f t f i 0 19 "" _null_ _null_ _null_ current_database - _null_ _null_ ));
 DESCR("returns the current database");
+DATA(insert OID = 817 (  current_query        PGNSP PGUID 12 1 0 f f f f v 0 25  "" _null_ _null_ _null_  current_query - _null_ _null_ ));
+DESCR("returns the currently executing query");
 
 DATA(insert OID =  862 (  int4_mul_cash                   PGNSP PGUID 12 1 0 f f t f i 2 790 "23 790" _null_ _null_ _null_ int4_mul_cash - _null_ _null_ ));
 DESCR("multiply");
index adc9760d4f05661f8425855ada4d981d5f362052..253fff29ee4826cb747bdd3e8b97e2c1425a851e 100644 (file)
@@ -414,6 +414,7 @@ extern Datum pg_ls_dir(PG_FUNCTION_ARGS);
 extern Datum nullvalue(PG_FUNCTION_ARGS);
 extern Datum nonnullvalue(PG_FUNCTION_ARGS);
 extern Datum current_database(PG_FUNCTION_ARGS);
+extern Datum current_query(PG_FUNCTION_ARGS);
 extern Datum pg_cancel_backend(PG_FUNCTION_ARGS);
 extern Datum pg_reload_conf(PG_FUNCTION_ARGS);
 extern Datum pg_tablespace_databases(PG_FUNCTION_ARGS);