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 1wCtUF-002PCe-2g for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Apr 2026 06:12:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wCtUE-00EuwR-08 for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Apr 2026 06:12:30 +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 1wCtUD-00EuwI-24 for pgsql-hackers@lists.postgresql.org; Wed, 15 Apr 2026 06:12:30 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCtUB-000000015tC-3gHx for pgsql-hackers@lists.postgresql.org; Wed, 15 Apr 2026 06:12:29 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-82735a41920so2168841b3a.2 for ; Tue, 14 Apr 2026 23:12:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776233547; x=1776838347; 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=F+8acOqAvm+wyfbnVjsejihBRFbmmi8W2VlmwOSWqzY=; b=MoS8EJdbi5wuQSSSMmjuCav2ZvsiSYLvdJ2AFV7GJUpiCuK3Za5qbqRO/wX+PsmCX2 i6mc7aO1Oe++G8BvAaHj+KddEEfS+zBaR9lT4H0+/25IRVjjMvKnZWd6xctRp0uNGci7 k3SewT+AjstMYT+VG1D3i6Jsv5xsNEXr+I6n0etMSwMRTHj8k+qNOGdciYkvQ64phNWM YqXrKiBIIiXIo7q8WK4JumT9E+FgUrLQ91WkFdwaupp3PW2VDubRYHDd5WNLn1HFpSr9 972SbisLetbi8AQkh08w9Ocqq6GB9xFVoOLHhuh/0UTF7Y8BECZv0A33LjkTFyJA2L66 5vxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776233547; x=1776838347; 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=F+8acOqAvm+wyfbnVjsejihBRFbmmi8W2VlmwOSWqzY=; b=cebzDoreh7zxp15ZANKPji85VIL2v5cHyidJbellwv7ewzPuiVP35vWIzVSrU1V/2x S737J4gyf6MLt3RhqIxFAmimvm45IqdTAcwsujorM8Xhq/uAE3yBGHFlj/5ZZ3ZakbR8 whvowD2qIwrpXap0KvtF4ORhcYNnNK32BnIbAyqIrWnu1eZ/x8Tm3ybNymM4jWh+T/ma z0EbRIuRauHgdITrzaLwKyT+8ajak9TPLjrV4C2GFdF3G8Danc28tKph6rtrZ6Bvxgv2 e2hXDpYqeqoh6dy38JPslwb/SiuPBQjxjtmdGPKQYBmSauM1BqTO1aH+3c8QL0ycN63+ jHyg== X-Forwarded-Encrypted: i=1; AFNElJ9R9VNJwkJLV2mMu7/K1LWwbWMZiOTWKJHjttEKGSEO9+h7zLBk5QpyZp7cU84ch573ZUHbJJflscI/gfrQ@lists.postgresql.org X-Gm-Message-State: AOJu0Yxw0JPX7mw1SrWqCiyNOmsF9w6lFEyFZ/6hYgIpd4TMftzdeUsi baFpFWbCtqrS7ghSal+iHjgJo7p/TIjhXLgHg/+4TWJC44/3AQZQR+Wo X-Gm-Gg: AeBDieth78vqXQLsGti1YmHzgpJdoJ4XlckH16mPU3aeD/ddF9lXo6Ez6G9dVNozrtc pv7WJfdIYMN6WkOGZMM/bMcweo5WEBTsQiFi82JgY1W99yheVcAP5K5w+v1ND5DIm2q2/UEZMa5 CSxXCQa/CO+AEqdVDl22yDqg9It2KcuAeqWXMZZmXrcuKEbwWEVdrrhsQ6voltp7GWWeNwEnj1a fh2dW77QWd7hsOjg2SHMLRnxduVM2JlphOflRz5ZqoIGy7yyysGP5tPXroARPDyNP7Ts4SK2YPJ jrUmeRnsi+Y9oeigWO8Cdr07ejMfgHZrhwSayBnshdN0JEYCrS2JCDyowqs5vlEpR150RcwuGrD MNkGlUPMfElileOrFL/KW4zlC8jYKeqw/TNGzS9kqg3CFble3DgT1L9Qa9iWz6Tuf8AnxP0lyt+ fYeTMGbT6t6JdhIw7AwC7ZD3nJU2aQ4c0= X-Received: by 2002:a05:6a00:3a10:b0:82a:6d3d:9ba1 with SMTP id d2e1a72fcca58-82f0c227e5fmr21658090b3a.24.1776233547249; Tue, 14 Apr 2026 23:12:27 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82f670791fasm1015237b3a.20.2026.04.14.23.12.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Apr 2026 23:12:26 -0700 (PDT) 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: <833f045b-286a-45dc-aad1-28854cbacc99@dunslane.net> Date: Wed, 15 Apr 2026 14:11:46 +0800 Cc: Jakub Wartak , Andres Freund , Jim Jones , PostgreSQL Hackers Content-Transfer-Encoding: quoted-printable Message-Id: <6F78A2BD-4C0A-4620-AD98-18DF9D9FB4CD@gmail.com> References: <1ba07c75-cc4e-410c-9af1-076feeeb9594@dunslane.net> <9179378a-42db-4315-9ff6-b849ad5eefaf@dunslane.net> <6cj4oxgdpavltoqdbydw2pgxfdbcejc26x3mitasrnswipxne5@7gkaeoibos4l> <71930E58-82A8-4DDC-BA8C-5E394331E463@gmail.com> <43ab327c-3ec7-4424-88de-22c7906e5529@uni-muenster.de> <8B3A69A4-8412-472D-BB39-47617E430420@gmail.com> <833f045b-286a-45dc-aad1-28854cbacc99@dunslane.net> To: Andrew Dunstan 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 Apr 15, 2026, at 04:38, Andrew Dunstan wrote: >=20 >=20 > On 2026-04-14 Tu 6:40 AM, Jakub Wartak wrote: >> Hi Andres, Andrew, Chao >>=20 >> thanks for putting so much effort into enhancing the the previous = implementation >> of this code. Earlier I was not aware of potential problems involved. >>=20 >> On Fri, Apr 10, 2026 at 9:41=E2=80=AFAM Chao Li = wrote: >>>=20 >>>=20 >>>> On Apr 9, 2026, at 18:59, Andrew Dunstan = wrote: >>>>=20 >>>>=20 >>>> On 2026-04-08 We 1:01 PM, Andres Freund wrote: >>>>> Hi, >>>>>=20 >>>>> Attached is a very rough first draft for how I think this needs to = look like. >>>>>=20 >>>>> Basically, SIGNAL_INFO always will pass both the signal number and = extended >>>>> information along to the signal handler. The extended information = is a >>>>> postgres specific struct. If the platform can't provide the = extended >>>>> information, the values are instead set to some default value = indicating that >>>>> the information is not known. >>>>>=20 >>>>> With that die() (and also StatementCancelHandler, ...) can just = set whatever >>>>> globals it wants, without pqsignal.c needing to know about it. >>>>>=20 >>>>> It also allows us to extend the amount of information in the = future. E.g. I'd >>>>> like to log the reason for a segfault (could e.g. be an OOM kill = or an umapped >>>>> region) to stderr. >>>>>=20 >>>>> The annoying thing about it is needing to change nearly all the = existing >>>>> references to SIG_IGN/SIG_DFL, to avoid warnings due to mismatched = types. >>>> I agree that's annoying. The only way around it I found was via = some casting to/from void* that I suspect you would find a cure worse = than the disease. >>>> I reworked your patch slightly. This version fixes the = translatability issue you raised earlier, makes the TAP test from the = original commit more robust, and tries to resolve your XXX issue by = moving the assignment of ProcDieSenderPid/Uid inside the "if = (!proc_exit_inprogress)" block. >>>>=20 >>> I reviewed this version. Besides the compile warning and uid 0 = issues, I got a few more comments, so I try to put them all together as = below. >>>=20 >> TL;DR; win/mingw is really unhappy with the state of rework patch = right now. >> I've tried to enhance and fix it, and now it's green for default CI = run and >> also for mingw too. Attached 0002-fixup-win32 patch does not = incorporate Chao's >> all findings so far. >>=20 >> [..] >>> 2 - uid 0 problem >>> ``` >>> +typedef struct pg_signal_info >>> +{ >>> + pid_t pid; /* pid of sending = process or 0 if unknown */ >>> + uid_t uid; /* uid of sending = process or 0 if unknown */ >>> +} pg_signal_info; >>> ``` >>>=20 >>> I think we can mention that =E2=80=9Cuid=E2=80=9D is only meaningful = when pid is set. >> [..] >>> 5 >>> ``` >>> + pg_info.uid =3D 0; >>> ``` >>>=20 >>> If you take comment 2, then when unavailable, we can just don=E2=80=99= t assign anything to pg_info.uid. Or "(uid_t)-1", maybe. >>>=20 >>=20 >> I. I've started from this and I vaguley remembered that I had = terrible >> experience >> when trying to chose the proper types for those field types, but I = couldn't >> remind myself why, so I've gave a try of the current rework patch and = got this >> on Windows Server 2022, vs2019, cirrus-ci said: >>=20 >> [08:40:22.848] c:\cirrus\src\include\c.h(1451): error C2061: syntax >> error: identifier 'pid_t' >> [08:40:22.848] c:\cirrus\src\include\c.h(1452): error C2061: syntax >> error: identifier 'uid' >> [08:40:22.848] c:\cirrus\src\include\c.h(1452): error C2059: syntax = error: ';' >> [08:40:22.848] c:\cirrus\src\include\c.h(1453): error C2059: syntax = error: '}' >>=20 >> for c.h: >> 1449 typedef struct pg_signal_info >> 1450 { >> 1451 pid_t pid; /* pid of >> sending process or 0 if unknown */ >> 1452 uid_t uid; /* uid of >> sending process or 0 if unknown */ >> 1453 } pg_signal_info; >>=20 >> so maybe we should just move that typedef with SIGNAL_ARGS to after >> "include of port.h" (line ~1471) in that c.h, because only then we'll = have >> access to: >> src/include/port/win32_port.h:typedef int pid_t; >> src/include/port/win32_port.h:typedef int uid_t; >> but the problem is that port.h itself requires SIGNAL_ARGS to be = defined >> and that seems to be like chicken and egg problem. I thought that = just >> using native "ints" could be the way to go, but the problem is that = now >> that uid_t can be bigger than pid_t as Linux kernel headers show = this: >>=20 >> x86_64-linux-gnu/bits/types.h:#define __S32_TYPE int >> x86_64-linux-gnu/bits/types.h:#define __U32_TYPE = unsigned int >> x86_64-linux-gnu/bits/types.h:__STD_TYPE __UID_T_TYPE __uid_t; /* >> Type of user identifications. */ >> x86_64-linux-gnu/bits/types.h:__STD_TYPE __PID_T_TYPE __pid_t; /* >> Type of process identifications. */ >> x86_64-linux-gnu/bits/typesizes.h:#define __UID_T_TYPE = __U32_TYPE >> x86_64-linux-gnu/bits/typesizes.h:#define __PID_T_TYPE = __S32_TYPE >>=20 >> so maybe do that typedef struct pg_signal_info with 2x uint32_t and = that's >> good enough? (it covers the ranges necessary and makes it platform = compatible) >>=20 >> II. While we are tthis so with above uid_t of up to being u32, = possibly >> `volatile int ProcDieSenderUid/Pid` should be also bigger like = uint32_t? >> My fixup-patch does not incude it, because I don't know really. >>=20 >> III. As Chao said I've used: >>=20 >> +#define PG_SIG_DFL (pqsigfunc) (pg_funcptr_t) SIG_DFL >> +#define PG_SIG_IGN (pqsigfunc) (pg_funcptr_t) SIG_IGN >>=20 >> IV. Then just got lots of warnings for use_wrapper/wrapp_handler and = so on >>=20 >> [09:27:33.602] ../src/port/pqsignal.c(206): error C2065: >> 'use_wrapper': undeclared identifier >> [09:27:33.602] ../src/port/pqsignal.c(206): warning C4113: = 'pqsigfunc' >> differs in parameter lists from 'void (__cdecl *)(int)' >>=20 >> that's for: >> 204 #else >> 205 /* Forward to Windows native signal system. */ >> 206 if (signal(signo, use_wrapper ? wrapper_handler : >> func) =3D=3D SIG_ERR) >> 207 Assert(false); /* probably >> indicates coding error */ >>=20 >> In the end, I've ended up using wrapper_handler for the windows path = there >> as signal() requires function with single param. >>=20 >> IV. Got some further issues, and VC complained that siginfo_t is used = for >> USE_SIGACTION - isn't it impossible on win32? Anyway, that makes some = sense, >> USE_SIGINFO is not defined and we use 'siginfo_t', so something like = fixes it: >>=20 >> @@ -90,7 +90,7 @@ static volatile pqsigfunc = pqsignal_handlers[PG_NSIG]; >> * >> * This wrapper also handles restoring the value of errno. >> */ >> -#ifdef USE_SIGACTION >> +#if defined(USE_SIGACTION) && defined(USE_SIGINFO) >> static void >> wrapper_handler(int postgres_signal_arg, siginfo_t *info, void = *context) >> #else >>=20 >> V. Later I've stumbled on series of other problems related to >> src/backend/port/win32/signal.c (it also uses SIG_DFL but not = PG_SIG_DFL and >> stil somewhat references pgsigfunc, so those changes seemed to impact = it). >>=20 >> Also there was: >> [10:37:02.824] ../src/backend/port/win32/signal.c(154): error C2198: >> 'sig': too few arguments for call >>=20 >> so I've fixed with adding nodata struct there and: >> @@ -151,7 +154,7 @@ pgwin32_dispatch_queued_signals(void) >> block_mask |=3D = sigmask(i); >>=20 >> sigprocmask(SIG_BLOCK, >> &block_mask, &save_mask); >> - sig(i); >> + sig(i, &nodata); >> sigprocmask(SIG_SETMASK, >> &save_mask, NULL); >>=20 >> VI. Possibly we could rename USE_SIGACTION define because at least to = me it >> is confusing to me to reason and communicate about in terms of win32 = context >> on win32 do we do have it or not? (it can be both ways): >> * win32 C API doesnt have it >> * PG does have sigaction win32 wrapper with override macro >> #define sigaction.. pqsigaction.. >> * however src/include/libpq/pqsignal.h says "sa_sigaction not yet = implemented" >> for it (so with USE_SIGACTION are we talking about sa_sigaction = field memeber >> that it's not used or about sigaction function?) >>=20 >> VII. FWIW, I've also removed superflous "else" >> #ifdef USE_SIGINFO >> if (!(is_ign || is_dfl)) >> { >> act.sa_sigaction =3D wrapper_handler; >> act.sa_flags |=3D SA_SIGINFO; >> } >> - else >> #else >>=20 >>=20 >=20 >=20 > I'm not 100% sure that else shouldn't be there. Maybe have another = look? >=20 > Attached is a consolidation that includes your other fixes, plus: >=20 > . uid comment -- "only meaningful when pid is not 0" > . const pg_signal_info *pg_siginfo in SIGNAL_ARGS > . 2 spaces after period in syncrep.c errdetail >=20 >=20 > cheers >=20 >=20 > andrew >=20 >=20 >=20 > -- > Andrew Dunstan > EDB: https://www.enterprisedb.com > V2 LGTM. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/