Implemented current_query
As suggested in the TODO list (and as I need the functionality
myself), I have implemented the current_query interface to
debug_query_string.
I'm not sure the best place to put this, suggestions welcome..
Please review the patch attached.
Cheers
Tom
Attachments:
pgsql-current_query.patchapplication/octet-stream; name=pgsql-current_query.patch; x-unix-mode=0644Download
Index: doc/TODO
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/TODO,v
retrieving revision 1.2180
diff -c -r1.2180 TODO
*** doc/TODO 5 May 2007 15:40:01 -0000 1.2180
--- doc/TODO 7 May 2007 18:45:19 -0000
***************
*** 1436,1442 ****
* Remove or relicense modules that are not under the BSD license, if possible
* %Remove memory/file descriptor freeing before ereport(ERROR)
* Acquire lock on a relation before building a relcache entry for it
- * %Promote debug_query_string into a server-side function current_query()
* Allow cross-compiling by generating the zic database on the target system
* Improve NLS maintenance of libpgport messages linked onto applications
* Allow ecpg to work with MSVC and BCC
--- 1436,1441 ----
Index: doc/src/FAQ/TODO.html
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/FAQ/TODO.html,v
retrieving revision 1.681
diff -c -r1.681 TODO.html
*** doc/src/FAQ/TODO.html 5 May 2007 15:40:01 -0000 1.681
--- doc/src/FAQ/TODO.html 7 May 2007 18:45:20 -0000
***************
*** 1278,1284 ****
</li><li>Remove or relicense modules that are not under the BSD license, if possible
</li><li>%Remove memory/file descriptor freeing before ereport(ERROR)
</li><li>Acquire lock on a relation before building a relcache entry for it
- </li><li>%Promote debug_query_string into a server-side function current_query()
</li><li>Allow cross-compiling by generating the zic database on the target system
</li><li>Improve NLS maintenance of libpgport messages linked onto applications
</li><li>Allow ecpg to work with MSVC and BCC
--- 1278,1283 ----
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.379
diff -c -r1.379 func.sgml
*** doc/src/sgml/func.sgml 7 May 2007 07:53:26 -0000 1.379
--- doc/src/sgml/func.sgml 7 May 2007 18:45:29 -0000
***************
*** 10233,10238 ****
--- 10233,10244 ----
</row>
<row>
+ <entry><literal><function>current_query</function></literal></entry>
+ <entry><type>name</type></entry>
+ <entry>text of the currently executing query</entry>
+ </row>
+
+ <row>
<entry><literal><function>inet_client_addr</function>()</literal></entry>
<entry><type>inet</type></entry>
<entry>address of the remote connection</entry>
Index: src/backend/utils/adt/misc.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/misc.c,v
retrieving revision 1.56
diff -c -r1.56 misc.c
*** src/backend/utils/adt/misc.c 5 Jan 2007 22:19:41 -0000 1.56
--- src/backend/utils/adt/misc.c 7 May 2007 18:45:30 -0000
***************
*** 29,34 ****
--- 29,35 ----
#include "storage/pmsignal.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
+ #include "tcop/tcopprot.h"
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
***************
*** 72,77 ****
--- 73,103 ----
/*
+ * current_query()
+ * Expose the current query to the user (useful in stored procedures)
+ */
+ Datum
+ current_query(PG_FUNCTION_ARGS)
+ {
+ int len;
+ text *result;
+
+ if (debug_query_string)
+ {
+ len = strlen(debug_query_string);
+ result = (text *) palloc(VARHDRSZ + len);
+ SET_VARSIZE(result, VARHDRSZ + len);
+ memcpy((void *) VARDATA(result), (void *) debug_query_string, len);
+
+ PG_RETURN_TEXT_P(result);
+ }
+ else
+ {
+ PG_RETURN_NULL();
+ }
+ }
+
+ /*
* Functions to send signals to other backends.
*/
static bool
--- 392,397 ----
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.454
diff -c -r1.454 pg_proc.h
*** src/include/catalog/pg_proc.h 2 Apr 2007 03:49:40 -0000 1.454
--- src/include/catalog/pg_proc.h 7 May 2007 18:45:38 -0000
***************
*** 1142,1147 ****
--- 1142,1149 ----
DATA(insert OID = 861 ( current_database PGNSP PGUID 12 1 0 f f t f i 0 19 "" _null_ _null_ _null_ current_database - _null_ ));
DESCR("returns the current database");
+ DATA(insert OID = 868 ( current_query PGNSP PGUID 12 1 0 f f f f v 0 2278 "" _null_ _null_ _null_ current_query - _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_ ));
DESCR("multiply");
Index: src/include/utils/builtins.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/utils/builtins.h,v
retrieving revision 1.291
diff -c -r1.291 builtins.h
*** src/include/utils/builtins.h 2 Apr 2007 03:49:41 -0000 1.291
--- src/include/utils/builtins.h 7 May 2007 18:45:39 -0000
***************
*** 421,426 ****
--- 421,427 ----
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);
On Mon, 2007-07-05 at 19:48 +0100, Tomas Doran wrote:
As suggested in the TODO list (and as I need the functionality
myself), I have implemented the current_query interface to
debug_query_string.
Comments:
* docs need a bit more detail (they should emphasize that it is the
current query string submitted by the client, as opposed to the
currently executing SPI command or the like). Also, the docs currently
claim current_query() returns "name".
* use textin() to convert C-style strings to text, rather than
constructing a text datum by hand
* perhaps we can get away with marking current_query() stable?
* AFAIK debug_query_string() still does the wrong thing when the user
submits multiple queries in a single protocol message (separated by
semi-colons). Not sure there's a way to fix that that is both easy and
efficient, though...
-Neil
On 7 May 2007, at 23:25, Neil Conway wrote:
On Mon, 2007-07-05 at 19:48 +0100, Tomas Doran wrote:
As suggested in the TODO list (and as I need the functionality
myself), I have implemented the current_query interface to
debug_query_string.* docs need a bit more detail (they should emphasize that it is the
Detail added. I'm none too happy with the phrasing, anyone suggest
better?
* use textin() to convert C-style strings to text, rather than
constructing a text datum by hand
Done.
* perhaps we can get away with marking current_query() stable?
Also done, note OID has changed as I was having conflicts (template1
wouldn't build). Should I either pick something else unused which is
lower (is there anything?), move current_query to the end of the file
or just leave it be..
* AFAIK debug_query_string() still does the wrong thing when the user
submits multiple queries in a single protocol message (separated by
semi-colons). Not sure there's a way to fix that that is both easy and
efficient, though...
Should that be added to the TODO list?
Cheers
Tom
Attachments:
pgsql-current_query_2.patchapplication/octet-stream; name=pgsql-current_query_2.patch; x-unix-mode=0644Download
Index: contrib/dblink/dblink.c
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/dblink.c,v
retrieving revision 1.63
diff -c -r1.63 dblink.c
*** contrib/dblink/dblink.c 6 Apr 2007 04:21:41 -0000 1.63
--- contrib/dblink/dblink.c 10 May 2007 01:57:30 -0000
***************
*** 1630,1652 ****
PG_RETURN_TEXT_P(GET_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(GET_TEXT(debug_query_string));
- else
- PG_RETURN_NULL();
- }
-
-
/*************************************************************
* internal functions
*/
--- 1630,1635 ----
Index: contrib/dblink/dblink.h
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/dblink.h,v
retrieving revision 1.18
diff -c -r1.18 dblink.h
*** contrib/dblink/dblink.h 5 Jan 2007 22:19:17 -0000 1.18
--- contrib/dblink/dblink.h 10 May 2007 01:57:30 -0000
***************
*** 56,61 ****
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 */
--- 56,60 ----
Index: contrib/dblink/dblink.sql.in
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/dblink.sql.in,v
retrieving revision 1.11
diff -c -r1.11 dblink.sql.in
*** contrib/dblink/dblink.sql.in 2 Sep 2006 21:11:15 -0000 1.11
--- contrib/dblink/dblink.sql.in 10 May 2007 01:57:30 -0000
***************
*** 140,150 ****
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'
--- 140,145 ----
Index: contrib/dblink/uninstall_dblink.sql
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/uninstall_dblink.sql,v
retrieving revision 1.3
diff -c -r1.3 uninstall_dblink.sql
*** contrib/dblink/uninstall_dblink.sql 11 Sep 2006 02:10:26 -0000 1.3
--- contrib/dblink/uninstall_dblink.sql 10 May 2007 01:57:30 -0000
***************
*** 1,5 ****
- 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);
--- 1,3 ----
Index: contrib/dblink/doc/misc
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/doc/misc,v
retrieving revision 1.4
diff -c -r1.4 misc
*** contrib/dblink/doc/misc 2 Sep 2006 21:11:15 -0000 1.4
--- contrib/dblink/doc/misc 10 May 2007 01:57:30 -0000
***************
*** 2,32 ****
==================================================================
Name
- dblink_current_query -- returns the current query string
-
- Synopsis
-
- dblink_current_query () RETURNS text
-
- Inputs
-
- None
-
- Outputs
-
- Returns text -- a copy of the currently executing query
-
- Example usage
-
- test=# select dblink_current_query() from (select dblink('dbname=postgres','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
- dblink_current_query
- -----------------------------------------------------------------------------------------------------------------------------------------------------
- select dblink_current_query() from (select dblink('dbname=postgres','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
- (1 row)
-
- ==================================================================
- Name
-
dblink_get_pkey -- returns the position and field names of a relation's
primary key fields
--- 2,7 ----
Index: contrib/dblink/expected/dblink.out
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/expected/dblink.out,v
retrieving revision 1.19
diff -c -r1.19 dblink.out
*** contrib/dblink/expected/dblink.out 1 Feb 2007 21:05:29 -0000 1.19
--- contrib/dblink/expected/dblink.out 10 May 2007 01:57:31 -0000
***************
*** 21,30 ****
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
--- 21,30 ----
INSERT INTO foo VALUES (9,'j','{"a9","b9","c9"}');
-- misc utilities
-- show the currently executing query
! SELECT 'hello' AS hello, current_query() AS query;
hello | query
! -------+----------------------------------------------------
! hello | SELECT 'hello' AS hello, current_query() AS query;
(1 row)
-- list the primary key fields
Index: contrib/dblink/sql/dblink.sql
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/sql/dblink.sql,v
retrieving revision 1.16
diff -c -r1.16 dblink.sql
*** contrib/dblink/sql/dblink.sql 2 Sep 2006 21:11:15 -0000 1.16
--- contrib/dblink/sql/dblink.sql 10 May 2007 01:57:31 -0000
***************
*** 26,32 ****
-- misc utilities
-- show the currently executing query
! SELECT 'hello' AS hello, dblink_current_query() AS query;
-- list the primary key fields
SELECT *
--- 26,32 ----
-- misc utilities
-- show the currently executing query
! SELECT 'hello' AS hello, current_query() AS query;
-- list the primary key fields
SELECT *
Index: doc/TODO
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/TODO,v
retrieving revision 1.2180
diff -c -r1.2180 TODO
*** doc/TODO 5 May 2007 15:40:01 -0000 1.2180
--- doc/TODO 10 May 2007 01:57:33 -0000
***************
*** 1436,1442 ****
* Remove or relicense modules that are not under the BSD license, if possible
* %Remove memory/file descriptor freeing before ereport(ERROR)
* Acquire lock on a relation before building a relcache entry for it
- * %Promote debug_query_string into a server-side function current_query()
* Allow cross-compiling by generating the zic database on the target system
* Improve NLS maintenance of libpgport messages linked onto applications
* Allow ecpg to work with MSVC and BCC
--- 1436,1441 ----
Index: doc/src/FAQ/TODO.html
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/FAQ/TODO.html,v
retrieving revision 1.681
diff -c -r1.681 TODO.html
*** doc/src/FAQ/TODO.html 5 May 2007 15:40:01 -0000 1.681
--- doc/src/FAQ/TODO.html 10 May 2007 01:57:34 -0000
***************
*** 1278,1284 ****
</li><li>Remove or relicense modules that are not under the BSD license, if possible
</li><li>%Remove memory/file descriptor freeing before ereport(ERROR)
</li><li>Acquire lock on a relation before building a relcache entry for it
- </li><li>%Promote debug_query_string into a server-side function current_query()
</li><li>Allow cross-compiling by generating the zic database on the target system
</li><li>Improve NLS maintenance of libpgport messages linked onto applications
</li><li>Allow ecpg to work with MSVC and BCC
--- 1278,1283 ----
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.379
diff -c -r1.379 func.sgml
*** doc/src/sgml/func.sgml 7 May 2007 07:53:26 -0000 1.379
--- doc/src/sgml/func.sgml 10 May 2007 01:57:43 -0000
***************
*** 10233,10238 ****
--- 10233,10244 ----
</row>
<row>
+ <entry><literal><function>current_query</function></literal></entry>
+ <entry><type>text</type></entry>
+ <entry>text of the current client query (not SPI queries or similar)</entry>
+ </row>
+
+ <row>
<entry><literal><function>inet_client_addr</function>()</literal></entry>
<entry><type>inet</type></entry>
<entry>address of the remote connection</entry>
Index: src/backend/utils/adt/misc.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/misc.c,v
retrieving revision 1.56
diff -c -r1.56 misc.c
*** src/backend/utils/adt/misc.c 5 Jan 2007 22:19:41 -0000 1.56
--- src/backend/utils/adt/misc.c 10 May 2007 01:57:44 -0000
***************
*** 29,34 ****
--- 29,35 ----
#include "storage/pmsignal.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
+ #include "tcop/tcopprot.h"
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
***************
*** 72,77 ****
--- 73,99 ----
/*
+ * current_query()
+ * Expose the current query to the user (useful in stored procedures)
+ */
+ Datum
+ current_query(PG_FUNCTION_ARGS)
+ {
+ int len;
+ text *result;
+
+ if (debug_query_string)
+ {
+ result = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(debug_query_string)));
+ PG_RETURN_TEXT_P(result);
+ }
+ else
+ {
+ PG_RETURN_NULL();
+ }
+ }
+
+ /*
* Functions to send signals to other backends.
*/
static bool
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.454
diff -c -r1.454 pg_proc.h
*** src/include/catalog/pg_proc.h 2 Apr 2007 03:49:40 -0000 1.454
--- src/include/catalog/pg_proc.h 10 May 2007 01:57:51 -0000
***************
*** 1142,1147 ****
--- 1142,1149 ----
DATA(insert OID = 861 ( current_database PGNSP PGUID 12 1 0 f f t f i 0 19 "" _null_ _null_ _null_ current_database - _null_ ));
DESCR("returns the current database");
+ DATA(insert OID = 3534 ( current_query PGNSP PGUID 12 1 0 f f f f s 0 25 "" _null_ _null_ _null_ current_query - _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_ ));
DESCR("multiply");
Index: src/include/utils/builtins.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/utils/builtins.h,v
retrieving revision 1.291
diff -c -r1.291 builtins.h
*** src/include/utils/builtins.h 2 Apr 2007 03:49:41 -0000 1.291
--- src/include/utils/builtins.h 10 May 2007 01:57:53 -0000
***************
*** 421,426 ****
--- 421,427 ----
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);
Tomas Doran wrote:
On 7 May 2007, at 23:25, Neil Conway wrote:
On Mon, 2007-07-05 at 19:48 +0100, Tomas Doran wrote:
As suggested in the TODO list (and as I need the functionality
myself), I have implemented the current_query interface to
debug_query_string.
FWIW I think you should still provide dblink_current_query, even if it's
only a wrapper over current_query(), for backwards compatibility.
Also, typically we don't remove items from the TODO list. We mark them
as "done" prepending them with a dash. Patch authors are not expected
to do it either (though I don't see it be a problem if they did).
Also done, note OID has changed as I was having conflicts (template1
wouldn't build). Should I either pick something else unused which is
lower (is there anything?), move current_query to the end of the file
or just leave it be..
Doesn't matter ... just make sure duplicate_oids doesn't report a
problem. unused_oids is useful to find, err, unused OIDs.
* AFAIK debug_query_string() still does the wrong thing when the user
submits multiple queries in a single protocol message (separated by
semi-colons). Not sure there's a way to fix that that is both easy and
efficient, though...Should that be added to the TODO list?
Probably ...
--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.
On 10 May 2007, at 03:09, Alvaro Herrera wrote:
FWIW I think you should still provide dblink_current_query, even if
it's
only a wrapper over current_query(), for backwards compatibility.
Good point. Done as suggested (I think, or did you mean also the
change of instances to use current_query()?). Replaced
dblink_current_query with an SQL procedure wrapper, I assume that's
the most efficient way of doing it?
Also, typically we don't remove items from the TODO list. We mark
them
as "done" prepending them with a dash. Patch authors are not expected
to do it either (though I don't see it be a problem if they did).
Not quite sure what you're suggesting (which way round), so I just
didn't do it (as you said I'm not expected to).
Doesn't matter ... just make sure duplicate_oids doesn't report a
problem. unused_oids is useful to find, err, unused OIDs.
Ahh, hadn't found those, thanks. They're in the dev FAQ too, *blush*.
I need this for something I'm doing at $ork, and thought I'd
implement it in the backend, as well as a .so, it's been a learning
experience :)
* AFAIK debug_query_string() still does the wrong thing when the
userShould that be added to the TODO list?
Probably ...
Done!
Cheers
Tom
Attachments:
pgsql-current_query.patchapplication/octet-stream; name=pgsql-current_query.patch; x-unix-mode=0644Download
Index: contrib/dblink/dblink.c
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/dblink.c,v
retrieving revision 1.63
diff -c -r1.63 dblink.c
*** contrib/dblink/dblink.c 6 Apr 2007 04:21:41 -0000 1.63
--- contrib/dblink/dblink.c 7 May 2007 20:33:25 -0000
***************
*** 1630,1652 ****
PG_RETURN_TEXT_P(GET_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(GET_TEXT(debug_query_string));
- else
- PG_RETURN_NULL();
- }
-
-
/*************************************************************
* internal functions
*/
--- 1630,1635 ----
Index: contrib/dblink/dblink.h
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/dblink.h,v
retrieving revision 1.18
diff -c -r1.18 dblink.h
*** contrib/dblink/dblink.h 5 Jan 2007 22:19:17 -0000 1.18
--- contrib/dblink/dblink.h 7 May 2007 20:33:25 -0000
***************
*** 56,61 ****
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 */
--- 56,60 ----
Index: contrib/dblink/dblink.sql.in
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/dblink.sql.in,v
retrieving revision 1.11
diff -c -r1.11 dblink.sql.in
*** contrib/dblink/dblink.sql.in 2 Sep 2006 21:11:15 -0000 1.11
--- contrib/dblink/dblink.sql.in 7 May 2007 20:33:25 -0000
***************
*** 140,150 ****
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'
--- 140,145 ----
Index: contrib/dblink/uninstall_dblink.sql
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/uninstall_dblink.sql,v
retrieving revision 1.3
diff -c -r1.3 uninstall_dblink.sql
*** contrib/dblink/uninstall_dblink.sql 11 Sep 2006 02:10:26 -0000 1.3
--- contrib/dblink/uninstall_dblink.sql 7 May 2007 20:33:25 -0000
***************
*** 1,5 ****
- 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);
--- 1,3 ----
Index: contrib/dblink/doc/misc
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/doc/misc,v
retrieving revision 1.4
diff -c -r1.4 misc
*** contrib/dblink/doc/misc 2 Sep 2006 21:11:15 -0000 1.4
--- contrib/dblink/doc/misc 7 May 2007 20:33:26 -0000
***************
*** 2,32 ****
==================================================================
Name
- dblink_current_query -- returns the current query string
-
- Synopsis
-
- dblink_current_query () RETURNS text
-
- Inputs
-
- None
-
- Outputs
-
- Returns text -- a copy of the currently executing query
-
- Example usage
-
- test=# select dblink_current_query() from (select dblink('dbname=postgres','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
- dblink_current_query
- -----------------------------------------------------------------------------------------------------------------------------------------------------
- select dblink_current_query() from (select dblink('dbname=postgres','select oid, proname from pg_proc where proname = ''byteacat''') as f1) as t1;
- (1 row)
-
- ==================================================================
- Name
-
dblink_get_pkey -- returns the position and field names of a relation's
primary key fields
--- 2,7 ----
Index: contrib/dblink/expected/dblink.out
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/expected/dblink.out,v
retrieving revision 1.19
diff -c -r1.19 dblink.out
*** contrib/dblink/expected/dblink.out 1 Feb 2007 21:05:29 -0000 1.19
--- contrib/dblink/expected/dblink.out 7 May 2007 20:33:26 -0000
***************
*** 21,30 ****
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
--- 21,30 ----
INSERT INTO foo VALUES (9,'j','{"a9","b9","c9"}');
-- misc utilities
-- show the currently executing query
! SELECT 'hello' AS hello, current_query() AS query;
hello | query
! -------+----------------------------------------------------
! hello | SELECT 'hello' AS hello, current_query() AS query;
(1 row)
-- list the primary key fields
Index: contrib/dblink/sql/dblink.sql
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/dblink/sql/dblink.sql,v
retrieving revision 1.16
diff -c -r1.16 dblink.sql
*** contrib/dblink/sql/dblink.sql 2 Sep 2006 21:11:15 -0000 1.16
--- contrib/dblink/sql/dblink.sql 7 May 2007 20:33:26 -0000
***************
*** 26,32 ****
-- misc utilities
-- show the currently executing query
! SELECT 'hello' AS hello, dblink_current_query() AS query;
-- list the primary key fields
SELECT *
--- 26,32 ----
-- misc utilities
-- show the currently executing query
! SELECT 'hello' AS hello, current_query() AS query;
-- list the primary key fields
SELECT *
Index: doc/TODO
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/TODO,v
retrieving revision 1.2180
diff -c -r1.2180 TODO
*** doc/TODO 5 May 2007 15:40:01 -0000 1.2180
--- doc/TODO 7 May 2007 20:33:28 -0000
***************
*** 1436,1442 ****
* Remove or relicense modules that are not under the BSD license, if possible
* %Remove memory/file descriptor freeing before ereport(ERROR)
* Acquire lock on a relation before building a relcache entry for it
- * %Promote debug_query_string into a server-side function current_query()
* Allow cross-compiling by generating the zic database on the target system
* Improve NLS maintenance of libpgport messages linked onto applications
* Allow ecpg to work with MSVC and BCC
--- 1436,1441 ----
Index: doc/src/FAQ/TODO.html
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/FAQ/TODO.html,v
retrieving revision 1.681
diff -c -r1.681 TODO.html
*** doc/src/FAQ/TODO.html 5 May 2007 15:40:01 -0000 1.681
--- doc/src/FAQ/TODO.html 7 May 2007 20:33:30 -0000
***************
*** 1278,1284 ****
</li><li>Remove or relicense modules that are not under the BSD license, if possible
</li><li>%Remove memory/file descriptor freeing before ereport(ERROR)
</li><li>Acquire lock on a relation before building a relcache entry for it
- </li><li>%Promote debug_query_string into a server-side function current_query()
</li><li>Allow cross-compiling by generating the zic database on the target system
</li><li>Improve NLS maintenance of libpgport messages linked onto applications
</li><li>Allow ecpg to work with MSVC and BCC
--- 1278,1283 ----
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.379
diff -c -r1.379 func.sgml
*** doc/src/sgml/func.sgml 7 May 2007 07:53:26 -0000 1.379
--- doc/src/sgml/func.sgml 7 May 2007 20:33:39 -0000
***************
*** 10233,10238 ****
--- 10233,10244 ----
</row>
<row>
+ <entry><literal><function>current_query</function></literal></entry>
+ <entry><type>name</type></entry>
+ <entry>text of the currently executing query</entry>
+ </row>
+
+ <row>
<entry><literal><function>inet_client_addr</function>()</literal></entry>
<entry><type>inet</type></entry>
<entry>address of the remote connection</entry>
Index: src/backend/utils/adt/misc.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/misc.c,v
retrieving revision 1.56
diff -c -r1.56 misc.c
*** src/backend/utils/adt/misc.c 5 Jan 2007 22:19:41 -0000 1.56
--- src/backend/utils/adt/misc.c 7 May 2007 20:33:41 -0000
***************
*** 29,34 ****
--- 29,35 ----
#include "storage/pmsignal.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
+ #include "tcop/tcopprot.h"
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
***************
*** 72,77 ****
--- 73,103 ----
/*
+ * current_query()
+ * Expose the current query to the user (useful in stored procedures)
+ */
+ Datum
+ current_query(PG_FUNCTION_ARGS)
+ {
+ int len;
+ text *result;
+
+ if (debug_query_string)
+ {
+ len = strlen(debug_query_string);
+ result = (text *) palloc(VARHDRSZ + len);
+ SET_VARSIZE(result, VARHDRSZ + len);
+ memcpy((void *) VARDATA(result), (void *) debug_query_string, len);
+
+ PG_RETURN_TEXT_P(result);
+ }
+ else
+ {
+ PG_RETURN_NULL();
+ }
+ }
+
+ /*
* Functions to send signals to other backends.
*/
static bool
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.454
diff -c -r1.454 pg_proc.h
*** src/include/catalog/pg_proc.h 2 Apr 2007 03:49:40 -0000 1.454
--- src/include/catalog/pg_proc.h 7 May 2007 20:33:48 -0000
***************
*** 1142,1147 ****
--- 1142,1149 ----
DATA(insert OID = 861 ( current_database PGNSP PGUID 12 1 0 f f t f i 0 19 "" _null_ _null_ _null_ current_database - _null_ ));
DESCR("returns the current database");
+ DATA(insert OID = 868 ( current_query PGNSP PGUID 12 1 0 f f f f v 0 2278 "" _null_ _null_ _null_ current_query - _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_ ));
DESCR("multiply");
Index: src/include/utils/builtins.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/utils/builtins.h,v
retrieving revision 1.291
diff -c -r1.291 builtins.h
*** src/include/utils/builtins.h 2 Apr 2007 03:49:41 -0000 1.291
--- src/include/utils/builtins.h 7 May 2007 20:33:49 -0000
***************
*** 421,426 ****
--- 421,427 ----
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);
This has been saved for the 8.4 release:
http://momjian.postgresql.org/cgi-bin/pgpatches_hold
---------------------------------------------------------------------------
Tomas Doran wrote:
On 10 May 2007, at 03:09, Alvaro Herrera wrote:
FWIW I think you should still provide dblink_current_query, even if
it's
only a wrapper over current_query(), for backwards compatibility.Good point. Done as suggested (I think, or did you mean also the
change of instances to use current_query()?). Replaced
dblink_current_query with an SQL procedure wrapper, I assume that's
the most efficient way of doing it?Also, typically we don't remove items from the TODO list. We mark
them
as "done" prepending them with a dash. Patch authors are not expected
to do it either (though I don't see it be a problem if they did).Not quite sure what you're suggesting (which way round), so I just
didn't do it (as you said I'm not expected to).Doesn't matter ... just make sure duplicate_oids doesn't report a
problem. unused_oids is useful to find, err, unused OIDs.Ahh, hadn't found those, thanks. They're in the dev FAQ too, *blush*.
I need this for something I'm doing at $ork, and thought I'd
implement it in the backend, as well as a .so, it's been a learning
experience :)* AFAIK debug_query_string() still does the wrong thing when the
userShould that be added to the TODO list?
Probably ...
Done!
Cheers
Tom
[ Attachment, skipping... ]
---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com
+ If your life is a hard drive, Christ can be your backup. +
Neil Conway wrote:
On Mon, 2007-07-05 at 19:48 +0100, Tomas Doran wrote:
As suggested in the TODO list (and as I need the functionality
myself), I have implemented the current_query interface to
debug_query_string.
It actually has been removed from the TODO list since you saw it last.
Comments:
...
* AFAIK debug_query_string() still does the wrong thing when the user
submits multiple queries in a single protocol message (separated by
semi-colons). Not sure there's a way to fix that that is both easy and
efficient, though...
The problem with the last bullet is pretty serious. It can be
illustrated with psql:
$ psql -c 'set log_statement="all";select 1;select 2;' test
Server log shows:
STATEMENT: set log_statement=all;select 1;select 2;
Obviously this is what current_query() would return if we commit this
patch, and it probably isn't 100% accurate. I see dblink exposes this:
http://www.postgresql.org/docs/8.3/static/contrib-dblink-current-query.html
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.
but making it more widely available with a possible inaccurate result is
a problem. We can't think of anyway to fix this cleanly --- it would
require a separate parser pass to split queries by semicolons (which
psql does by default in interactive mode). Right now the parser does
the splitting as part of its normal single-parse operation and just
creates parse trees that don't have string representations.
Perhaps we could name it received_query() to indicate it is what the
backend received and it not necessarily the _current_ query.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ If your life is a hard drive, Christ can be your backup. +
On 28 Mar 2008, at 17:23, Bruce Momjian wrote:
Neil Conway wrote:
On Mon, 2007-07-05 at 19:48 +0100, Tomas Doran wrote:
As suggested in the TODO list (and as I need the functionality
myself), I have implemented the current_query interface to
debug_query_string.It actually has been removed from the TODO list since you saw it last.
I submitted a patch to make it do that a while ago :)
Comments:
...
* AFAIK debug_query_string() still does the wrong thing when the user
submits multiple queries in a single protocol message (separated by
semi-colons). Not sure there's a way to fix that that is both easy
and
efficient, though...The problem with the last bullet is pretty serious. It can be
illustrated with psql:$ psql -c 'set log_statement="all";select 1;select 2;' test
Server log shows:
STATEMENT: set log_statement=all;select 1;select 2;
Obviously this is what current_query() would return if we commit this
patch, and it probably isn't 100% accurate.
Yeah, this was pointed out to me at the time.
Fortunately, for what I wanted to do, 'Don't do that then' was a very
viable answer..
I see dblink exposes this:
http://www.postgresql.org/docs/8.3/static/contrib-dblink-
current-query.htmlReturns 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.
My patch provided this functionality in core, and made dblink's
current procedure to do the same just delegate to the one that I
provided (for backwards compatibility reasons)
but making it more widely available with a possible inaccurate
result is
a problem. We can't think of anyway to fix this cleanly --- it would
require a separate parser pass to split queries by semicolons (which
psql does by default in interactive mode). Right now the parser does
the splitting as part of its normal single-parse operation and just
creates parse trees that don't have string representations.Perhaps we could name it received_query() to indicate it is what the
backend received and it not necessarily the _current_ query.
reveived_query() sounds like a very sane name for me, and documenting
it as such would allow you to expose the functionality without the
possible complaints...
In a lot of environments where you actually want this, then
constraining to 1 query per statement (outside the DB level) is very
doable... I wouldn't like to see the functionality skipped over as
providing this only solves 80% of cases.
In the particular application that I wrote the patch for, we needed
to audit 'all access to encrypted credit card numbers' for PCI
requirements..
Our solution was to put all cc number containing tables into their
own schema / with no general permissions, and to use SECURITY DEFINER
stored procedures to access them (and log the access).. However that
wasn't quite good enough, so we got our DB access layer to iterate up
the call stack (till outside our SQL abstraction), and add a comment
to every query such that it took the form:
/* CodeFile-LineNo-UserId */ SELECT stored_procedure(arg1, arg2);
for all queries - so the caller information was encoded in the query
info... Therefore, inside 'stored_procedure', logging the value of
current_query() was perfect to satisfy our audit requirements, and we
can just log the current query when we enter 'stored_procedure'.
Hope this helps to clarify that, whilst the current mechanism isn't
in any way perfect - there are a number of use cases for including
the functionality as-is.
Cheers
Tom
Cheers
Tom
Tomas Doran wrote:
On 28 Mar 2008, at 17:23, Bruce Momjian wrote:
Perhaps we could name it received_query() to indicate it is what the
backend received and it not necessarily the _current_ query.reveived_query() sounds like a very sane name for me, and documenting it
as such would allow you to expose the functionality without the possible
complaints...
client_query perhaps?
--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Alvaro Herrera wrote:
Tomas Doran wrote:
On 28 Mar 2008, at 17:23, Bruce Momjian wrote:
Perhaps we could name it received_query() to indicate it is what the
backend received and it not necessarily the _current_ query.reveived_query() sounds like a very sane name for me, and documenting it
as such would allow you to expose the functionality without the possible
complaints...client_query perhaps?
Yea, that is consistent with what we do with other functions.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ If your life is a hard drive, Christ can be your backup. +
On Fri, 2008-03-28 at 14:32 -0400, Bruce Momjian wrote:
Alvaro Herrera wrote:
Tomas Doran wrote:
On 28 Mar 2008, at 17:23, Bruce Momjian wrote:
Perhaps we could name it received_query() to indicate it is what the
backend received and it not necessarily the _current_ query.reveived_query() sounds like a very sane name for me, and documenting it
as such would allow you to expose the functionality without the possible
complaints...client_query perhaps?
Yea, that is consistent with what we do with other functions.
How about client_request()
It's then clear that a request can be made up of many statements, which
will be executed in turn.
--
Simon Riggs
2ndQuadrant http://www.2ndQuadrant.com
PostgreSQL UK 2008 Conference: http://www.postgresql.org.uk
Simon Riggs wrote:
On Fri, 2008-03-28 at 14:32 -0400, Bruce Momjian wrote:
Alvaro Herrera wrote:
Tomas Doran wrote:
On 28 Mar 2008, at 17:23, Bruce Momjian wrote:
Perhaps we could name it received_query() to indicate it is what the
backend received and it not necessarily the _current_ query.reveived_query() sounds like a very sane name for me, and documenting it
as such would allow you to expose the functionality without the possible
complaints...client_query perhaps?
Yea, that is consistent with what we do with other functions.
How about client_request()
It's then clear that a request can be made up of many statements, which
will be executed in turn.
The problem with client_request() is that it is not clear it is a query
--- it could be a disonnection or cancel request, for example.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ If your life is a hard drive, Christ can be your backup. +
Bruce Momjian wrote:
Alvaro Herrera wrote:
Tomas Doran wrote:
On 28 Mar 2008, at 17:23, Bruce Momjian wrote:
Perhaps we could name it received_query() to indicate it is what the
backend received and it not necessarily the _current_ query.reveived_query() sounds like a very sane name for me, and documenting it
as such would allow you to expose the functionality without the possible
complaints...client_query perhaps?
Yea, that is consistent with what we do with other functions.
Uh, I think based on other usage it should be called client_statement().
Peter has us using statement instead of query in many cases.
FYI, log_statement also prints the combined query string.
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ If your life is a hard drive, Christ can be your backup. +
Bruce Momjian <bruce@momjian.us> writes:
Uh, I think based on other usage it should be called client_statement().
That is *exactly* the wrong thing, because "statement" specifically
means one SQL statement.
"client_query" seems about the best compromise I've heard so far.
It's too bad we didn't have this debate before pg_stat_activity got out
into the wild, because it's now too late to rename its column
current_query. Possibly we should stick with current_query() just
for consistency with that view ...
regards, tom lane
I have applied a modified version of this patch, attached. I made a few
changes:
o You had current_query() returning 'void' so it didn't work
o I removed the dblink regression tests for current_query() as
it is now a backend function
o Update documentation to mention the possibility of multiple
statements
o Used the new cstring_to_text() usage that Tom had updated in
CVS for this function
o The pg_proc.h oids and number of columns didn't match CVS
Thanks for the patch.
---------------------------------------------------------------------------
Tomas Doran wrote:
On 10 May 2007, at 03:09, Alvaro Herrera wrote:
FWIW I think you should still provide dblink_current_query, even if
it's
only a wrapper over current_query(), for backwards compatibility.Good point. Done as suggested (I think, or did you mean also the
change of instances to use current_query()?). Replaced
dblink_current_query with an SQL procedure wrapper, I assume that's
the most efficient way of doing it?Also, typically we don't remove items from the TODO list. We mark
them
as "done" prepending them with a dash. Patch authors are not expected
to do it either (though I don't see it be a problem if they did).Not quite sure what you're suggesting (which way round), so I just
didn't do it (as you said I'm not expected to).Doesn't matter ... just make sure duplicate_oids doesn't report a
problem. unused_oids is useful to find, err, unused OIDs.Ahh, hadn't found those, thanks. They're in the dev FAQ too, *blush*.
I need this for something I'm doing at $ork, and thought I'd
implement it in the backend, as well as a .so, it's been a learning
experience :)
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ If your life is a hard drive, Christ can be your backup. +
Attachments:
/rtmp/difftext/x-diffDownload
Index: contrib/dblink/dblink.c
===================================================================
RCS file: /cvsroot/pgsql/contrib/dblink/dblink.c,v
retrieving revision 1.71
diff -c -c -r1.71 dblink.c
*** contrib/dblink/dblink.c 26 Mar 2008 21:10:36 -0000 1.71
--- contrib/dblink/dblink.c 4 Apr 2008 16:45:36 -0000
***************
*** 1631,1653 ****
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
*/
--- 1631,1636 ----
Index: contrib/dblink/dblink.h
===================================================================
RCS file: /cvsroot/pgsql/contrib/dblink/dblink.h,v
retrieving revision 1.19
diff -c -c -r1.19 dblink.h
*** contrib/dblink/dblink.h 1 Jan 2008 19:45:45 -0000 1.19
--- contrib/dblink/dblink.h 4 Apr 2008 16:45:36 -0000
***************
*** 56,61 ****
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 */
--- 56,60 ----
Index: contrib/dblink/dblink.sql.in
===================================================================
RCS file: /cvsroot/pgsql/contrib/dblink/dblink.sql.in,v
retrieving revision 1.14
diff -c -c -r1.14 dblink.sql.in
*** contrib/dblink/dblink.sql.in 13 Nov 2007 04:24:27 -0000 1.14
--- contrib/dblink/dblink.sql.in 4 Apr 2008 16:45:36 -0000
***************
*** 163,173 ****
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'
--- 163,168 ----
Index: contrib/dblink/uninstall_dblink.sql
===================================================================
RCS file: /cvsroot/pgsql/contrib/dblink/uninstall_dblink.sql,v
retrieving revision 1.5
diff -c -c -r1.5 uninstall_dblink.sql
*** contrib/dblink/uninstall_dblink.sql 13 Nov 2007 04:24:27 -0000 1.5
--- contrib/dblink/uninstall_dblink.sql 4 Apr 2008 16:45:36 -0000
***************
*** 3,10 ****
-- 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);
--- 3,8 ----
Index: contrib/dblink/expected/dblink.out
===================================================================
RCS file: /cvsroot/pgsql/contrib/dblink/expected/dblink.out,v
retrieving revision 1.21
diff -c -c -r1.21 dblink.out
*** contrib/dblink/expected/dblink.out 3 Jan 2008 21:27:59 -0000 1.21
--- contrib/dblink/expected/dblink.out 4 Apr 2008 16:45:36 -0000
***************
*** 22,34 ****
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');
--- 22,27 ----
Index: contrib/dblink/sql/dblink.sql
===================================================================
RCS file: /cvsroot/pgsql/contrib/dblink/sql/dblink.sql,v
retrieving revision 1.18
diff -c -c -r1.18 dblink.sql
*** contrib/dblink/sql/dblink.sql 3 Jan 2008 21:27:59 -0000 1.18
--- contrib/dblink/sql/dblink.sql 4 Apr 2008 16:45:36 -0000
***************
*** 27,35 ****
-- 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');
--- 27,32 ----
Index: doc/src/sgml/dblink.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/dblink.sgml,v
retrieving revision 1.3
diff -c -c -r1.3 dblink.sgml
*** doc/src/sgml/dblink.sgml 6 Dec 2007 04:12:09 -0000 1.3
--- doc/src/sgml/dblink.sgml 4 Apr 2008 16:45:37 -0000
***************
*** 1346,1394 ****
</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>
--- 1346,1351 ----
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.425
diff -c -c -r1.425 func.sgml
*** doc/src/sgml/func.sgml 23 Mar 2008 00:24:19 -0000 1.425
--- doc/src/sgml/func.sgml 4 Apr 2008 16:45:37 -0000
***************
*** 10725,10730 ****
--- 10725,10736 ----
</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>
<entry>address of the remote connection</entry>
Index: src/backend/utils/adt/misc.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v
retrieving revision 1.58
diff -c -c -r1.58 misc.c
*** src/backend/utils/adt/misc.c 1 Jan 2008 19:45:52 -0000 1.58
--- src/backend/utils/adt/misc.c 4 Apr 2008 16:45:39 -0000
***************
*** 29,34 ****
--- 29,35 ----
#include "storage/pmsignal.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
+ #include "tcop/tcopprot.h"
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
***************
*** 72,77 ****
--- 73,91 ----
/*
+ * 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.
*/
static bool
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.485
diff -c -c -r1.485 pg_proc.h
*** src/include/catalog/pg_proc.h 27 Mar 2008 03:57:34 -0000 1.485
--- src/include/catalog/pg_proc.h 4 Apr 2008 16:45:40 -0000
***************
*** 1117,1122 ****
--- 1117,1124 ----
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: src/include/utils/builtins.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/utils/builtins.h,v
retrieving revision 1.310
diff -c -c -r1.310 builtins.h
*** src/include/utils/builtins.h 25 Mar 2008 22:42:45 -0000 1.310
--- src/include/utils/builtins.h 4 Apr 2008 16:45:41 -0000
***************
*** 414,419 ****
--- 414,420 ----
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);