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 1wg1fb-005roa-0Y for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jul 2026 14:48:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wg1fY-00ASuM-2z for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jul 2026 14:48:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wg1fY-00ASuD-1u for pgsql-hackers@lists.postgresql.org; Sat, 04 Jul 2026 14:48:36 +0000 Received: from mail-yw1-x112b.google.com ([2607:f8b0:4864:20::112b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wg1fW-00000001hUH-1A5e for pgsql-hackers@postgresql.org; Sat, 04 Jul 2026 14:48:36 +0000 Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-80814edb536so17207247b3.2 for ; Sat, 04 Jul 2026 07:48:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783176512; x=1783781312; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Aw1esLV7F2oQqU9DrZuEu0YhZ9Ye2PiuvtHJf9ZyAto=; b=oiCJ5mZNg6fcVdlHEyxokkvJ0fs0hO5FnRPHIXpotypcJ/NH5vg6r8jajjCKm/XE/y 4+B2jaPdSv95CObZwkL1BSY4lWEYdRhBP7tbcnF/GMbwXEeGo5FJ3rERt9KLlqYGdQLs 22Ex5gRkYvIaTQ3wmXlerYna+fAVizJsOpxrJ5YCEckXxubRXaEChZ7r4NJ+O973YjAL CGlYa1rkrl7/VV5gkEKRKeQotPHHV5/xbERG3XambBGSAHM4BSP8CaXVtiCn5mWMxs7Y tdYypzOp//nzN1MTdejp1qQBqVZFKRooQyzwN8njpidfX6rVHrbNiHUPze+p33ap3dPk FpJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783176512; x=1783781312; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Aw1esLV7F2oQqU9DrZuEu0YhZ9Ye2PiuvtHJf9ZyAto=; b=spEcAdgtwnn9uJhONtYNF4+8bUCffehbfqsgyEfEZH8QLowYiIEiop+i+WXRU0899h rtAOtpACqkHY9veQTEBttmlMrNdgQMtCizeAaLsouT5Do6rjcALm9Cxbc+urs/6cWH3N 6+coGQ6P1OTByUm7ZEiKTCttyM6zJGmN3UGENCWwdI0+qPL9tWWJ4yoUdvu6bcG0WOMS 6u08V7iI8hYYZcOUoA1LUARxCkcNHnvOrEGu4gopw/jddOjZV8EA9uFtNp2rkgFRXSfk gHt5/kceG7hjQPF8fUT/N4auPb+AIFTp1pvlFIWZ7IPTj1wt8ILH+B/szcTg+iIleyCG MM0A== X-Gm-Message-State: AOJu0YyODh+fEnTNzHAEsJ3Ty+HfsuCKOQmKC5E1b/tmYStljZmibiWB RRgduNR9GSrFOx4MV59tFjNDmhr0kwIvFiPzsvFC1pIBzwgFrQ2PO2Hi X-Gm-Gg: AfdE7ckyuzDgMn189j9XBnFiw6Ib4aSAwvh5Z6FXdHWy7zzLGyDAQrQQZC/wjFzCd00 o8RNaTkDf6IS9PHEMVBLSLjV0jVvGj7oIBbew/T+Yna62O0XJBNgZTmQ0o17lZfWRddyCX45IPE v/Xt35mI+xTyD8sGJvCmjy5EuB9YgWnvv3r1b4Prj7/MBoCxkC4BJxvxgpwSGaya5qBKxW0oiQd X4wVz+SYZgxneim1Ew6w3IsXAdvy38hNfnfwuvMKvGnAxVCjnxUljmkoatkyz5DVB9IyLadVLMU Ft/59sUvQ9Fm5d4Luh9NV37+EYOiUf7ep8VxazlSiPhnVGExUzUryQn+KxnYaTK+Q9cQaz2KvA7 7fAdb0Qu1rItAyioVybbyuf/uYSjQrFC4XjeGiCBWmgeS8qAZ9PeEclptMGXFfjSuBksh8F2oLz 4XBCzzBlLxvbgZ9JSaKGvoe2/6L5Gxuy83EkG7lHL8SD8dyXdBKt6skpbNvAcEePnQBJeyMsl2b 1vC5UC/fapWYjSCdtiEZs465w== X-Received: by 2002:a05:690c:c4cd:b0:80c:ff94:e87c with SMTP id 00721157ae682-817395eebdcmr37707917b3.44.1783176512236; Sat, 04 Jul 2026 07:48:32 -0700 (PDT) Received: from ?IPV6:2600:1700:8952:80:8926:e550:95d5:8e40? ([2600:1700:8952:80:8926:e550:95d5:8e40]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8144bf15b88sm35178857b3.49.2026.07.04.07.48.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 04 Jul 2026 07:48:31 -0700 (PDT) Message-ID: <328c51a6-93cb-49e3-87f3-1cb539d03dd2@gmail.com> Date: Sat, 4 Jul 2026 09:48:05 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Can we get rid of TerminateThread() in pg_dump? To: Thomas Munro Cc: pgsql-hackers , Tom Lane , Kyotaro Horiguchi References: Content-Language: en-US From: Bryan Green In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/3/2026 7:38 PM, Thomas Munro wrote: > On Sat, Jul 4, 2026 at 1:56 AM Bryan Green wrote: >> It works. It compiles clean, and on a ^C during a parallel dump it >> suppresses the worker cancel messages the same way the TerminateThread >> call did-- I checked against unmodified master with the TerminateThread >> call still in place, and the output is identical-- "terminated by user" >> and nothing else. > > Thank you for confirming! So now I'm wondering, do we want to let > sleeping dogs lie, or do we want to back-patch this? If we're happy > to leave the back-branches, which have never received a user > complaint, then I'll simply adopt this approach in my > pg_dump-with-threads-on-Unix-too patch set (which I'll post shortly), > ie the same dup2() code will be used on all systems. But if we think > this problem is worth fixing in back-branches, we could back-patch > this stand-alone patch first. Thoughts? > >> Moving write_stderr off fileno(stderr) to STDERR_FILENO is worth having >> on its own: that shim's comment notes _fileno returns -1 when the stream >> is closed, which is a state the shutdown path can be in. > > Cool. Yeah, I failed to mention why I'd done that in my previous > email, and that was it. > >> Opening NUL in the handler worked here; I didn't need to open it in advance. > > I was worrying that open() might fail with EMFILE. I don't think it's > really going to happen though, given the bounded numbers of files that > pg_dump/pg_restore should be working with, so maybe it's OK as it is. > >> Since you mention moving parallel.c to threads on non-Windows, I've been >> replacing the socketpair/select() worker protocol on Windows with an >> in-process queue (a mutex and two condition variables)[1], heading the >> same direction to unify across platforms. There a worker signals its own >> death through the condition variable rather than the leader seeing pipe >> EOF, so ^C hits an extra pg_fatal("a worker process died unexpectedly") >> in the leader-- and your redirect swallows that one too. I put your > > Excellent, and 100% agreed. No need for pipes/sockets and clunky > protocols if the fork() path is gone. I had already made a start on > exactly same idea, because I hadn't seen your thread (sorry). I have > a few comments/thoughts (short version: can we make a reusable generic > thread pool component for that?). I will look more closely at your > patches and comment on your thread. > Part of it -- the worker lifecycle and the work-item queue are generic and worth factoring out. The scheduling isn't, because on the restore side it's dependency and lock-conflict aware (pop_next_work_item and the ready heap). It's not workers pulling the next item off a queue -- what's runnable depends on the whole in-flight set. A pool would own the threads and the channel, with that policy on top. Dump is basically a plain pool already, so it'd drop straight in. Good to know we landed on the same shape independently, and thanks for offering to look at the patches. I'll take the pool/scheduler split up in more detail over there. >> patch on top of that series and hit ^C on a 2.3 GB dump repeatedly at -j >> 2, 4, 8, and 100: clean exit every time, "terminated by user", nothing >> else. pg_stat_activity right afterward showed no live COPYs, up through >> -j 100-- so the PQcancel()s land and the backends actually stop, which >> TerminateThread never did. So your change composes with that work. > > Interesting. -- Bryan Green EDB: https://www.enterprisedb.com