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 1oXzfY-0002hB-7s for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Sep 2022 06:45:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oXzfX-0000tQ-6P for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Sep 2022 06:45:15 +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 1oXzfW-0000rS-Tr for pgsql-hackers@lists.postgresql.org; Tue, 13 Sep 2022 06:45:14 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oXzfU-0000HD-SL for pgsql-hackers@lists.postgresql.org; Tue, 13 Sep 2022 06:45:14 +0000 Received: by mail-pj1-x1029.google.com with SMTP id o70-20020a17090a0a4c00b00202f898fa86so965543pjo.2 for ; Mon, 12 Sep 2022 23:45:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:cc:to:message-id:date:from:to:cc:subject :date; bh=lNaPUlSL+ba1bjjarJKWup0dZNbjz1JxKksfqU6BxWI=; b=QRf0o6UJnqs1kaiTgRajdimM0ZpZGXDpcK8pXZ3knEhB5doKGsNLiuzmioSXGyGj4W VNFl2zg9EhQnQn1Jb0DVJQK3SF2zCgJi6NnQm9V2MxmVOs4ptOdAzoKmI4ck9YnE0hwO L4gDrqHcLPouRty3o0CWWfLRXdxaq9EHJEzd0vNTq8twKySvQrgDy4aCQLgL3iZQ/iMM 69x4b57IesAEVAcRTFukrFN3jpelN5NO+idsiooy9yxsDuoTa6yEIzV+f8ykaqQfVv7m zcSf3dVqmG0C9SQ8NjV075QHImyBAn8xddBEpwKQXHjfv8J/Y3c9wW/+UBKa9DF0QC9D PrGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:cc:to:message-id:date:x-gm-message-state :from:to:cc:subject:date; bh=lNaPUlSL+ba1bjjarJKWup0dZNbjz1JxKksfqU6BxWI=; b=S/yI6nVT+Wk3PcFxBkgoIdgZKCWKI+gSWtoWbV/fNUzI8rL5obD6bAgQebrU07PBSU GD8YvcihidAo47jXtLyIPCc3v1ZQKyUGU5LNQdopaSUXYElgN+K6G6+P6iR/8V7jKHJg sIl93WEnszHnVqlgZAwRniUfPszFGVL0ZTabbag9vt0SZeLI0VM22c7uosQ8KLULpvDf 7Py0x13uW3iELnkEg60v+x0hP6AJK32LhTaXwiefXEF0ZlvS++5s8EJ8W7nZMHXGZPYP sEjvaNNmAOhQyOQ+jVu6kGqHqCwQaXPkvBCV6Vnbasvj5enMEbBZ8x8RCA8ivxOHzA0i 2v+w== X-Gm-Message-State: ACgBeo01QboRrY6gFCPLKr+i/hTc/PL8b6f5xm/jObOGCl/QnlldiWA6 6jRDagehT3IDo88bY9BdCwf+3u+Muug= X-Google-Smtp-Source: AA6agR4/ABOhln4rxsOFboDLzSx59TdlFmK73EIuBsPfJZy39HCkwuP6sXsZbASsHbIq0LOhcAqvnA== X-Received: by 2002:a17:90a:8b92:b0:202:c361:9dba with SMTP id z18-20020a17090a8b9200b00202c3619dbamr2370425pjn.62.1663051510382; Mon, 12 Sep 2022 23:45:10 -0700 (PDT) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id p20-20020a63e654000000b00434abd19eeasm6817256pgj.78.2022.09.12.23.45.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 12 Sep 2022 23:45:09 -0700 (PDT) Date: Tue, 13 Sep 2022 15:45:07 +0900 (JST) Message-Id: <20220913.154507.1031550660908356731.horikyota.ntt@gmail.com> To: andres@anarazel.de Cc: jchampion@timescale.com, y.sokolov@postgrespro.ru, dilipbalaut@gmail.com, rjuju123@gmail.com, pgsql-hackers@lists.postgresql.org Subject: Re: Error "initial slot snapshot too large" in create replication slot From: Kyotaro Horiguchi In-Reply-To: <20220912215156.zi52zu6n7od3k3v3@awork3.anarazel.de> References: <20220719.115506.1518451006264600540.horikyota.ntt@gmail.com> <20220912215156.zi52zu6n7od3k3v3@awork3.anarazel.de> User-Agent: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk At Mon, 12 Sep 2022 14:51:56 -0700, Andres Freund wrote in > Hi, > > Thanks for working on this! > > > I think this should include a test that fails without this change and succeeds > with it... > > > On 2022-07-19 11:55:06 +0900, Kyotaro Horiguchi wrote: > > From abcf0a0e0b3e2de9927d8943a3e3c145ab189508 Mon Sep 17 00:00:00 2001 > > From: Kyotaro Horiguchi > > Date: Tue, 19 Jul 2022 11:50:29 +0900 > > Subject: [PATCH v6] Create correct snapshot during CREATE_REPLICATION_SLOT > > This sees a tad misleading - the previous snapshot wasn't borken, right? I saw it kind of broken that ->xip contains sub transactions. But I didn't meant it's broken by "correct". Is "proper" suitable there? > > +/* > > + * ReorderBufferXidIsKnownSubXact > > + * Returns true if the xid is a known subtransaction. > > + */ > > +bool > > +ReorderBufferXidIsKnownSubXact(ReorderBuffer *rb, TransactionId xid) > > +{ > > + ReorderBufferTXN *txn; > > + > > + txn = ReorderBufferTXNByXid(rb, xid, false, > > + NULL, InvalidXLogRecPtr, false); > > + > > + /* a known subtxn? */ > > + if (txn && rbtxn_is_known_subxact(txn)) > > + return true; > > + > > + return false; > > +} > > The comments here just seem to restate the code.... Yeah, it is pulled from the existing code but result looks like so.. > It's not obvious to me that it's the right design (or even correct) to ask > reorderbuffer about an xid being a subxid. Maybe I'm missing something, but > why would reorderbuffer even be guaranteed to know about all these subxids? I think you're missing that the code is visited only after the reorder buffer's state becomes SNAPBUILD_CONSISTENT. I think rbtxn_is_known_subxact() is reliable at that stage. > > @@ -568,9 +571,17 @@ SnapBuildInitialSnapshot(SnapBuild *builder) > > > > MyProc->xmin = snap->xmin; > > > > - /* allocate in transaction context */ > > + /* > > + * Allocate in transaction context. > > + * > > + * We could use only subxip to store all xids (takenduringrecovery > > + * snapshot) but that causes useless visibility checks later so we hasle to > > + * create a normal snapshot. > > + */ > > I can't really parse this comment at this point, and I seriously doubt I could > later on. Mmm. The "takenduringrecovery" is relly perplexing (it has been somehow lower-cased..), but after removing the parenthesized part, it looks like this. And it had a misspelling but I removed that word. Is this still unreadable? We could use only subxip to store all xids but that causes useless visibility checks later so we create a normal snapshot. > > @@ -591,12 +605,24 @@ SnapBuildInitialSnapshot(SnapBuild *builder) > > > > if (test == NULL) > > { > > - if (newxcnt >= GetMaxSnapshotXidCount()) > > - ereport(ERROR, > > - (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE), > > - errmsg("initial slot snapshot too large"))); > > - > > - newxip[newxcnt++] = xid; > > + /* Store the xid to the appropriate xid array */ > > + if (ReorderBufferXidIsKnownSubXact(builder->reorder, xid)) > > + { > > + if (!overflowed) > > + { > > + if (newsubxcnt >= GetMaxSnapshotSubxidCount()) > > + overflowed = true; > > + else > > + newsubxip[newsubxcnt++] = xid; > > + } > > + } > > + else > > + { > > + if (newxcnt >= GetMaxSnapshotXidCount()) > > + elog(ERROR, > > + "too many transactions while creating snapshot"); > > + newxip[newxcnt++] = xid; > > + } > > } > > Hm, this is starting to be pretty deeply nested... Yeah, at least one if() is removable. > I wonder if a better fix here wouldn't be to allow importing a snapshot with a > larger ->xid array. Yes, we can't do that in CurrentSnapshotData, but IIRC we > need to be in a transactional snapshot anyway, which is copied anyway? The other reason for oversized xip array is it causes visibility check when it is used. AFAICS XidInMVCCSnapshot has additional path for takenDuringRecovery snapshots that contains a linear search (currently it is replaced by pg_lfind32()). This saves us from doing this for that snapshot. regards. -- Kyotaro Horiguchi NTT Open Source Software Center