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 1j1EUg-0002eO-NQ for pgsql-performance@arkaria.postgresql.org; Mon, 10 Feb 2020 19:13:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j1EUf-0000Wk-0h for pgsql-performance@arkaria.postgresql.org; Mon, 10 Feb 2020 19:13:17 +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 1j1EUe-0000Wd-Jw for pgsql-performance@lists.postgresql.org; Mon, 10 Feb 2020 19:13:16 +0000 Received: from mail-yw1-xc43.google.com ([2607:f8b0:4864:20::c43]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j1EUX-0005tI-1E for pgsql-performance@lists.postgresql.org; Mon, 10 Feb 2020 19:13:15 +0000 Received: by mail-yw1-xc43.google.com with SMTP id 10so3948280ywv.5 for ; Mon, 10 Feb 2020 11:13:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-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=nOUVAgB9JSMxYksTTICcyuKZJrxAUxE6Qo3wHG63sRk=; b=uDCBK2EOd5Q63l2C01fKdLIgAv/H/t234DmOT8T/g0c1tBs2879tTgg0sHtLvZcifu uPaj5TObVP/MWCZODi0w1T2sSJ7MuRGNb6rZmJzvqvntWej1tJbscfiGSRHbTAWq0zRE XUY+zVVFllmx38mD6eDOoCuAJUfixvt85+lhXXDSrwpohf5xPBUrRMFKCM3H31dy90p7 AykIcTOQzvi3XpCDc3pPA7qcBcQBJ59KxNtZOjKnGET/UXhAy5Bxxyp7vSGsvVHmkDtq IzmfUrSBk8yH9zrhAKyqlIq+BcHY8qpyJN97oLAee705yxGCzUB5mQkYcjqMPhodcqka NXDw== 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=nOUVAgB9JSMxYksTTICcyuKZJrxAUxE6Qo3wHG63sRk=; b=oldl7QRfOIxA5kfgrVJJwZZOAsXmYnabjCrGJGvHfZgPy2wC35S2DdCmJQW9o7/562 0oPWFxp5540D9SHaAxb0miXZv9nTaK8XrYFIp/3xe6MFQiygCl1zEQOvdrKIku95lgKR I5K4KBL+DbEkSxyC8nQowWvpLN+KfVEJZlwOHd95ZSqU0C3oH6j5Zr+s+oSRUbOZoXvf WXNfwErAc0ow8vFW6EqAFKBHQ4d+2YGk8MIQUE5UQH3TavuzHCK75r0KxWbml2OLENu4 0eG8lrJGCvQ1uE4WLpXQiGerJmFk9Ph6hrmvSxQOVK899CxKJHY6hc1YIcKqXNlMa3ZA 7HVg== X-Gm-Message-State: APjAAAXzXO+hoYFZsxLNeQolbSTD4l22n6YLuik5MM1X/wIO/9MB3d/2 3DZM7fKQQor6jaUagKpitEaK7w== X-Google-Smtp-Source: APXvYqxB17QfeipYPzpRAfgZXXpFhIAkldmPSBDJ8yszCg9gqD0bZWbIMcH6EHo0Pb6B9bpQ44ciMA== X-Received: by 2002:a81:98f:: with SMTP id 137mr2433308ywj.151.1581361987070; Mon, 10 Feb 2020 11:13:07 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id w128sm653452ywf.72.2020.02.10.11.13.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Feb 2020 11:13:06 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 877AA800827; Mon, 10 Feb 2020 13:13:04 -0600 (CST) Date: Mon, 10 Feb 2020 13:13:04 -0600 From: Justin Pryzby To: Chris Borckholder Cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad selectivity estimate when using a sub query to determine WHERE condition Message-ID: <20200210191304.GF1412@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Feb 10, 2020 at 11:34:01AM +0100, Chris Borckholder wrote: > I have a large table of immutable events that need to be aggregated > regularly to derive statistics. To improve the performance, that table is > rolled up every 15minutes, so that online checks can aggregate rolled up > data and combine it with latest events created after the last roll up. > > To implement this a query is executed that selects only events after the > time of the last rollup. > That time is determined dynamically based on a log table. Perhaps that could be done as an indexed column in the large table, rather than querying a 2nd log table. Possibly with a partial index on that column: WHERE unprocessed='t'. > When using a sub select or CTE to get the latest roll up time, the query > planner fails to recognize that a most of the large table would be filtered > out by the condition and tries a sequential scan instead of an index scan. > When using the literal value for the WHERE condition, the plan correctly > uses an index scan, which is much faster. > > I analyzed the involved tables and increased the collected histogram, but > the query plan did not improve. Is there a way to help the query planner > recognize this in the dynamic case? Also, if you used partitioning with pgostgres since v11, then I think most partitions would be excluded: https://www.postgresql.org/docs/12/release-12.html |Allow partition elimination during query execution (David Rowley, Beena Emerson) |Previously, partition elimination only happened at planning time, meaning many joins and prepared queries could not use partition elimination. https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=499be013de65242235ebdde06adb08db887f0ea5 https://www.postgresql.org/about/featurematrix/detail/332/ Justin