|
| 1 | +## PostgreSQL 命令行 top 工具 - pg_activity |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2021-01-30 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , top , pg_activity |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | + |
| 16 | + |
| 17 | +Command line tool for PostgreSQL server activity monitoring. |
| 18 | + |
| 19 | +[](https://pypi.python.org/pypi/pg_activity) |
| 20 | +[](https://travis-ci.com/dalibo/pg_activity) |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +Installation |
| 25 | +------------ |
| 26 | + |
| 27 | +`pg_activity` requires Python 3.6 or later. It can be installed using pip |
| 28 | +(available, e.g., as `apt install python3-pip` on Debian-based distributions): |
| 29 | + |
| 30 | + $ python3 -m pip install pg_activity psycopg2-binary |
| 31 | + |
| 32 | +or directly from your Linux distribution, if available, e.g.: |
| 33 | + |
| 34 | + $ sudo apt install pg-activity |
| 35 | + |
| 36 | + |
| 37 | +Usage |
| 38 | +----- |
| 39 | + |
| 40 | +`pg_activity` works locally or remotely. In local execution context, to obtain |
| 41 | +sufficient rights to display system informations, the system user running |
| 42 | +`pg_activity` must be the same user running postgresql server (`postgres` by |
| 43 | +default), or have more rights like `root`. Otherwise, `pg_activity` can fallback |
| 44 | +to a degraded mode without displaying system informations. On the same way, |
| 45 | +PostgreSQL user used to connect to the database must be super-user. |
| 46 | +ex: |
| 47 | + |
| 48 | + sudo -u postgres pg_activity -U postgres |
| 49 | + |
| 50 | +Options |
| 51 | +------- |
| 52 | + |
| 53 | + pg_activity [options] |
| 54 | + |
| 55 | + Options: |
| 56 | + --version Show program's version number and exit |
| 57 | + -U USERNAME, --username=USERNAME |
| 58 | + Database user name (default: "postgres"). |
| 59 | + -p PORT, --port=PORT Database server port (default: "5432"). |
| 60 | + -h HOSTNAME, --host=HOSTNAME |
| 61 | + Database server host or socket directory (default: |
| 62 | + "localhost"). |
| 63 | + -d DBNAME, --dbname=DBNAME |
| 64 | + Database name to connect to (default: "postgres"). |
| 65 | + --blocksize=BLOCKSIZE Filesystem blocksize (default: 4096). |
| 66 | + --rds Enable support for AWS RDS. |
| 67 | + --output=FILEPATH Store running queries as CSV. |
| 68 | + --help Show this help message and exit. |
| 69 | + --no-db-size Skip total size of DB. |
| 70 | + --min-duration Don't display queries with smaller than specified |
| 71 | + duration (in seconds). |
| 72 | + --verbose-mode=VERBOSE_MODE |
| 73 | + Queries display mode. Values: 1-TRUNCATED, |
| 74 | + 2-FULL(default), 3-INDENTED |
| 75 | + --duration-mode=DURATION_MODE |
| 76 | + Duration mode. Values: 1-QUERY(default), |
| 77 | + 2-TRANSACTION, 3-BACKEND |
| 78 | + |
| 79 | + |
| 80 | + Display options, you can exclude some columns by using them : |
| 81 | + --no-database Disable DATABASE. |
| 82 | + --no-user Disable USER. |
| 83 | + --no-client Disable CLIENT. |
| 84 | + --no-cpu Disable CPU%. |
| 85 | + --no-mem Disable MEM%. |
| 86 | + --no-read Disable READ/s. |
| 87 | + --no-write Disable WRITE/s. |
| 88 | + --no-time Disable TIME+. |
| 89 | + --no-wait Disable W. |
| 90 | + --no-app-name Disable App. |
| 91 | + |
| 92 | + |
| 93 | +Notes |
| 94 | +----- |
| 95 | + |
| 96 | +Length of SQL query text that `pg_activity` reports relies on PostgreSQL |
| 97 | +parameter `track_activity_query_size`. Default value is `1024` (expressed in |
| 98 | +bytes). If your SQL query text look truncated, you should increase |
| 99 | +`track_activity_query_size`. |
| 100 | + |
| 101 | + |
| 102 | +Interactives commands |
| 103 | +--------------------- |
| 104 | + |
| 105 | +| Key | Action | |
| 106 | +|-----------|------------------------------------------------------------------| |
| 107 | +| `r` | Sort by READ/s, descending | |
| 108 | +| `w` | Sort by WRITE/s, descending | |
| 109 | +| `c` | Sort by CPU%, descending | |
| 110 | +| `m` | Sort by MEM%, descending | |
| 111 | +| `t` | Sort by TIME+, descending | |
| 112 | +| `T` | Change duration mode: query, transaction, backend | |
| 113 | +| `Space` | Pause on/off | |
| 114 | +| `v` | Change queries display mode: full, indented, truncated | |
| 115 | +| `UP/DOWN` | Scroll processes list | |
| 116 | +| `k/j` | Scroll processes list | |
| 117 | +| `q` | Quit | |
| 118 | +| `+` | Increase refresh time. Maximum value : 5s | |
| 119 | +| `-` | Decrease refresh time. Minimum Value : 0.5s | |
| 120 | +| `F1/1` | Running queries list | |
| 121 | +| `F2/2` | Waiting queries list | |
| 122 | +| `F3/3` | Blocking queries list | |
| 123 | +| `h` | Help page | |
| 124 | +| `R` | Refresh | |
| 125 | +| `D` | Refresh Database Size (including when --no-dbzise option applied)| |
| 126 | + |
| 127 | +Navigation mode |
| 128 | +--------------- |
| 129 | + |
| 130 | +| Key | Action | |
| 131 | +|------------|-----------------------------------------------| |
| 132 | +| `UP`/`k` | Move up the cursor | |
| 133 | +| `DOWN`/`j` | Move down the cursor | |
| 134 | +| `K` | Terminate the current backend/tagged backends | |
| 135 | +| `C` | Cancel the current backend/tagged backends | |
| 136 | +| `Space` | Tag or untag the process | |
| 137 | +| `q` | Quit | |
| 138 | +| `Other` | Back to activity | |
| 139 | + |
| 140 | +FAQ |
| 141 | +--- |
| 142 | + |
| 143 | +**I can't see my queries only TPS is shown** |
| 144 | + |
| 145 | +`pg_activity` scans the view `pg_stat_activity` with a user defined refresh |
| 146 | +time comprised between O.5 and 5 seconds. It can be modified in the interface |
| 147 | +with the `+` and `-` keys. Any query executed between two scans won't be |
| 148 | +displayed. |
| 149 | + |
| 150 | + |
| 151 | +What is more, `pg_activity` uses different queries to get : |
| 152 | + |
| 153 | +* settings from `pg_settings` |
| 154 | +* version info using `version()` |
| 155 | +* queries and number of connections from `pg_stat_activity` |
| 156 | +* locks from `pg_locks` |
| 157 | +* tps from `pg_database` using `pg_stat_get_db_xact_commit()` and |
| 158 | + `pg_stat_get_db_xact_rollback()` |
| 159 | +* and more ( eg : `pg_cancel_backend()` and `pg_terminate_backend()` ) |
| 160 | + |
| 161 | +Thoses queries cannot be seen in the query tab because all queries issued from |
| 162 | +the `pg_activity` backend are considered as noise and are not displayed . On |
| 163 | +the other hand, the transactions used to get the info for `pg_activity`'s |
| 164 | +reporting are still accounted for by postgres in `pg_stat_get_db_xact_commit()` |
| 165 | +and `pg_stat_get_db_xact_commit()`. Therefore `pg_activity` will display a non |
| 166 | +zero TPS even with no activity on the database, and/or no activity displayed on |
| 167 | +screen. |
| 168 | + |
| 169 | + |
| 170 | +#### [PostgreSQL 许愿链接](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216") |
| 171 | +您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。[开不开森](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216"). |
| 172 | + |
| 173 | + |
| 174 | +#### [9.9元购买3个月阿里云RDS PostgreSQL实例](https://www.aliyun.com/database/postgresqlactivity "57258f76c37864c6e6d23383d05714ea") |
| 175 | + |
| 176 | + |
| 177 | +#### [PostgreSQL 解决方案集合](https://yq.aliyun.com/topic/118 "40cff096e9ed7122c512b35d8561d9c8") |
| 178 | + |
| 179 | + |
| 180 | +#### [德哥 / digoal's github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
0 commit comments