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 1omwHU-0000uj-Qh for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Oct 2022 12:10:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1omwHT-0008RB-P1 for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Oct 2022 12:10:11 +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 1omwEP-0002W5-MP for pgsql-hackers@lists.postgresql.org; Mon, 24 Oct 2022 12:07:03 +0000 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1omwEG-0004f3-Vo for pgsql-hackers@lists.postgresql.org; Mon, 24 Oct 2022 12:07:01 +0000 Received: by mail-wr1-x431.google.com with SMTP id z14so2287940wrn.7 for ; Mon, 24 Oct 2022 05:06:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=IYCQCmy/Q+1HbbbS/Xpqtey0VWR/aF3YVnUpGHppBuk=; b=fDu9d9vUpuaZWaNuMGz9J3ykDdk2DOXJ+zJN0HWo4eE4QHKMDHFyrT2QZ0UoSyPIFQ jL/zIVU1TSQNiUq6u849ILxuPVBufbJz7AK3+LI8OV4RHAwXO3aEScsC8c50Wrzg3QDY 4qzHpWoSacNJha0pKx98goBtaGkwnHJ9Xew/QwA3txNSITIC1HAa5iLxoUHI4yNreXob 7Hkd3PvAQajv1abKd5fkUSK+hIHd6KnQxWqtpjTZhjdh2D3U45gzGOoNa4A00yojiYdW t5G9W1twN+Xctu48a6r9WerHgWnPZhBIk85f0RjoJjJalKmJrmhrx5C4RGmIONqHNojC QJ6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IYCQCmy/Q+1HbbbS/Xpqtey0VWR/aF3YVnUpGHppBuk=; b=djil3H41e7II1XzzaNaq/et5wnuNUGDSgW5tJvu9jUpfG8e6wI5l0+RhaDw8FhB3EH sBQKkqUptnKA4gubx9Q5sxPS/GNA7zQbv4r3AEJYxtt1sfEILl2k5feQbmIyIEifMQNf YJVKMxwcAp2Jg9YKs3FWYxTrL81bs3aVmFZeidZOMKVSqyVGDyW12EjBntoDUGliToNU HjRTHTn32SN6WfgEHxdqoggcB66qE0hSBDD9sWlgc82hXuGsgue+G8FMC023R4Grr3jR EvAedlv7lmZ+M9M2eEjA3opQTS7LthYSjPoHqvGAAF7RfFDDlDR/Fj4LYHK9V3CNTxqJ YHOw== X-Gm-Message-State: ACrzQf0mq3LwRBtWQpWg10WwN+rNL33Hi4yF9co0Txigt/+0cs4//Wqx Oi7b5BJrcahMk7Xqxb2T7nnOR0fugqODLBgJw+rj3v7G8ZrHfe3/ytdpY7QQJF+anJ/o3YvuEDi RgQgWkzpnnoAq35UvO3pgaNc+4isMAhMEx8mfMX/qaowj3oGoOhgwuo6hL9upXy9Bm2M7cwC2Hz K1OAo7qrA4Elx9gbcjX6Lg78flEiWd4AtgL4C6KiReWlQLPrnfwb3xaSKrDXfJj7VOHzFMLd9bF W4DuWcYdmgtj+Iw8lQ0fb2cSbxfgeBrPgLmvVW+OENiES31MeuvUjMdX5uX X-Google-Smtp-Source: AMsMyM4i06XEC/aQr3b6BR+PxTRX5AuDu5aAW24GQjZ+NSurD43+77P3gRtbG+kvJCEb+YmtFVvSyg== X-Received: by 2002:a05:6000:4084:b0:22e:5b99:4c4a with SMTP id da4-20020a056000408400b0022e5b994c4amr22067634wrb.538.1666613211816; Mon, 24 Oct 2022 05:06:51 -0700 (PDT) Received: from [10.137.0.17] (cst-prg-6-62.cust.vodafone.cz. [46.135.6.62]) by smtp.gmail.com with ESMTPSA id l13-20020a7bc44d000000b003c6237e867esm10938653wmi.0.2022.10.24.05.06.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Oct 2022 05:06:50 -0700 (PDT) Message-ID: Date: Mon, 24 Oct 2022 14:06:50 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 Subject: Re: PATCH: Using BRIN indexes for sorted output To: Justin Pryzby Cc: pgsql-hackers@lists.postgresql.org, Matthias van de Meent References: <20221024043237.GA16921@telsasoft.com> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20221024043237.GA16921@telsasoft.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/24/22 06:32, Justin Pryzby wrote: > On Sat, Oct 15, 2022 at 02:33:50PM +0200, Tomas Vondra wrote: >> Of course, if there are e.g. BTREE indexes this is going to be slower, >> but people are unlikely to have both index types on the same column. > > On Sun, Oct 16, 2022 at 05:48:31PM +0200, Tomas Vondra wrote: >> I don't think it's all that unfair. How likely is it to have both a BRIN >> and btree index on the same column? And even if you do have such indexes > > Note that we (at my work) use unique, btree indexes on multiple columns > for INSERT ON CONFLICT into the most-recent tables: UNIQUE(a,b,c,...), > plus a separate set of indexes on all tables, used for searching: > BRIN(a) and BTREE(b). I'd hope that the costing is accurate enough to > prefer the btree index for searching the most-recent table, if that's > what's faster (for example, if columns b and c are specified). > Well, the costing is very crude at the moment - at the moment it's pretty much just a copy of the existing BRIN costing. And the cost is likely going to increase, because brinsort needs to do regular BRIN bitmap scan (more or less) and then also a sort (which is an extra cost, of course). So if it works now, I don't see why would brinsort break it. Moreover, if you don't have ORDER BY in the query, I don't see why would we create a brinsort at all. But if you could test this once the costing gets improved, that'd be very valuable. >> + /* There must not be any TID scan in progress yet. */ >> + Assert(node->ss.ss_currentScanDesc == NULL); >> + >> + /* Initialize the TID range scan, for the provided block range. */ >> + if (node->ss.ss_currentScanDesc == NULL) >> + { > > Why is this conditional on the condition that was just Assert()ed ? > Yeah, that's a mistake, due to how the code evolved. >> >> +void >> +cost_brinsort(BrinSortPath *path, PlannerInfo *root, double loop_count, >> + bool partial_path) > > It's be nice to refactor existing code to avoid this part being so > duplicitive. > >> + * In some situations (particularly with OR'd index conditions) we may >> + * have scan_clauses that are not equal to, but are logically implied by, >> + * the index quals; so we also try a predicate_implied_by() check to see > > Isn't that somewhat expensive ? > > If that's known, then it'd be good to say that in the documentation. > Some of this is probably a residue from create_indexscan_path and may not be needed for this new node. >> + { >> + {"enable_brinsort", PGC_USERSET, QUERY_TUNING_METHOD, >> + gettext_noop("Enables the planner's use of BRIN sort plans."), >> + NULL, >> + GUC_EXPLAIN >> + }, >> + &enable_brinsort, >> + false, > > I think new GUCs should be enabled during patch development. > Maybe in a separate 0002 patch "for CI only not for commit". > That way "make check" at least has a chance to hit that new code paths. > > Also, note that indxpath.c had the var initialized to true. > Good point. >> + attno = (i + 1); >> + nranges = (nblocks / pagesPerRange); >> + node->bs_phase = (nullsFirst) ? BRINSORT_LOAD_NULLS : BRINSORT_LOAD_RANGE; > > I'm curious why you have parenthesis these places ? > Not sure, it seemed more readable when writing the code I guess. >> +#ifndef NODEBrinSort_H >> +#define NODEBrinSort_H > > NODEBRIN_SORT would be more consistent with NODEINCREMENTALSORT. > But I'd prefer NODE_* - otherwise it looks like NO DEBRIN. > Yeah, stupid search/replace on the indescan code, which was used as a starting point. > This needed a bunch of work needed to pass any of the regression tests - > even with the feature set to off. > > . meson.build needs the same change as the corresponding ./Makefile. > . guc missing from postgresql.conf.sample > . brin_validate.c is missing support for the opr function. > I gather you're planning on changing this part (?) but this allows to > pass tests for now. > . mingw is warning about OidIsValid(pointer) in nodeBrinSort.c. > https://cirrus-ci.com/task/5771227447951360?logs=mingw_cross_warning#L969 > . Uninitialized catalog attribute. > . Some typos in your other patches: "heuristics heuristics". ste. > lest (least). > Thanks, I'll get this fixed. I've posted the patch as a PoC to showcase it and gather some feedback, I should have mentioned it's incomplete in these ways. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company