Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v5oTK-00CKCe-7F for pgsql-general@arkaria.postgresql.org; Mon, 06 Oct 2025 16:54:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1v5oTI-009T1w-21 for pgsql-general@arkaria.postgresql.org; Mon, 06 Oct 2025 16:54:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v5m0N-008O2s-4q for pgsql-general@lists.postgresql.org; Mon, 06 Oct 2025 14:16:00 +0000 Received: from mail-yx1-xb12e.google.com ([2607:f8b0:4864:20::b12e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v5m0L-000KYe-09 for pgsql-general@lists.postgresql.org; Mon, 06 Oct 2025 14:15:58 +0000 Received: by mail-yx1-xb12e.google.com with SMTP id 956f58d0204a3-636d5cefab2so6612217d50.2 for ; Mon, 06 Oct 2025 07:15:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759760156; x=1760364956; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=FeEuzI+T0q5i7Gux2wrQy5ZZZKuqFLqtW/c4gFYcLek=; b=mLRYgnHZkhhpZ2dfPb4Ezu2gZZdL97iCL5y7HJrvIJqFXzILzoL0z2BXYeNrZypQGK t90AUwB1299xbjPdcsM2CYvLpLpq87NdVUoLednMXphlE+WDKA/8fp/nOQcMqSHa5OBS JPK5F+5zwUy0TmlEF4HGwDTkjOFU4b6AXkBYZJvOZP3Jix3OpJcvfcPghcfDcmw9bI41 uzeQizCv30z/bZppmt1K27CQeuTi5CpLWMXfhoYwG4awYQFdAWEO/ddCK9gW07zp2MzJ H+A41M/sE2r7Tzz0WsxEzfsvLduHOjUtSd9hqg0sZLMPlRhxSHaHZmkaMZPN1cns2AZQ eWuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759760156; x=1760364956; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=FeEuzI+T0q5i7Gux2wrQy5ZZZKuqFLqtW/c4gFYcLek=; b=dzMujYw+nLM6pn7MvsL8kErYb/dZ/bv0JbeVDjsXCbv3V6J34Z5oPjQqxGBKa4mNRL lp9dcKDY2f2H2wwFVy7pd+dDhLt0t2S/JFrimmDoVb4Da+Pq0y/toh81+6Ogf07NYKKX jgJ1rdAZMQ82+Xf2PNOYkd0s+EKPuu3xSc9CSYilI8c3svsaX/EBgfeOX2azzNh0pEyt xzqVNKhfkLKDwZurb7nZGMdyryotC3rS3AgIxsMpGef3ijHitbau6n/5JLuVZPwQQ4yj 4haZOTA4cM36f04Xhn9tr9yeYSH0vYwBgTQrHg5A0g+cRRhtvauvQMDqYqJKymoMCbrk 1FAw== X-Gm-Message-State: AOJu0YzqshbCrVcAjqU9lAAM3cY4jKJygINAXh3YR1XPIzrRcbcuGBuk 3fcy+az+RIXymLIxdzemh0lXypt4kTeWhQ28gmRzj9KKIrSLgyphfASaJ4ArtOqNthQtTrxH1Vs jn+b0dOUWYVOSTIa940YGpRW0Jjk56Y1svRqI X-Gm-Gg: ASbGncsDAy15eAodtccuUFSFqb9G5nX1XgcXtK55fAZ7w3Oj9OYSfqKyE8KAH6oKk2c IHcVevBT2wMKl2cYe+83xBeovVxOz/gznzuDra3YBAGl23o3ea1ZfHvCMFKut8eiPuz2NED4Jz1 Ws9LmZvCLhsm8pqdzianS9aCGwn+IiKxHGMuJutoenoYSD38cqkBBi9Tqw7LmiHxoLAzJf6AjGX 8N44/b5AS5cKUX6HE7F6BiKP9Lg0O0Ln31BExkitRxO9IYSwxftWZ7l X-Google-Smtp-Source: AGHT+IFLskiKBec83VFDk4Yjl68t5lSJEnYccbejVWKSYubQaojLV6rwcAPgAQeta9LDKB2O1F+H6IIvBGFzwOE4T3I= X-Received: by 2002:a05:690e:15c5:b0:62c:f19f:794d with SMTP id 956f58d0204a3-63b9a106064mr9176048d50.36.1759760155864; Mon, 06 Oct 2025 07:15:55 -0700 (PDT) MIME-Version: 1.0 From: Bernice Southey Date: Mon, 6 Oct 2025 15:15:20 +0100 X-Gm-Features: AS18NWD4X60qe-YFkmRXiLLAuM4s6qjNgimSUhAaZmMyBvthEDv51dKr8TEruzo Message-ID: Subject: Are SQL-language function lock ordering deadlocks solved in PostgreSQL 18? To: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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]. 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] 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] 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. Best regards, Bernice [1] https://www.postgresql.org/message-id/flat/20150728162823.25043.27625%40wrigleys.postgresql.org [2] https://postgr.es/c/0dca5d68d