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 1kkC8I-0002h7-5G for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Dec 2020 20:20:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kkC8H-0001gu-2K for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Dec 2020 20:20:17 +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 <9erthalion6@gmail.com>) id 1kkC8G-0001gl-Ru for pgsql-hackers@lists.postgresql.org; Tue, 01 Dec 2020 20:20:16 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1kkC8E-00021W-TW for pgsql-hackers@postgresql.org; Tue, 01 Dec 2020 20:20:16 +0000 Received: by mail-wr1-x432.google.com with SMTP id r3so4773022wrt.2 for ; Tue, 01 Dec 2020 12:20:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=cpGLLWlc+NVoh3jyiZeN+Zv+Tz72+Vs/u959Iz3fWyA=; b=YkGUUbt43xRtKmne+T4nkXJ7x6xgtaoxdro/BbVjFdMcLPt7UvmfQ6OiaQ3d6XVchy oXo+7vXa3gmHAy3l+atGR+syxyQryo443PXN34Cx0rA5zHNmvOlLhxpmSmodVmI/Gkgn yVcTCbMsU6oT7HzMVKxordO7o8AjFUdnGWz9afUSgxWzPEZxw+rSbm3wN2RJOl5XTV+z RPUN5/VvytggnFsik3mvQs02eXoiheDMo+jECNLHbd4T8phphT72g1Kw/R1pDfNoOkeT 6USXLTjv9733JiJZAJqUPsvPx8Ou3kMskp0ukMIbIGO/IL6i3gPOORmzizb5pdirnvAo aT1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=cpGLLWlc+NVoh3jyiZeN+Zv+Tz72+Vs/u959Iz3fWyA=; b=qYsXogM6pwX/7/J1WjWggCV/w/6RXMgJdU3YFTo3BXSwsd9m9JggJskA1Lu7apGmVR 7pIrrtwsRGr2w2BYGzINjWFJHeMKhkfUZqIQCPjfFvmtpDjgsDjLxRp830KB6CLAtq/Z mqtrQLUUTgChZRNkSp3kF9Qa0pR/tS5MjdXDEuRNk6f0WE6ptFz7G0x2SsBG0hPuwQcD Cs5DVxIGyHQlkfggmZmDMgrZv0lWIQD358+UeQWUtg0ECkxi+/khdVsmn+9edqrEeQ3o 82QsiMC6aeDL5bGzGKs5Af8CsNBTpYBVi10eZugbSfe1B+5d9+/JHzUuGsLccNog3EQn ZYqA== X-Gm-Message-State: AOAM530mh8zCNwAONHj5WA0gxoRfIyA+gWGbEHc1CbdgmlnnBzGiuSGH Y5/rIYNb/LObClze1/xPi3w= X-Google-Smtp-Source: ABdhPJwyEmhnPHMF1kb1xS6vcophPxm+rkpj81OQKEb3Nage1t4RtZUnHrBvAJTW/iIj7RW5bJNIjA== X-Received: by 2002:a5d:630b:: with SMTP id i11mr6367375wru.404.1606854013283; Tue, 01 Dec 2020 12:20:13 -0800 (PST) Received: from localhost (dslb-094-222-016-204.094.222.pools.vodafone-ip.de. [94.222.16.204]) by smtp.gmail.com with ESMTPSA id m21sm1420550wml.13.2020.12.01.12.20.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Dec 2020 12:20:12 -0800 (PST) Date: Tue, 1 Dec 2020 21:21:19 +0100 From: Dmitry Dolgov <9erthalion6@gmail.com> To: Heikki Linnakangas Cc: Peter Geoghegan , PostgreSQL-development , Jesper Pedersen , David Rowley , Floris Van Nee , Kyotaro Horiguchi , Thomas Munro , Tomas Vondra , Andy Fan , Dilip Kumar Subject: Re: Index Skip Scan (new UniqueKeys) Message-ID: <20201201202119.jcsr7z6cdiffuops@localhost> References: <20200609102247.jdlatmfyeecg52fi@localhost> <20200711161258.5modwkdeszhf55ux@localhost> <20200727102431.mjspwec4yhzeyny2@localhost> <20200815141240.wuyc6ijkmnywywtn@localhost> <20201006152039.xs2ul7il52sowhlu@localhost> <20201024164553.tel5uynf2dzyh4az@localhost> <598ff988-5ebb-f6f7-0c9d-82208dff3bbd@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <598ff988-5ebb-f6f7-0c9d-82208dff3bbd@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Mon, Nov 30, 2020 at 04:42:20PM +0200, Heikki Linnakangas wrote: > > I had a quick look at this patch. I haven't been following this thread, so > sorry if I'm repeating old arguments, but here we go: Thanks! > - I'm surprised you need a new index AM function (amskip) for this. Can't > you just restart the scan with index_rescan()? The btree AM can check if the > new keys are on the same page, and optimize the rescan accordingly, like > amskip does. That would speed up e.g. nested loop scans too, where the keys > just happen to be clustered. An interesting point. At the moment I'm not sure whether it's possible to implement skipping via index_rescan or not, need to take a look. But checking if the new keys are on the same page would introduce some overhead I guess, wouldn't it be too invasive to add it into already existing btree AM? > - Does this optimization apply to bitmap index scans? No, from what I understand it doesn't. > - This logic in build_index_paths() is not correct: > > > + /* > > + * Skip scan is not supported when there are qual conditions, which are not > > + * covered by index. The reason for that is that those conditions are > > + * evaluated later, already after skipping was applied. > > + * > > + * TODO: This implementation is too restrictive, and doesn't allow e.g. > > + * index expressions. For that we need to examine index_clauses too. > > + */ > > + if (root->parse->jointree != NULL) > > + { > > + ListCell *lc; > > + > > + foreach(lc, (List *)root->parse->jointree->quals) > > + { > > + Node *expr, *qual = (Node *) lfirst(lc); > > + Var *var; > > + bool found = false; > > + > > + if (!is_opclause(qual)) > > + { > > + not_empty_qual = true; > > + break; > > + } > > + > > + expr = get_leftop(qual); > > + > > + if (!IsA(expr, Var)) > > + { > > + not_empty_qual = true; > > + break; > > + } > > + > > + var = (Var *) expr; > > + > > + for (int i = 0; i < index->ncolumns; i++) > > + { > > + if (index->indexkeys[i] == var->varattno) > > + { > > + found = true; > > + break; > > + } > > + } > > + > > + if (!found) > > + { > > + not_empty_qual = true; > > + break; > > + } > > + } > > + } > > If you care whether the qual is evaluated by the index AM or not, you need > to also check that the operator is indexable. Attached is a query that > demonstrates that problem. > ... > Also, you should probably check that the index quals are in the operator > family as that used for the DISTINCT. Yes, good point, will change this in the next version. > I'm actually a bit confused why we need this condition. The IndexScan > executor node should call amskip() only after checking the additional quals, > no? This part I don't quite get, what exactly you mean by checking the additional quals in the executor node? But at the end of the day this condition was implemented exactly to address the described issue, which was found later and added to the tests.