Postgresql 中文操作指南

28.1. Standard Unix Tools #

在大多数 Unix 平台上,PostgreSQL 会修改其命令标题,如 ps 所述,以便可以很容易地标识各个服务器进程。示例显示如下:

On most Unix platforms, PostgreSQL modifies its command title as reported by ps, so that individual server processes can readily be identified. A sample display is

$ ps auxww | grep ^postgres
postgres  15551  0.0  0.1  57536  7132 pts/0    S    18:02   0:00 postgres -i
postgres  15554  0.0  0.0  57536  1184 ?        Ss   18:02   0:00 postgres: background writer
postgres  15555  0.0  0.0  57536   916 ?        Ss   18:02   0:00 postgres: checkpointer
postgres  15556  0.0  0.0  57536   916 ?        Ss   18:02   0:00 postgres: walwriter
postgres  15557  0.0  0.0  58504  2244 ?        Ss   18:02   0:00 postgres: autovacuum launcher
postgres  15582  0.0  0.0  58772  3080 ?        Ss   18:04   0:00 postgres: joe runbug 127.0.0.1 idle
postgres  15606  0.0  0.0  58772  3052 ?        Ss   18:07   0:00 postgres: tgl regression [local] SELECT waiting
postgres  15610  0.0  0.0  58772  3056 ?        Ss   18:07   0:00 postgres: tgl regression [local] idle in transaction

(对 ps 的适当调用因不同的平台而异,所示内容的详细信息也是如此。此示例来自最近的 Linux 系统。)此处列出的第一个进程是主服务器进程。为其显示的命令参数与启动时使用的命令参数相同。接下来的四个进程是由主进程自动启动的后台工作进程。(如果您已将系统设置为不运行自动清理,则“自动清理启动器”进程将不存在。)其余每个进程是处理一个客户端连接的服务器进程。每个此类进程按以下形式设置其命令行显示:

(The appropriate invocation of ps varies across different platforms, as do the details of what is shown. This example is from a recent Linux system.) The first process listed here is the primary server process. The command arguments shown for it are the same ones used when it was launched. The next four processes are background worker processes automatically launched by the primary process. (The “autovacuum launcher” process will not be present if you have set the system not to run autovacuum.) Each of the remaining processes is a server process handling one client connection. Each such process sets its command line display in the form

postgres: user database host activity

用户、数据库和(客户端)主机项目在客户端连接的生命周期内保持不变,但活动指示符会发生变化。活动可以是 idle (即,等待客户端命令)、 idle in transaction (在 BEGIN 块中等待客户端)或命令类型名称,例如 SELECT 。此外,如果服务器进程当前正在等待另一会话持有的锁,则附加 waiting 。在上面的示例中,我们可以推断进程 15606 正在等待进程 15610 完成其事务并从而释放某个锁。(进程 15610 必须是阻止者,因为没有其他活动会话。在更复杂的情况下,有必要查看 pg_locks 系统视图以确定谁阻止了谁。)

The user, database, and (client) host items remain the same for the life of the client connection, but the activity indicator changes. The activity can be idle (i.e., waiting for a client command), idle in transaction (waiting for client inside a BEGIN block), or a command type name such as SELECT. Also, waiting is appended if the server process is presently waiting on a lock held by another session. In the above example we can infer that process 15606 is waiting for process 15610 to complete its transaction and thereby release some lock. (Process 15610 must be the blocker, because there is no other active session. In more complicated cases it would be necessary to look into the pg_locks system view to determine who is blocking whom.)

如果 cluster_name 已配置,群集名称也会显示在 ps 输出中:

If cluster_name has been configured the cluster name will also be shown in ps output:

$ psql -c 'SHOW cluster_name'
 cluster_name
--------------
 server1
(1 row)

$ ps aux|grep server1
postgres   27093  0.0  0.0  30096  2752 ?        Ss   11:34   0:00 postgres: server1: background writer
...

如果你已关闭 update_process_title,则活动指示器不会被更新;只有在启动新进程时才会设置一次进程标题。在某些平台上,这可以节省可测量的每条命令开销;在其他平台上,这无关紧要。

If you have turned off update_process_title then the activity indicator is not updated; the process title is set only once when a new process is launched. On some platforms this saves a measurable amount of per-command overhead; on others it’s insignificant.

Tip

Solaris 需要特别处理。您必须使用 /usr/ucb/ps,而不是 /bin/ps。您还必须使用两个 w 标志,而不仅仅是一个。此外,您对 postgres 命令的原始调用必须具有比每个服务器进程提供的更短的 ps 状态显示。如果您未能同时完成所有三项任务,则每个服务器进程的 ps 输出将是原始 postgres 命令行。

Solaris requires special handling. You must use /usr/ucb/ps, rather than /bin/ps. You also must use two w flags, not just one. In addition, your original invocation of the postgres command must have a shorter ps status display than that provided by each server process. If you fail to do all three things, the ps output for each server process will be the original postgres command line.