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 1nZ6s7-00042d-TJ for pgsql-hackers@arkaria.postgresql.org; Tue, 29 Mar 2022 08:06:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nZ6s5-0001xy-PB for pgsql-hackers@arkaria.postgresql.org; Tue, 29 Mar 2022 08:06:33 +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 1nZ6s5-0001x1-Da for pgsql-hackers@lists.postgresql.org; Tue, 29 Mar 2022 08:06:33 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nZ6rz-00044S-6F for pgsql-hackers@lists.postgresql.org; Tue, 29 Mar 2022 08:06:33 +0000 Received: by mail-pj1-x1036.google.com with SMTP id m22so16662701pja.0 for ; Tue, 29 Mar 2022 01:06:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=QpZTT5IqKc+57kdPFTrRlAfIYLMdIYsnKsEG3zf+W8Q=; b=nFBA4BjZ8e8skEB4VtTbcysyHn82+VpL/MMWMRGiLX1tgmxbhRN/LNa8pla3AeDAvF TumrHZlcgj6z3/kU9lZPpswFpqMl4f3hsKOKDChwC4q4QaC19XNSIle8n71tfV+JO2xC xQ9RX+4CRqw8wm/GUmy8Dz1zmHgXiZxmY233Kl4dshvW9rqG6JGjKKmrmLCOSw30K6mY z9OJCt7qEOIiWQ4N5KJMfYrA10zEMuYe3xgNowj3VUk3E14upD4fQxecEqLSTYh2G+ft +t+S1uI2Hle8sOL6zHyZJKjgae9+euR9jTOqZyHVkUnucFuoSY4EXBk34gKn/IpO97HF aruA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=QpZTT5IqKc+57kdPFTrRlAfIYLMdIYsnKsEG3zf+W8Q=; b=4F7YodQhxiLrseiOc/7t3B5JyboklbjJqlBFNlzXoenOA6AfR5J05qy2nUgq3dUvRg S/m5oEn4z7dstgISGGg7uzgfcdVUEIIIS2Cp68Ugcb5rijkEUtwlTWVPZ4bEQNzz3kFi UhJLdlnNfozpf6mDlWp5GCDXkJ3YG3qCr/xVX0QvkLPpeYEEatvFbeO0vejWpd9ROfYC 0ZecnHqVjJ9DN4CvudIodjeb6YZSUFcAILuSxM62sNgTqm/Ht75MkBl1adZilbigNPYk RV6IDjzYaaO72dpE/cDTSsxeBW1doFJjT6yWrXiyM1Ce4GBEGG64rwETKH8Zr1FPsKLc zlZw== X-Gm-Message-State: AOAM532JKmbThI4Dg/De61BFp5M80hX6mugZrhdqVhMyqImQuG8HYbEI EFPH+/w7UjxbKosb25YGQrU= X-Google-Smtp-Source: ABdhPJxdTTcmFtBo25d1kXHD08f52d+KoWmkR0aazbFvLSfl/ZloUmoP8pObY0QNNXmb3Gtug9llKA== X-Received: by 2002:a17:90b:3b44:b0:1c7:9ca8:a19e with SMTP id ot4-20020a17090b3b4400b001c79ca8a19emr3217553pjb.245.1648541184725; Tue, 29 Mar 2022 01:06:24 -0700 (PDT) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id q20-20020a056a00151400b004fb31b073cesm11795831pfu.109.2022.03.29.01.06.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 29 Mar 2022 01:06:23 -0700 (PDT) Date: Tue, 29 Mar 2022 17:06:21 +0900 (JST) Message-Id: <20220329.170621.2101149643828659444.horikyota.ntt@gmail.com> To: er@xs4all.nl Cc: pgsql-hackers@lists.postgresql.org Subject: Re: TRAP: FailedAssertion("HaveRegisteredOrActiveSnapshot()", File: "toast_internals.c", Line: 670, PID: 19403) From: Kyotaro Horiguchi In-Reply-To: <20220328.183646.1041002117568499041.horikyota.ntt@gmail.com> References: <20220328.183646.1041002117568499041.horikyota.ntt@gmail.com> User-Agent: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Tue_Mar_29_17_06_21_2022_990)--" Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ----Next_Part(Tue_Mar_29_17_06_21_2022_990)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit At Mon, 28 Mar 2022 18:36:46 +0900 (JST), Kyotaro Horiguchi wrote in > Inserting EnsurePortalSnapshotExists() to RunFromStore fixes this but > I'm not sure where is the right place to do this.. Then, I found that portal->holdSnapshot is that. I came up with the attached. It does the follows: 1. Teach PlannedStmtRequiresSnapshot() to return true for FetchStmt. 2. Use holdSnapshot in RunFromStore if any. The rerpducer is reduced to as small as the following. CREATE TABLE t (a text); INSERT INTO t VALUES('some random text'); BEGIN; DECLARE c CURSOR FOR SELECT * FROM t; FETCH ALL IN c; But I haven't come up with a reasonable way to generate the 'some random text' yet. regards. -- Kyotaro Horiguchi NTT Open Source Software Center ----Next_Part(Tue_Mar_29_17_06_21_2022_990)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="use_holdsnapshot_to_read_cursor.patch" diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c index 5aa5a350f3..8354029f2a 100644 --- a/src/backend/tcop/pquery.c +++ b/src/backend/tcop/pquery.c @@ -1068,6 +1068,18 @@ RunFromStore(Portal portal, ScanDirection direction, uint64 count, dest->rStartup(dest, CMD_SELECT, portal->tupDesc); + /* + * If holdSnapshot is set, that means we should use the snapshot to read + * this store. + */ + if (portal->holdSnapshot) + { + Assert(portal->portalSnapshot == 0); + PushActiveSnapshotWithLevel(portal->holdSnapshot, + portal->createLevel); + portal->portalSnapshot = GetActiveSnapshot(); + } + if (ScanDirectionIsNoMovement(direction)) { /* do nothing except start/stop the destination */ @@ -1114,6 +1126,13 @@ RunFromStore(Portal portal, ScanDirection direction, uint64 count, dest->rShutdown(dest); + if (portal->holdSnapshot) + { + Assert(portal->portalSnapshot == GetActiveSnapshot()); + PopActiveSnapshot(); + portal->portalSnapshot = NULL; + } + ExecDropSingleTupleTableSlot(slot); return current_tuple_count; @@ -1756,7 +1775,6 @@ PlannedStmtRequiresSnapshot(PlannedStmt *pstmt) IsA(utilityStmt, VariableShowStmt) || IsA(utilityStmt, ConstraintsSetStmt) || /* efficiency hacks from here down */ - IsA(utilityStmt, FetchStmt) || IsA(utilityStmt, ListenStmt) || IsA(utilityStmt, NotifyStmt) || IsA(utilityStmt, UnlistenStmt) || ----Next_Part(Tue_Mar_29_17_06_21_2022_990)----