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 1jQMGk-0001wW-AU for pgsql-performance@arkaria.postgresql.org; Mon, 20 Apr 2020 02:34:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jQMGA-0004eF-V0 for pgsql-performance@arkaria.postgresql.org; Mon, 20 Apr 2020 02:34:10 +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 ) id 1jQMGA-0004db-K9 for pgsql-performance@lists.postgresql.org; Mon, 20 Apr 2020 02:34:10 +0000 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jQMG1-0007tr-It for pgsql-performance@lists.postgresql.org; Mon, 20 Apr 2020 02:34:09 +0000 Received: by mail-qk1-x741.google.com with SMTP id t3so9145875qkg.1 for ; Sun, 19 Apr 2020 19:34:01 -0700 (PDT) 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:mime-version:content-disposition :in-reply-to:user-agent; bh=550rghpNTSjEzjzsEvA8vLpuExAd9kJGgw7dWFFMAuw=; b=Tos3mYCkXBluGyLM/BBOejsy7EwsHw/kRVUviCRO19IVUySjuUKGqubo4k8NGFnXD+ o7+9ckHOfd6NWXA4TuwboFE79cR2c2SSiCcIvnmDbaFtBxr/qQBRuMM2fveooplaWxT6 eZW7cvQv0Bw+T0ya3zdhAu1rUvk7ML5g3tcA7DLa58ZmTyLj+vqOhrOb52RgsVGIn+01 JNCXiWHEYj87ZxPQX7H3xkApDpqXVBVVWLG8zGzxL8CB/tNeB8waa/MLKcRyk1LKMvie SPUfC6hr7ilmvRBzINx+Ukvc3z3jxuwK98ovTkWNnKf0JUMvqZCeiwrfwpwZ75NRBA4k Yp7A== 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:mime-version :content-disposition:in-reply-to:user-agent; bh=550rghpNTSjEzjzsEvA8vLpuExAd9kJGgw7dWFFMAuw=; b=SK25Noc6Z7PucKSftRMBEt/O4INnhS60z0tj4IUX8Jktr/eGtHKPQp6l1mIQQIi3bB 1NKLi1N1RWAW6ySl7YSDX6m9j68cfYWqE3n/GGRVQ2/U6VgJRsEpWCKOnipJpOL71mx0 Ic2wqmF9udaeusVRSeWj2bP/oa2dvz4Cihd10D17SjguSxNyQtIMQT92bP9bErBNVMUO 5vNDBMuiKmj1HF+cD9HplWRtBCM3o4bMCcN4IaD5WdTEUm/tRKiD9KcxjFQPNAkxM5FL 7/HZHaocPitMRtCRmKAC7FibRisS/eSdX9SfZWBdhrXLp+VOTSMsvDL8BfIYUWv+Cxmz KD2w== X-Gm-Message-State: AGi0Pubuw4pXJh7vYLqcXXzoSPrRUnbcV7wmU2avpCvvucSPJM89gz1l Ea3YC6OHuBrbCOnk6joHwqofDg== X-Google-Smtp-Source: APiQypLRlxZjAclHHo3ojcg5dYN5uDZWeqEmBD8fyAs8PJQbuulSnqeDTJrR5b5ETxDSNgd/llm7pg== X-Received: by 2002:a05:620a:22b1:: with SMTP id p17mr14223628qkh.249.1587350040093; Sun, 19 Apr 2020 19:34:00 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id x55sm13694062qtk.3.2020.04.19.19.33.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 Apr 2020 19:33:59 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 5C73E801644; Sun, 19 Apr 2020 21:33:57 -0500 (CDT) Date: Sun, 19 Apr 2020 21:33:57 -0500 From: Justin Pryzby To: "singh400@gmail.com" Cc: pgsql-performance@lists.postgresql.org, David Rowley Subject: Re: Duplicate WHERE condition changes performance and plan Message-ID: <20200420023357.GR26953@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Apr 15, 2020 at 08:55:53PM +0100, singh400@gmail.com wrote: > We have an odd issue where specifying the same where clause twice causes PG > to pick a much more efficent plan. We would like to know why. > Query B (this is the 'fast' query): > UPDATE problem_instance SET processed = false > FROM problem > WHERE problem.id = problem_instance.problem_id > AND problem.status != 2 > AND problem.status != 2 > AND processed = true; When you specify redundant condition, it results in an underestimate, as expected: Index Scan using problem_id_idx1 on public.problem (cost=0.28..624.68 ROWS=73 width=14) (actual time=0.011..0.714 ROWS=841 loops=1) Filter: ((problem.status <> 2) AND (problem.status <> 2)) In this case, doing an index scans on problem_instance is apparently faster than an seq scan. I think the duplicate condition is fooling the planner, and by chance it's giving a better plan. That might indicate that your settings aren't ideal. Maybe random_page_cost should be lower, which would encourage index scans. If you're using SSD storage, or if the DB is small compared with shared_buffers or RAM, then random_page_cost should be closer to seq_page_cost. How large are the indexes? problem_id_idx1 ? On Mon, Apr 20, 2020 at 01:50:17AM +0100, singh400@gmail.com wrote: > Even after upgrading my local install of PG to "PostgreSQL 12.2, > compiled by Visual C++ build 1914, 64-bit" and I'm still seeing the > same behaviour. > Server Configuration: > https://gist.github.com/indy-singh/8386d59206af042d365e5cd49fbae68f > shared_buffers 2GB configuration file > effective_cache_size 6GB configuration file Note, until v10, the documentation said this: https://www.postgresql.org/docs/9.6/runtime-config-resource.html |Also, on Windows, large values for shared_buffers aren't as effective. You may |find better results keeping the setting relatively low and using the operating |system cache more instead. The useful range for shared_buffers on Windows |systems is generally from 64MB to 512MB. It would be interesting to know -- Justin