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 1rN9ai-009gLk-3n for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Jan 2024 10:44: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 1rN9af-00BoEE-Vz for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Jan 2024 10:44:13 +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 1rN9ae-00BoE6-Ca for pgsql-hackers@lists.postgresql.org; Tue, 09 Jan 2024 10:44:13 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rN9aW-000dKk-81 for pgsql-hackers@postgresql.org; Tue, 09 Jan 2024 10:44:11 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 96FA43200AB9; Tue, 9 Jan 2024 05:43:59 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Tue, 09 Jan 2024 05:44:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1704797039; x= 1704883439; bh=++SVrjzaz0uWeSz0szrqM+KrC+In7lKW57Tdh0V844U=; b=y U9CiDWrYLCuW9X+ZGfLTjKnFXef3mOe9T+YcHmmiCYh00pIMNtZwihGADhnKBfOq QOeAQR4Q9Oaqd6HbAJVChYX7rQzaQ8cPihej7zHKitF9/UykW2mICcV9g4wmeB5T sTZkzoL9hijzBQl5/tXjaDlwjdxNuQTMc/of1UIsB0IGsrkaTTPdjsjtYMfHjF4p Mg/GIMDTvE8FZyl5Ywk5ndUYT4+1knG9UO6wGYOpO2kYJUQXkOVRJ+HRFfQsnX/a L1pn1rlrmFqiN0CgUPVtU5RLF8D8G+by8WgvTYDCHMQSIwk3Mb3IwacxoKAd2C5u foKNjOnUj2oimtvI7Ln/A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdehledgudelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesmhekreertddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepudelkeekgfdttdettdekgfduvdeiffelteegjeeihfet heekveeftedtuddvhfeunecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 9 Jan 2024 05:43:58 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1704797036; bh=uSHLmhXdzPsOO+BigMRO6ZR1yV3QSnhMqCrOR2sEo3w=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=oiwVFRy+P2WyLga6RWGzTiiU1y+9ijA3uurHE5zNIPnAi2N6cTPo5CvzOePs1MquS 7NTiILX/N6iLuR9As+2cziduRytSFVKWeSX6wGHIgvRn8u4aFi80/v08QPhjH6wDem k/X25DVy3aSCxg1kuZ0yNOhgyCKi4wk2VHTqpaHG6mj63oSz2Dk+kGwoKE6FK+ElDo ibHMK6/TjDbn4cJPBKQREvkaeaF4dsoG08Vy7XvpL3xYDgj+BxpRV2UArgwy25y++M nYuMN6g7b5TmjUFRZIWfo2VjK/LWqHAm3kf2Uot5SRx6NuOy8oLbvD6DhZoYVGxVrG WImgJ27DfswKw== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id B68DE9D; Tue, 9 Jan 2024 11:43:56 +0100 (CET) Date: Tue, 9 Jan 2024 11:43:56 +0100 From: Alvaro Herrera To: Tomas Vondra Cc: Alexander Lakhin , Soumyadeep Chakraborty , Matthias van de Meent , pgsql-hackers , Ashwin Agrawal Subject: Re: brininsert optimization opportunity Message-ID: <202401091043.e3nrqiad6gb7@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="lvz367sfohxlwjc7" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <202401081551.3olwx6pyw2mn@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --lvz367sfohxlwjc7 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2024-Jan-08, Alvaro Herrera wrote: > So I think we should do 0001 and perhaps some further tweaks to the > original brininsert optimization commit: [...] So I propose the attached patch, which should fix the reported bug and the things I mentioned above, and also the typos Alexander mentioned elsewhere in the thread. > Lastly, I kinda disagree with the notion that only some of the callers > of aminsert should call aminsertcleanup, even though btree doesn't have > an aminsertcleanup and thus it can't affect TOAST or catalogs. [...] I didn't do anything about this. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ --lvz367sfohxlwjc7 Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="v2-0001-call-cleanup-in-validate_index.patch" From 5eff6872a446c7a3e86607b5d73a18e1a95b7da7 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Tue, 12 Dec 2023 12:01:07 +0100 Subject: [PATCH v2] call cleanup in validate_index --- doc/src/sgml/indexam.sgml | 14 +++++++------- src/backend/access/brin/brin.c | 19 +++++++++++-------- src/backend/access/index/indexam.c | 5 ++--- src/backend/catalog/index.c | 2 ++ src/include/access/amapi.h | 2 +- src/include/access/brin_internal.h | 2 +- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/doc/src/sgml/indexam.sgml b/doc/src/sgml/indexam.sgml index cc4135e394..4817acd31f 100644 --- a/doc/src/sgml/indexam.sgml +++ b/doc/src/sgml/indexam.sgml @@ -367,21 +367,21 @@ aminsert (Relation indexRelation, within an SQL statement, it can allocate space in indexInfo->ii_Context and store a pointer to the data in indexInfo->ii_AmCache (which will be NULL - initially). After the index insertions complete, the memory will be freed - automatically. If additional cleanup is required (e.g. if the cache contains - buffers and tuple descriptors), the AM may define an optional function - indexinsertcleanup, called before the memory is released. + initially). If resources other than memory have to be released + after index insertions, aminsertcleanup may + be provided, which will be called before the memory is released. void -aminsertcleanup (IndexInfo *indexInfo); +aminsertcleanup (Relation indexRelation, + IndexInfo *indexInfo); Clean up state that was maintained across successive inserts in indexInfo->ii_AmCache. This is useful if the data - requires additional cleanup steps, and simply releasing the memory is not - sufficient. + requires additional cleanup steps (e.g., releasing pinned + buffers), and simply releasing the memory is not sufficient. diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c index 1087a9011e..6ac12b9ba8 100644 --- a/src/backend/access/brin/brin.c +++ b/src/backend/access/brin/brin.c @@ -222,7 +222,7 @@ static bool add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup, const Datum *values, const bool *nulls); static bool check_null_keys(BrinValues *bval, ScanKey *nullkeys, int nnullkeys); static void brin_fill_empty_ranges(BrinBuildState *state, - BlockNumber prevRange, BlockNumber maxRange); + BlockNumber prevRange, BlockNumber nextRange); /* parallel index builds */ static void _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, @@ -498,11 +498,14 @@ brininsert(Relation idxRel, Datum *values, bool *nulls, * Callback to clean up the BrinInsertState once all tuple inserts are done. */ void -brininsertcleanup(IndexInfo *indexInfo) +brininsertcleanup(Relation idx, IndexInfo *indexInfo) { - BrinInsertState *bistate = (BrinInsertState *) indexInfo->ii_AmCache; + BrinInsertState *bistate; - Assert(bistate); + if (indexInfo->ii_AmCache == NULL) + return; + + bistate = (BrinInsertState *) indexInfo->ii_AmCache; /* * Clean up the revmap. Note that the brinDesc has already been cleaned up @@ -1149,8 +1152,8 @@ brinbuild(Relation heap, Relation index, IndexInfo *indexInfo) * * XXX plan_create_index_workers makes the number of workers dependent on * maintenance_work_mem, requiring 32MB for each worker. That makes sense - * for btree, but not for BRIN, which can do away with much less memory. - * So maybe make that somehow less strict, optionally? + * for btree, but not for BRIN, which can do with much less memory. So + * maybe make that somehow less strict, optionally? */ if (indexInfo->ii_ParallelWorkers > 0) _brin_begin_parallel(state, heap, index, indexInfo->ii_Concurrent, @@ -2543,7 +2546,7 @@ _brin_end_parallel(BrinLeader *brinleader, BrinBuildState *state) /* * Initialize BrinMemTuple we'll use to union summaries from workers (in - * case they happened to produce parts of the same paga range). + * case they happened to produce parts of the same page range). */ memtuple = brin_new_memtuple(state->bs_bdesc); @@ -2867,7 +2870,7 @@ _brin_parallel_build_main(dsm_segment *seg, shm_toc *toc) * specified block number. The empty tuple is initialized only once, when it's * needed for the first time, stored in the memory context bs_context to ensure * proper life span, and reused on following calls. All empty tuples are - * exactly the same except for the bs_blkno field, which is set to the value + * exactly the same except for the bt_blkno field, which is set to the value * in blkno parameter. */ static void diff --git a/src/backend/access/index/indexam.c b/src/backend/access/index/indexam.c index 63dff101e2..ff84bf0fa0 100644 --- a/src/backend/access/index/indexam.c +++ b/src/backend/access/index/indexam.c @@ -205,10 +205,9 @@ index_insert_cleanup(Relation indexRelation, IndexInfo *indexInfo) { RELATION_CHECKS; - Assert(indexInfo); - if (indexRelation->rd_indam->aminsertcleanup && indexInfo->ii_AmCache) - indexRelation->rd_indam->aminsertcleanup(indexInfo); + if (indexRelation->rd_indam->aminsertcleanup) + indexRelation->rd_indam->aminsertcleanup(indexRelation, indexInfo); } /* diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 88f7994b5a..40ac683b35 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -3414,6 +3414,8 @@ validate_index(Oid heapId, Oid indexId, Snapshot snapshot) /* Done with tuplesort object */ tuplesort_end(state.tuplesort); + index_insert_cleanup(indexRelation, indexInfo); + elog(DEBUG2, "validate_index found %.0f heap tuples, %.0f index tuples; inserted %.0f missing tuples", state.htups, state.itups, state.tups_inserted); diff --git a/src/include/access/amapi.h b/src/include/access/amapi.h index 2c6c307efc..683ad13f07 100644 --- a/src/include/access/amapi.h +++ b/src/include/access/amapi.h @@ -114,7 +114,7 @@ typedef bool (*aminsert_function) (Relation indexRelation, struct IndexInfo *indexInfo); /* cleanup after insert */ -typedef void (*aminsertcleanup_function) (struct IndexInfo *indexInfo); +typedef void (*aminsertcleanup_function) (Relation indexRelation, struct IndexInfo *indexInfo); /* bulk delete */ typedef IndexBulkDeleteResult *(*ambulkdelete_function) (IndexVacuumInfo *info, diff --git a/src/include/access/brin_internal.h b/src/include/access/brin_internal.h index 1c7eabe604..a5a9772621 100644 --- a/src/include/access/brin_internal.h +++ b/src/include/access/brin_internal.h @@ -96,7 +96,7 @@ extern bool brininsert(Relation idxRel, Datum *values, bool *nulls, IndexUniqueCheck checkUnique, bool indexUnchanged, struct IndexInfo *indexInfo); -extern void brininsertcleanup(struct IndexInfo *indexInfo); +extern void brininsertcleanup(Relation index, struct IndexInfo *indexInfo); extern IndexScanDesc brinbeginscan(Relation r, int nkeys, int norderbys); extern int64 bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm); extern void brinrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, -- 2.39.2 --lvz367sfohxlwjc7--