Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hhEKM-0002Vo-TL for pgsql-hackers@arkaria.postgresql.org; Sat, 29 Jun 2019 14:27:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hhEKL-0006Fp-03 for pgsql-hackers@arkaria.postgresql.org; Sat, 29 Jun 2019 14:27:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hhEKK-0006Fi-Nm for pgsql-hackers@lists.postgresql.org; Sat, 29 Jun 2019 14:27:40 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hhEKD-0005sp-Fy for pgsql-hackers@lists.postgresql.org; Sat, 29 Jun 2019 14:27:40 +0000 Received: by mail-wr1-x441.google.com with SMTP id p11so9137305wre.7 for ; Sat, 29 Jun 2019 07:27:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=akgeCx40u1UJ9p8xiJxpxckKrJc+3CvlELmrQIhnhos=; b=H3I/Om9mFmkUqrGePxoKuyuWPWMC52z7J1le0KKfFmNM3t5vztixQPeTPumRQQIjg2 PgSBtW8t7RWZKrVxr1twhLrqINYOCLtCoNTf8tcU8djGf1+mMyEccTExe6IKVtMurAXs USKTkv/HuI+Rae6ildrUDLP9Sic4vTxhv3qK0yZGBDC9S3AuZPM/3lj33eqrzzo7NOcQ snYDBP63lgGa4VdtUC8CNEYrnEPpuOB61vi9OPDZ+Nf1P3JdmcTatz2kCS6BtWXZIrTB pQmMzolTmYAB9ofDc9kblx5tmL7ParEC4Ibml0RJHYprhtD6KCfy1GOvgWrmRjXfycaS 3TSA== 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:user-agent; bh=akgeCx40u1UJ9p8xiJxpxckKrJc+3CvlELmrQIhnhos=; b=ATS+rAfQ9h6yNDEej8IyO4Ch/UO5/kk36tIqydjVnCcQ+/VpHklWNNGwwarBTH9hAT bql4PURWW+KfImoGQn4wg3C/jqiPZ/QzpXSPDC5Q0+Ql9t1MjjMAmMb550drVDa85RqY IyJAS5xbfzeCvDMjqv8ivl8QvM1m9VIHfYwvLYVTVUIVxEB9ymUDKMidcNRDghiiKkSY Kf/A8Mao68o6XW9thX8SodhAdm2m2OpiTYxq+EN2NolR8dWDUlBMsl0DBLYcHrZPKD6G PZKmxtjO7nPhIwBfmbe4XQMPomoW9Oq6ESlkerY5BNcKJC/TJvSkxSp4eynUiOGsecya V+ew== X-Gm-Message-State: APjAAAVFkSIQGwLwl7vpvSurNAsyIznxETFO3zxRYwyWA1YacR1ah7sH lkoQQTnGQGDF3V0bPLjd9MHVjw== X-Google-Smtp-Source: APXvYqzfms1EFCNJjxwifzS1lbdGwNqvaamFE10B6RDZmHbxmAnshtvlI3kZ8cuh3zXnBr8kLZbGaw== X-Received: by 2002:adf:dcc2:: with SMTP id x2mr11897873wrm.55.1561818452255; Sat, 29 Jun 2019 07:27:32 -0700 (PDT) Received: from localhost (ip-86-49-253-160.net.upcbroadband.cz. [86.49.253.160]) by smtp.gmail.com with ESMTPSA id y6sm5268464wrp.12.2019.06.29.07.27.27 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 29 Jun 2019 07:27:27 -0700 (PDT) Date: Sat, 29 Jun 2019 16:27:26 +0200 From: Tomas Vondra To: Julien Rouhaud Cc: Nikita Glukhov , PostgreSQL Hackers , Tom Lane , Marc Cousin Subject: Re: Avoid full GIN index scan when possible Message-ID: <20190629142726.gljead3qhbywahbx@development> References: <4547.1561748599@sss.pgh.pa.us> <20190628195401.frwhcga76rrytbc4@development> <7773.1561752983@sss.pgh.pa.us> <20190629102514.ucfzglxu7ccjsbjr@development> <20190629131135.ilktnqtollvbfnml@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716-1444-295967 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sat, Jun 29, 2019 at 03:28:11PM +0200, Julien Rouhaud wrote: >On Sat, Jun 29, 2019 at 3:11 PM Tomas Vondra > wrote: >> >> On Sat, Jun 29, 2019 at 02:50:51PM +0200, Julien Rouhaud wrote: >> >On Sat, Jun 29, 2019 at 12:25 PM Tomas Vondra >> >> A related issue is that during costing is too late to modify cardinality >> >> estimates, so the 'Bitmap Index Scan' will be expected to return fewer >> >> rows than it actually returns (after ignoring the full-scan quals). >> >> Ignoring redundant quals (the way btree does it at execution) does not >> >> have such consequence, of course. >> >> >> >> Which may be an issue, because we essentially want to modify the list of >> >> quals to minimize the cost of >> >> >> >> bitmap index scan + recheck during bitmap heap scan >> >> >> >> OTOH it's not a huge issue, because it won't affect the rest of the plan >> >> (because that uses the bitmap heap scan estimates, and those are not >> >> affected by this). >> > >> >Doesn't this problem already exists, as the quals that we could drop >> >can't actually reduce the node's results? >> >> How could it not reduce the node's results, if you ignore some quals >> that are not redundant? My understanding is we have a plan like this: >> >> Bitmap Heap Scan >> -> Bitmap Index Scan >> >> and by ignoring some quals at the index scan level, we trade the (high) >> cost of evaluating the qual there for a plain recheck at the bitmap heap >> scan. But it means the index scan may produce more rows, so it's only a >> win if the "extra rechecks" are cheaper than the (removed) full scan. > >Sorry, by node I meant the BitmapIndexScan. AIUI, if you have for >instance WHERE val LIKE '%abcde%' AND val LIKE '%z%' and a trgm index, >the BitmapIndexScan will have to through the whole index and discard >rows based on the only opclass-optimizable qual (LIKE '%abcde%'), >letting the recheck do the proper filtering for the other qual. So >whether you have the LIKE '%z%' or not in the index scan, the >BitmapIndexScan will return the same number of rows, the only >difference being that in one case you'll have to scan the whole index, >while in the other case you won't. > Oh! I thought 'full scan' means we have to scan all the keys in the GIN index, but we can still eliminate some of the keys (for example for the trigrams we might check if the trigram contains the short string). But clearly I was mistaken and it does not work like that ... regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services