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 1hlIo0-0001jC-2S for pgsql-bugs@arkaria.postgresql.org; Wed, 10 Jul 2019 20:03:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hlIny-0003w0-2S for pgsql-bugs@arkaria.postgresql.org; Wed, 10 Jul 2019 20:03:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hlInx-0003sk-PK for pgsql-bugs@lists.postgresql.org; Wed, 10 Jul 2019 20:03:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hlInv-0008No-5i for pgsql-bugs@lists.postgresql.org; Wed, 10 Jul 2019 20:03:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x6AK2oVw012490; Wed, 10 Jul 2019 16:02:50 -0400 From: Tom Lane To: Peter Geoghegan cc: Andres Freund , Manuel Rigger , PostgreSQL mailing lists Subject: Re: ERROR: found unexpected null value in index In-reply-to: References: <9288.1562720919@sss.pgh.pa.us> <20825.1562723034@sss.pgh.pa.us> <8251.1562726821@sss.pgh.pa.us> Comments: In-reply-to Peter Geoghegan message dated "Tue, 09 Jul 2019 20:06:50 -0700" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <12410.1562788882.0@sss.pgh.pa.us> Date: Wed, 10 Jul 2019 16:02:50 -0400 Message-ID: <12489.1562788970@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <12410.1562788882.1@sss.pgh.pa.us> Peter Geoghegan writes: > On Tue, Jul 9, 2019 at 7:47 PM Tom Lane wrote: >> Hm. So maybe we need to teach it to ignore tuples that are not the tips >> of their HOT chains? > An approach like that was the first thing that I thought of, but I'll > have to study the problem some more before coming up with a firm > opinion. I experimented with the attached. It solves the reported problem and passes check-world. I made it just ignore any tuple for which HeapTupleHeaderIsHotUpdated is true. It might seem like there's a risk of ignoring valid data, if the end tuple of a HOT chain is dead due to a transaction abort, but since HeapTupleHeaderIsHotUpdated checks for xmax validity I judge that the risk of that is small enough to be acceptable. A bigger problem with this is that in the tableam world, this seems like a complete disaster modularity-wise. I think it won't actually fail --- non-heap AMs are probably not returning BufferHeapTupleTableSlots, and even if they are, they shouldn't be marking tuples HOT_UPDATED unless that concept applies to them. But it sure seems like this leaves get_actual_variable_range() knowing way more than it ought to about storage-level concerns. Should we try to transpose some of this logic to below the AM API, and if so, what should that look like? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-get_actual_variable_range-for-HOT-chains-wip.patch"; charset="us-ascii" Content-ID: <12410.1562788882.2@sss.pgh.pa.us> Content-Description: fix-get_actual_variable_range-for-HOT-chains-wip.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/self= uncs.c index d7e3f09..b51cbed 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -5232,8 +5232,6 @@ get_actual_variable_range(PlannerInfo *root, Variabl= eStatData *vardata, InvalidOid, /* no reg proc for this */ (Datum) 0); /* constant */ = - have_data =3D true; - /* If min is requested ... */ if (min) { @@ -5262,15 +5260,33 @@ get_actual_variable_range(PlannerInfo *root, Varia= bleStatData *vardata, * get_actual_variable_range() call will not have to visit * that heap entry. In this way we avoid repetitive work when * this function is used a lot during planning. + * + * However, all is not perfect, because if the index was + * recently created it may have entries pointing to broken HOT + * chains that contain tuples that don't match the index entry + * but aren't yet known-dead either. We need to ignore such + * tuples, since they might not be representative of the index + * extremal values, indeed could even contain NULLs. We + * approximate this by ignoring any hot-updated tuples we see + * and continuing to scan. */ index_scan =3D index_beginscan(heapRel, indexRel, &SnapshotNonVacuumable, 1, 0); index_rescan(index_scan, scankeys, 1, NULL, 0); = - /* Fetch first tuple in sortop's direction */ - if (index_getnext_slot(index_scan, indexscandir, slot)) + /* Fetch first/next tuple in sortop's direction */ + while (index_getnext_slot(index_scan, indexscandir, slot)) { + /* Ignore hot-updated tuples, per comment above */ + if (TTS_IS_BUFFERTUPLE(slot)) + { + BufferHeapTupleTableSlot *bslot =3D (BufferHeapTupleTableSlot *) sl= ot; + + if (HeapTupleHeaderIsHotUpdated(bslot->base.tupdata.t_data)) + continue; + } + /* Extract the index column values from the slot */ FormIndexDatum(indexInfo, slot, estate, values, isnull); @@ -5284,24 +5300,40 @@ get_actual_variable_range(PlannerInfo *root, Varia= bleStatData *vardata, MemoryContextSwitchTo(oldcontext); *min =3D datumCopy(values[0], typByVal, typLen); MemoryContextSwitchTo(tmpcontext); + have_data =3D true; + break; } - else - have_data =3D false; = index_endscan(index_scan); } + else + { + /* If min not requested, assume index has data */ + have_data =3D true; + } = /* If max is requested, and we didn't find the index is empty */ if (max && have_data) { + have_data =3D false; + index_scan =3D index_beginscan(heapRel, indexRel, &SnapshotNonVacuumable, 1, 0); index_rescan(index_scan, scankeys, 1, NULL, 0); = - /* Fetch first tuple in reverse direction */ - if (index_getnext_slot(index_scan, -indexscandir, slot)) + /* Fetch first/next tuple in reverse direction */ + while (index_getnext_slot(index_scan, -indexscandir, slot)) { + /* Ignore hot-updated tuples, per comment above */ + if (TTS_IS_BUFFERTUPLE(slot)) + { + BufferHeapTupleTableSlot *bslot =3D (BufferHeapTupleTableSlot *) sl= ot; + + if (HeapTupleHeaderIsHotUpdated(bslot->base.tupdata.t_data)) + continue; + } + /* Extract the index column values from the slot */ FormIndexDatum(indexInfo, slot, estate, values, isnull); @@ -5315,9 +5347,9 @@ get_actual_variable_range(PlannerInfo *root, Variabl= eStatData *vardata, MemoryContextSwitchTo(oldcontext); *max =3D datumCopy(values[0], typByVal, typLen); MemoryContextSwitchTo(tmpcontext); + have_data =3D true; + break; } - else - have_data =3D false; = index_endscan(index_scan); } ------- =_aaaaaaaaaa0--