Thread: Implemented current_query

Implemented current_query

From
Tomas Doran
Date:
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


Attachment

Re: Implemented current_query

From
Neil Conway
Date:
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



Re: Implemented current_query

From
Tomas Doran
Date:
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


Attachment

Re: Implemented current_query

From
Alvaro Herrera
Date:
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.

Re: Implemented current_query

From
Tomas Doran
Date:
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
>>> user
>>
>> Should that be added to the TODO list?
>
> Probably ...

Done!

Cheers
Tom


Attachment

Re: Implemented current_query

From
Bruce Momjian
Date:
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
> >>> user
> >>
> >> Should 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. +


Re: Implemented current_query

From
Bruce Momjian
Date:
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. +
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);