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 1nywjw-0002ne-Tc for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2022 14:32:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nywjv-0001hB-PG for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2022 14:32:55 +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 1nywjv-0001h2-D9 for pgsql-performance@lists.postgresql.org; Wed, 08 Jun 2022 14:32:55 +0000 Received: from mail-il1-x134.google.com ([2607:f8b0:4864:20::134]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nywjr-0006gI-6M for pgsql-performance@lists.postgresql.org; Wed, 08 Jun 2022 14:32:55 +0000 Received: by mail-il1-x134.google.com with SMTP id h18so16691811ilj.7 for ; Wed, 08 Jun 2022 07:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=/2i0+B+CpiFuWjoKGbdfWRjw1/ltfHOb+w1+tFcBBhI=; b=KSYLQ8Sxe96GC/QTvJN+othmZTsV1cnlDL1L5JT2PlykLtB0KnZOVztW2quYPvArLF Zj+O5j+yOU3DGH4y8mPkhcgXqwQd7RcC5hZHs7Yu9aAcEbJJ60aE3PiPJyKSTun+zLxb uO8mBZaWfIsg7oxP2938MqR55iZGQHyt8SmIbwMwwUsC4Mo2O6aOS2oKfWLHnFAsjdSk /lLnjftL6SWMJ7vB/EyAR0JNKw2CR7JDm/5VlrBIf44oYhrbC2BPNYjZ4wNJKZeChVHZ CMUQ0bfBNzJ9V7idkKk0q1o9VeNX3bKhbSUS37qQrpO1sDxr4wMwQjjfXTlTLgyi8Uv3 fKgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=/2i0+B+CpiFuWjoKGbdfWRjw1/ltfHOb+w1+tFcBBhI=; b=m+QLre8JLXmXpelyR0AYKlVsvZyfRqsofjwHSP/NU9Zn0TT7AO/nvnqKZWT4iHWid/ d9kLphYyAkIP+M3HVVLiYp3/YWpMWTqOkXYPeoOM5mKzHzKGE4Dpyn3dlZ8+E/3+nysM nOgC818G6ARzZYZRtqxsn9rV7GwYchecPwFQfvfEcFoa8L1cN9GtfaXIrxpOZ92vkvJs xaSNl1yRGgaWl8CfJN5uoN1MqhrfK+OUbBwn6MTZyxHYnozgYIr44xQ5fKIqzutCMT0A 1p+n4G/pRGdQIniR0mGlcxTTCusfcKzSvpryzFFRD7j1NBKGYCC4hygiSpNmt4s+vPUg +bKw== X-Gm-Message-State: AOAM532mzJDsEPjyWxSbS9axNA1s4idrHohmIktCnEC8R6Meh3MAndK6 8gOYvJZb0KswFrPdKl6AgTM80Q== X-Google-Smtp-Source: ABdhPJz6/gJA/8wjIjZ4ik1V6aaY98qhJMGGWDTzz+VLFLf3iT2yXmjbmH5QRqtF/vQeFO2YsH6pcw== X-Received: by 2002:a92:cd41:0:b0:2d6:6553:db13 with SMTP id v1-20020a92cd41000000b002d66553db13mr1077794ilq.307.1654698769017; Wed, 08 Jun 2022 07:32:49 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id p5-20020a056e02144500b002d392d98afdsm9028469ilo.9.2022.06.08.07.32.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Jun 2022 07:32:48 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 75EF9800A8D; Wed, 8 Jun 2022 09:32:47 -0500 (CDT) Date: Wed, 8 Jun 2022 09:32:47 -0500 From: Justin Pryzby To: Paulo Silva Cc: pgsql-performance@lists.postgresql.org Subject: Re: Strange behavior of limit clause in complex query Message-ID: <20220608143247.GO29853@telsasoft.com> References: 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: Archived-At: Precedence: bulk On Wed, Jun 08, 2022 at 09:44:08AM +0100, Paulo Silva wrote: > But if I add an ORDER BY and a LIMIT something goes very wrong (Q2): A somewhat common problem. A common workaround is to change "ORDER BY a" to something like "ORDER BY a+0" (if your framework will allow it). > An EXPLAIN (ANALYZE, BUFFERS) for Q2 returns this: ... > -> Index Scan Backward using ix_ng_content_date on ng_content "Extent1" (cost=0.43..40616715.85 rows=2231839 width=12) (actual time=11027.808..183839.289 rows=5 loops=1) > Filter: ((2 = id_status) AND (date_from <= LOCALTIMESTAMP) AND (date_to >= LOCALTIMESTAMP) AND (SubPlan 1)) > Rows Removed by Filter: 4685618 > Buffers: shared hit=15414533 read=564480 written=504 I'm not sure if it would help your original issue, but the rowcount estimate here is bad - overestimating 2231839 rows instead of 5. Could you try to determine which of those conditions (id_status, date_from, date_to, or SubPlan) causes the mis-estimate, or if the estimate is only wrong when they're combined ? -- Justin