Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1efr73-0008Pd-Ob for pgsql-performance@arkaria.postgresql.org; Sun, 28 Jan 2018 17:51:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1efr72-00046e-IE for pgsql-performance@arkaria.postgresql.org; Sun, 28 Jan 2018 17:51:28 +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 1efr72-00046T-5e for pgsql-performance@lists.postgresql.org; Sun, 28 Jan 2018 17:51:28 +0000 Received: from mail-io0-x233.google.com ([2607:f8b0:4001:c06::233]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1efr6p-0000LN-9y for pgsql-performance@postgresql.org; Sun, 28 Jan 2018 17:51:27 +0000 Received: by mail-io0-x233.google.com with SMTP id m11so5236454iob.2 for ; Sun, 28 Jan 2018 09:51:14 -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=e9J5Xqdhhl1xbY13WPL7QllB592hkL+bLHvklZTCDg4=; b=MK3youqqVm94julBhu01VG1OdFmqBRSemTEdf/XSRkcHJUU5TjdsQ3V2gQemR0SYL/ +YYzYmIJl6olsDM9QML2yuALJTcGscD3kOOONcNN/1gK/ypdoinjWJ2MjTykHNtZdoD9 blHjYqpZ3zftrWf8UIjkiKmwpCjvyEJh9DBmzQxcW+J3iMnVK8u1WDFIWi1dfqyaYdp6 0lelxWjk5cleVlkLWoYMXfs+y2S58fPSJKl+jzAKG90HVm6L66GYG/tZl1KKf+1ZI+nk gF9fwytCzfhUv9fIJj8XXRQGahHw/FdVqaFV/3Av+IcI+L83usMr471plrCCldIR/O22 r20g== 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=e9J5Xqdhhl1xbY13WPL7QllB592hkL+bLHvklZTCDg4=; b=kvPQY1quNHKMCmtU011hqYZjLlqTCD8IeXZYPFD2DRi5Tg/4ltf21B4kc0j/sgPxgc USA9bAo2xLNyjKyU1E1dsM/YBEwCstzBsXYwrqP+n37YsDrM7a6JcAjXrAkpD5Mtp2uP XXl4mv+4DJLWgqQXisXbJIKXUdGJoOV5N0cxpYDnvIyL3uu+CC7EVeF4O143NaTPoF9d CB+hoXwttPfpkc6Ekuxw3DZd+uvRk+u71yEhPKI7ArBW4wLNS8nW6VI+fOzGzdw3tIiU ruJ2noZcwiA43+AYeKLvmMBPSU944I3LvpCI5UIDRsYaKHoXr12X7N7svE4ts7+D4Gal okyQ== X-Gm-Message-State: AKwxytcmOAGBWyUTw7g+E1NlmsmySYVEfdL8ZBoodhVOn1HN353PaQ3u P/sfiYwiyAEx2rxChqVJV1IOrXCkL78= X-Google-Smtp-Source: AH8x227AjI0eaSzkHN17S/daBXK+Wu7iiGI+bFjM9YMB/a6H8O1nVZpaPIFMlP45eJ1bD9l3epevnw== X-Received: by 10.107.19.213 with SMTP id 82mr22699347iot.276.1517161872729; Sun, 28 Jan 2018 09:51:12 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id f7sm3132181iod.60.2018.01.28.09.51.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jan 2018 09:51:12 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id C025D801102; Sun, 28 Jan 2018 11:51:10 -0600 (CST) Date: Sun, 28 Jan 2018 11:51:10 -0600 From: Justin Pryzby To: Nur Agus Cc: pgsql-performance@postgresql.org Subject: Re: Query Slow After 2018 Message-ID: <20180128175110.GA18115@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.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Jan 29, 2018 at 12:32:59AM +0700, Nur Agus wrote: > The following query run in just 9 ms: > "distrib_reports"."month" = 1 AND > "distrib_reports"."year" = 2017 AND > "distrib_reports"."state" = 'SUBMITTED' AND > "distrib_report_groups"."distrib_report_group_type_id" = > '559a5fdc-418d-4494-aebf-80ecf8743d35' > The explain analyze of the 2 queries are resulting on really different > query plan, here are the links to depesz: > 2017 --> explain result on postgres-9: https://explain.depesz.com/s/qJF1 > 2018 --> explain result on postgres-9: https://explain.depesz.com/s/pT0y > The question is, why the query planner choose such very different path just > by changing one parameter? Looks like this badly underestimates its rowcount: Index Scan using index_distrib_reports_on_year on distrib_reports (cost=0.42..40.62 rows=8 width=32) (actual time=0.034..50.452 rows=17,055 loops=1) Index Cond: (year = 2018) Filter: ((month = 1) AND ((state)::text = 'SUBMITTED'::text)) Rows Removed by Filter: 1049 Maybe because "if year==2018" then, month=1 does essentialy nothing .. ..but postgres thinks it'll filters out some 90% of the rows. And possibly the same for SUBMITTED (?) You should probably use timestamp column rather than integer year+month. On PG10, you could probably work around it using "CREATE STATISTICS". > This behaviour is *not-reproducable* on postgres-10. On postgres-10, the > query plan are consistent, and both have very acceptable time: > 2017 --> explain result on postgres-10: https://explain.depesz.com/s/N9r5 > 2018 --> --> explain result on postgres-10: > https://explain.depesz.com/s/Tf5K ..I think default max_parallel_workers_per_gather=3 by chance causes the plan to be the same. I think there's still a underestimate rowcount with PG10 (without CREATE STATISTICS), but it's masked by "rows=1 actual rows=0" roundoff error with high loop count. Justin