PostgreSQL参数文件位置及生效方式与不生效的检查 | 8月更文挑战

613 阅读2分钟

PostgreSQL服务器参数文件的位置在$PGDATA下,通常在数据目录下。 该文件遵循如下规则: 每一行指定一个参数。这意味着不能出现如下情况:

listen_addresses = '*' log_destination = 'csvlog'

等号是可选的,空白是没有任何意义的,这意味着如下也是正确的:

log_rotation_age'2d'
log_rotation_size =     '0'

井号(#)指示该行的剩余部分是一个注释。

如果要在参数值里嵌入单引号, 或者写两个单引号或者在引号前放反斜线,推荐前者。

除postgresql.conf之外,PostgreSQL 数据目录还包含一个文件 postgresql.auto.conf,它具有和postgresql.conf相同的格式,但是不应该被手工编辑。这个文件保存了通过ALTER SYSTEM命令提供的设置。每当postgresql.conf被读取时这个文件会被自动读取,并且它的设置会以同样的方式生效。 postgresql.auto.conf中的设置会覆盖postgresql.conf中的设置。

所以当数据库中show出来的参数和postgresql.conf不一致时,可以排查下是否设置在了postgresql.auto.conf中。 如果也没有,那可能是在alter user和alter database的时候有set参数值。

当不明确参数在哪个参数文件生效的话,可以检查系统表pg_file_settings,通过该表可以直观体现出目前生效的参数及参数文件。

postgres=# select * from pg_file_settings ;
           sourcefile            | sourceline | seqno |            name            |      setting       | applied | error 
---------------------------------+------------+-------+----------------------------+--------------------+---------+-------
 /pg13/data/postgresql.conf      |         64 |     1 | max_connections            | 100                | t       | 
 /pg13/data/postgresql.conf      |        121 |     2 | shared_buffers             | 128MB              | t       | 
 /pg13/data/postgresql.conf      |        142 |     3 | dynamic_shared_memory_type | posix              | t       | 
 /pg13/data/postgresql.conf      |        228 |     4 | max_wal_size               | 1GB                | t       | 
 /pg13/data/postgresql.conf      |        229 |     5 | min_wal_size               | 80MB               | t       | 
 /pg13/data/postgresql.conf      |        425 |     6 | log_destination            | csvlog             | t       | 
 /pg13/data/postgresql.conf      |        563 |     7 | log_timezone               | PRC                | f       | 
 /pg13/data/postgresql.conf      |        678 |     8 | datestyle                  | iso, mdy           | t       | 
 /pg13/data/postgresql.conf      |        680 |     9 | timezone                   | PRC                | t       | 
 /pg13/data/postgresql.conf      |        694 |    10 | lc_messages                | en_US.UTF-8        | t       | 
 /pg13/data/postgresql.conf      |        696 |    11 | lc_monetary                | en_US.UTF-8        | t       | 
 /pg13/data/postgresql.conf      |        697 |    12 | lc_numeric                 | en_US.UTF-8        | t       | 
 /pg13/data/postgresql.conf      |        698 |    13 | lc_time                    | en_US.UTF-8        | t       | 
 /pg13/data/postgresql.conf      |        701 |    14 | default_text_search_config | pg_catalog.english | t       | 
 /pg13/data/postgresql.conf      |        705 |    15 | shared_preload_libraries   | test_decoding      | t       | 
 /pg13/data/postgresql.conf      |        708 |    16 | jit_provider               | llvmjit            | t       | 
 /pg13/data/postgresql.auto.conf |          3 |    17 | wal_level                  | logical            | t       | 
 /pg13/data/postgresql.auto.conf |          4 |    18 | logging_collector          | on                 | t       | 
 /pg13/data/postgresql.auto.conf |          5 |    19 | log_timezone               | Etc/GMT-8          | t       | 
 /pg13/data/postgresql.auto.conf |          6 |    20 | listen_addresses           | *                  | t       | 
 /pg13/data/postgresql.auto.conf |          7 |    21 | synchronous_standby_names  |                    | t       | 
 /pg13/data/postgresql.auto.conf |          8 |    22 | synchronous_commit         | remote_apply       | t       | 
 /pg13/data/postgresql.auto.conf |          9 |    23 | recovery_min_apply_delay   | 1d                 | t       | 
(23 rows)

或者直接查询pg_settings表,可以列出所有参数的设置值及生效来源。