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 1s1YXe-00D7AI-Nx for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Apr 2024 21:28:06 +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 1s1YXb-0029X8-Ka for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Apr 2024 21:28:04 +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 1s1YXb-0029Wr-6d for pgsql-hackers@lists.postgresql.org; Mon, 29 Apr 2024 21:28:04 +0000 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s1YXZ-000fMG-81 for pgsql-hackers@lists.postgresql.org; Mon, 29 Apr 2024 21:28:02 +0000 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-617f8a59a24so45016967b3.1 for ; Mon, 29 Apr 2024 14:28:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1714426080; x=1715030880; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=vtQGjLzhGNFbULPexxtDWuA5kWfuyoAVBT/q+kASUKY=; b=Nev5NrfGQ6AVQN00QHuvt8j6ZQCBGXKv4wl/zVI1xbNBqGwyFd8Esoy6BgRjkLrlor 7mlLqflGsFvR2EJ0whgAerP7eri0iTMDRzsrAfeZr/lJcHmCU53J78oMBae3eAEbgUXs T7iPd5eEwg2J3jja6H5svl8CVZg3+s/I3nIzs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714426080; x=1715030880; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vtQGjLzhGNFbULPexxtDWuA5kWfuyoAVBT/q+kASUKY=; b=hXdeugb9ryZ4oEnk+o8WHugtQWJOhi0yq6noH4EIT5H5y4lVR6n8PC4iukBI3b5ZCe uEjkp+klkIy+asq6H/f4gWDkf2wPiO8qddWrFxZMTqH3fmdzK73o3cfsUWj+7PRw5KwX BtP4OvYUqr2nmB6PZ4wgIfFflCIKBlPcDZvs2Bj7y1x8PWU1tiYG3XsIPY9m15LlLmFS OfLW87lDFhFYGEtZpUkmmHQYIUg7RbcLup4hrIH3/5VAsi4vkfQrMBauc0jGsrGi0Uzy PaE0TPQAEy39IHtp1qXWJH6+jtBkRaXpV82watk0sStd4n+V4PXmdvOrRGK7JkdkFvKe 39Aw== X-Forwarded-Encrypted: i=1; AJvYcCXBvFq1U+41NVxZd2i3W1pi0JRVnVusuB8rsJBjQs/8Nwg9MtZnj4eEiKacLQw/ylNE0NmOZEqoG5HASGa4IA03nw8cYqOSPDk5BrVuFENtQ5vz X-Gm-Message-State: AOJu0YwuxtN0w2HWeuy4bDgB7VFEJ2PtTpF7KNZO3aBDcSvn4xdGVLnz llBCnTMdSjH7u57q1XkLD3JUebBui6bJIWGNfAoScryHPdRHOwKCJdMrIKy5UQ== X-Google-Smtp-Source: AGHT+IFQ5R+cL3U8kU8koybo+edaFxj8PcX96iX+VpwIM1WahL4L6FXvKi38/tt9/iiJ2iCn36rG1g== X-Received: by 2002:a05:690c:6910:b0:615:10f8:124a with SMTP id if16-20020a05690c691000b0061510f8124amr11864162ywb.29.1714426079774; Mon, 29 Apr 2024 14:27:59 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id k19-20020a81ac13000000b0061be678259bsm174020ywh.36.2024.04.29.14.27.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Apr 2024 14:27:59 -0700 (PDT) Date: Mon, 29 Apr 2024 14:27:56 -0700 From: Noah Misch To: Amit Kapila Cc: Kirill Reshke , PostgreSQL Hackers Subject: Re: TerminateOtherDBBackends code comments inconsistency. Message-ID: <20240429212756.60.nmisch@google.com> References: <20240422162639.68@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8rvLfbG1D9Wg8g7g" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --8rvLfbG1D9Wg8g7g Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Mon, Apr 29, 2024 at 10:18:35AM +0530, Amit Kapila wrote: > On Mon, Apr 22, 2024 at 9:56 PM Noah Misch wrote: > > > > On Mon, Apr 15, 2024 at 11:17:54AM +0530, Amit Kapila wrote: > > > On Thu, Apr 11, 2024 at 6:58 PM Kirill Reshke wrote: > > > > > > > > While working on [0] i have noticed this comment in > > > > TerminateOtherDBBackends function: > > > > > > > > /* > > > > * Check whether we have the necessary rights to terminate other > > > > * sessions. We don't terminate any session until we ensure that we > > > > * have rights on all the sessions to be terminated. These checks are > > > > * the same as we do in pg_terminate_backend. > > > > * > > > > * In this case we don't raise some warnings - like "PID %d is not a > > > > * PostgreSQL server process", because for us already finished session > > > > * is not a problem. > > > > */ > > > > > > > > This statement is not true after 3a9b18b. > > > > "These checks are the same as we do in pg_terminate_backend." > > > > The comment mismatch is a problem. Thanks for reporting it. The DROP > > DATABASE doc mimics the comment, using, "permissions are the same as with > > pg_terminate_backend". > > > > How about updating the comments as in the attached? I see that commit I think the rationale for the difference should appear, being non-obvious and debatable in this case. > 3a9b18b309 didn't change the docs of pg_terminate_backend and whatever > is mentioned w.r.t permissions in the doc of that function sounds > valid for drop database force to me. Do you have any specific proposal > in your mind? Something like the attached. One could argue the function should also check isBackgroundWorker and ignore even bgworkers that set proc->roleId, but I've not done that. --8rvLfbG1D9Wg8g7g Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="TerminateOtherDBBackends-doc-v2nm.patch" Author: Noah Misch Commit: Noah Misch diff --git a/doc/src/sgml/ref/drop_database.sgml b/doc/src/sgml/ref/drop_database.sgml index ff01450..9215b1a 100644 --- a/doc/src/sgml/ref/drop_database.sgml +++ b/doc/src/sgml/ref/drop_database.sgml @@ -79,12 +79,14 @@ DROP DATABASE [ IF EXISTS ] name [ It doesn't terminate if prepared transactions, active logical replication slots or subscriptions are present in the target database. + - This will fail if the current user has no permissions to terminate other - connections. Required permissions are the same as with - pg_terminate_backend, described in - . This will also fail if we - are not able to terminate connections. + This terminates background worker connections and connections that the + current user has permission to terminate + with pg_terminate_backend, described in + . If connections remain, this + command will fail. diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index 1a83c42..ab7651d 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -3808,8 +3808,8 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared) * The current backend is always ignored; it is caller's responsibility to * check whether the current backend uses the given DB, if it's important. * - * It doesn't allow to terminate the connections even if there is a one - * backend with the prepared transaction in the target database. + * If the target database has a prepared transaction or permissions checks + * fail for a connection, this fails without terminating anything. */ void TerminateOtherDBBackends(Oid databaseId) @@ -3854,14 +3854,19 @@ TerminateOtherDBBackends(Oid databaseId) ListCell *lc; /* - * Check whether we have the necessary rights to terminate other - * sessions. We don't terminate any session until we ensure that we - * have rights on all the sessions to be terminated. These checks are - * the same as we do in pg_terminate_backend. + * Permissions checks relax the pg_terminate_backend checks in two + * ways, both by omitting the !OidIsValid(proc->roleId) check: * - * In this case we don't raise some warnings - like "PID %d is not a - * PostgreSQL server process", because for us already finished session - * is not a problem. + * - Accept terminating autovacuum workers, since DROP DATABASE + * without FORCE terminates them. + * + * - Accept terminating bgworkers. For bgworker authors, it's + * convenient to be able to recommend FORCE if a worker is blocking + * DROP DATABASE unexpectedly. + * + * Unlike pg_terminate_backend, we don't raise some warnings - like + * "PID %d is not a PostgreSQL server process", because for us already + * finished session is not a problem. */ foreach(lc, pids) { @@ -3870,7 +3875,6 @@ TerminateOtherDBBackends(Oid databaseId) if (proc != NULL) { - /* Only allow superusers to signal superuser-owned backends. */ if (superuser_arg(proc->roleId) && !superuser()) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), @@ -3878,7 +3882,6 @@ TerminateOtherDBBackends(Oid databaseId) errdetail("Only roles with the %s attribute may terminate processes of roles with the %s attribute.", "SUPERUSER", "SUPERUSER"))); - /* Users can signal backends they have role membership in. */ if (!has_privs_of_role(GetUserId(), proc->roleId) && !has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_BACKEND)) ereport(ERROR, --8rvLfbG1D9Wg8g7g--