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 1sOO1G-00Dv2w-BN for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Jul 2024 20:53: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 1sOO1E-004GLq-1L for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Jul 2024 20:53:00 +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 1sOO1C-004GLh-1m for pgsql-hackers@lists.postgresql.org; Mon, 01 Jul 2024 20:53:00 +0000 Received: from mail-yb1-xb35.google.com ([2607:f8b0:4864:20::b35]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sOO19-004YYp-Mz for pgsql-hackers@postgresql.org; Mon, 01 Jul 2024 20:52:57 +0000 Received: by mail-yb1-xb35.google.com with SMTP id 3f1490d57ef6-e0354459844so3264903276.3 for ; Mon, 01 Jul 2024 13:52:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1719867173; x=1720471973; darn=postgresql.org; h=user-agent: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=/BvUsyLWlCx5XUgtAooErc/KR45i3MWZxAHrENFK1UQ=; b=D4+ZyUUrbp2d3WpEvNB88dVvWlrDxFYESky3CVIvcXq7ZyM4IjxRPGAKY0keeJww49 VIPALTbYFkprxhOaduhx69jPiiojPN2W9gyHopFQEvihFfLvsljQkWwC51KaU36gBNn2 QkQdmT1Y+zItyCCoS0DMP2GMdBpfgmQDY/q+A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719867173; x=1720471973; h=user-agent: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=/BvUsyLWlCx5XUgtAooErc/KR45i3MWZxAHrENFK1UQ=; b=QCs+0zOVNilyf/XqOvTi+0XAyzQrQYhW0MUnY39D0YkiSxvVwizh1P2jAu/pDMXdAt jfpMhwpUXSlx9/kOJskblcDGmGB2f+cHR6ynaS570QQciGPq5CTjB7U9SnK7F40h7xT3 bl1iW2kQQDysOw9kD7AKK/KwtqamM+aGSr/Fq8ZeRiXgA47FhiHo9tFd4RBb0Ttod7K5 gzFAsaqIqZDEfEG3LMwZcxiNsYfZ3QQyvkvCnCZPzyVTRd8qxteOqhP0C5fxOPrNvUPB 7viRn944jUI5S+DnFqEA/q70VQ0h1QXWuuALDW5/mYDjF5lQCGUy8qjn/uYmdNjPOXST 5EWg== X-Forwarded-Encrypted: i=1; AJvYcCXVgDTGjzIdNWhmB+NkU78CAabicVtQTMHI+3Rk66xYRLLYHeIEU4vyeOtwt/UPZURDpnpJuvZhYqY+XO3UA5b53e3xMZIL0kq6UYVf X-Gm-Message-State: AOJu0Yx2wunJosDnzNUdi0812M95sFLCB4ZqU8pjwkiiMhihYYdbeDEY V992caRb0/HfrjG8rjn14aWgsh9OwX9fDjYki/gLEKnAIVDusfYx9DOX4Ew1Lw== X-Google-Smtp-Source: AGHT+IE8+F6RgvckY6vOhESo6QKR8ddI48G3iGVg2ky+KuaNLNM+UobN7U4Cgl+LPTjpG9XE/276jQ== X-Received: by 2002:a5b:70f:0:b0:e03:4648:5248 with SMTP id 3f1490d57ef6-e036eb779e2mr7412098276.31.1719867173488; Mon, 01 Jul 2024 13:52:53 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e035fef9af1sm1204342276.3.2024.07.01.13.52.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Jul 2024 13:52:52 -0700 (PDT) Date: Mon, 1 Jul 2024 13:52:50 -0700 From: Noah Misch To: Heikki Linnakangas Cc: Peter Smith , Robert Haas , vignesh C , Andres Freund , pgsql-hackers , Melanie Plageman Subject: Re: Relation bulk write facility Message-ID: <20240701205250.62.nmisch@google.com> References: <30e8f366-58b3-b239-c521-422122dd5150@iki.fi> <20231119000416.fuyrbo6av7dytmf6@awork3.anarazel.de> <93e91b02-e07f-4205-8de6-3942b91760cb@iki.fi> <98832b4f-441f-4691-b9d4-9725eea8aed9@iki.fi> <6db39a5e-67ab-495d-9dfb-38b596df0d58@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 On Fri, Feb 23, 2024 at 04:27:34PM +0200, Heikki Linnakangas wrote: > Committed this. Thanks everyone! Commit 8af2565 wrote: > --- /dev/null > +++ b/src/backend/storage/smgr/bulk_write.c > +/* > + * Finish bulk write operation. > + * > + * This WAL-logs and flushes any remaining pending writes to disk, and fsyncs > + * the relation if needed. > + */ > +void > +smgr_bulk_finish(BulkWriteState *bulkstate) > +{ > + /* WAL-log and flush any remaining pages */ > + smgr_bulk_flush(bulkstate); > + > + /* > + * When we wrote out the pages, we passed skipFsync=true to avoid the > + * overhead of registering all the writes with the checkpointer. Register > + * the whole relation now. > + * > + * There is one hole in that idea: If a checkpoint occurred while we were > + * writing the pages, it already missed fsyncing the pages we had written > + * before the checkpoint started. A crash later on would replay the WAL > + * starting from the checkpoint, therefore it wouldn't replay our earlier > + * WAL records. So if a checkpoint started after the bulk write, fsync > + * the files now. > + */ > + if (!SmgrIsTemp(bulkstate->smgr)) > + { Shouldn't this be "if (bulkstate->use_wal)"? The GetRedoRecPtr()-based decision is irrelevant to the !wal case. Either we don't need fsync at all (TEMP or UNLOGGED) or smgrDoPendingSyncs() will do it (wal_level=minimal). I don't see any functional problem, but this likely arranges for an unnecessary sync when a checkpoint starts between mdcreate() and here. (The mdcreate() sync may also be unnecessary, but that's longstanding.) > + /* > + * Prevent a checkpoint from starting between the GetRedoRecPtr() and > + * smgrregistersync() calls. > + */ > + Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0); > + MyProc->delayChkptFlags |= DELAY_CHKPT_START; > + > + if (bulkstate->start_RedoRecPtr != GetRedoRecPtr()) > + { > + /* > + * A checkpoint occurred and it didn't know about our writes, so > + * fsync() the relation ourselves. > + */ > + MyProc->delayChkptFlags &= ~DELAY_CHKPT_START; > + smgrimmedsync(bulkstate->smgr, bulkstate->forknum); > + elog(DEBUG1, "flushed relation because a checkpoint occurred concurrently"); > + } > + else > + { > + smgrregistersync(bulkstate->smgr, bulkstate->forknum); > + MyProc->delayChkptFlags &= ~DELAY_CHKPT_START; > + } > + } > +} This is an elegant optimization.