From 71e51a3ae34e21c273a6d21ab1320ccf0cef6f77 Mon Sep 17 00:00:00 2001 From: Mark Wong Date: Mon, 6 Apr 2020 20:45:35 -0700 Subject: Fix display of locks help by process Add a column for the schema name and index name. | database | schema | table | index | type | granted --+----------+--------+-------+--------+-----------------+-------- 1 | mark | public | | a_pkey | AccessShareLock | t 2 | mark | public | a | | AccessShareLock | t Previously the schema was not being shown and tables and indexes were being shown in the same column. --- HISTORY.rst | 2 ++ commands.c | 28 +++++++++++++++++++--------- pg.c | 24 ++++++++++++++++++------ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index c956635..76cf32a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -22,6 +22,8 @@ YYYY-MM-DD v4.0.0 * Remove toggle for display raw I/O statistics * Fix bug for specifing number of displays to show on the command line without -x flag +* Fix view of locks held by process. Separate table and index locks. Also + showing schema. 2013-07-31 v3.7.0 ----------------- diff --git a/commands.c b/commands.c index f975e6b..0c293b5 100644 --- a/commands.c +++ b/commands.c @@ -690,7 +690,7 @@ show_locks(struct pg_conninfo_ctx *conninfo, int procpid) k; int rows; char info[64]; - int width[5] = {1, 8, 5, 4, 7}; + int width[7] = {1, 8, 6, 5, 5, 4, 7}; PGresult *pgresult = NULL; char header_format[1024]; char line_format[1024]; @@ -724,16 +724,25 @@ show_locks(struct pg_conninfo_ctx *conninfo, int procpid) width[3] = strlen(PQgetvalue(pgresult, i, 2)); if (strlen(PQgetvalue(pgresult, i, 3)) > width[4]) width[4] = strlen(PQgetvalue(pgresult, i, 3)); - } - sprintf(header_format, "%%-%ds | %%-%ds | %%-%ds | %%-%ds | %%-%ds\n", - width[0], width[1], width[2], width[3], width[4]); - sprintf(line_format, "%%%dd | %%-%ds | %%-%ds | %%-%ds | %%-%ds\n", - width[0], width[1], width[2], width[3], width[4]); + if (strlen(PQgetvalue(pgresult, i, 4)) > width[5]) + width[5] = strlen(PQgetvalue(pgresult, i, 4)); + if (strlen(PQgetvalue(pgresult, i, 5)) > width[6]) + width[6] = strlen(PQgetvalue(pgresult, i, 5)); + } + sprintf(header_format, + "%%-%ds | %%-%ds | %%-%ds | %%-%ds | %%-%ds | %%-%ds | %%-%ds\n", + width[0], width[1], width[2], width[3], width[4], width[5], + width[6]); + sprintf(line_format, + "%%%dd | %%-%ds | %%-%ds | %%-%ds | %%-%ds | %%-%ds | %%-%ds\n", + width[0], width[1], width[2], width[3], width[4], width[5], + width[6]); /* Display the header. */ - sprintf(line, header_format, "", "database", "table", "type", "granted"); + sprintf(line, header_format, "", "database", "schema", "table", "index", + "type", "granted"); display_pager(line); - for (i = 0, k = 0; i < 5; i++) + for (i = 0, k = 0; i < 7; i++) { for (j = 0; j < width[i]; j++, k++) { @@ -752,7 +761,8 @@ show_locks(struct pg_conninfo_ctx *conninfo, int procpid) { sprintf(line, line_format, i + 1, PQgetvalue(pgresult, i, 0), PQgetvalue(pgresult, i, 1), PQgetvalue(pgresult, i, 2), - PQgetvalue(pgresult, i, 3)); + PQgetvalue(pgresult, i, 3), PQgetvalue(pgresult, i, 4), + PQgetvalue(pgresult, i, 5)); display_pager(line); } display_pager("\n"); diff --git a/pg.c b/pg.c index b43686f..b65728c 100644 --- a/pg.c +++ b/pg.c @@ -70,19 +70,31 @@ " FROM pg_stat_replication;" #define GET_LOCKS \ - "SELECT datname, relname, mode, granted\n" \ + "SELECT datname, nspname, r.relname, i.relname, mode, granted\n" \ "FROM pg_stat_activity, pg_locks\n" \ - "LEFT OUTER JOIN pg_class\n" \ - "ON relation = pg_class.oid\n"\ + "LEFT OUTER JOIN pg_class r \n" \ + " ON relation = r.oid\n"\ + " AND r.relkind = 'r'\n" \ + "LEFT OUTER JOIN pg_class i \n" \ + " ON relation = i.oid\n"\ + " AND i.relkind = 'i'\n" \ + "LEFT OUTER JOIN pg_namespace nsp\n" \ + " ON coalesce(r.relnamespace, i.relnamespace) = nsp.oid\n" \ "WHERE pg_stat_activity.pid = %d\n" \ " AND pg_stat_activity.pid = pg_locks.pid\n" \ " AND relation IS NOT NULL;" #define GET_LOCKS_9_1 \ - "SELECT datname, relname, mode, granted\n" \ + "SELECT datname, nspname, r.relname, i.relname, mode, granted\n" \ "FROM pg_stat_activity, pg_locks\n" \ - "LEFT OUTER JOIN pg_class\n" \ - "ON relation = pg_class.oid\n"\ + "LEFT OUTER JOIN pg_class r \n" \ + " ON relation = r.oid\n"\ + " AND r.relkind = 'r'\n" \ + "LEFT OUTER JOIN pg_class i \n" \ + " ON relation = i.oid\n"\ + " AND i.relkind = 'i'\n" \ + "LEFT OUTER JOIN pg_namespace nsp\n" \ + " ON coalesce(r.relnamespace, i.relnamespace) = nsp.oid\n" \ "WHERE procpid = %d\n" \ " AND procpid = pid\n" \ " AND relation IS NOT NULL;" -- cgit v1.2.3