Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iNkiC-0006gQ-R1 for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Oct 2019 21:32:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iNkiB-0001vu-L4 for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Oct 2019 21:32:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iNkiB-0001vn-A4 for pgsql-bugs@lists.postgresql.org; Thu, 24 Oct 2019 21:32:03 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iNki8-0003ye-Hs for pgsql-bugs@lists.postgresql.org; Thu, 24 Oct 2019 21:32:02 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 13FB752E; Thu, 24 Oct 2019 17:31:59 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Thu, 24 Oct 2019 17:31:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=NwRnsmxCe42k7R1wqXnJ9USvEdt RcENt79Z6Q7kiItI=; b=gWUrQN9INL0P6ELzqCJXNgdRdYgeUsBMu2YLp7twIZ4 4Xs6N1bXqApOKcGISRSIrAcsTGpY+zG6BLj6B5iknF2j7BCsC4d+k/WdErYEJ8+9 zvWnT/or2MxS//WDvy/dGrraOcy08DseJ6R1agGO43tUVsRnG0q8UPQnO4r47VXS l7tY5gQX6OD+y6MS6Gre7xw1979X7M0LiuO8y2S7aD/Tt3StRvrrydXLgQCtq+KK s7ky0dF9vBQ3Jg7Jor11j6NyPjuwYJ9NWE+oFUrb1RxxTE+B9qd0PVxQQWZiGy+F +1vMYv/XWKLnwEifyGqvMxjRIgQGWsWMmCvHxKTxwGg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=NwRnsm xCe42k7R1wqXnJ9USvEdtRcENt79Z6Q7kiItI=; b=C8HX88+KiR1+Df/yV2f/o1 hguTgzAbtv2OLUjVZIk7PauPxQ2DIg2kSVuu/8cD0Wc7fi+fKbKwoAW2QNOLZXXN p36Tb+ITE9vvJI1XKnf+A5esA27KCOiY20t8ex3JCE+4lI5rnYVKArq4H1xSdxhm M20xDFX4vT9sP9V2ajoPPF6UbEY1ZBE+KNS4t/iTWeh7gb3jEtPxo6+hQwz2jLZS YfN6KpNemSxTFsoctg8nex7+2vaJFhtxVC9lnz7scmohmRvU1zjdCVN1HKiVapcu Nk6ghbZJ+5mg9Pu0U83/gGHgDUhz9pHxxwmgiw6nJNR9NUPmSpHaQ1pKdDKec0ug == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrledugdduiedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeetnhgurhgv shcuhfhrvghunhguuceorghnughrvghssegrnhgrrhgriigvlhdruggvqeenucffohhmrg hinhepphhoshhtghhrrdgvshenucfkphepieejrdduiedtrddvudekrddvfeejnecurfgr rhgrmhepmhgrihhlfhhrohhmpegrnhgurhgvshesrghnrghrrgiivghlrdguvgenucevlh hushhtvghrufhiiigvpedt X-ME-Proxy: Received: from intern.anarazel.de (c-67-160-218-237.hsd1.ca.comcast.net [67.160.218.237]) by mail.messagingengine.com (Postfix) with ESMTPA id 30139D6005B; Thu, 24 Oct 2019 17:31:58 -0400 (EDT) Date: Thu, 24 Oct 2019 14:31:57 -0700 From: Andres Freund To: Arseny Sher Cc: "Hsu, John" , "pgsql-bugs@lists.postgresql.org" Subject: Re: ERROR: subtransaction logged without previous top-level txn record Message-ID: <20191024213157.7pm6niybfxgpvmgg@alap3.anarazel.de> References: <87ftjifoql.fsf@ars-thinkpad> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87ftjifoql.fsf@ars-thinkpad> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2019-10-24 12:59:30 +0300, Arseny Sher wrote: > Our customer also encountered this issue and I've looked into it. The problem is > reproduced well enough using the instructions in the previous message. Is this with commit bac2fae05c7737530a6fe8276cd27d210d25c6ac Author: Alvaro Herrera Date: 2019-09-13 16:36:28 -0300 logical decoding: process ASSIGNMENT during snapshot build Most WAL records are ignored in early SnapBuild snapshot build phases. But it's critical to process some of them, so that later messages have the correct transaction state after the snapshot is completely built; in particular, XLOG_XACT_ASSIGNMENT messages are critical in order for sub-transactions to be correctly assigned to their parent transactions, or at least one assert misbehaves, as reported by Ildar Musin. Diagnosed-by: Masahiko Sawada Author: Masahiko Sawada Discussion: https://postgr.es/m/CAONYFtOv+Er1p3WAuwUsy1zsCFrSYvpHLhapC_fMD-zNaRWxYg@mail.gmail.com applied? > The check leading to this ERROR is too strict, it forbids legit behaviours. Say > we have in WAL > > [ confirmed_flush_lsn> ] > > - First xl_xact_assignment record is beyond reading, i.e. earlier > restart_lsn, where ready snapshot will be taken from disk. > - After restart_lsn there is some change of a subxact. > - After that, there is second xl_xact_assignment (for another subxact) > revealing relationship between top and first subxact, where this ERROR fires. > > Such transaction won't be streamed because we hadn't seen it in full. It must be > finished before streaming will start, i.e. before confirmed_flush_lsn. > > Of course, the easiest fix is to just throw the check out. I don't think that'd actually be a fix, and just hiding a bug. > Snapbuilder enters into SNAPBUILD_CONSISTENT immediately after deserializing the > snapshot. Generally this is incorrect because SNAPBUILD_CONSISTENT means not > just complete snapshot (snapshot by itself in FULL state is just good as in > CONSISTENT), but also reorderbuffer filled with all currently running > xacts. This is painless for decoding sessions with existing slots because they > won't stream anything before confirmed_flush_lsn is reached anyway, at which > point all transactions which hadn't got into reorderbuffer would definitely > finish. However, new slots might be created too early, thus losing (not > decoding) parts of transactions committed after freshly created > confirmed_flush_lsn. This can happen under the following extremely unlucky > circumstances: > - New slot creation reserves point in WAL since which it would read it > (GetXLogInsertRecPtr); > - It logs xl_running_xacts to start assembling a snapshot; > - Running decoding session with another slot quickly reads this > xl_running_xacts and serializes its snapshot; > - New slot reads xl_running_xacts and picks this snapshot up, saying that it > is ready to stream henceforth, though its reorderbuffer is empty. Yea, that's a problem :( > Exact reproducing steps: > > -- session 1 > create table t (i int); > select pg_create_logical_replication_slot('slot_1', 'test_decoding'); > > -- session 2 > begin; > insert into t values (1); > > -- session 3, start slot creation > select pg_create_logical_replication_slot('slot_2', 'test_decoding'); > -- stop (with gdb or something) it at DecodingContextFindStartpoint(ctx); > > -- session 1 > -- xl_running_xacts is dumped by ReplicationSlotReserveWal in previous command, no > -- need to sleep; our snap will be immediately serialized there > SELECT data FROM pg_logical_slot_get_changes('slot_1', NULL, NULL, 'include-xids', '1', 'skip-empty-xacts', '0'); > > -- continue slot_2 creation > > -- session 2: insert some more and commit > insert into t values (1); > commit; > > -- now this would find second insert, but not the first one > SELECT data FROM pg_logical_slot_get_changes('slot_2', NULL, NULL, 'include-xids', '1', 'skip-empty-xacts', '0'); It'd be good to make this into an isolation test. > What we can do here? I think the easiest fix might actually be to just ignore serialized snapshots when building the initial snapshot. Greetings, Andres Freund