Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1djucU-0006Ng-Cy for pgsql-performance@arkaria.postgresql.org; Mon, 21 Aug 2017 21:52:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1djucT-0007uX-Nx for pgsql-performance@arkaria.postgresql.org; Mon, 21 Aug 2017 21:52:25 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1djucS-0007sw-Bc for pgsql-performance@postgresql.org; Mon, 21 Aug 2017 21:52:24 +0000 Received: from mail-ua0-x22d.google.com ([2607:f8b0:400c:c08::22d]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1djucO-0002aL-Hr for pgsql-performance@postgresql.org; Mon, 21 Aug 2017 21:52:23 +0000 Received: by mail-ua0-x22d.google.com with SMTP id q42so38761018uad.2 for ; Mon, 21 Aug 2017 14:52:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=qPjojWLH98Q0BN7D2cGVEQsgMB13xhv3vQftQZSDSRg=; b=UOEbxUxWJTNmnHMYXrrmlU5EpSg047k7JHDI+JLfywOQjj5BiiR++zC2DpzNjyc3yO 8kmsoedjb6dOqK89H73Ds2WNwSmDAZOf75YLz9pgXEm7Cqzran1zrWDqAEHciKZaaH7a qjuIUF+NyOWl4H+LU4bijqYSxL1UgGnj7R9wKjjxHBBniF8Xoxu5OMh/tMCmrGl/3Mz9 f4stQxOE0qZee/u8tTz1Cc2BVTiuGzSSB5d9GZKNSq4YXyUAUuA6nmAYyxcBnNBpm7HY kTcolJhvDAUFGLnvVX32djZli6bzbuEASk/55mRjOuhZX+M16u1StK8rr3maz2GPk/aP pBbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=qPjojWLH98Q0BN7D2cGVEQsgMB13xhv3vQftQZSDSRg=; b=ZzXRgBO7gOgmVupDcF7RKTYQNiupy41aK+czgNu2WcrkntTM+QqEjF4hHClW6ENS7I yIo6vWHtvJk5DYynjWqYUPF9J81/IJuGjxZ2r+SjFw6dTwnXWuK07Nu1gX5Ezaels1JB ggT+fetYu/1qrGTMeVWnhHzvokOJEJkLIRWyiePcVN4uhvHi6uwAW+jhvP5Ppn4RnMp4 u888tIOyqNMqg3+bvObs6hLqchd8mUK7IgRjOwoZuXQ0KawQCW/37LrqsI54wTKoAW19 umhoRYeSD/8HN7xY6WNU5BQqCHnwWsbAwiFr5iGVoJqsz8vfYpGdAPqoPRNU/2zoJvs7 Us+Q== X-Gm-Message-State: AHYfb5gvd5ntJ7n2BK7mKbg4m/lIWCNjGC5zJe6NcnL5jT/dUGL3C4a9 5Dsvs8/+fxJ4z56pWSAe+PJuADnO1yIi X-Received: by 10.176.76.92 with SMTP id d28mr1649813uag.116.1503352337989; Mon, 21 Aug 2017 14:52:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.159.55.98 with HTTP; Mon, 21 Aug 2017 14:52:17 -0700 (PDT) In-Reply-To: References: From: David Rowley Date: Tue, 22 Aug 2017 09:52:17 +1200 Message-ID: Subject: Re: Query plan for views and WHERE clauses, Luke is not using the index To: kimaidou Cc: postgres performance list Content-Type: text/plain; charset="UTF-8" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 19 August 2017 at 04:46, kimaidou wrote: > When we call the WHERE on the view: > > EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS) > SELECT * > FROM "qgep"."vw_qgep_reach" > WHERE "progression_geometry" && > st_makeenvelope(1728327.03249295568093657,8240789.26074041239917278,1728608.10987572139129043,8240958.16933418624103069,3949) > > > The query plan is "wrong", as PostgreSQL seems to consider it should do a > seq scan on the tables, and only afterwards filter with the WHERE: > https://explain.depesz.com/s/wXV > > The query takes about 1 second instead of less than 100ms. > > Do you have any hint on this kind of issue ? This is by design due to the DISTINCT ON() clause. Only quals which filter columns which are in the DISTINCT ON can be safely pushed down. Consider the following, where I've manually pushed the WHERE clause. postgres=# create table tt (a int, b int); CREATE TABLE postgres=# create index on tt (a); CREATE INDEX postgres=# insert into tt values(1,1),(1,2),(2,1),(2,2); INSERT 0 4 postgres=# select * from (select distinct on (a) a,b from tt order by a,b) tt where b = 2; a | b ---+--- (0 rows) postgres=# select * from (select distinct on (a) a,b from tt where b = 2 order by a,b) tt; a | b ---+--- 1 | 2 2 | 2 (2 rows) Note the results are not the same. If I'd done WHERE a = 2, then the planner would have pushed the qual down into the subquery. More reading in check_output_expressions() in allpaths.c: /* If subquery uses DISTINCT ON, check point 3 */ if (subquery->hasDistinctOn && !targetIsInSortList(tle, InvalidOid, subquery->distinctClause)) { /* non-DISTINCT column, so mark it unsafe */ safetyInfo->unsafeColumns[tle->resno] = true; continue; } The comment for point 3 reads: * 3. If the subquery uses DISTINCT ON, we must not push down any quals that * refer to non-DISTINCT output columns, because that could change the set * of rows returned. (This condition is vacuous for DISTINCT, because then * there are no non-DISTINCT output columns, so we needn't check. Note that * subquery_is_pushdown_safe already reported that we can't use volatile * quals if there's DISTINCT or DISTINCT ON.) -- David Rowley http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance