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.94.2) (envelope-from ) id 1sMYCr-002xv4-DJ for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jun 2024 19:21:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sMYCp-006Gqf-OY for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jun 2024 19:21:24 +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.94.2) (envelope-from ) id 1sMYCp-006GqH-AG for pgsql-hackers@lists.postgresql.org; Wed, 26 Jun 2024 19:21:23 +0000 Received: from mail-ot1-x32f.google.com ([2607:f8b0:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sMYCm-003GqM-EX for pgsql-hackers@lists.postgresql.org; Wed, 26 Jun 2024 19:21:22 +0000 Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6f8d0a1e500so5519156a34.3 for ; Wed, 26 Jun 2024 12:21:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1719429679; x=1720034479; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=jH0LZXpBxDw8dX2pnH+pYe9iAgZzk/T74QzDEyrNW6U=; b=NzCcDzR5w85UJHpBMucj+2QxYJb2n6xb30rmDqcY6feJUa7QHgd1l/BHtT04uuJGiD uJ6gVEoRY4whCNrXdmo9xxzEcxo5A2BW5Wg7Syh+5i35D/6GrxNqizSViSZtHdajcbVe txFJoOFFr/yi/taFSmfBRjMr9mzWndhnbeU8U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719429679; x=1720034479; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jH0LZXpBxDw8dX2pnH+pYe9iAgZzk/T74QzDEyrNW6U=; b=S0bFX300kKs6fYVVdN0RFaeKJ9SpCVaS0FuFdsgFl+qMwRtNjY8hl+dR4jcx+tKdcK 8bD19AE3V+0IfnXV8tNOVhdl2ypfU+saIBrPN1HeJIuKoNi4B0wTJesDg/XqIbucljMq d+lA/8bRHceyUpkf5wHkiaiNItEMGapKwAqBHToEnbcr/4sNAi1/LsVWYAKb3TXEKJ95 tSj7dY3BJKAelddLavoK95+1Dafq5Le4zfSd2zKaEw2KLpo6Yv7Monly4rmbpBJcZ4oM ey15RnIYd9+5lE6euj/yxWFGk6bUOoZDnYxq2v0ihtOor+IZDyHlCzVmvDGMvhIzwXKq D0+w== X-Forwarded-Encrypted: i=1; AJvYcCXtf7JrCVLgYbQ2I5UwbdgohXaVOXJohv/8IOPJSt/9QFAMI678+j/hzAuLJ8O0UlEz+ddUl9BhtnV3YXmAnVbugrwdMAzlSsOsUVNJojxVzQ/Z X-Gm-Message-State: AOJu0YwShlRGZO2BW58Md0spZ+XLElnbOqR540VjloYw8eXAWN2nCz3j uUKab9baa/XsUvB5hy5WvHlp3HxE+LVYWqX5zjKMfR2wugS2VAD7KogAQYY4Bw== X-Google-Smtp-Source: AGHT+IEhbB1pRBDNvflfVoPv5xvqU8JwLez74fIpDmj5MwxjlFiUuM+iTlgF6FrCjmaHG+UJ8VS3Ig== X-Received: by 2002:a9d:6f92:0:b0:6fc:3a9b:395d with SMTP id 46e09a7af769-700b12a1054mr11413328a34.32.1719429679473; Wed, 26 Jun 2024 12:21:19 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-700cfcf3d7esm316824a34.27.2024.06.26.12.21.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Jun 2024 12:21:18 -0700 (PDT) Date: Wed, 26 Jun 2024 12:21:16 -0700 From: Noah Misch To: Euler Taveira Cc: Amit Kapila , Peter Eisentraut , Shlok Kyal , "kuroda.hayato@fujitsu.com" , Tomas Vondra , "pgsql-hackers@lists.postgresql.org" , Michael Paquier , Andres Freund , Ashutosh Bapat , =?iso-8859-1?Q?Fabr=EDzio?= de Royes Mello , vignesh C Subject: Re: speed up a logical replica setup Message-ID: <20240626192116.a8.nmisch@google.com> References: <7a970912-0b77-4942-84f7-2c9ca0bc05a5@eisentraut.org> <44de4e04-db22-40ed-8ba9-46387dc8bf83@app.fastmail.com> <3fa9ef0f-b277-4c13-850a-8ccc04de1406@eisentraut.org> <20240623062157.97.nmisch@google.com> <20240624220845.ba.nmisch@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Jun 25, 2024 at 09:50:59PM -0300, Euler Taveira wrote: > On Tue, Jun 25, 2024, at 3:24 AM, Amit Kapila wrote: > > On Tue, Jun 25, 2024 at 3:38 AM Noah Misch wrote: > > > On Mon, Jun 24, 2024 at 05:20:21PM +0530, Amit Kapila wrote: > > > > On Sun, Jun 23, 2024 at 11:52 AM Noah Misch wrote: > > > > > > +static void > > > > > > +create_publication(PGconn *conn, struct LogicalRepInfo *dbinfo) > > > > > > +{ > > > > > > > > > > > + appendPQExpBuffer(str, "CREATE PUBLICATION %s FOR ALL TABLES", > > > > > > + ipubname_esc); > > > > > > > > > > This tool's documentation says it "guarantees that no transaction will be > > > > > lost." I tried to determine whether achieving that will require something > > > > > like the fix from > > > > > https://postgr.es/m/flat/de52b282-1166-1180-45a2-8d8917ca74c6@enterprisedb.com. > > > > > (Not exactly the fix from that thread, since that thread has not discussed the > > > > > FOR ALL TABLES version of its race condition.) I don't know. On the one > > > > > hand, pg_createsubscriber benefits from creating a logical slot after creating > > > > > the publication. That snapbuild.c process will wait for running XIDs. On the > > > > > other hand, an INSERT/UPDATE/DELETE acquires its RowExclusiveLock and builds > > > > > its relcache entry before assigning an XID, so perhaps the snapbuild.c process > > > > > > Correction: it doesn't matter how the original INSERT/UPDATE/DELETE builds its > > > relcache entry, just how pgoutput of the change builds the relcache entry from > > > the historic snapshot. > > > > > > > > isn't enough to prevent that thread's race condition. What do you think? > > > > > > > > I am not able to imagine how the race condition discussed in the > > > > thread you quoted can impact this patch. The problem discussed is > > > > mainly the interaction when we are processing the changes in logical > > > > decoding w.r.t concurrent DDL (Alter Publication ... Add Table). The > > > > problem happens because we use the old cache state. > > > > > > Right. Taking the example from > > > http://postgr.es/m/20231119021830.d6t6aaxtrkpn743y@awork3.anarazel.de, LSNs > > > between what that mail calls 4) and 5) are not safely usable as start points. > > > pg_createsubscriber evades that thread's problem if the consistent_lsn it > > > passes to pg_replication_origin_advance() can't be in a bad-start-point LSN > > > span. I cautiously bet the snapbuild.c process achieves that: > > > > > > > I am missing your > > > > point about the race condition mentioned in the thread you quoted with > > > > snapbuild.c. Can you please elaborate a bit more? > > > > > > When pg_createsubscriber calls pg_create_logical_replication_slot(), the key > > > part starts at: > > > > > > /* > > > * If caller needs us to determine the decoding start point, do so now. > > > * This might take a while. > > > */ > > > if (find_startpoint) > > > DecodingContextFindStartpoint(ctx); > > > > > > Two factors protect pg_createsubscriber. First, (a) CREATE PUBLICATION > > > committed before pg_create_logical_replication_slot() started. Second, (b) > > > DecodingContextFindStartpoint() waits for running XIDs to complete, via the > > > process described at the snapbuild.c "starting up in several stages" diagram. > > > Hence, the consistent_lsn is not in a bad-start-point LSN span. It's fine > > > even if the original INSERT populated all caches before CREATE PUBLICATION > > > started and managed to assign an XID only after consistent_lsn. From the > > > pgoutput perspective, that's indistinguishable from the transaction starting > > > at its first WAL record, after consistent_lsn. The linked "long-standing data > > > loss bug in initial sync of logical replication" thread doesn't have (a), > > > hence its bug. How close is that to accurate? > > > > Yeah, this theory sounds right to me. The key point is that no DML > > (processing of WAL corresponding to DML) before CREATE PUBLICATION ... > > command would have reached pgoutput level because we would have waited > > for it during snapbuild.c. Can we conclude that the race condition > > discussed in the other thread won't impact this patch? > > As Noah said the key point is the CREATE PUBLICATION *before* creating the > replication slots -- that wait transactions to complete. Let's consider the transaction loss topic concluded. Thanks for contemplating it. I've added an open item for the "dbname containing a space" topic.