postgresql中的pg_tables锁定持续时间

时间:2018-04-28 18:30:18

标签: postgresql

当我们在postgresql中运行像create table这样的查询时,字典表pg_tables会被锁定一段时间。是否有任何命令或查询可以跟踪任何查询锁定字典表的时间长度?
我访问了pg_locks表,但它只显示了lock的类型。我需要找到执行查询锁定pg_tables的持续时间。

1 个答案:

答案 0 :(得分:0)

Postgres没有任何锁定时间监控工具。为此,我们为Postgres提供了一个补丁。无论如何 - pg_tables不是表 - 它是视图

postgres=# \d+ pg_tables;
                           View "pg_catalog.pg_tables"
+-------------+---------+-----------+----------+---------+---------+-------------+
|   Column    |  Type   | Collation | Nullable | Default | Storage | Description |
+-------------+---------+-----------+----------+---------+---------+-------------+
| schemaname  | name    |           |          |         | plain   |             |
| tablename   | name    |           |          |         | plain   |             |
| tableowner  | name    |           |          |         | plain   |             |
| tablespace  | name    |           |          |         | plain   |             |
| hasindexes  | boolean |           |          |         | plain   |             |
| hasrules    | boolean |           |          |         | plain   |             |
| hastriggers | boolean |           |          |         | plain   |             |
| rowsecurity | boolean |           |          |         | plain   |             |
+-------------+---------+-----------+----------+---------+---------+-------------+
View definition:
 SELECT n.nspname AS schemaname,
    c.relname AS tablename,
    pg_get_userbyid(c.relowner) AS tableowner,
    t.spcname AS tablespace,
    c.relhasindex AS hasindexes,
    c.relhasrules AS hasrules,
    c.relhastriggers AS hastriggers,
    c.relrowsecurity AS rowsecurity
   FROM pg_class c
     LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
     LEFT JOIN pg_tablespace t ON tpostgres=# \d+ pg_tables;
                           View "pg_catalog.pg_tables"
+-------------+---------+-----------+----------+---------+---------+-------------+
|   Column    |  Type   | Collation | Nullable | Default | Storage | Description |
+-------------+---------+-----------+----------+---------+---------+-------------+
| schemaname  | name    |           |          |         | plain   |             |
| tablename   | name    |           |          |         | plain   |             |
| tableowner  | name    |           |          |         | plain   |             |
| tablespace  | name    |           |          |         | plain   |             |
| hasindexes  | boolean |           |          |         | plain   |             |
| hasrules    | boolean |           |          |         | plain   |             |
| hastriggers | boolean |           |          |         | plain   |             |
| rowsecurity | boolean |           |          |         | plain   |             |
+-------------+---------+-----------+----------+---------+---------+-------------+
View definition:
 SELECT n.nspname AS schemaname,
    c.relname AS tablename,
    pg_get_userbyid(c.relowner) AS tableowner,
    t.spcname AS tablespace,
    c.relhasindex AS hasindexes,
    c.relhasrules AS hasrules,
    c.relhastriggers AS hastriggers,
    c.relrowsecurity AS rowsecurity
   FROM pg_class c
     LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
     LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
  WHERE c.relkind = ANY (ARRAY['r'::"char", 'p'::"char"]);
 .oid = c.reltablespace
  WHERE c.relkind = ANY (ARRAY['r'::"char", 'p'::"char"]);

您可以检查这些表中的一个或多个是否未锁定 - 应该只有少数操作可以锁定这些表以防止读取。有关它的更多文档https://wiki.postgresql.org/wiki/Lock_Monitoring