Re: Is it OK to perform logging while holding a LWLock?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Is it OK to perform logging while holding a LWLock?
Date: 2026-02-11 03:19:54
Message-ID: 4006831.1770779994@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> writes:
> As $SUBJECT says, my understanding is no.

It's not a great idea, but I'm not sure it's fatal. There are places
that hold LWLocks for awhile.

> I think LWLocks are generally only held for a very short duration,
> like a few CPU cycles to read or modify some shared data,

Spinlocks are treated that way, but we're willing to hold LWLocks
longer. The main thing I'd be concerned about is that there is no
deadlock-detection infrastructure for LWLocks, so you'd better be
darn certain there is no possibility of deadlock. That usually
means you want to limit the extent of code that could run while
you're holding the lock.

In your specific example, the thing I'd be afraid of is that an
errcontext callback might do something you're not expecting.
We don't forbid errcontext callbacks from doing catalog lookups,
for instance. So on the whole I agree with this patch, with
or without any concrete example that fails.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message shveta malik 2026-02-11 03:30:02 Re: [PATCH] Support automatic sequence replication
Previous Message Chao Li 2026-02-11 03:16:07 Re: Little cleanup: Move ProcStructLock to the ProcGlobal struct