Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nL7Zq-0006o1-Gs for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Feb 2022 18:01:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nL7Zp-0003U4-6C for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Feb 2022 18:01:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nL7Zo-0003Tu-Pw for pgsql-hackers@lists.postgresql.org; Fri, 18 Feb 2022 18:01:52 +0000 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nL7Zl-0000GC-GM for pgsql-hackers@postgresql.org; Fri, 18 Feb 2022 18:01:52 +0000 Received: by mail-pg1-x535.google.com with SMTP id 75so8517846pgb.4 for ; Fri, 18 Feb 2022 10:01:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=6RP6z5wVXUxijs3ivEpIFcZI9AD8R2ZcOLLfFoJsnaE=; b=ly3F5H9hdnJRMCmT2jLELr0gj4Gg+FtMnehv7cSr+I4Ujwx8xWr5QAzm4WWjhXCrG2 /jzPSE2uoQF7NXtvQmAf27ypKHIchCmAcBlTiAjkgwNXPFYGjXydFoNUqYn8Vniu79tk WskbsVjBGgwVm9vbX4CCz9l+5bKbUtSAyBqihHwlp2LW29EP4763O8gCtyhGpXVwDkF7 jy2b18LNqda/pVvUC+xqGoM0e/OZ14KvW6uempB/PHutsb7VvYx5O6zIfT9cvPMSGO/m VxNkXHKWjMvVx6aL+ZviyCt+qdI+q181TVZOmG54DhBQhuUnpYVEw/HRdaUvNiBA5+IA m6Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6RP6z5wVXUxijs3ivEpIFcZI9AD8R2ZcOLLfFoJsnaE=; b=JCK6sHLamze4lmcCennObZEWHSf54O47PiJkZfSwbnmSuwk3zyO93Z6yIqNVwrIQgm vtAV7wn5/ifMQhZgrjtsWqj8LXJ8o0nAVkzlFfUnuzD0iVtTLLcMX70AVLJ2NK/sc9Im GQwG9GbTdCAFKJAKGhel+uWljkEIr/x+U+ugLWygzuHdYUdq6AlRbwJmNSY0THRuDEdh dko2QiiZ5q0u3UFQMN1y3zVs1Vl/SkFzaEi+yL45rBtSDZkPOqJO059s1TPXzRzChpmp bG6CIURKyaizu1X32lzAhw8rV8q+261BaFsUVT1m5Ka/81LbTudvx+93yKw3fF2QqEn6 FrhA== X-Gm-Message-State: AOAM531ag5eHtqH1NGPxmPKiXXHZE87eL+LE4qpL9oylJEdQWhKk2tD6 bFo1zgz0gL64Kl0nkJayDBQ= X-Google-Smtp-Source: ABdhPJw+BMCGZCShEO7LxwuKLJtO6QAXnwx9de6rAUIWV/w7u74CBRaGwZUamCUur5HNnD3nvukZSA== X-Received: by 2002:a63:4b09:0:b0:372:c793:ab50 with SMTP id y9-20020a634b09000000b00372c793ab50mr7075935pga.495.1645207306798; Fri, 18 Feb 2022 10:01:46 -0800 (PST) Received: from nathanxps13 ([50.54.155.70]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm78258pju.8.2022.02.18.10.01.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 10:01:45 -0800 (PST) Date: Fri, 18 Feb 2022 10:01:44 -0800 From: Nathan Bossart To: Andres Freund Cc: Thomas Munro , pgsql-hackers Subject: Re: USE_BARRIER_SMGRRELEASE on Linux? Message-ID: <20220218180144.GA3446294@nathanxps13> References: <20220216045911.GA2791824@nathanxps13> <20220216050645.7u3nguccykpqaw7p@alap3.anarazel.de> <20220216061404.GA2813338@nathanxps13> <20220216065732.GA2932808@nathanxps13> <20220216164442.GA2935315@nathanxps13> <20220216173721.GA3007497@nathanxps13> <20220216192731.txn3vnlooa2bhdsm@alap3.anarazel.de> <20220216210053.GA3031150@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ikeVEW9yuYc//A+q" Content-Disposition: inline In-Reply-To: <20220216210053.GA3031150@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ikeVEW9yuYc//A+q Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Feb 16, 2022 at 01:00:53PM -0800, Nathan Bossart wrote: > On Wed, Feb 16, 2022 at 11:27:31AM -0800, Andres Freund wrote: >> That doesn't strike me as great architecturally. E.g. in theory the same >> problem could exist in single user mode. I think it doesn't today, because >> RegisterSyncRequest() will effectively "absorb" it immediately, but that kind >> of feels like an implementation detail? > > Yeah, maybe that is a reason to add an absorb somewhere within > CreateCheckPoint() instead, like v1 [0] does. Then the extra absorb would > be sufficient for single-user mode if the requests were not absorbed > immediately. Here is a new patch. This is essentially the same as v1, but I've spruced up the comments and the commit message. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --ikeVEW9yuYc//A+q Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Fix-race-condition-between-DROP-TABLESPACE-and-ch.patch" From adf7f7c9717897cc72a3c76d90ad0db082a7a432 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 15 Feb 2022 22:53:04 -0800 Subject: [PATCH v3 1/1] Fix race condition between DROP TABLESPACE and checkpointing. Commands like ALTER TABLE SET TABLESPACE may leave files for the next checkpoint to clean up. If such files are not removed by the time DROP TABLESPACE is called, we request a checkpoint so that they are deleted. However, there is presently a window before checkpoint start where new unlink requests won't be scheduled until the following checkpoint. This means that the checkpoint forced by DROP TABLESPACE might not remove the files we expect it to remove, and the following ERROR will be emitted: ERROR: tablespace "mytblspc" is not empty To fix, add a call to AbsorbSyncRequests() just before advancing the unlink cycle counter. This ensures that any unlink requests forwarded prior to checkpoint start (i.e., when ckpt_started is incremented) will be processed by the current checkpoint. Since AbsorbSyncRequests() performs memory allocations, it cannot be called within a critical section, so we also need to move SyncPreCheckpoint() to before CreateCheckPoint()'s critical section. This is an old bug, so back-patch to all supported versions. Author: Nathan Bossart Reported-by: Nathan Bossart Diagnosed-by: Thomas Munro, Nathan Bossart Reviewed-by: Andres Freund Discussion: https://postgr.es/m/20220215235845.GA2665318%40nathanxps13 --- src/backend/access/transam/xlog.c | 15 ++++++++------- src/backend/storage/sync/sync.c | 14 +++++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index ce78ac413e..174aab5ae4 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -6294,6 +6294,14 @@ CreateCheckPoint(int flags) MemSet(&CheckpointStats, 0, sizeof(CheckpointStats)); CheckpointStats.ckpt_start_t = GetCurrentTimestamp(); + /* + * Let smgr prepare for checkpoint; this has to happen outside the critical + * section and before we determine the REDO pointer. Note that smgr must + * not do anything that'd have to be undone if we decide no checkpoint is + * needed. + */ + SyncPreCheckpoint(); + /* * Use a critical section to force system panic if we have trouble. */ @@ -6307,13 +6315,6 @@ CreateCheckPoint(int flags) LWLockRelease(ControlFileLock); } - /* - * Let smgr prepare for checkpoint; this has to happen before we determine - * the REDO pointer. Note that smgr must not do anything that'd have to - * be undone if we decide no checkpoint is needed. - */ - SyncPreCheckpoint(); - /* Begin filling in the checkpoint WAL record */ MemSet(&checkPoint, 0, sizeof(checkPoint)); checkPoint.time = (pg_time_t) time(NULL); diff --git a/src/backend/storage/sync/sync.c b/src/backend/storage/sync/sync.c index e161d57761..f16b25faa1 100644 --- a/src/backend/storage/sync/sync.c +++ b/src/backend/storage/sync/sync.c @@ -173,7 +173,9 @@ InitSync(void) * counter is incremented here. * * This must be called *before* the checkpoint REDO point is determined. - * That ensures that we won't delete files too soon. + * That ensures that we won't delete files too soon. Since this calls + * AbsorbSyncRequests(), which performs memory allocations, it cannot be + * called within a critical section. * * Note that we can't do anything here that depends on the assumption * that the checkpoint will be completed. @@ -181,6 +183,16 @@ InitSync(void) void SyncPreCheckpoint(void) { + /* + * Operations such as DROP TABLESPACE assume that the next checkpoint will + * process all recently forwarded unlink requests, but if they aren't + * absorbed prior to advancing the cycle counter, they won't be processed + * until a future checkpoint. The following absorb ensures that any unlink + * requests forwarded before the checkpoint began will be processed in the + * current checkpoint. + */ + AbsorbSyncRequests(); + /* * Any unlink requests arriving after this point will be assigned the next * cycle counter, and won't be unlinked until next checkpoint. -- 2.25.1 --ikeVEW9yuYc//A+q--