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 1q6hMo-0007TB-HU for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jun 2023 00:49:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q6hLo-0005Wq-5H for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jun 2023 00:48:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q6hLn-0005Wf-Mo for pgsql-hackers@lists.postgresql.org; Wed, 07 Jun 2023 00:48:35 +0000 Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q6hLk-000caK-AW for pgsql-hackers@postgresql.org; Wed, 07 Jun 2023 00:48:33 +0000 Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-51478f6106cso525656a12.1 for ; Tue, 06 Jun 2023 17:48:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1686098910; x=1688690910; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=3udwNBUIpbpaNWynUYebtjxWEHWbDr+KUDDnzftwlb8=; b=bOOBefTBAmHQge3LBqtY66/pt64tOYYfaX2iXx7I4nGo2DaMEcZorgEjEURSubKVtP zqBA2tOW0cBTCsp4nDtBgXZpxWVrsfOQsn27rN1XCOSyCa2GYl7FzEfOlX6UJQTHd7e+ gYTfPvzJg08JBUKKKGV5ymrffOHyXmaAWydgjx0f40G7lhXuPlDxQiFxrYpFaIgkVxff PLkawNZkfFxHPHgnfo1E8LtrI+jc7lpFD+kb6qvyrxli5Ja5h5Q5cV/WlA+hgI7QHQoA rJOsf8+XkFyoDnmWX9Dpx8whJRVZ3Cyx0fIr07EJ4PcIKxU/AoGyLLKSjIAAV/kWqMEj DnMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686098910; x=1688690910; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3udwNBUIpbpaNWynUYebtjxWEHWbDr+KUDDnzftwlb8=; b=YWhIPuD+Cg+4J39p3j7J2V0RZEvduTK5cR00QuRlo0CkRAUMnjy2o5EPtk19GDfFAd cksGlsu03/xC8I7opx47sdqJ+r5ddF6qNb06DhQA7ZeKt+UWr2GPp/LrYiv8daWKN6qu Pz3VctQuCZf5ND0i15X1914Du0Vk5QoOqJHbPr/XPJ9hrV8xldSYoIrdZ1k1kWS51mX3 ffPkN3AF42WTfkSMfN5Eg2Ex+jkKWsLuVRj2gEb8xCRAeseNhWP0FGssC1dfSRvcezp3 cOzUv72/vMkuy16xRLU+BCm9h9ivGtYL/i91ApjZiEPa5GSKWZE7wh7WHznSHjK/IWBx T1tg== X-Gm-Message-State: AC+VfDx3ecX8tMNWZ2E8SYXFWPWrGkg5D42uNxy+1Wsyodni9EqJVcyW 4uQKoksbSRkc3x9B/ut83xbNaw== X-Google-Smtp-Source: ACHHUZ5ns8009YjjPYCHKuHSV2a2D4EFqIHl7xGSEEw285S1F3chqo9xg6vGFE55c3/Ra5rgaIaZzw== X-Received: by 2002:a50:fc13:0:b0:516:642e:f92a with SMTP id i19-20020a50fc13000000b00516642ef92amr3290072edr.0.1686098910476; Tue, 06 Jun 2023 17:48:30 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id t24-20020aa7d4d8000000b0050c524253dasm5567655edr.20.2023.06.06.17.48.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 06 Jun 2023 17:48:29 -0700 (PDT) Message-ID: Date: Wed, 7 Jun 2023 02:48:27 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: TRAP: FailedAssertion("prev_first_lsn < cur_txn->first_lsn", File: "reorderbuffer.c", Line: 927, PID: 568639) From: Tomas Vondra To: Alexander Lakhin , Amit Kapila , Masahiko Sawada Cc: pgsql-hackers References: <99b45770-e3d8-4522-ea52-f4509931c65b@enterprisedb.com> <1f860e40-9f95-f0f8-ff4b-4b780f9151ad@gmail.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/6/23 17:42, Tomas Vondra wrote: > > > On 6/6/23 14:00, Alexander Lakhin wrote: >> Hello Tomas, >> >> 06.06.2023 12:56, Tomas Vondra wrote: >>> On 6/6/23 11:00, Alexander Lakhin wrote: >>>> Hello, >>>> ...> With the debug logging added inside AssertTXNLsnOrder() I see: >>>> ctx->snapshot_builder->start_decoding_at: 209807224, >>>> ctx->reader->EndRecPtr: 210043072, >>>> SnapBuildXactNeedsSkip(ctx->snapshot_builder, ctx->reader->EndRecPtr): 0 >>>> and inside the loop: >>>> cur_txn->first_lsn: 209792872 >>>> cur_txn->first_lsn: 209975744 >>>> cur_txn->first_lsn: 210043008 >>>> cur_txn->first_lsn: 210043008 >>>> and it triggers the Assert. >>>> >>> So what's the prev_first_lsn value for these first_lsn values? How does >>> it change over time? Did you try looking at the pg_waldump for these >>> positions? >> >> With more logging I've got (for another run): >> ReorderBufferTXNByXid| xid: 3397, lsn: c1fbc80 >> >> ctx->snapshot_builder->start_decoding_at: c1f2cc0, >> ctx->reader->EndRecPtr: c1fbcc0, >> SnapBuildXactNeedsSkip(ctx->snapshot_builder, ctx->reader->EndRecPtr): 0 >> prev_first_lsn: 0, cur_txn->first_lsn: c1fbc80 >> prev_first_lsn: c1fbc80, cur_txn->first_lsn: c1fbc80 >> TRAP: failed Assert("prev_first_lsn < cur_txn->first_lsn") ... >> >> waldump for 00000001000000000000000C shows: >> grep c1fbc80: >> rmgr: Heap2       len (rec/tot):     60/    60, tx:       3398, lsn: >> 0/0C1FBC80, prev 0/0C1FBC50, desc: NEW_CID rel: 1663/18763/19987, tid: >> 0/1, cmin: 1, cmax: 4294967295, combo: 4294967295 >> rmgr: Heap        len (rec/tot):     59/    59, tx:       3398, lsn: >> 0/0C1FBCC0, prev 0/0C1FBC80, desc: INSERT+INIT off: 1, flags: 0x08, >> blkref #0: rel 1663/18763/19987 blk 0 >> >> grep '( 3397| 3398)' > > I've been able to reproduce this, after messing with the script a little > bit (I had to skip the test_decoding regression tests, because that was > complaining about slots already existing etc). > > Anyway, AssertTXNLsnOrder sees these two transactions (before aborting): > > 26662 0/6462E6F0 (first 0/0) > 26661 0/6462E6F0 (first 0/6462E6F0) > > > where 26661 is the top xact, 26662 is a subxact of 26661. This is > clearly a problem, because we really should not have subxact in this > list once the assignment gets applied. > > And the relevant WAL looks like this: > > --------------------------------------------------------------------- > 26662, lsn: 0/645EDAA0, prev 0/645EDA60, desc: ASSIGNMENT xtop 26661: > subxacts: 26662 > 26662, lsn: 0/645EDAD0, prev 0/645EDAA0, desc: INSERT+INIT off: 1, > flags: 0x08, blkref #0: rel 1663/125447/126835 blk 0 > ... > 0, lsn: 0/6462E5D8, prev 0/6462E2A0, desc: RUNNING_XACTS nextXid > 26673 latestCompletedXid 26672 oldestRunningXid 26661; 3 xacts: 26667 > 26661 26664; 3 subxacts: 26668 26662 26665 > ... > 26662, lsn: 0/6462E6F0, prev 0/6462E678, desc: NEW_CID rel: > 1663/125447/126841, tid: 0/1, cmin: 1, cmax: 4294967295, combo: 4294967295 > 26662, lsn: 0/6462E730, prev 0/6462E6F0, desc: INSERT+INIT off: 1, > flags: 0x08, blkref #0: rel 1663/125447/126841 blk 0 > 26661, lsn: 0/6462E770, prev 0/6462E730, desc: COMMIT 2023-06-06 > 16:41:24.442870 CEST; subxacts: 26662 > --------------------------------------------------------------------- > > so the assignment is the *first* thing that happens for these xacts. > > However, we skip the assignment, because the log for this call of > get_changes says this: > > LOG: logical decoding found consistent point at 0/6462E5D8 > > so we fail to realize the 26662 is a subxact. > > Then when processing the NEW_CID, SnapBuildProcessNewCid chimes in and > does this: > > ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn); > > ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn, > xlrec->target_locator, xlrec->target_tid, > xlrec->cmin, xlrec->cmax, > xlrec->combocid); > > and ReorderBufferAddNewTupleCids() proceeds to enter an entry for the > passed XID (which is xlrec->top_xid, 26661), but with LSN of the WAL > record. But ReorderBufferXidSetCatalogChanges() already did the same > thing for the subxact 26662, as it has no idea it's a subxact (due to > the skipped assignment). > > I haven't figured out what exactly is happening / what it should be > doing instead. But it seems wrong to skip the assignment - I wonder if > SnapBuildProcessRunningXacts might be doing that too eagerly. > In investigated this a bit more, and the problem actually seems to be more like this: 1) we create a new logical replication slot 2) while building the initial snapshot, we start with current insert location, and then process records 3) for RUNNING_XACTS we call SnapBuildProcessRunningXacts, which calls SnapBuildFindSnapshot 4) SnapBuildFindSnapshot does this: else if (!builder->building_full_snapshot && SnapBuildRestore(builder, lsn)) { /* there won't be any state to cleanup */ return false; } 5) because create_logical_replication_slot and get_changes both call CreateInitDecodingContext with needs_full_snapshot=false, we end up calling SnapBuildRestore() 6) once in a while this likely hits a snapshot created by a concurrent session (for another logical slot) with SNAPBUILD_CONSISTENT state I don't know what's the correct fix for this. Maybe we should set needs_full_snapshot=true in create_logical_replication_slot when creating the initial snapshot? Maybe we should use true even in pg_logical_slot_get_changes_guts? This seems to fix the crashes ... That'll prevent reading the serialized snapshots like this, but how could that ever work? It seems pretty much guaranteed to ignore any assignments that happened right before the snapshot? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company