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 1ruvZB-008ZJz-9S for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Apr 2024 14:38:17 +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 1ruvZA-001RLo-7P for pgsql-hackers@arkaria.postgresql.org; Thu, 11 Apr 2024 14:38: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 1ruvZ9-001RLZ-Tm for pgsql-hackers@lists.postgresql.org; Thu, 11 Apr 2024 14:38:15 +0000 Received: from mail-il1-x12c.google.com ([2607:f8b0:4864:20::12c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ruvZ5-000O51-Mi for pgsql-hackers@postgresql.org; Thu, 11 Apr 2024 14:38:15 +0000 Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-36afb4b4a3eso4058365ab.3 for ; Thu, 11 Apr 2024 07:38:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712846289; x=1713451089; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=CE76BVJTAYtpGY+ZiY6OTDMqfA2Pbhm4esMXYbeKXiE=; b=Q4CygPojdTuq1mA4Txpr2eG6Dp6q0YFZTKcNb+nBilgvg0QouhfTIEdKh7IgDIpN9s ZFYLG3AjvMwcm9UURQRsKl0I11J+s19Tq5qSmlW3GZl93FEubDOUzXVz0nU9xEf+gAah 91I5/kTws1dkcgN0vYKqwlY/8gU7qR9nORCQh2NrqGq7Mh9LjwdI3SxEw6NKOpHX9zjt bAPVZpA6jyJfgB8FKWT8GJQH1od/y+f7FFO50Mt7e5PZdS01O2fMRWr6eII+uKiUDG6d d9dWqQeoNuZZ77bDxEBskqdapdIuqDin2pr2S1iXF6TdU7L/jv/KBqrpFixplmy7DKTD jr+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846289; x=1713451089; h=in-reply-to: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=CE76BVJTAYtpGY+ZiY6OTDMqfA2Pbhm4esMXYbeKXiE=; b=pMcI6yjLnrKFDtOMLSQix99GD2z1bYOCtUgYUGc3okxjegKnBfsyeYsn2tpzSUYXIN m83BA7B4QoESETqZDivZe9vnYugqjg0BI+kK1i2LpXNpnch30TvMK/oYtrk4+f2oYm2J AM7e9GWGJP/W1DkDgMDi8ENwwGiwdZs15m55pW5VUC768BjS4S0EBoEQiPeYyRbYM4Ro NVxX4cKvQq1Sszrl9nVRGBbojnKUVBywMlKqaDu11T4mitd9dned10BKWvPKIOAA+osR QV9q5/5O3A8y63SyVJYJHeeWDmap3ZOCGhA9a/0caVJVZJAI499ybwsqfXJYlKIcktE6 dqRg== X-Forwarded-Encrypted: i=1; AJvYcCWteXuZozWBG4mrao7aMrPndboNh14ySj6ptlYwNsyZmrX2gnUg907ffBQPFOg29efZmlOI8ofwqdiF2dLY8lrMGJONootymuYunEIN X-Gm-Message-State: AOJu0Ywy1MSbcvwVkxaY14GdA5F4OyKuWONQFeiy57LtIJ9EI4u0mQGT ef+fbxNNLFE8KhhCGCC8YWM8mkPuUsG3dDoQZCsuy+CUdKobTUIA X-Google-Smtp-Source: AGHT+IFO6zH9n8SdnFARanMMAjseufGKXzAkOKaEapDoRv8Fml0knkhAgnt7wxY/FBBE9SfM4j7p/w== X-Received: by 2002:a05:6e02:1a24:b0:36a:217b:7787 with SMTP id g4-20020a056e021a2400b0036a217b7787mr6388481ile.16.1712846289496; Thu, 11 Apr 2024 07:38:09 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id g5-20020a92d7c5000000b0036afcb93409sm399603ilq.76.2024.04.11.07.38.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:38:08 -0700 (PDT) Date: Thu, 11 Apr 2024 09:38:07 -0500 From: Nathan Bossart To: Kirill Reshke Cc: Michael Paquier , "Leung, Anthony" , "Andrey M. Borodin" , "pgsql-hackers@postgresql.org" Subject: Re: Allow non-superuser to cancel superuser tasks. Message-ID: <20240411143807.GC1882158@nathanxps13> References: <20240406010751.GA167028@nathanxps13> <20240410150034.GA1673069@nathanxps13> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Apr 11, 2024 at 04:55:59PM +0500, Kirill Reshke wrote: >> It's feeling more natural here to check that we have a superuser-owned >> backend first, and then do a lookup of the process type. > >> I figured since there's no reason to rely on that behavior, we might as >> well do a bit of future-proofing in case autovacuum workers are ever not >> run as InvalidOid. > > I have combined them into this: > > if ((!OidIsValid(proc->roleId) || superuser_arg(proc->roleId)) > && pgstat_get_backend_type(GetNumberFromPGProc(proc)) == B_AUTOVAC_WORKER) > > This is both future-proofing and natural, I suppose. Downside of this > is double checking condition (!OidIsValid(proc->roleId) || > superuser_arg(proc->roleId)), but i think that is ok for the sake of > simplicity. If we want to retain the check, IMO we might as well combine the first two blocks like Anthony proposed: if (!OidIsValid(proc->roleId) || superuser_arg(proc->roleId)) { ProcNumber procNumber = GetNumberFromPGProc(proc); PGBackendStatus procStatus = pgstat_get_beentry_by_proc_number(procNumber); if (procStatus && procStatus->st_backendType == B_AUTOVAC_WORKER && !has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_AUTOVAC_WORKER)) return SIGNAL_BACKEND_NOAUTOVAC; else if (!superuser()) return SIGNAL_BACKEND_NOSUPERUSER; } + + pg_signal_autovacuum_worker + Allow signaling autovacuum worker backend to cancel or terminate + I think we need to be more specific about what pg_cancel_backend() and pg_terminate_backend() do for autovacuum workers. The code offers some clues: /* * SIGINT is used to signal canceling the current table's vacuum; SIGTERM * means abort and exit cleanly, and SIGQUIT means abandon ship. */ pqsignal(SIGINT, StatementCancelHandler); pqsignal(SIGTERM, die); +/* ---------- + * pgstat_get_backend_type() - + * + * Return the backend type of the backend for the given proc number. + * ---------- + */ +BackendType +pgstat_get_backend_type(ProcNumber procNumber) +{ + PgBackendStatus *ret; + + ret = pgstat_get_beentry_by_proc_number(procNumber); + + if (!ret) + return B_INVALID; + + return ret->st_backendType; +} I'm not sure we really need to introduce a new function for this. I avoided using it in my example snippet above. But, maybe it'll come in handy down the road... -- Nathan Bossart Amazon Web Services: https://aws.amazon.com