Permanently Attach a Comment to a Query

Jim Knicely authored this tip.

There are several system tables like QUERY_REQUESTS that store the queries executed in the database. To help understand why a query was executed (i.e., for debugging purposes), you might want to add a comment to the SQL code.

Example:

dbadmin=> /* This is a SELECT from the table JIM */
dbadmin-> SELECT * FROM jim;
c
---
1
(1 row)

dbadmin=> SELECT request FROM query_requests WHERE transaction_id = current_trans_id() AND statement_id = current_statement() - 1;
      request
--------------------
 SELECT * FROM jim;
(1 row)

What happened to the comment?

If you’d like to “glue” the comment to the query permanently, use a “nested” comment:

dbadmin=> SELECT /* This is a SELECT from the table JIM */ * FROM jim;
c
---
1
(1 row)

dbadmin=> SELECT request FROM query_requests WHERE transaction_id = current_trans_id() AND statement_id = current_statement() - 1;
                           request
--------------------------------------------------------------
 SELECT /* This is a SELECT from the table JIM */ * FROM jim;
(1 row)

Have fun!

Sign In or Register to comment.