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 1oaB0c-0004uk-A7 for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Sep 2022 07:16:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oaB0b-0002q1-8O for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Sep 2022 07:16:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oaB0a-0002ps-Tk for pgsql-hackers@lists.postgresql.org; Mon, 19 Sep 2022 07:16:00 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oaB0V-0001GE-0o for pgsql-hackers@lists.postgresql.org; Mon, 19 Sep 2022 07:15:59 +0000 Received: by mail-wr1-x434.google.com with SMTP id cc5so36197360wrb.6 for ; Mon, 19 Sep 2022 00:15:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :organization:message-id:date:subject:cc:to:from:from:to:cc:subject :date; bh=6O4GYCLazgqK/F7s1LzLVeEAC9naBuFDoWi9pVCtZUE=; b=FYPqV+m66m84NMEMRaTdGQ5tcZajwB1BZFaWZ2CP19EHipivHBWGAzL8A80YzcXzdt C8RHdDuSw+1Vkv9q2lDqsYk636M+8qF8Jmm2LRdaLZvU/NYYk2O7Q0oE3JBI5a+31SD5 8lFPb2NRV47S+RGkFbKkoEJw9aYoxSBBr2P7o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :organization:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date; bh=6O4GYCLazgqK/F7s1LzLVeEAC9naBuFDoWi9pVCtZUE=; b=5NwfDiIXY3Druynqx3HhmbkRpwcmWU7e7SQvuxiSVTTql7DWERjsVcsoHxGBYV75QE AWisRsdZoOB/4I2EOpB0maRY5P8UkF4Ak4EKy/YMIbQx1H9ppxqvhHxR6xrVYfsPFl+B WeMoR0UORFugsSJLK4Brdi4qPYyfjkugEkO+qF/CCQLag9zTnukYqHqQ/rK/81kqnwu4 bIlRqKmm7Zng3nPzdJbmyX9HoLj2uRW8V8/SB3FzlecfoSgauQYRkj7ghCJMXq1xxJy6 KITDD2y8wayHUIWJz96Iov6r5NYq7VEfXR0MttYRn9lP5OII4nllprkfdmM8KJvusUj2 14Og== X-Gm-Message-State: ACrzQf1ezyIwFi7S13YzTykw5uoMOFWdcqvS00yO8cTGvy5TI+MQmZwa G4xTyC6jqvCLy6qFxua76V6Ah+tOeWJVxw== X-Google-Smtp-Source: AMsMyM642kqZBnTPPIX/1FnfoGdMURKDrnJM+9dJh+vxaLuaXE12ekulM18mT98gNI+14YfySvEyOA== X-Received: by 2002:a5d:6c6b:0:b0:225:dde:ab40 with SMTP id r11-20020a5d6c6b000000b002250ddeab40mr9887180wrz.690.1663571753278; Mon, 19 Sep 2022 00:15:53 -0700 (PDT) Received: from aivenronan.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id h67-20020a1c2146000000b003a83ca67f73sm13910705wmh.3.2022.09.19.00.15.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Sep 2022 00:15:52 -0700 (PDT) From: Ronan Dunklau To: Tom Lane Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Fix gin index cost estimation Date: Mon, 19 Sep 2022 09:15:25 +0200 Message-ID: <4768224.31r3eYUQgx@aivenronan> Organization: aiven In-Reply-To: <3871020.1663358699@sss.pgh.pa.us> References: <3188617.44csPzL39Z@aivenronan> <4212593.ejJDZkT8p0@aivenronan> <3871020.1663358699@sss.pgh.pa.us> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Le vendredi 16 septembre 2022, 22:04:59 CEST Tom Lane a =E9crit : > Ronan Dunklau writes: > > The attached does that and is much simpler. I only took into account > > entryPagesFetched, not sure if we should also charge something for data= =20 pages. >=20 > I think this is wrong, because there is already a CPU charge based on > the number of tuples visited, down at the very end of the routine: >=20 > *indexTotalCost +=3D (numTuples * *indexSelectivity) *=20 (cpu_index_tuple_cost + qual_op_cost); >=20 > It's certainly possible to argue that that's incorrectly computed, > but just adding another cost charge for the same topic can't be right. I don't think it's the same thing. The entryPagesFetched is computed=20 independently of the selectivity and the number of tuples. As such, I think= it=20 makes sense to use it to compute the cost of descending the entry tree. As mentioned earlier, I don't really understand the formula for computing=20 entryPagesFetched. If we were to estimate the tree height to compute the=20 descent cost as I first proposed, I feel like we would use two different me= trics=20 for what is essentially the same cost: something proportional to the size o= f=20 the entry tree. >=20 > I do suspect that that calculation is bogus, because it looks like it's > based on the concept of "apply the quals at each index entry", which we > know is not how GIN operates. So maybe we should drop that bit in favor > of a per-page-ish cost like you have here. Not sure. In any case it > seems orthogonal to the question of startup/descent costs. Maybe we'd > better tackle just one thing at a time. Hum, good point. Maybe that should be revisited too. >=20 > (BTW, given that that charge does exist and is not affected by > repeated-scan amortization, why do we have a problem in the first place? > Is it just too small? I guess that when we're only expecting one tuple > to be retrieved, it would only add about cpu_index_tuple_cost.) Because with a very low selectivity, we end up under-charging for the cost = of=20 walking the entry tree by a significant amount. As said above, I don't see = how=20 those two things are the same: that charge estimates the cost of applying=20 index quals to the visited tuples, which is not the same as charging per en= try=20 page visited. >=20 > > Is the power(0.15) used an approximation for a log ? If so why ? Also > > shouldn't we round that up ? >=20 > No idea, but I'm pretty hesitant to just randomly fool with that equation > when (a) neither of us know where it came from and (b) exactly no evidence > has been provided that it's wrong. >=20 > I note for instance that the existing logic switches from charging 1 page > per search to 2 pages per search at numEntryPages =3D 15 (1.5 ^ (1/0.15)). > Your version would switch at 2 pages, as soon as the pow() result is even > fractionally above 1.0. Maybe the existing logic is too optimistic there, > but ceil() makes it too pessimistic I think. I'd sooner tweak the power > constant than change from rint() to ceil(). You're right, I was too eager to try to raise the CPU cost proportionnally = to=20 the number of array scans (scalararrayop). I'd really like to understand wh= ere=20 this equation comes from though...=20 Best regards, =2D-=20 Ronan Dunklau