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 1tzLk2-0016I3-MK for pgsql-general@arkaria.postgresql.org; Mon, 31 Mar 2025 20:28:18 +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 1tzLk0-00BoMR-Au for pgsql-general@arkaria.postgresql.org; Mon, 31 Mar 2025 20:28:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tzLjz-00BoMI-UI for pgsql-general@lists.postgresql.org; Mon, 31 Mar 2025 20:28:15 +0000 Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tzLjx-002IX5-1x for pgsql-general@postgresql.org; Mon, 31 Mar 2025 20:28:15 +0000 Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-5499614d3d2so5590524e87.3 for ; Mon, 31 Mar 2025 13:28:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743452893; x=1744057693; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=y24/ywJTX3W/Fw7eqZkSwFgqPpKwpXUMwuHkNHUToz0=; b=QmdDXFWICKfm1mhQwH/MDsX9zpLYItDEDVEG3LAdgP1TBxpNzOtZucop3x49EGiUlF 7gilJQmU4fvRU5xqAQ0EHrM7qhJUgzOIWSSzf1TGc/me3xLLZ7a7XjjVWgymyioo04Jt 3o1A3rbOdoSIS2E+sXbbV8QnZzUExRfpEzNqIk8hF7Wnawl7Z3nR+tosBcQhynYd9E4i zruEn4X1OqdABuJ8RTW/PtZGA8LI64kViWC6zi1OrxIOuyk/ips9mnhh8Ex4evykz4x8 cLvzho6w01VLkBV3KBRUyizAxDlOCZ7dAg3q8jLAe75ksCPqY3GiGUqaWO8YAQHoVTqK u5dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743452893; x=1744057693; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=y24/ywJTX3W/Fw7eqZkSwFgqPpKwpXUMwuHkNHUToz0=; b=hGne1MSgerdH0si88JTZ+bOcf6QumIX3jn3DL7iUqXQxENPszMSCMCEOo76Ae1gJBH Lb3Eh1Fn5pEe4k+5RBMPsWhHn2i+8N8tE2gWMImenF1Iy51rOLS+LfYIVQBRlIbfbAi9 W/AxSQVHGabjW+gnRI48w7DESATXY83VjA11kQrCpobQ+vt2PvXYQQ9og4cVB3deQrmk EHYMYlJ854MW+OJkAkxNO6jtc8jKPkY4LK9Oy4TeFiugm4CmSI4/ghYZuqr3N7unxIHJ n77Kv2wcC7h6x6OYKuu7EeGYq/8eBMcprOY+pDuRyXANTTPBET8tM5WLtOJQ8YipoMem GcQQ== X-Gm-Message-State: AOJu0YzDRSIcUL6E3XLDqz7x3QHssmyP+tmLSNZYBDWtuh6Jpodf2Dfa 2zdVOXJaJZvvv5LLfZ0yaopNkBOEsdJyBYh4D4e8JxkR3UYgV78TkgxRzfmJWxu12RgLjb43Vf7 NRNjxXG8CrKjn3sISeNtA1yd23h6Eo00f X-Gm-Gg: ASbGncuRD+NjDqdBPSBUpUSRDQhR8cVZTMnOEXVZtpl1n/T/bQzJAc9zRC1Wu+j+/ru +sZ6ANk2bBkiMbeMiYfVjw2z69JQfCk2NZ7aHv+Q6XfnoKnkDp6D3m04Ws8XW8LRn68eiEfe+Fh LH5GEEgyT56q88nTQxJ+O6owG+yrdq6v4xgXPN5Y+SLMClmeP2Fx2f94iDqNkm X-Google-Smtp-Source: AGHT+IEsYF046HI2lvka6jzvoSkxjxtQVQhCMPxIjOlUF1fROiJT7fToJsyR8HgPUuc6wJgd/wyjwbY3V2GbUwmhE6U= X-Received: by 2002:a05:6512:b14:b0:549:54f7:e54 with SMTP id 2adb3069b0e04-54b1115f34amr3199351e87.50.1743452892523; Mon, 31 Mar 2025 13:28:12 -0700 (PDT) MIME-Version: 1.0 References: <01020195b987abd3-a008b77d-8c63-4931-80a4-be36a351c8b2-000000@eu-west-1.amazonses.com> <01020195ddb28a27-4a576e04-8cd7-4a0b-abc7-acb901700ee7-000000@eu-west-1.amazonses.com> In-Reply-To: <01020195ddb28a27-4a576e04-8cd7-4a0b-abc7-acb901700ee7-000000@eu-west-1.amazonses.com> From: David Rowley Date: Tue, 1 Apr 2025 09:28:00 +1300 X-Gm-Features: AQ5f1JoxST9X3zwIr9Q5JyggjK8RObWCo11Os1QZoOowuJwR_lih2JNsSGULDuQ Message-ID: Subject: Re: Querying one partition in a function takes locks on all partitions To: Evgeny Morozov Cc: PostgreSQL General Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 29 Mar 2025 at 06:00, Evgeny Morozov wrote: > > On 23/03/2025 2:35 pm, David Rowley wrote: > >> alter table entity_2 add column new_column text; > > Is this just an example command? You can't add a column to a > > partition directly. > > Yes, it was just the simplest way I could think of to take an exclusive > lock. But on this note: I guess it takes a lock before checking that the > table is partition because it might otherwise get attached/detached as a > partition between doing the check and taking a lock, but it would be > nice if it took a shared lock to do all checks, then upgraded to an > exclusive lock to actually do the work. Then the user would not have to > wait for an exclusive lock only for the command to fail. Unfortunately, that's probably just swapping one problem for another. Once you have 2 sessions following such a pattern of locking, you're prone to unnecessary deadlocking. For example: -- session1 begin; lock table t in access share mode; -- gets lock -- session2 begin; lock table t in access share mode; -- gets lock lock table t in access exclusive mode; -- waits -- session1 lock table t in access exclusive mode; -- deadlock If you don't bother with the access share lock, there's no deadlock. David