summaryrefslogtreecommitdiffstats
path: root/commands.c
diff options
context:
space:
mode:
authorMark Wong <markwkm@gmail.com>2007-09-19 14:31:49 -0700
committerMark Wong <markwkm@gmail.com>2007-09-19 14:31:49 -0700
commit03fd691022a4da68306aa11f42fee3776581c55d (patch)
treecc90177042f1475d604103d7a9aa8422d5b9ab48 /commands.c
parent13bc3ab29999e2e95387d88a8b1426ad201354c7 (diff)
Added a 'Q' command to show the current query for a given process id.
Needs error checking...
Diffstat (limited to 'commands.c')
-rw-r--r--commands.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/commands.c b/commands.c
index 22682fb..6ba120c 100644
--- a/commands.c
+++ b/commands.c
@@ -73,6 +73,11 @@ static char *err_listem =
errs[errcnt++].errnum = (e); \
}
+#define CURRENT_QUERY \
+ "SELECT current_query\n" \
+ "FROM pg_stat_activity\n" \
+ "WHERE procpid = %d;"
+
/*
* err_compar(p1, p2) - comparison routine used by "qsort"
* for sorting errors.
@@ -492,3 +497,29 @@ renice_procs(char *str)
return(" operation not supported");
#endif
}
+
+void
+show_current_query(PGconn *pgconn, int procpid)
+{
+ int i;
+ int rows;
+ char *sql;
+ char info[64];
+ PGresult *pgresult;
+
+ sql = (char *) malloc(strlen(CURRENT_QUERY) + 7);
+ sprintf(sql, CURRENT_QUERY, procpid);
+ sprintf(info, "Current query for procpid %d:\n\n", procpid);
+ display_pager(info);
+
+ /* Get the currently running query. */
+ pgresult = PQexec(pgconn, sql);
+ rows = PQntuples(pgresult);
+ for (i = 0; i < rows; i++) {
+ display_pager(PQgetvalue(pgresult, i, 0));
+ }
+ display_pager("\n\n");
+
+ free(sql);
+ PQclear(pgresult);
+}