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 1iHDI2-0001Vx-1X for pgsql-performance@arkaria.postgresql.org; Sun, 06 Oct 2019 20:38:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iHDI0-0001aC-N5 for pgsql-performance@arkaria.postgresql.org; Sun, 06 Oct 2019 20:38:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iHDI0-0001a2-Aj for pgsql-performance@lists.postgresql.org; Sun, 06 Oct 2019 20:38:00 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iHDHw-0004eh-W9 for pgsql-performance@lists.postgresql.org; Sun, 06 Oct 2019 20:37:58 +0000 Received: by mail-wr1-x441.google.com with SMTP id h4so4092869wrv.7 for ; Sun, 06 Oct 2019 13:37:56 -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=9XOTvET2MC834ezE0KREDmgDcfCYcno1gOzYVALuLu4=; b=AorV28MbWfbvn8Ay2mNCylgzuvmqkiO8Nc7EjkMd6hlZnE5chWWberAom/0SeSo72O vMm6f1YShqwV1pESJcOSVxblYfVZfNkg9yoUk78GFaDnG7PAG3kFF4r2OhVG/7p+HOo6 /WaDcRgrdzNgk2clbNEas+nfzPjEXJNwWv3kqJqU4Q5fiWC1/8LuhQ1ejhGCeCP5AZ2g rCDfFva37QF4ajgN9GXdikyL7CcODM84Eq23TpH7OyQgJAuNFfRz4sSi9ys24y714Uhf ovjGIAbSKhIfhybl5ZA7EtvN6JmvByitASlx1VJcziKoP/p4vupJUcfYmIJlMYVJ+ioY 4LtA== 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=9XOTvET2MC834ezE0KREDmgDcfCYcno1gOzYVALuLu4=; b=hT9RbxVkDhbTUDCv0xM3GUNJaodmz/2XT4s+5V12PUYrEOhLO8aY4TZQqza4YFAAlA CXM6vWUeZfHAK6lkrcQeVhgM4u8pYQNNM22Xz8+2D4w/55LFVc1UUi7T4ttP6MYmbdyv CuTOrgKMq/t6nTUls8zwc4p4pgws9oiBXlgvSItsU1S9aQYdMOGHc388KTDxAzvL6Tvx UBzu5xnIX8uw1XuRI3qM69Gxkz/MWdz3IzFLyf6MCyBAIeAXwMT0Lz0JnUHVoYYVHOB6 7bK758lWlAdwygaOvIO5wVZHlz3q92o1M3OCroCSVx2V4DoYeW7MjLZCcibke4f0VDsz Ty6A== X-Gm-Message-State: APjAAAUric3Xmh8aSYe5W06gr7wq+6oI0jYj6un96SFSdOHlhWbPuYw7 V38PW4y5nwGrKRRE90c4HnJ5Ig== X-Google-Smtp-Source: APXvYqzopLiDCXMzwYfyNI//gRR0CnxtpZRFHrhHnRuSprTCrZXWHZ4XJeZHYPzF1X7qy5AXe/jTcA== X-Received: by 2002:a5d:4c45:: with SMTP id n5mr21062976wrt.100.1570394275520; Sun, 06 Oct 2019 13:37:55 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id c17sm4958793wrc.60.2019.10.06.13.37.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Oct 2019 13:37:54 -0700 (PDT) Date: Sun, 6 Oct 2019 22:37:53 +0200 From: Tomas Vondra To: Behrang Saeedzadeh Cc: Tom Lane , pgsql-performance@lists.postgresql.org Subject: Re: Slow PostgreSQL 10.6 query Message-ID: <20191006203753.wobkgsg625ej7k2m@development> References: <9624.1569936426@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Oct 01, 2019 at 11:42:33PM +1000, Behrang Saeedzadeh wrote: >Thanks. That eliminated the bottleneck! > >Any ideas why adding ORDER BY to the subquery also changes the plan in a >way that eliminates the bottleneck? > IIRC the ORDER BY clause makes it impossible to "collapse" the subquery into the main (upper) one, and it probably happens to constrict the choices so that the planner ends up picking a good plan. I guess adding "OFFSET 0" to the subquery would have the same effect. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services