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 1x3xwJ-006nKv-1x for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 15:40:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x3xwI-008Lwz-1D for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Sep 2026 15:40:50 +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 1x3xu0-008IYn-00 for pgsql-hackers@lists.postgresql.org; Tue, 08 Sep 2026 15:38:28 +0000 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x3xty-00000004cPx-0OpB for pgsql-hackers@lists.postgresql.org; Tue, 08 Sep 2026 15:38:27 +0000 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-482e257a23aso3304547f8f.0 for ; Tue, 08 Sep 2026 08:38:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1788881904; x=1789486704; darn=lists.postgresql.org; h=message-id:date:content-type:mime-version:subject:to:from:from:to :cc:subject:date:message-id:reply-to:content-type; bh=O0dT9efExhTVEw/4EQCodoqDrI+vAflj9RFBmHNGZ8k=; b=Jefr3yaDPQZncJofXM/ZPO0YldP2d8aBUIrNOW23+JCLEunV0IlUhkGExXcB/I712g 4oi86tseZEm5K73G2+8ATRNe2DJWZsv0QOb3rjFh6CSW8NI+yyBQRMEwfekcuxCWd7Pf v37oWYPmjLFJFq8Nk+pX5r1OjKb4Ej6S3amDqUKKwfPMOolejXFm3i97feb+yNEmIu7T 6q2dSj43r9kFay2CfoZ448TNEAcuJONIRB7gyul/w5GV+cm3yBwHggenyXtT+9487MNf dTXh60nvTYr2ApXpsOnH+3vTFQIUo5VvCQG8QQhJ6pv0m/8/UEpVwhJYVtJRyjSUItP5 UCjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788881904; x=1789486704; h=message-id:date:content-type:mime-version:subject:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=O0dT9efExhTVEw/4EQCodoqDrI+vAflj9RFBmHNGZ8k=; b=DEs7rCg5IPjwQNTd6Mpf9QnvVpOETLzZe2HEW3Y6eODE3mBHJaqd6H9RvK2pr4itwg zN0lmX17QcnemvaVqJV66ghU2kvUge9XfXneZPf14swmAwSl0sdlhAqtNQtdWEhKaSIF Kt/XKPIU2XEq6ySKkFNpru3xpCxXjhKKaBnU046BMAhPsBd48j75cl++GdaGIMZyDT2J js41QoF/CaV/A9OJdJQOjJn8qm0EnAO3Qd5VoQG7I5HBbJD39n7ewTQkrVSAx9EfvFZm 7MjmuA0FSrzbtDFi9x072fk0zPYVjlbNTBxI2FOicGF9SvXz3jd2/k0oX5jtELoHcXGz fbOw== X-Gm-Message-State: AFuF++lkKKAyx4H9loHQSDgGlkGf8/ag3JG34cm4ctcJ0hrreQaFpZcP yVvVdnHtWAMPyrSQ30IBE4eAX7u/2qRhovTya0K0EbRPqcB2PaZ+maXnqgQSpwH4eFbC9fH11Zs mzTQ6Sjk= X-Gm-Gg: AYBFou3+ZwEYmT3sImlAJsb+dfoWTqGab1ZXrn5m0TAJBmvbIpElYGOvu3zIDrQVUgW dOzoMExRS7RuvlTOmCLwnXvPcu6utXPWG/68mP3YIxXHS9R4LnavPqJ7QSIG/jYxdB7XJSvf9nr /psrTq8yB5BtXxYcODxgVlTxLVQBx0b/kyKLajDfX6rvSXgbSsLcVp+naTDyhz2rfFqJxyjI1SO zgf0RyaK7DzI04cZGnQeTKiv0PDYVLKQYNV9rqrA/glu2opAoAmmEjbEaHUOE/FN52/Y2Zb1DH5 LNH9x8AO1fiV78TwK+lHhL2UIsSfg2TB8FN09Qn4IC2Wvtv0Qh+z3ZkYNmx3q9Q9ztePTFiXqCC H6sps0x+sPoSnQJNs2tI3Jrr23r0GRMP3WE/ko55A+SgouJqagpU8RciK69Z7TiImaAT6X82VDm nGITam+vrip0FYYKyTC5IJ8ZxFAO5SF6cpzvHcUBgg7yGZZMuF10Uu3QooqvAgi02jOI9iOUang A== X-Received: by 2002:adf:edc1:0:b0:485:85f3:7506 with SMTP id ffacd0b85a97d-485872b4247mr24397144f8f.23.1788881904158; Tue, 08 Sep 2026 08:38:24 -0700 (PDT) Received: from localhost (109-81-170-190.rct.o2.cz. [109.81.170.190]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-485885b7bfasm37568893f8f.29.2026.09.08.08.38.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Sep 2026 08:38:23 -0700 (PDT) From: Antonin Houska To: pgsql-hackers@lists.postgresql.org Subject: Race conditions during parallel worker (unclean) exit X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.3 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Tue, 08 Sep 2026 17:38:22 +0200 Message-ID: <64338.1788881902@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain In [1] I noted that ERROR like "lost connection to ... worker" (currently issued in ProcessParallelMessages()) might also be appropriate for REPACK (CONCURRENTLY). While I'm still not sure about that, I suspect that something is wrong about handling unclean exits in parallel.c. According to the header comment, ParallelWorkerShutdown() should handle cases like direct call of proc_exit(). By adding one like this diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 17fcd246b0c..e8700ce8594 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -1385,6 +1385,7 @@ ParallelWorkerMain(Datum main_arg) * Hooray! Primary initialization is complete. Now, we need to set up our * backend-local state to match the original backend. */ + proc_exit(1); /* * Join locking group. We must do this before anything that could try to and by running the following begin; create table a(i int); set parallel_setup_cost to 0; set parallel_tuple_cost to 0; set min_parallel_table_scan_size to 0; set min_parallel_index_scan_size to 0; set max_parallel_workers_per_gather to 1; set parallel_leader_participation to off; set debug_parallel_query to 'regress'; table a; rollback; I can reproduce the expected behavior: ERROR: lost connection to parallel worker However, an additional sleep() call in ParallelWorkerShutdown() diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 17fcd246b0c..1a05f16e0bc 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -1618,7 +1618,7 @@ ParallelWorkerShutdown(int code, Datum arg) SendProcSignal(ParallelLeaderPid, PROCSIG_PARALLEL_MESSAGE, ParallelLeaderProcNumber); - + sleep(1); dsm_detach((dsm_segment *) DatumGetPointer(arg)); } appears to cause race conditions. What I see is that the leader process runs ProcessParallelMessages() while the worker is still attached to the error queue, so the leader gets SHM_MQ_WOULD_BLOCK when trying to read from the queue. Thus the ERROR is not raised. Moreover, the leader, after having seen the worker detached from the tuple queue (TupleQueueReaderNext) a bit later, starts executor cleanup and, as the worker does not send signals anymore, it gets stuck in WaitForParallelWorkersToExit(). Attached is what might be a fix, but I'm not sure if the Terminate message is appropriate even if the worker in fact didn't finish with success. [1] https://www.postgresql.org/message-id/32123.1788806780%40localhost -- Antonin Houska Web: https://www.cybertec-postgresql.com --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=fix_parallel_worker_unclean_exit.diff diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 17fcd246b0c..b4802623b80 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -1597,10 +1598,9 @@ ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end) } /* - * Make sure the leader tries to read from our error queue one more time. - * This guards against the case where we exit uncleanly without sending an - * ErrorResponse to the leader, for example because some code calls proc_exit - * directly. + * Make sure the leader knows we're done. This guards against the case where + * we exit uncleanly without sending an ErrorResponse to the leader, for + * example because some code calls proc_exit directly. * * Also explicitly detach from dsm segment so that subsystems using * on_dsm_detach() have a chance to send stats before the stats subsystem is @@ -1615,6 +1615,8 @@ ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end) static void ParallelWorkerShutdown(int code, Datum arg) { + pq_putmessage(PqMsg_Terminate, NULL, 0); + SendProcSignal(ParallelLeaderPid, PROCSIG_PARALLEL_MESSAGE, ParallelLeaderProcNumber); --=-=-=--