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 1rcz69-003GUU-HL for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Feb 2024 02:46:10 +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 1rcz67-007nyl-RX for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Feb 2024 02:46:08 +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 1rcz67-007nyd-J7 for pgsql-bugs@lists.postgresql.org; Thu, 22 Feb 2024 02:46:08 +0000 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rcz65-000Gd9-ER for pgsql-bugs@lists.postgresql.org; Thu, 22 Feb 2024 02:46:07 +0000 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6e4cf834113so5510b3a.3 for ; Wed, 21 Feb 2024 18:46:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708569963; x=1709174763; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:to:message-id:date:from:to:cc:subject:date :message-id:reply-to; bh=bVFulER07wmPz8eCeyXek3kJJgkeUIdeKayyoZmL4rM=; b=bJ6LgBHWC5wefDOY4KCfUTZEc7kH5fLFLWFJMpPuRVlCNPYLKO8LnepH0eOhZ1s0oX ekj9mLEqoE/ybPLou3EPd3LJ2VgjysMhsc95V5q8j3ARUtWtUbXddLoGSdb3R1a+zmiv V+cifbP+Mdg4MwpWAEcNBmG3A++2x34QFW/jtqSA6TovzMVW2WoNPs/wjxUtRyH1tDkw utEQjVO65077mZyS2r0+kD98vPOOxKPAG7xD/cIMc40Nb5mQ6vJ4idpt8oNJsbzLkHyr Vl9Ab822rNNYuWH+CSTTWIwjfjoohBIDcJeE46fLOsqyaSeuBZpO2Ndo5wlRF8rFwvPU vzsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708569963; x=1709174763; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:to:message-id:date:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=bVFulER07wmPz8eCeyXek3kJJgkeUIdeKayyoZmL4rM=; b=nlgXc1sfEaK2neyC/N5wbbwN9bzn5L5Ghfb4pAW/XxHS1OSiJFxfDteDSMA1OOlhWE nq1YEyvS/+xCGHrlC0A69HZq33mgyiFIekuLPc6pqJFFpJDzDY893emAmWWrom5ea4e7 zHklg06IdBQH4AeorpnXTJld7vBtmTO2dW1igxp3y3JtFEWUc6N3VMgxx3fY3EtpoJvG mf+QErwafS10TQoQtB7qF5t06YSDQFuftJOfSTwhBBuWnczOO9zhLFo0HcC0LNB0TlrJ Z8A7YO/HLyNKKMCu9z6SMUi2fHWF2XXC6y7nXpneK9fsz8AY0CfW41tQTDjCLt02/B08 0EOQ== X-Forwarded-Encrypted: i=1; AJvYcCVd7B8EXpZo8v0NOQepqm8zKqEFAnKOvVLZb99S3CuhnI6nkCVZ6OwmigB3jfVq2TSNPLi7MJluOSCWhZN5526GVgwtPXwJ0/QtiVobZDdy X-Gm-Message-State: AOJu0YwBQ1UBsZlwsUQYNQmh2o6Rt3t3wA4w/FqCJLivn8c8GqdhLjdi ZK22XDcMnIL3XCiY/j0BBErFOFL0XzrJzzjV8Vu9OsAJP9qqt01s8gAp4fUs X-Google-Smtp-Source: AGHT+IFcqkoreHEYF+HayQXpiz5j6tAXRW/Du3CgunopKRx2A/A92uKXadiiwsyMkJoG8HvPOACUuQ== X-Received: by 2002:a62:be08:0:b0:6e4:ce12:fa77 with SMTP id l8-20020a62be08000000b006e4ce12fa77mr218835pff.21.1708569963277; Wed, 21 Feb 2024 18:46:03 -0800 (PST) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id a24-20020a637f18000000b005dc27ff85c1sm9458114pgd.0.2024.02.21.18.46.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 18:46:02 -0800 (PST) Date: Thu, 22 Feb 2024 11:46:00 +0900 (JST) Message-Id: <20240222.114600.1019580904613326727.horikyota.ntt@gmail.com> To: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18354: Aborted transaction aborted during cleanup when temp_file_limit exceeded From: Kyotaro Horiguchi In-Reply-To: <18354-140864c09686b5a6@postgresql.org> References: <18354-140864c09686b5a6@postgresql.org> User-Agent: Mew version 6.8 on Emacs 27.2 Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Thu_Feb_22_11_46_00_2024_016)--" Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ----Next_Part(Thu_Feb_22_11_46_00_2024_016)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit At Wed, 21 Feb 2024 12:00:01 +0000, PG Bug reporting form wrote in > triggers two errors, a warning, and an assertion failure: > ERROR: temporary file size exceeds temp_file_limit (100kB) > WARNING: AbortTransaction while in ABORT state > ERROR: temporary file size exceeds temp_file_limit (100kB) > server closed the connection unexpectedly ... > For the second error: > 2024-02-21 11:40:07.001 UTC|law|regression|65d5e116.13cfcb|ERROR: temporary > file size exceeds temp_file_limit (100kB) > 2024-02-21 11:40:07.001 UTC|law|regression|65d5e116.13cfcb|BACKTRACE: > FileWrite at fd.c:2183:5 > BufFileDumpBuffer at buffile.c:537:18 > BufFileFlush at buffile.c:723:3 > BufFileClose at buffile.c:419:9 > tuplestore_end at tuplestore.c:459:5 > MemoryContextSwitchTo at palloc.h:142:23 > (inlined by) PortalDrop at portalmem.c:587:3 > AtCleanup_Portals at portalmem.c:907:3 Therefore, BufFileClose should not flush the content during error handling. In the first place, tuplestore doesn't need to flush the underlying files in _end and _clear. In this case, I would choose to change the general behavior of tuplestore. The attached PoC patch fixes the issue for me. It introduces a new "extended" function to control flushing, avoiding the addition of an unnatural parameter to BufFileClose. I suspect that it is usable in some other places, but I haven't checked that. regards. -- Kyotaro Horiguchi NTT Open Source Software Center ----Next_Part(Thu_Feb_22_11_46_00_2024_016)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="don_t_flush_tuplestore_at_end.diff" diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index 5315d8a714..9dae2e512f 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -410,11 +410,21 @@ BufFileExportFileSet(BufFile *file) */ void BufFileClose(BufFile *file) +{ + BufFileCloseExtended(file, true); +} + +/* + * Close a BufFile with specifying wheter the content is flushed out + */ +void +BufFileCloseExtended(BufFile *file, bool flush) { int i; - /* flush any unwritten data */ - BufFileFlush(file); + /* flush any unwritten data if requested */ + if (flush) + BufFileFlush(file); /* close and delete the underlying file(s) */ for (i = 0; i < file->numFiles; i++) FileClose(file->files[i]); diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c index 947a868e56..08c1bcced9 100644 --- a/src/backend/utils/sort/tuplestore.c +++ b/src/backend/utils/sort/tuplestore.c @@ -421,7 +421,7 @@ tuplestore_clear(Tuplestorestate *state) TSReadPointer *readptr; if (state->myfile) - BufFileClose(state->myfile); + BufFileCloseExtended(state->myfile, false); state->myfile = NULL; if (state->memtuples) { @@ -455,7 +455,7 @@ tuplestore_end(Tuplestorestate *state) int i; if (state->myfile) - BufFileClose(state->myfile); + BufFileCloseExtended(state->myfile, false); if (state->memtuples) { for (i = state->memtupdeleted; i < state->memtupcount; i++) diff --git a/src/include/storage/buffile.h b/src/include/storage/buffile.h index 5f6d7c8e3f..d4796f9cb4 100644 --- a/src/include/storage/buffile.h +++ b/src/include/storage/buffile.h @@ -38,6 +38,7 @@ typedef struct BufFile BufFile; extern BufFile *BufFileCreateTemp(bool interXact); extern void BufFileClose(BufFile *file); +extern void BufFileCloseExtended(BufFile *file, bool flush); extern pg_nodiscard size_t BufFileRead(BufFile *file, void *ptr, size_t size); extern void BufFileReadExact(BufFile *file, void *ptr, size_t size); extern size_t BufFileReadMaybeEOF(BufFile *file, void *ptr, size_t size, bool eofOK); ----Next_Part(Thu_Feb_22_11_46_00_2024_016)----