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 1oi32p-0004KC-KO for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Oct 2022 00:22:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oi32o-0002gQ-GW for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Oct 2022 00:22:50 +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 1oi32o-0002gH-4k for pgsql-hackers@lists.postgresql.org; Tue, 11 Oct 2022 00:22:50 +0000 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oi32k-00039Z-P8 for pgsql-hackers@lists.postgresql.org; Tue, 11 Oct 2022 00:22:49 +0000 Received: by mail-oi1-x232.google.com with SMTP id g130so14284440oia.13 for ; Mon, 10 Oct 2022 17:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=olm7Fnqn18j3e0o69kP6sNwpNx5uGHVu80cnxS0fT+w=; b=bBy/Cv06YZcHYuPe74Rpot+53tb0PDUWccdaMRadOOEWqMymHd5Z40GBT43wcyXIMv yczIcFMPegsrj9lS38rpjkhickbyL9R2/xovYmBB7BGu2WlCbIO3+eV88dhwg61XK3li 7+LUhk6AXcHeYYfkIlXCVw0a7VI4eprwOVf3qZUHwtCs6CGvPPupVMprIZOTrPTZ6YGe BjNJ+gojZLpkUjV2MH3Y+97nOEaREw908FtYbQbxrPDZpi5l9DWIOmGGegt7jTeDfyNq cdDQ4Bxrj2t4Xt1N7uiDeZXNPyENqOPTzrfkYEMk7h63/QGnnWnBbyvMFSJjNctVeX+O oxPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=olm7Fnqn18j3e0o69kP6sNwpNx5uGHVu80cnxS0fT+w=; b=YW54IejyPNrFM6GTLjGe0w943jjLnYQqQcFfxj9tue5z/HZUNPZf6wnbSbm8+EFZBz 5hlHf9XfY1GHGz0V7h+5vTAl6YW9TXPD8QXIeGR9iYYlOdY0cdvTrfUTxEcdHUeOVcVX akj/ztsNY4Uq7/8KLNwgnbY4dVoI3zmVfjKzcwcs0lG6sZy7N6OTj52Ze7Cz5IhwZ9YD 5JYYL0Gi+qt+15kRmSLm/vtz/A1jzeAW41T8BpEK6TqJJACwT7O2LqjaX8dVi5vlYQkU M3QPhjxyqCDTtUqicgNQ/E32JRHcRqescDQFuVDQQy4r+/wrzO7lZD5dFFH2/D9FzJ9w //bw== X-Gm-Message-State: ACrzQf3Tq2mJvcx0aLOFPObUnBZuC1W/amR3U96p4yRvgaiW2Qb0sQP5 kBRm5p79+9YR5eNX8kha23k9sWLJnj+ehEowLe4= X-Google-Smtp-Source: AMsMyM4EbF9sOXqipENzEBZaXYSj9pQsLptylZ5mNhCFtJFyhPr40sMmTC5hhGyL6oEKZKXyAdlfwbDH7/0oE1euWgE= X-Received: by 2002:a05:6808:2211:b0:350:b551:3b15 with SMTP id bd17-20020a056808221100b00350b5513b15mr14861968oib.64.1665447763991; Mon, 10 Oct 2022 17:22:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Masahiko Sawada Date: Tue, 11 Oct 2022 09:22:08 +0900 Message-ID: Subject: Re: Perform streaming logical transactions by background workers and parallel apply To: Amit Kapila Cc: "houzj.fnst@fujitsu.com" , "wangw.fnst@fujitsu.com" , Peter Smith , Dilip Kumar , "shiy.fnst@fujitsu.com" , PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Oct 7, 2022 at 2:00 PM Amit Kapila wrote: > > On Fri, Oct 7, 2022 at 8:47 AM Masahiko Sawada wrote: > > > > On Thu, Oct 6, 2022 at 9:04 PM houzj.fnst@fujitsu.com > > wrote: > > > > > > I think the root reason for this kind of deadlock problems is the table > > > structure difference between publisher and subscriber(similar to the unique > > > difference reported earlier[1]). So, I think we'd better disallow this case. For > > > example to avoid the reported problem, we could only support parallel apply if > > > pubviaroot is false on publisher and replicated tables' types(relkind) are the > > > same between publisher and subscriber. > > > > > > Although it might restrict some use cases, but I think it only restrict the > > > cases when the partitioned table's structure is different between publisher and > > > subscriber. User can still use parallel apply for cases when the table > > > structure is the same between publisher and subscriber which seems acceptable > > > to me. And we can also document that the feature is expected to be used for the > > > case when tables' structure are the same. Thoughts ? > > > > I'm concerned that it could be a big restriction for users. Having > > different partitioned table's structures on the publisher and the > > subscriber is quite common use cases. > > > > From the feature perspective, the root cause seems to be the fact that > > the apply worker does both receiving and applying changes. Since it > > cannot receive the subsequent messages while waiting for a lock on a > > table, the parallel apply worker also cannot move forward. If we have > > a dedicated receiver process, it can off-load the messages to the > > worker while another process waiting for a lock. So I think that > > separating receiver and apply worker could be a building block for > > parallel-apply. > > > > I think the disadvantage that comes to mind is the overhead of passing > messages between receiver and applier processes even for non-parallel > cases. Now, I don't think it is advisable to have separate handling > for non-parallel cases. The other thing is that we need to someway > deal with feedback messages which helps to move synchronous replicas > and update subscriber's progress which in turn helps to keep the > restart point updated. These messages also act as heartbeat messages > between walsender and walapply process. > > To deal with this, one idea is that we can have two connections to > walsender process, one with walreceiver and the other with walapply > process which according to me could lead to a big increase in resource > consumption and it will bring another set of complexities in the > system. Now, in this, I think we have two possibilities, (a) The first > one is that we pass all messages to the leader apply worker and then > it decides whether to execute serially or pass it to the parallel > apply worker. However, that can again deadlock in the truncate > scenario we discussed because the main apply worker won't be able to > receive new messages once it is blocked at the truncate command. (b) > The second one is walreceiver process itself takes care of passing > streaming transactions to parallel apply workers but if we do that > then walreceiver needs to wait at the transaction end to maintain > commit order which means it can also lead to deadlock in case the > truncate happens in a streaming xact. I imagined (b) but I had missed the point of preserving the commit order. Separating the receiver and apply worker cannot resolve this problem. > > The other alternative is that we allow walreceiver process to wait for > apply process to finish transaction and send the feedback but that > seems to be again an overhead if we have to do it even for small > transactions, especially it can delay sync replication cases. Even, if > we don't consider overhead, it can still lead to a deadlock because > walreceiver won't be able to move in the scenario we are discussing. > > About your point that having different partition structures for > publisher and subscriber, I don't know how common it will be once we > have DDL replication. Also, the default value of > publish_via_partition_root is false which doesn't seem to indicate > that this is a quite common case. So how can we consider these concurrent issues that could happen only when streaming = 'parallel'? Can we restrict some use cases to avoid the problem or can we have a safeguard against these conflicts? We could find a new problematic scenario in the future and if it happens, logical replication gets stuck, it cannot be resolved only by apply workers themselves. Regards, -- Masahiko Sawada PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com