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 1nKjfC-0005Wp-QG for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Feb 2022 16:29:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nKjfB-0000O5-Im for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Feb 2022 16:29:49 +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 1nKjfA-0000Nw-VU for pgsql-bugs@lists.postgresql.org; Thu, 17 Feb 2022 16:29:49 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nKjf4-0004bx-TK for pgsql-bugs@lists.postgresql.org; Thu, 17 Feb 2022 16:29:48 +0000 Received: by mail-wm1-x32a.google.com with SMTP id k127-20020a1ca185000000b0037bc4be8713so6621585wme.3 for ; Thu, 17 Feb 2022 08:29:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language :from:to:cc:references:in-reply-to:content-transfer-encoding; bh=/aQGijNInkVCotXVghlpCfn2BqS+RW4SToqq7K3Xao0=; b=SlcN1ukrMkDOH1jM0CR4ylQLdGKI2YRVQr6/V3JrLP23leX/khmhyMnpJabNtfQ4Cf eHMP5u2abuVeZASqCC0yrWXn7AH119AIlH8hzEansEjU33J0o98o98VnykRWNSGM4utE kYbKEqiob6GKw1TGlrDdyDuxLAecnxjV5NdPZf0MJQpaBvfQGPqlVGFOSb9wsIvCeZ+w xxvYYCuIc+BebaZmK5wm/iadsCoXGol1WgjJfWtaubjLRHFS/iIy+f6KymJbj77uWPDp KVHYL0IQJNXSGlmtcmkpQyERid2sKvGD7lHOaXGL8Z1/U+dT4qARasicyce76Vt1BqcP yr+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:from:to:cc:references:in-reply-to :content-transfer-encoding; bh=/aQGijNInkVCotXVghlpCfn2BqS+RW4SToqq7K3Xao0=; b=yZyfmFl9jeZN37o8LL8+QpjC2f8ZcIHCR71JwSF0c2JJrQjRrJlGPC7TSaejrRyFa6 yN2N720JlvMleO5Wn8ayRgzGzHoTaYRXzLYuAQFfJPPEpERzUaKi0+F3u0Hn6Ui9nLH7 RUPaPP4mgN/ptEgQ3TS9pQq7AVAwDs7e7D9eiG9oZWJUhMFsbfJyjZzqBXYuVVgheY0t oj8NYNY4sZ25IhB3dmC0aeMFju6dgLzKDrmj+VkVDw6oX2VcBFCXoKm6vBj1hnG/9n7x kqS5iaBb+ys1beP64DJr09Bd/qtG2C0xg8ulqr2b3rHXKEdKlTSbETZTPaIRLatLhixi e+yQ== X-Gm-Message-State: AOAM530r4y5+4NSMI+uhUy6TRu7bjVyvY6fLoaSmFvDboLwX8YfFRTqf DEYryUh2atpWUtLJdngcZKKdA0OvyzilAxHVtwU1ei9YHUVsJe/tZTiPB1ptsF/NuKXRU7uVV35 qGKkqonXKuKg20++Ms3DbjRXaQqUOmRgFeEzuGiTEBNFhQOhdv1YYampjLamk6FOeWw6P+GuonW n3BnWKddcJ/BWxMpgCMPQMrxIyKZ4EiEJ/N3QJIy5VauZV X-Google-Smtp-Source: ABdhPJxwf1Dn5Ld6KMNEkVit5ucqyLX8Sbjun2M6JxTPJxvmIFMW3Ff9F6alJOwGgxva6PSSfTMgmA== X-Received: by 2002:a05:600c:4e0a:b0:37b:c548:622a with SMTP id b10-20020a05600c4e0a00b0037bc548622amr7186030wmq.55.1645115380739; Thu, 17 Feb 2022 08:29:40 -0800 (PST) Received: from [10.137.0.18] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id r11sm29679950wrt.28.2022.02.17.08.29.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Feb 2022 08:29:39 -0800 (PST) Message-ID: <78098996-ca5e-a2ce-ae33-68ec8648ccc7@enterprisedb.com> Date: Thu, 17 Feb 2022 17:29:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: BUG #17406: Segmentation fault on GiST index after 14.2 upgrade Content-Language: en-US From: Tomas Vondra To: Peter Geoghegan Cc: Victor Yegorov , PostgreSQL mailing lists References: <17406-71e02820ae79bb40@postgresql.org> <788a0a51-b1e3-3601-5783-3f6488796ec4@enterprisedb.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/17/22 13:26, Tomas Vondra wrote: > On 2/17/22 03:52, Peter Geoghegan wrote: >> On Wed, Feb 16, 2022 at 4:16 PM Tomas Vondra >> wrote: >>> test=# SELECT * FROM gist_page_items(get_raw_page('path_gist_idx', 0), >>> 'path_gist_idx'); >> >> Maybe this is obvious, but just in case: gist_page_items is *very* >> trusting here. It's necessary to use gist_page_items_bytea() if you're >> not 100% sure about the index definition for the index page you >> provide. The fact that you can display the contents of each tuple >> using the underlying type's own output function is very handy. But >> potentially hazardous. >> > > Sure, but I used this to inspect the index from example in our docs: > >   https://www.postgresql.org/docs/14/ltree.html#id-1.11.7.30.8 > > I doubt we can be even more sure about the index definition. > > > I don't know what the root cause is yet, but the memory corruption > happens simply because ltree_out() sees entirely bogus data when > executed from gist_page_items. > > For example it gets 16B varlena, allocates 16B string for it, and starts > iterating over the ltree data. The it finds the first element is 60B > long and happily copies it "into" the 16B buffer. That can't end well, > obviously. > > Clearly, the ltree values are mangled somehow / somewhere. The funny > thing is the bytea always looks 8B longer and with bogus data. So for > example if you select this ltree from the table directly > >   Top.Collections.Pictures.Astronomy.Astronauts > > then ltree_out sees it as 80B varlena, with 5 levels. But if ltree_out > gets called from gist_page_items, then it sees 88B with 1 level, which > is completely bogus. > > And funnily enough, if you do this at the beginning of ltree_out: > >   in = (ltree *) ((char *) in + 8); > > then gist_page_items() starts working just fine. > > So, I guess gist_page_items() is broken/confused in some way. I'm not > sure if this is related to the crash reported by Victor or a separate, > independent issue. > I may be wrong, but it seems gist_page_items() is entirely oblivious to the fact that GiST does "compression", and fails to decompress the values before passing them to out function. So instead of "ltree" it extracts "ltree_gist" and passes that to "ltree_out". This is consistent with the 8B offset I mentioned before, because ltree_gist has 8B header and then stores the original ltree value. But I may be missing something - this seems like such an obvious gap that I don't know how this could have worked with anything but the most trivial cases without GiST compression. Furthermore, it seems more like a pageinspect issue, unrelated to the original issue reported by Victor. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company