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 1ia4zd-0000qj-6L for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Nov 2019 21:37:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ia4zb-0000ci-MV for pgsql-hackers@arkaria.postgresql.org; Wed, 27 Nov 2019 21:36:59 +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 1ia4zb-0000cb-8E for pgsql-hackers@lists.postgresql.org; Wed, 27 Nov 2019 21:36:59 +0000 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ia4zY-00081r-3q for pgsql-hackers@postgresql.org; Wed, 27 Nov 2019 21:36:58 +0000 Received: by mail-qt1-x843.google.com with SMTP id g24so20054822qtq.11 for ; Wed, 27 Nov 2019 13:36:55 -0800 (PST) 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:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=cTYjDWb3bklzUR+0eCnvMxRt3oQOnntycLAg3Ej9ZHI=; b=P0CEQAvsLpgjtS3L60YtiFrhY31Zadde6L13Lju+ul5fozvA8H+f0QtYIbdzCpOGOC eC8kvGEpLnO1Oznj7B7TItJtPx6b4wuAfgbKCwrJGJn6L/bUJuY4AbnxeXjyibxPGlYg cocnSgiotqUwtM50PhoJmtAglVa0YcenJgvN2ywGiSMBul7tzurYDD1AMZwDTaFMB8S2 YFX0EdKT2JSmqfbNtrsmepAgbRuEzlwqc9QkJ8DXjWPOr4HumgrCxUVWoVzTklxRJVrX YxFRLh26Qg2vLRimyNXU7uukWhLM6oehTd55pg4iWVg81uI3DE9xazZHxriJaiEnTvwt CBQA== 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:content-transfer-encoding:in-reply-to :user-agent; bh=cTYjDWb3bklzUR+0eCnvMxRt3oQOnntycLAg3Ej9ZHI=; b=H/EIVdF/0tI46ENHkpwWud3shYEdozftQ4TzPyPAq2hQv4pFz4NGKmcsIdM1jYdCXZ m3clAadHDDjkxspRobBx/VDnJkyLfZ+DTWoGlzauESmJa5Qz3P2fjEJ0mWwq3eFYwRig SVoL0YI54t1ZGPcb4uV0q+gZiFB4dg+tGTqiMxDQ4q5e0HYkW7nE2hgedJiV1q5Xu0/0 6sPo1tDzHlaCE7yXXBuHuoQW6uoaMK/iVrpueFk7KeI2/gEsNZmctBFWcxbQWg6A3WyX rogylkeVRpe+ZQXTtVhWOXIG42OLEug0vDvH/5zPv7g9oL8ulINpKM0wt30MB8JMIphA F3BQ== X-Gm-Message-State: APjAAAXP3vnIuWFFa3+kedqLpfiDypghppCt6DAaBKXgWiXKgu5Ro4tR rPmkvbV0hJuJdJTumtqM3v0ZUg== X-Google-Smtp-Source: APXvYqx2yuhPhAtRwRJlSOd5KqEjSJXpFZxg2JZUSEFxsWr8wq3nCuv56ayOP8MF3Kz4ndoWc7HvCg== X-Received: by 2002:aed:2041:: with SMTP id 59mr42383101qta.79.1574890614080; Wed, 27 Nov 2019 13:36:54 -0800 (PST) Received: from nimloth.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id f19sm7382972qkg.44.2019.11.27.13.36.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2019 13:36:53 -0800 (PST) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 40DA3300661; Wed, 27 Nov 2019 18:36:50 -0300 (-03) Date: Wed, 27 Nov 2019 18:36:50 -0300 From: Alvaro Herrera To: Surafel Temesgen Cc: Tomas Vondra , PostgreSQL Hackers , Andrew Gierth Subject: Re: FETCH FIRST clause WITH TIES option Message-ID: <20191127213650.GA17453@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thanks. (I would suggest renaming the new state LIMIT_WINDOWEND_TIES, because it seems to convey what it does a little better.) I think you should add a /* fall-though */ comment after changing state. Like this (this flow seems clearer; also DRY): if (!node->noCount && node->position - node->offset >= node->count) { if (node->limitOption == LIMIT_OPTION_COUNT) { node->lstate = LIMIT_WINDOWEND; return NULL; } else { node->lstate = LIMIT_WINDOWEND_TIES; /* fall-through */ } } else ... I've been playing with this a little more, and I think you've overlooked a few places in ExecLimit that need to be changed. In the regression database, I tried this: 55432 13devel 17282=# begin; BEGIN 55432 13devel 17282=# declare c1 cursor for select * from int8_tbl order by q1 fetch first 2 rows with ties; DECLARE CURSOR 55432 13devel 17282=# fetch all in c1; q1 │ q2 ─────┼────────────────── 123 │ 456 123 │ 4567890123456789 (2 filas) 55432 13devel 17282=# fetch all in c1; q1 │ q2 ────┼──── (0 filas) 55432 13devel 17282=# fetch forward all in c1; q1 │ q2 ────┼──── (0 filas) So far so good .. things look normal. But here's where things get crazy: 55432 13devel 17282=# fetch backward all in c1; q1 │ q2 ──────────────────┼────────────────── 4567890123456789 │ 123 123 │ 4567890123456789 (2 filas) (huh???) 55432 13devel 17282=# fetch backward all in c1; q1 │ q2 ────┼──── (0 filas) Okay -- ignoring the fact that we got a row that should not be in the result, we've exhausted the cursor going back. Let's scan it again from the start: 55432 13devel 17282=# fetch forward all in c1; q1 │ q2 ──────────────────┼─────────────────── 123 │ 4567890123456789 4567890123456789 │ 123 4567890123456789 │ 4567890123456789 4567890123456789 │ -4567890123456789 (4 filas) This is just crazy. I think you need to stare a that thing a little harder. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services