Are SQL-language function lock ordering deadlocks solved in PostgreSQL 18?

Started by Bernice Southeyabout 1 year ago2 messagesgeneral
Jump to latest
#1Bernice Southey
bernice.southey@gmail.com

Hi pgsql-general community,

I recently ran into deadlocks in a SQL-language function when,
confusingly, a first-line advisory lock didn't prevent them. I
leveled-up my troubleshooting skills on this one, but of course it was
well explained a decade ago [1]/messages/by-id/20150728162823.25043.27625@wrigleys.postgresql.org. As best I understand it, the later
locks are acquired first, during the upfront full parsing of
SQL-language functions. The fix is to use PL/pgSQL.

I'm wondering if the change in 18 to "Improve SQL-language function
plan caching" [2]https://postgr.es/c/0dca5d68d fixes this? "for old-style SQL functions, it will
now work much as it does with PL/pgSQL functions, because we delay
parse analysis and planning of each query until we're ready to run
it."

I tried the simple test from the original mail [1]/messages/by-id/20150728162823.25043.27625@wrigleys.postgresql.org and indeed got
deadlocks in version 17, but not 18. Yet absence of evidence is not
proof of no race conditions.

This might be my favourite change in 18 [2]https://postgr.es/c/0dca5d68d, if I'm understanding it
right. I much prefer the install-time checking of SQL-language
functions, and the elegance of pure SQL. But I've been convinced the
performance benefit of plan caching gave PL/pgSQL the edge. Now I'm
not so sure.

Best regards, Bernice

[1]: /messages/by-id/20150728162823.25043.27625@wrigleys.postgresql.org
[2]: https://postgr.es/c/0dca5d68d

#2Merlin Moncure
mmoncure@gmail.com
In reply to: Bernice Southey (#1)
Re: Are SQL-language function lock ordering deadlocks solved in PostgreSQL 18?

On Mon, Oct 6, 2025 at 10:54 AM Bernice Southey <bernice.southey@gmail.com>
wrote:

Hi pgsql-general community,

I tried the simple test from the original mail [1] and indeed got
deadlocks in version 17, but not 18. Yet absence of evidence is not
proof of no race conditions.

This might be my favourite change in 18 [2], if I'm understanding it
right. I much prefer the install-time checking of SQL-language
functions, and the elegance of pure SQL. But I've been convinced the
performance benefit of plan caching gave PL/pgSQL the edge. Now I'm
not so sure.

It sure seems like the intent of the change was to modify exactly that
behavior.

If you look at the major built-in sql-esque languages, sql(old), sql(new),
PSM (new standard syntax) and pl/pgsql, I guess psm style and pure SQL
might be overlapped with PSM style functions, especially if PSM functions
can be inlined (I haven't looked yet)-- inlining of functions such as
'SELECT $1 + $2' being the only reason I write sql functions these days.
I have always found the strict compile time checking to be highly
obnoxious except in the immutable/inlinable cases that come up now and
then.

Plan caching is not so important IMO, since execution time tends to greatly
exceed plan time in my experience and the real performance bugaboo is bad,
not uncached plans. FIxing bad plans typically involves dipping into the
pl/pgsql toolbox, using variable temp tables, planner directives, and other
forbidden tools in PSM. In simple terms, I also used to restrict server
side coding views and pure sql functions for abstraction, but have long
since given up, and mainly write procedures :-).

I really wish PSM functions could do what pl/pgsql functions do, since I
like the creation syntax vs the 'string is body of code',but, alas.

merlin