POSTGRESQL 关于2023-08-14 数据库自动启动文章中使用KILL 来进行配置RELOAD的问题解释

数据库技术
206
0
0
2024-01-14
标签   PostgreSQL

首先道歉,昨天关于POSTGRESQL 的文章中的部分内容个人觉得有误导的部分或者说没有解释清楚的地方。虽然相关的部分是官方的提供的相关的文件,特通过此文更新相关的内容。

在上一篇文章中,有这样一个部分

因为这个地方有不同的意见

1 实际上这块的部分代表了reload 的部分,而通常我们撰写这块的方式是

ExecReload=/usr/local/postgres/bin/pg_ctl reload -D ${PGDATA}

但是官方安装完毕后,这个部门给的命令是 kill -HUP $MAINPID ,熟悉这PG 这块的小伙伴,都明白,kill 在对于PG 来说是一个要了命的命令,所以本文最后会给出更新的部分,咱们不按官方的来。

但是问题又来了,为甚一个RELOAD的命令本身,在官方的命令里面,是通过 kill 来完成的,而不是我们常用的命令。

这里为避免歧义,特此解释:

"kill -hup" 是一个用于发送 SIGHUP 信号给进程的命令。SIGHUP 信号是一种发送给进程的 POSIX 信号,代表终端挂起信号(hangup signal)。

当一个进程收到 SIGHUP 信号时,通常会导致该进程重新加载配置文件、重新初始化或重新启动。这通常用于实现热重载配置或重新加载程序的功能。

所以说 kill -hup 并不是等同于 kill -9 XXX (两个没有可比性), 他是通过kill命令来给进程发送信号的。

 * Re-read config files, and tell children to do same.
 */
static void
process_pm_reload_request(void)
{
 pending_pm_reload_request = false;

 ereport(DEBUG2,
   (errmsg_internal("postmaster received reload request signal")));

 if (Shutdown <= SmartShutdown)
 {
  ereport(LOG,
    (errmsg("received SIGHUP, reloading configuration files")));
  ProcessConfigFile(PGC_SIGHUP);
  SignalChildren(SIGHUP);
  if (StartupPID != 0)
   signal_child(StartupPID, SIGHUP);
  if (BgWriterPID != 0)
   signal_child(BgWriterPID, SIGHUP);
  if (CheckpointerPID != 0)
   signal_child(CheckpointerPID, SIGHUP);
  if (WalWriterPID != 0)
   signal_child(WalWriterPID, SIGHUP);
  if (WalReceiverPID != 0)
   signal_child(WalReceiverPID, SIGHUP);
  if (AutoVacPID != 0)
   signal_child(AutoVacPID, SIGHUP);
  if (PgArchPID != 0)
   signal_child(PgArchPID, SIGHUP);
  if (SysLoggerPID != 0)
   signal_child(SysLoggerPID, SIGHUP);

  /* Reload authentication config files too */
  if (!load_hba())
   ereport(LOG,
   /* translator: %s is a configuration file */
     (errmsg("%s was not reloaded", HbaFileName)));

  if (!load_ident())
   ereport(LOG,
     (errmsg("%s was not reloaded", IdentFileName)));

代码中,在接受到kill -hup 主进程号后,开始针对PG的配置文件,和 PG的 hba 文件和 ident 等部分信息的加载。

下面重写了相关的文件,昨天昨天文件的替换

# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  It is recommended to use systemd
# "dropin" feature;  i.e. create file with suffix .conf under
# /etc/systemd/system/postgresql-14.service.d directory overriding the
# unit's defaults. You can also use "systemctl edit postgresql-14"
# Look at systemd.unit(5) manual page for more info.

# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-14-setup.
[Unit]
Description=PostgreSQL 14 database server
Documentation=https://www.postgresql.org/docs/14/static/
After=syslog.target
After=network-online.target

[Service]
Type=notify

User=postgres
Group=postgres


# Location of database directory
Environment=PGDATA=/pgdata/data/   #请根据实际情况修改你的PG数据库目录地址到这个位置
Environment=PGPORT=5432            #请根据实际情况修改此位置为你的PG的端口号


# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0

#请根据实际的情况,来编辑下方的

ExecStart=/usr/local/postgres/bin/postmaster -D ${PGDATA}
ExecStop=/usr/local/postgres/bin/pg_ctl stop -D ${PGDATA} -m fast
ExecReload=/usr/local/postgres/bin/pg_ctl reload -D ${PGDATA}
ExecStatus=/usr/local/postgres/bin/pg_ctl status -D ${PGDATA}


#ExecReload=/bin/kill -HUP $MAINPID
#KillMode=mixed
#KillSignal=SIGINT
 
# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0

# 0 is the same as infinity, but "infinity" needs systemd 229
TimeoutStartSec=0

TimeoutStopSec=1h

[Install]
WantedBy=multi-user.target