From: Antonin Houska <ah@cybertec.at>
To: Matthias van de Meent <boekewurm+postgres@gmail.com>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: Alexander Lakhin <exclusion@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: Unexpected behavior after OOM errors
Date: Tue, 21 Jul 2026 15:28:22 +0200
Message-ID: <62382.1784640502@localhost> (raw)
In-Reply-To: <CAEze2WjfEW79bA+gyQ1F6ABt6ngQRejhcsLeWzG+obV+Lp8hJg@mail.gmail.com>
References: <e77acaac-a1b3-40b3-99ee-5769b4e453e4@gmail.com>
<CAEze2Wgktuo+_QWfbALRW-UXXZhbSC+OgGGDFDV9m_BLy5KozQ@mail.gmail.com>
<ajN2DkXPOzzC1GSj@paquier.xyz>
<CAEze2Wj3HUz+Z4A8GaoSQyP1V_qGZEkxhp6dquF3+gNyoAmJNw@mail.gmail.com>
<ajSFcksGRhNJEs5H@paquier.xyz>
<CAEze2Wjh4_VmtD8C54q2drth2HSkCzBtgMZocuwj=z2gLDFcNQ@mail.gmail.com>
<ajUkLyWHxCeFVop_@paquier.xyz>
<ajZFM1XwbDpTQYZV@paquier.xyz>
<95c64dc2-3abe-4f4e-b285-4c681f565d9f@gmail.com>
<alswHQQfAdmC7M4B@paquier.xyz>
<91199.1784569717@localhost>
<CAEze2WjfEW79bA+gyQ1F6ABt6ngQRejhcsLeWzG+obV+Lp8hJg@mail.gmail.com>
Matthias van de Meent <boekewurm+postgres@gmail.com> wrote:
> On Mon, 20 Jul 2026 at 19:48, Antonin Houska <ah@cybertec.at> wrote:
> >
> > Michael Paquier <michael@paquier.xyz> wrote:
> >
> > > On Fri, Jul 17, 2026 at 12:00:01PM +0300, Alexander Lakhin wrote:
> > > > 4) make_new_heap():
> > > > --- a/src/backend/commands/repack.c
> > > > +++ b/src/backend/commands/repack.c
> > > > @@ -1255,3 +1255,5 @@ make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid NewAccessMethod,
> > > > */
> > > > +oom_prob = 0.01;
> > > > CommandCounterIncrement();
> > > > +oom_prob = 0;
> > > >
> > > > triggers:
> > > > TRAP: failed Assert("ItemIdIsNormal(lp)"), File: "heapam.c", Line: 2813, PID: 943626
> > > > also:
> > > > ERROR: attempted to delete invisible tuple
> > >
> > > I'll let Alvaro comment on this one. It deserves an open item.
> >
> > I was also curious because I worked on repack.c recently. After applying (a
> > bit rebased) random-oom-errors.patch from [1] and after turning some "out of
> > memory" errors to PANIC, I could reproduce the error even on PG 18. The stack
> > trace is below. So it does not seem to be PG 19 specific.
>
> A PANIC with AllocSetAlloc on the stacktrace isn't an indication of a
> bug when you add both an OOM probability on palloc, and set the
> OOM-error-thrower to PANIC instead of ERROR.
>
> This thread contains various examples of backend-local state remaining
> in various levels of incomplete modifications/corruptions even after
> the ERROR handling of an OOM has been completed. PANICs cause the
> backend to be terminated, and so are much less likely to cause
> problems down the line.
>
> Note, too, that the problem that Alexander reported is a failing
> Assert(), and an ERROR reporting "attempted to delete invisible
> tuple", rather than actual "out of memory" ERRORs.
I supposed that these problems happen because relcache remains in wrong state
due to the "out of memory" ERROR. So the point of using PANIC was to find out
when exactly the cache gets broken. However, I missed the fact that
Alexander's patch triggers the ERROR at several different places, so the stack
trace I posted might be unrelated.
Nevertheless, on a closer look I concluded that the problem is not related to
OOM. Rather, *any* ERROR that happens during cache invalidation can cause
this. Attached here is cache_inval_failure.diff that reproduces the problem
reliably:
postgres=# CREATE TABLE t1(i int PRIMARY KEY); CLUSTER t1 USING t1_pkey; DROP TABLE t1;
CREATE TABLE
ERROR: failure during cache invalidation
ERROR: attempted to delete invisible tuple
(The Assert(ItemIdIsNormal(lp)) IMO fires only if VACUUM manages to reset the
item pointer before heap_delete() gets called. With autovacuum=off, I get only
the "failure during cache invalidation" error.)
So I think we need to make sure that ERROR during cache invalidation becomes
FATAL.
--
Antonin Houska
Web: https://www.cybertec-postgresql.comAttachments:cache_inval_failure.diff (text/x-diff, 1.8K) downloaddiff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.cindex 94a5aee90b5..7d9812804ce 100644--- a/src/backend/commands/cluster.c+++ b/src/backend/commands/cluster.c@@ -784,7 +784,9 @@ make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid NewAccessMethod,
* Advance command counter so that the newly-created relation's catalog
* tuples will be visible to table_open.
*/
+ invalidation_failure = true;
CommandCounterIncrement();
+ invalidation_failure = false;
/*
* If necessary, create a TOAST table for the new relation.
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.cindex 30048e153e5..ca8a16c50f6 100644--- a/src/backend/utils/cache/inval.c+++ b/src/backend/utils/cache/inval.c@@ -180,6 +180,8 @@ typedef struct InvalMessageArray
static InvalMessageArray InvalMessageArrays[2];
+bool invalidation_failure = false;+
/* Control information for one logical group of messages */
typedef struct InvalidationMsgsGroup
{
@@ -1419,6 +1421,9 @@ CommandEndInvalidationMessages(void)
ProcessInvalidationMessages(&transInvalInfo->ii.CurrentCmdInvalidMsgs,
LocalExecuteInvalidationMessage);
+ if (invalidation_failure)+ elog(ERROR, "failure during cache invalidation");+
/* WAL Log per-command invalidation messages for wal_level=logical */
if (XLogLogicalInfoActive())
LogLogicalInvalidations();
diff --git a/src/include/utils/inval.h b/src/include/utils/inval.hindex b5fbd754c2c..1512e47d38d 100644--- a/src/include/utils/inval.h+++ b/src/include/utils/inval.h@@ -20,6 +20,8 @@
extern PGDLLIMPORT int debug_discard_caches;
+extern bool invalidation_failure;+
typedef void (*SyscacheCallbackFunction) (Datum arg, int cacheid, uint32 hashvalue);
typedef void (*RelcacheCallbackFunction) (Datum arg, Oid relid);
typedef void (*RelSyncCallbackFunction) (Datum arg, Oid relid);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: ah@cybertec.at, boekewurm+postgres@gmail.com, michael@paquier.xyz, exclusion@gmail.com
Subject: Re: Unexpected behavior after OOM errors
In-Reply-To: <62382.1784640502@localhost>
References: <e77acaac-a1b3-40b3-99ee-5769b4e453e4@gmail.com> <CAEze2Wgktuo+_QWfbALRW-UXXZhbSC+OgGGDFDV9m_BLy5KozQ@mail.gmail.com> <ajN2DkXPOzzC1GSj@paquier.xyz> <CAEze2Wj3HUz+Z4A8GaoSQyP1V_qGZEkxhp6dquF3+gNyoAmJNw@mail.gmail.com> <ajSFcksGRhNJEs5H@paquier.xyz> <CAEze2Wjh4_VmtD8C54q2drth2HSkCzBtgMZocuwj=z2gLDFcNQ@mail.gmail.com> <ajUkLyWHxCeFVop_@paquier.xyz> <ajZFM1XwbDpTQYZV@paquier.xyz> <95c64dc2-3abe-4f4e-b285-4c681f565d9f@gmail.com> <alswHQQfAdmC7M4B@paquier.xyz> <91199.1784569717@localhost> <CAEze2WjfEW79bA+gyQ1F6ABt6ngQRejhcsLeWzG+obV+Lp8hJg@mail.gmail.com> <62382.1784640502@localhost>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox