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 1sLrrs-00H0FK-LI for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Jun 2024 22:08:56 +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 1sLrrq-00CKt4-Mx for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Jun 2024 22:08:55 +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.94.2) (envelope-from ) id 1sLrrq-00CKsv-65 for pgsql-hackers@lists.postgresql.org; Mon, 24 Jun 2024 22:08:54 +0000 Received: from mail-ot1-x32f.google.com ([2607:f8b0:4864:20::32f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sLrrn-003NIT-FF for pgsql-hackers@lists.postgresql.org; Mon, 24 Jun 2024 22:08:53 +0000 Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6f8edde24b3so3056614a34.2 for ; Mon, 24 Jun 2024 15:08:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1719266929; x=1719871729; 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=JWIws+C6lveevKvrUgYKmiq7YQG0Q0yihpnJWChciUk=; b=W5UzIizp9GRpd8mN6j/IhKvF7lbFVfs6K57dzL/COjyi5aPr7amKBEnSMc/2l8CnwK MMUqJUcdrgkTHXlBWCCcvRzWjxE6krR2pmcSapmx7Lwc+SX+WXoFSkcX7JpZYSmt2vGU lPBbWmZ5olJZMMta6KLTNKK8fGiAxVvPj1Ylo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719266929; x=1719871729; 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=JWIws+C6lveevKvrUgYKmiq7YQG0Q0yihpnJWChciUk=; b=eH1Sq1phrzU0bGCXC9NABQX04dXREjK0j48BJKctXYTTJGkRjFdhXiQMEcIe+/aaz2 PnUjI8jP9/BQDJUWHve57CmmKuQvmMmxJHfOhcSf2FsIHhmz8jnlpKLjQTMA5fYdcZw9 0dgnGklw22L+iuhf5bIdziu6Fy0dGF1dBhKQp8SjYghuR0LaxhwtD+/tLzw+glPVxzt4 VGwZhilMWFL0CEDXpSCV5HPqkpks/fy5V8lcgkg9Nbsj5ytPsV/DtFgJMeXqYY/YAkjF smZppXNWmUDGIueczMXE8swsDbEjcfChwFMHCGTQs83EL77xrAtbMm0PKVVqStXGKZlc hPyg== X-Forwarded-Encrypted: i=1; AJvYcCWIGjQ1O9VOvzcmkBcPGxwH6pTgqCJo/hxp4JxX7sXMfuPG8GH0nQi03oKDQwOgl+YadHGwTjIsISjg06mSR7xU0u2Oxv/9YCmnN2AiD5axJVDB X-Gm-Message-State: AOJu0YzQO2lzCk8DpKJeP/QaTdD5llp2BUIkk/4IjsTDjxqlxL5DYG8w dkB0Vx2DpZpHEZMC/g/84c89VZG/yEbtH5rwNbGtvP9E1OqHZQgDxUFF1fO7rQ== X-Google-Smtp-Source: AGHT+IHq+vx1uNybE8W3yoN1/NQ3Rfv2tG1SgWJPNBFdQtlIZaa/cfaqhT5upwjDUBMmCoSKjZ1W6g== X-Received: by 2002:a05:6830:1352:b0:6f9:94f4:c670 with SMTP id 46e09a7af769-700b1273372mr6530854a34.28.1719266928718; Mon, 24 Jun 2024 15:08:48 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7009c691d05sm1340451a34.77.2024.06.24.15.08.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Jun 2024 15:08:48 -0700 (PDT) Date: Mon, 24 Jun 2024 15:08:45 -0700 From: Noah Misch To: Amit Kapila Cc: Peter Eisentraut , Euler Taveira , 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: <20240624220845.ba.nmisch@google.com> References: <2f0b73f3-e7c0-4e95-9712-1993442a6b64@app.fastmail.com> <34637e7f-0330-420d-8f45-1d022962d2fe@app.fastmail.com> <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> 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 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? Thanks, nm