Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vvRkS-002KAG-0Q for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Feb 2026 03:09:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vvRkQ-009SbW-2y for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Feb 2026 03:09:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vvRkQ-009SbN-1a for pgsql-hackers@lists.postgresql.org; Thu, 26 Feb 2026 03:09:06 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vvRkN-00000001Dof-1PYA for pgsql-hackers@lists.postgresql.org; Thu, 26 Feb 2026 03:09:05 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2a7d98c1879so2196105ad.3 for ; Wed, 25 Feb 2026 19:09:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772075344; x=1772680144; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=R4OnHYggt6XCGmAZtesO/lhH6QLG0fDYpZDYzptEh1g=; b=Pemi6qo0szQyQhqBYk8qRpWmSDQRlN8MJVFRgrG7T0W59dbB01tyjMssbn0kn9sZzz letusfmsm7UD60s2tzx7aF9wO7kOzpj/LHM0oyQeER3bJF9fS4YiqbRPz2ZDbocjBl6M /hkCngqvvXTm/p8qQtomRX7ExJZWMLbF9w0uwfFGXdGwyUVZ9K+Tc8Ox4kfGJg9stngb Oef178nJFqcxo9z3jjGQu/2PfmKilS+elXyKN/6fCUadI6OdRWw4/727qYjUShHFrZVJ kqd2kNyAq9Jcl9C4lg9l9TbFashgZHHa5MwSoHzuOoEzSQMV53gqve1sH4tNALrCO/1E HJbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772075344; x=1772680144; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=R4OnHYggt6XCGmAZtesO/lhH6QLG0fDYpZDYzptEh1g=; b=l5mUeQLTah8JZvqf/Zc8YecQceom72ZjqRmVoNqeJVxFBK/WzV1feclbqxKbTppjvA ede/MNV0+twcLpLQv7ykMtyc7ktQlVqJRInLqhdamKF5YzQv+7M3f9HRDQ2z8E3aCxfW o03dSIXaaAvATSK94woujRwvVld5sp2DO+jXzsHgvkdIbfgCYOxyCnA3mNlD8mG0O2yl UQo27bnNY89D7Mifrw0rm9dHKV7CccRquH5Y0BfKEzTRUMSw+wxPudAPlMRaXj45aGog pouO8vM3M3qkVqfh55uTfHnB4ExTixIt62yttu0f+ifP9oH8m6zNKb5K6hV5x0qlls9C GCpQ== X-Forwarded-Encrypted: i=1; AJvYcCWuvUNgWPLZS8DDofGSwK7wXU0ghvQt9by/DdTu/Zssnd6cc6cuXA/Iq4OZGoV2J5w7Wif4QNADbWZQgWbf@lists.postgresql.org X-Gm-Message-State: AOJu0Yy5m4nejMcuJ2sP7ZKYREJ+DJvmGGdPmJbQHN5o8zChcIgN2ZZY FBEiYk9KzvR8P+WCnf+srZeY/ZRbaFcprfE3qXzshRDJVVAONOc7yaHI X-Gm-Gg: ATEYQzwksqB7T6tptIv21UWR1/K04X+QB+ZhSRRxUyh7TL2HVmbF9JIstvdcMNpaLWL Dv/BD94egXWnBwXNWgQowK71/oyNamS9PPGJZOjg1o0LYA7SYNmi7LtP4S7udH3Q8TFSQNpNscZ UzyM+m2vBxma04ZJs9N5nQtbde+Cs6keshqxwc10G7JxjYby78tP1ws/TvzsSFL2rVUxlmD+oD5 LT1aM97SNZBGrv3ERO/0Zr9cRWi3Ghv/xjBs9XnjyjiHR54ayB7BhqaxLLBT10QBL3O2x3ApmkK hXX8Ewqlaf7i5mHrPiyxZKKbndcGR5rzVPw/1Al2QcyEUngq7u7lILBkHAPZKEBOgZcYeA9Z2zj C9fUTUG5bf7ccWJjQ+ZdmbiZCgkqAhwEn+xVOcC3WBqsdvSj51ZAw0tjF8CfdTxgdNCYgNZiMeH OkCe9S5Tnz9TwrAtaJfxznmpNX7ca2RQ== X-Received: by 2002:a17:903:1212:b0:2a9:4bd9:bb8a with SMTP id d9443c01a7336-2ad744ab23fmr183843875ad.25.1772075343720; Wed, 25 Feb 2026 19:09:03 -0800 (PST) Received: from smtpclient.apple ([203.10.98.27]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2adfb5b53f4sm7127115ad.18.2026.02.25.19.09.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Feb 2026 19:09:03 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: Add errdetail() with PID and UID about source of termination signal From: Chao Li In-Reply-To: Date: Thu, 26 Feb 2026 11:08:28 +0800 Cc: Andrew Dunstan , Jim Jones , PostgreSQL Hackers Content-Transfer-Encoding: quoted-printable Message-Id: References: <94268299-f61e-4588-89b7-15a74a705cca@uni-muenster.de> To: Jakub Wartak X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Feb 25, 2026, at 18:45, Jakub Wartak = wrote: >=20 > On Wed, Feb 25, 2026 at 10:15=E2=80=AFAM Chao Li = wrote: >=20 > Hi Chao, thanks for review. >=20 >> A few comments for v3: >>=20 >> 1 - syncrep.c > [..] >> + proc_die_sender_pid = =3D=3D 0 ? 0 : >> + = errdetail("Signal sent by PID %lld, UID %lld.", >> + (long = long)proc_die_sender_pid, (long long)proc_die_sender_uid) >> + )); >> ``` >>=20 >> Here errdetail is used twice. I guess the second conditional one = should be errhint. >>=20 >> 2 - syncrep.c > [..] >> Same as comment 1. >=20 > You are right, apparently I copy/pasted code from = src/backend/tcop/postgres.c > way too fast... fixed. >=20 >> 3 >> ``` >> +volatile uint32 proc_die_sender_pid =3D 0; >> +volatile uint32 proc_die_sender_uid =3D 0; >> ``` >>=20 >> These two globals are only written in the signal handler, I think = they should be sig_atomic_t to ensure atomic writes. >=20 > Well the problem that sig_atomic_t is int and we need at least uint32 = and > I couldn't find better way. I think that 4 bytes writes will be mostly > always atomic (for 64-bits it would depend on > PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY) >=20 > Yet I moved those little below, so it's more aligned to the other = uses. >=20 >> 4 >> ``` >> - errmsg("terminating = walreceiver process due to administrator command"))); >> + errmsg("terminating = walreceiver process due to administrator command"), >> + proc_die_sender_pid =3D=3D 0 = ? 0 : >> + errdetail("Signal = sent by PID %lld, UID %lld.", >> + (long = long)proc_die_sender_pid, (long long)proc_die_sender_uid) >> + )); >> ``` >>=20 >> Why do we need to format pid and uid in =E2=80=9Clong long=E2=80=9D = format? I searched over the source tree, the current postmaster.c just = formats pid as int (%d): >> ``` >> /* in parent, successful fork */ >> ereport(DEBUG2, >> (errmsg_internal("forked new %s, pid=3D%d = socket=3D%d", >> = GetBackendTypeDesc(bn->bkend_type), >> (int) pid, = (int) client_sock->sock))); >> ``` >=20 > Yes, I think I was kind of lost when thinking about it (v1 had = sig_atomic_t, > later had pid_t, I read somewhere about 64-bit pids, and so on) vs > platform-agnostic hell of putting that into printf). Possible I was > overthinking it > and I have reverted it to just using %d with that uint32. BTW I've = also found: > elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) pid, signal); >=20 > v4 attached. Thanks again for the review. >=20 > -J. > I just reviewed v4 again and got a few more comments: 1. This patch only set the global proc_die_sender_pid/uid to 0 at = startup, then assign values to them upon receiving SIGTERM, and never = reset them, which assumes a process must die upon SIGTERM. Is the = assumption true? I guess not. If a process receives SIGTERM and not die = immediately, then die for other reason, then it may report a misleading = PID and UID. So, I think we may need to reset proc_die_sender_pid/uid = somewhere. For example, in ProcessInterrupts(), copy them to local = variables and reset them to 0, then use the local variables for = ereport(). 2.=20 ``` @@ -319,7 +323,11 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit) ereport(WARNING, (errmsg("canceling wait for = synchronous replication due to user request"), - errdetail("The transaction has = already committed locally, but might not have been replicated to the = standby."))); + errdetail("The transaction has = already committed locally, but might not have been replicated to the = standby."), + proc_die_sender_pid =3D=3D= 0 ? 0 : + errhint("Signal = sent by PID %d, UID %d.", + = proc_die_sender_pid, proc_die_sender_uid) + )); ``` syncrpe.c uses errhint to print PID and UID, and postgres.c uses = errdetail. We should keep consistency, maybe all use errhint. 3. ``` @@ -319,7 +323,11 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit) QueryCancelPending =3D false; ereport(WARNING, (errmsg("canceling wait for = synchronous replication due to user request"), - errdetail("The transaction has = already committed locally, but might not have been replicated to the = standby."))); + errdetail("The transaction has = already committed locally, but might not have been replicated to the = standby."), + proc_die_sender_pid =3D=3D= 0 ? 0 : + errhint("Signal = sent by PID %d, UID %d.", + = proc_die_sender_pid, proc_die_sender_uid) + )); SyncRepCancelWait(); break; } ``` I don=E2=80=99t think the query cancel case relates to SIGTERM, so we = don=E2=80=99t need to log PID and UID here. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/