Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1psHRM-0003Iq-VU for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Apr 2023 06:18:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1psHRL-0003eO-Dk for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Apr 2023 06:18:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1psHRL-0003e2-3b for pgsql-hackers@lists.postgresql.org; Fri, 28 Apr 2023 06:18:43 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1psHRH-005Wff-UW for pgsql-hackers@lists.postgresql.org; Fri, 28 Apr 2023 06:18:42 +0000 Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-6a5fbc19fe1so1771305a34.0 for ; Thu, 27 Apr 2023 23:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682662718; x=1685254718; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=QZiZt64ZRtp7185RWArYUxH3TqvyIYBx/4pa3nzT9Eg=; b=WZJy1rDzfxQsYTNLehoY+cH0mlT/iaVvWVlzgoLLK0l4r06ctac2wdc5uGOwZ+X767 EyzkWTAIEpWT3xrU7i/FWf5eW1pa6bllyM2Kj6f9ecgCqi3WCVIElkSh2aBb48idoMe/ WnRSkXaWUyYiqiTaXTj6C4DJ+HE3hCCJyqHBvCJ7wTRuI1p21JrdqHowu3RMqZYvQIn6 TjPqq0yOxE7atmTpJl+E0ybezAeIm3zL2C2xPnnwPKFP5F7Th8NXrL9i4+CXuxNQ1b5l T9lgXp4Yl7vHatCvEA19GR8ADVZApgxH2UN+h7fSjZgKvg1NezCQ0ob/FAL2nKpUW0Xn WgEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682662718; x=1685254718; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QZiZt64ZRtp7185RWArYUxH3TqvyIYBx/4pa3nzT9Eg=; b=iCvYPRXKdiHFal+HG6g4hhIQSbd9Wxq9H7E28j8UduneTr+uwFUvQCRspg8OaQ9X0U 8lEFvZLdkcmiJPSu2g91wpWXnAu52CzyHuqCUf3gf6URyP9UY7aXpddox782b/auOzAE wEZKQI8eRsycH8ZJ5EwLF4OjqEJnVJPRaxnKPjYhiPVdzp9UFMeaXFYP361ZSpWvZmiX oghLFDinulgp41OffgGTe6Fb18ygbByHVygoCzbPLGC0h/nN8ItHSjlEGhpgzIQB9dLn pb3X3DWhlOkza6iiz8nmKzg5voEuWTSCganSx/viaoewPCl5RLXatb4ClJoYPU6KqGgB oiOg== X-Gm-Message-State: AC+VfDwBvWgBWw3IoyXV7cOllXql3juoaqLnZZMi5CNhI26YRjMo+N5J +KMOW7XJiAsqYEVLTjr6BcPSPn+pbDsfB0OuiJM= X-Google-Smtp-Source: ACHHUZ7ZrMalivyHyH0qfpaLkLU1TcyzaiUuuxlmIP2QWmKZ+tku0H7cjUWY0OsmJvn8GcT+qKyF5FHhLgQ+sRgWuJs= X-Received: by 2002:a4a:bb92:0:b0:547:12df:5daa with SMTP id h18-20020a4abb92000000b0054712df5daamr1530290oop.1.1682662717747; Thu, 27 Apr 2023 23:18:37 -0700 (PDT) MIME-Version: 1.0 References: <2185d65f-5aae-3efa-c48f-fb42b173ef5c@gmail.com> In-Reply-To: From: Masahiko Sawada Date: Fri, 28 Apr 2023 15:18:01 +0900 Message-ID: Subject: Re: Perform streaming logical transactions by background workers and parallel apply To: Amit Kapila Cc: "Zhijie Hou (Fujitsu)" , Alexander Lakhin , PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Apr 28, 2023 at 11:51=E2=80=AFAM Amit Kapila wrote: > > On Wed, Apr 26, 2023 at 4:11=E2=80=AFPM Zhijie Hou (Fujitsu) > wrote: > > > > On Wednesday, April 26, 2023 5:00 PM Alexander Lakhin wrote: > > > > > > IIUC, that assert will fail in case of any error raised between > > > ApplyWorkerMain()->logicalrep_worker_attach()->before_shmem_exit() an= d > > > ApplyWorkerMain()->InitializeApplyWorker()->BackgroundWorkerInitializ= eC > > > onnectionByOid()->InitPostgres(). > > > > Thanks for reporting the issue. > > > > I think the problem is that it tried to release locks in > > logicalrep_worker_onexit() before the initialization of the process is = complete > > because this callback function was registered before the init phase. So= I think we > > can add a conditional statement before releasing locks. Please find an = attached > > patch. > > > > Alexander, does the proposed patch fix the problem you are facing? > Sawada-San, and others, do you see any better way to fix it than what > has been proposed? I'm concerned that the idea of relying on IsNormalProcessingMode() might not be robust since if we change the meaning of IsNormalProcessingMode() some day it would silently break again. So I prefer using something like InitializingApplyWorker, or another idea would be to do cleanup work (e.g., fileset deletion and lock release) in a separate callback that is registered after connecting to the database. While investigating this issue, I've reviewed the code around callbacks and worker termination etc and I found a problem. A parallel apply worker calls the before_shmem_exit callbacks in the following order: 1. ShutdownPostgres() 2. logicalrep_worker_onexit() 3. pa_shutdown() Since the worker is detached during logicalrep_worker_onexit(), MyLogicalReplication->leader_pid is an invalid when we call pa_shutdown(): static void pa_shutdown(int code, Datum arg) { Assert(MyLogicalRepWorker->leader_pid !=3D InvalidPid); SendProcSignal(MyLogicalRepWorker->leader_pid, PROCSIG_PARALLEL_APPLY_MESSAGE, InvalidBackendId); Also, if the parallel apply worker fails shm_toc_lookup() during the initialization, it raises an error (because of noError =3D false) but ends up a SEGV as MyLogicalRepWorker is still NULL. I think that we should not use MyLogicalRepWorker->leader_pid in pa_shutdown() but instead store the leader's pid to a static variable before registering pa_shutdown() callback. And probably we can remember the backend id of the leader apply worker to speed up SendProcSignal(). FWIW, we might need to be careful about the timing when we call logicalrep_worker_detach() in the worker's termination process. Since we rely on IsLogicalParallelApplyWorker() for the parallel apply worker to send ERROR messages to the leader apply worker, if an ERROR happens after logicalrep_worker_detach(), we will end up with the assertion failure. if (IsLogicalParallelApplyWorker()) SendProcSignal(pq_mq_parallel_leader_pid, PROCSIG_PARALLEL_APPLY_MESSAGE, pq_mq_parallel_leader_backend_id); else { Assert(IsParallelWorker()); It normally would be a should-no-happen case, though. Regards, --=20 Masahiko Sawada Amazon Web Services: https://aws.amazon.com