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 1wg2Em-005sG1-2b for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jul 2026 15:25:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wg2Ej-00AVdO-1Q for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jul 2026 15:24:57 +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 1wg2Ej-00AVdG-0V for pgsql-hackers@lists.postgresql.org; Sat, 04 Jul 2026 15:24:57 +0000 Received: from mail-yx1-xb129.google.com ([2607:f8b0:4864:20::b129]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wg2Eg-00000001i0F-0zPX for pgsql-hackers@postgresql.org; Sat, 04 Jul 2026 15:24:56 +0000 Received: by mail-yx1-xb129.google.com with SMTP id 956f58d0204a3-664b05d408bso1198098d50.1 for ; Sat, 04 Jul 2026 08:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783178692; x=1783783492; 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=jiKf7sDyI86CiZiHSsyg6k58DxAFitKJWbNU/MWDPS0=; b=HQc85Y4/47zNEcTizKrwVY+rD3n7rLThYYcD2kBYS6wpx37EWt81iraRr7Ezox1hfG 14HYdSOTNV5U36gltNrefC7Zyjz3jXl00RGoHXPntq8weJ3BUaMcs/ugKCtt9cgSKCnA Rh1lO4x31wmLFtn5KoPqXlfacJBDLqIdcq3fAf1kyaloa+59aQ4HiwKNPjydlckgItm7 k5KIZiaRIEQ7F0FTsdpYyR/Y/W+HRaB5qKAFCI7xzRAihjoLalvMB3g4a1oM8WJd7mBZ K98n4uDmSB4N7xXWVxW/PdJFWdGUpS/TBg1My/28VqmpNS2lWPdkGtOgj56R7ga0WiUs d21A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783178692; x=1783783492; 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=jiKf7sDyI86CiZiHSsyg6k58DxAFitKJWbNU/MWDPS0=; b=I/M7rzLCZTFd5HUVKoK0+u5gzd+xNq3JODWYIsWmf1J5JRdLiN58KHhS3k+rQntumK zpjjhZBqJ2YP9rtTjL7dvxeR6II+ve+HqVX+QpQGdPBLBLTk8Tn/dpLNoUAFCmr+v95S B/ecgdGdaFftcP34BKXvrIeZpI7XDJisOsyjua3wSDifqXBtu6QbRYR5r1gjIKSc3M2q 5e0EjK+taeIdRZ8RQMW3qqdUNvXkQMHxB7lUEYVzpl4p//R08uUv+pT/wI9S/t5NoK1j dVoK3CZrYqWUNKb2NvKcyftLE7DobOYUjmbStRyFiNWLyNnpr0wdywy9dZvWuTDpl8IH Y24A== X-Forwarded-Encrypted: i=1; AHgh+RpeSEEUp2E/2TezHLgJ1y06X+VuqSrU70wr5PJQjBxayP7ic3yfvXL0oRTjNZeaw4wovDT/aWP1QMaubk0l@postgresql.org X-Gm-Message-State: AOJu0YyuO5c6rYmMA6AjZGZoofCdVY3OKbcjxpzgw+B3dp7NX6hC+Udg bbMtA/hryOft/CRvsV/BgTMu/wFxP8/67lu9RMQCXHVLi9sKWWlu0u8Y X-Gm-Gg: AfdE7ck7mZhZURlMpLV/j2ErrY/IoTb8I80ADLWbTbB0tHLJrMPCOrdmlY2LY4gWRft VP3ldKNS9bWoNrysRQ4kgb/y8N0c9k+leTRu55eshkajcWuhgRRiHNoKc9KPLqMSGwMDNasub58 6ZQk8aJMY7cQl6qH/IscAVqIuFk1QMyuiaDXIbEzRUl3fJ73aQBJBo+KfQPVqa5c/ExbyNrISn4 ux7R5KM2L2Q/U1AYO66y0wn4Ky6V3dfTIIOXkckTa3/vUuctTJefQjna2nThxVOKjGZ4+OtsbD/ ZaZvx9RlIVfUc7JRQWVGzeQ6LDJ74MKjuDLdncgJJaCFTLISUAebAqZZsZqDq2Mnl/Slvxjbr12 r84qhUJoIVPa0I/ijtJhtsCTwYEPZZZT0GGRB8PZT4OkCr6vY6wnNRetE/KEJrLRjg/9qKQQ2ac vUWsf3W8T1aPE3Nim/ygdZWOspYjlesYJVxO3zZ/xSN4WZevZ+edT10FDgvQRI0da130xPVmU4p El/4F/T6uOTdQunB5KcGOhrSQ== X-Received: by 2002:a05:690e:12c1:b0:664:ae6a:eee with SMTP id 956f58d0204a3-6665303e028mr3206164d50.76.1783178692124; Sat, 04 Jul 2026 08:24:52 -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 956f58d0204a3-66640541621sm2271792d50.9.2026.07.04.08.24.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 04 Jul 2026 08:24:49 -0700 (PDT) Message-ID: <0d9c391f-5014-48f5-a680-45d7cfabe8a8@gmail.com> Date: Sat, 4 Jul 2026 10:24:24 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Can we get rid of TerminateThread() in pg_dump? To: Jelte Fennema-Nio , Thomas Munro Cc: Heikki Linnakangas , pgsql-hackers , Tom Lane , Kyotaro Horiguchi References: <56821e0d-bfd5-4619-b13f-12e28c78289b@iki.fi> 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/4/2026 6:15 AM, Jelte Fennema-Nio wrote: > On Sat, 4 Jul 2026 at 02:51, Thomas Munro wrote: >> We don't actually care about the threads >> themselves, and it doesn't seem that great if we have to introduce an >> IPC ping-pong of some kind with each thread.  > > Agreed. But I do agree with Heikki that swapping out stderr seems pretty > hacky. At the very least because now the main thread cannot write to > stderr either anymore (which is why you removed the "terminated by user" > write I guess). One correction from running it on Windows -- the "terminated by user" line still prints. Thomas moved that write ahead of the dup2(), so it goes out before stderr is redirected. > > How about instead we do something like the attached? > > To be clear, I do think we should stop using TerminateThread because I > wanna replace PQcancel there with PQcancelBlocking[1] PQcancelBlocking > does a whole TLS handshake, which is almost certainly taking some locks. > > Note that the way to achieve that I moved the Ctrl+C handler to a > dedicated thread on Unix too, so it starts behaving the same as Windows > in that respect. I think combined with you changing pg_dump to use > worker *threads* on Unix too, we would then get pretty much identical > behaviour across OSes for pg_dump. > > P.S. I now realize that anything involving the (already existing) > CancelRequested flag is actually not actually safe/correct on Windows, > because it's not using read nor written using atomic operations while > the consoleHandler runs on its own thread. Would be good to fix that too > I guess, but it seems that for now it has worked in practice at least. > > [1]: https://www.postgresql.org/message-id/flat/ > DJPAH0WPJV3K.1PYZ8P0QXZVMX%40jeltef.nl#5642e337a4e4d04b21c66e089484f80d > I ran v2 (both patches) on Windows: parallel restore, -j4, into a dump with PK + secondary indexes so there is a CREATE INDEX phase to interrupt. I then ^C in the middle of the index build. The workers canceled cleanly (no orphaned backends, process exits, no hang. No "query failed" spam. Your guard swallows them. I don't know whether this is worth restructuring the code for, but at -v you will get (as an example) pg_restore: while PROCESSING TOC: pg_restore: from TOC entry NNNN; ... INDEX t1_v_idx postgres Those context lines sit above is_cancel_in_progress() check in warn_or_exit_horribly... so the preamble does not get suppressed. The die_on_query_failure is unguarded, it might spam if a ^C happens to land for metadata/SET queries. I did not test that. I may roll your patches onto my branch to test with the multi-threaded queue. -- Bryan Green EDB: https://www.enterprisedb.com