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 1oXkdC-0004o5-1B for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Sep 2022 14:41:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oXkdA-000217-T1 for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Sep 2022 14:41:48 +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 1oXkdA-00020x-Hh for pgsql-hackers@lists.postgresql.org; Mon, 12 Sep 2022 14:41:48 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oXkd8-0000zP-DQ for pgsql-hackers@lists.postgresql.org; Mon, 12 Sep 2022 14:41:48 +0000 Received: by mail-wr1-x434.google.com with SMTP id e20so15693458wri.13 for ; Mon, 12 Sep 2022 07:41:45 -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=RfbXfF0DNC8RQ9QtaUTtxFDopjA/tYV3BI5abf3n1jo=; b=UqvEW0ugRD6qUYmmSJwyzKJpopknT1ZfX2nyJLDV0oFPQPAYw1i97LZMmpsA8UnTlj dZzN+QSrWXoAOaadBsvt5zk1wdDK3/WVelNiijpbvM1FIAbll66sOXzqu1Xeo8EFmDKM Il/UJ4ianrlnIVnG3rkUy+/nj3HOh27j6br3g= 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=RfbXfF0DNC8RQ9QtaUTtxFDopjA/tYV3BI5abf3n1jo=; b=gB2rzKpYUPwekBpcPsdERR0yXbSn6I/NpuxFo7ia5h/nu0g+u5T6qpcD+teU4q1WQp 8sAZhxJpa/CkluSoicbE5R9ZIjek871KI9xTLVDEueNwT3DchXxk9mMsIPwpHYAcgWx2 hE6zvLpERWOM9sPeQkr1I6/ooqyzrNOTi4/rZQHEw8rRuAWlNjTVEtZ1CGXCiRjFsFFE vRzdQT/WnmRUByrsjwRC56NX+8qwQCMYE90EuI2EbvOFruiR5EqP78Er4fuwBHWOu7m2 LJvo3j/lEcLrVEocIZOYa8XYLMeYF9cNVdNuxKgyc6C3e95NpnyCdZqsmYOe54nedjZU wpKA== X-Gm-Message-State: ACgBeo01z+nyyK+NJslFIeGGQhYIqp/DfwRvSIrAlCMMhQJ03E7u6w2r dcblzwNoVvL5mWR4CteKojviGinyX6a15g== X-Google-Smtp-Source: AA6agR66m1glBhCtOfjt2H2bL/iwf70yOw8jRgxFnRVeoIHVb9B0w1JEcsTBdg2KP+ATXOr4sfuhLw== X-Received: by 2002:a5d:6581:0:b0:228:e143:ddd7 with SMTP id q1-20020a5d6581000000b00228e143ddd7mr15376672wru.329.1662993705034; Mon, 12 Sep 2022 07:41:45 -0700 (PDT) Received: from aivenronan.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id f12-20020a05600c154c00b003a5f3f5883dsm11281067wmg.17.2022.09.12.07.41.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Sep 2022 07:41:44 -0700 (PDT) From: Ronan Dunklau To: Tom Lane Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Fix gin index cost estimation Date: Mon, 12 Sep 2022 16:41:16 +0200 Message-ID: <1924159.usQuhbGJ8B@aivenronan> Organization: aiven In-Reply-To: <4157141.1662676376@sss.pgh.pa.us> References: <3188617.44csPzL39Z@aivenronan> <4157141.1662676376@sss.pgh.pa.us> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thank you for looking at it. > I looked this over briefly. I think you are correct to charge an > initial-search cost per searchEntries count, but don't we also need to > scale up by arrayScans, similar to the "corrections for cache effects"? > > + * We model index descent costs similarly to those for btree, but we also > + * need an idea of the tree_height. > + * We use numEntries / numEntryPages as the fanout factor. > > I'm not following that calculation? It seems like it'd be correct > only for a tree height of 1, although maybe I'm just misunderstanding > this (overly terse, perhaps) comment. I don't really understand why that would work only with a tree height of one ? Every entry page contains a certain amount of entries, and as such computing the average number of entries per page seems to be a good approximation for the fanout. But I may have misunderstood what was done in other index types. For consistency, maybe we should just use a hard coded value of 100 for the fanout factor, similarly to what we do for other index types. But I realised that another approach might be better suited: since we want to charge a cpu cost for every page visited, actually basing that on the already estimated entryPagesFetched and dataPagesFetched would be better, instead of copying what is done for other indexes type and estimating the tree height. It would be simpler, as we don't need to estimate the tree height anymore. I will submit a patch doing that. > > + * We charge descentCost once for every entry > + */ > + if (numTuples > 1) > + { > + descentCost = ceil(log(numTuples) / log(2.0)) * cpu_operator_cost; > + *indexStartupCost += descentCost * counts.searchEntries; > + } > > I had to read this twice before absorbing the point of the numTuples > test. Maybe help the reader a bit: > > + if (numTuples > 1) /* ensure positive log() */ > Ok. On second read, I think that part was actually wrong: what we care about is not the number of tuples here, but the number of entries. > Personally I'd duplicate the comments from nbtreecostestimate rather > than just assuming the reader will go consult them. For that matter, > why didn't you duplicate nbtree's logic for charging for SA scans? > This bit seems just as relevant for GIN: > > * If there are ScalarArrayOpExprs, charge this once per SA scan. The > * ones after the first one are not startup cost so far as the overall > * plan is concerned, so add them only to "total" cost. > You're right. So what we need to do here is scale up whatever we charge for the startup cost by the number of arrayscans for the total cost. > Keep in mind also that pgindent will have its own opinions about how to > format these comments, and it can out-stubborn you. Either run the > comments into single paragraphs, or if you really want them to be two > paras then leave an empty comment line between. Another formatting > nitpick is that you seem to have added a number of unnecessary blank > lines. Thanks, noted. I'll submit a new patch soon, as soon as i've resolved some of the problems I have when accounting for scalararrayops. Best regards, -- Ronan Dunklau