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 1gjNGs-0003tp-EN for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Jan 2019 11:52:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gjNFs-0002wP-Q1 for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Jan 2019 11:51:40 +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 1gjNFs-0002wI-IW for pgsql-hackers@lists.postgresql.org; Tue, 15 Jan 2019 11:51:40 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjNFm-0002U1-8G for pgsql-hackers@postgresql.org; Tue, 15 Jan 2019 11:51:39 +0000 Received: by mail-wm1-x343.google.com with SMTP id d15so2968536wmb.3 for ; Tue, 15 Jan 2019 03:51:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=BuUuIOsUNiWdxIOdw53oD/9wdFMlWxS98rtSCwsy8is=; b=hNHJuHpQsMH/nhn2ou7YY8qunKWioQcvAmgV7v87Lanp9XqMxYco9ofWJeb9Y5J7Ye 6jtUnic2YUQPrEBaUGsCwhvdAWUXXL45CPB3Vwp/LKkKAZJPhPUbtvLQHfW+8Cx0rlBQ 9/w3DCSqGiz1vQ81IiuHE49rejPh/B+QMY6o48cjFGkxPRxn3ZOUCgec8IciMUDeGMSF 3inS42hv9JQ0+7/rv0qL2FXVFJDF8PzbUuhoY3jfPGFezagMtFdFDuRY7Sb90/zSbjcb eRyXFUeFcxlHLVHhOcxsuZK0aNqytLmvKm4iQjgp3NZSwJaf3pZRYO9eE6eUUsWibbdm TYHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=BuUuIOsUNiWdxIOdw53oD/9wdFMlWxS98rtSCwsy8is=; b=Q4+RYt/OukeTfBOCvTQSa5Bd1jF9PSJSr+h+sVXlVaP1C/J7HGHTH/JWi0ov5djJF/ 7Z6PIIV502BFowL+WnVm5seXdV12ZbX8j8ERDCqyqqTA4qGT6bu6pf2/1e5uraK6tKbv RI3beODDYDdw2ys8nFTT64j4m/1cuOSAHbBG8eZco96ZFde1AoevvTRUDpdV8YRYKb4M oQ/fHslLc81T5ihVsi63TvkanBA730BNQho855gH9o1vPFVwAkI4cRLX5qqYUaDuMFmn UeGnFfkUaagT7lN4u4NSVM704fZygA11MiI5z3W7MS21+PZh/FMBdYl6GejmvANUOsES /Ozg== X-Gm-Message-State: AJcUukd6UVGK8srWAMFiztbcqaeppzhcZkhtuXEaARs06AXDS6827rwW g80xxMRvjRLw0+eHFcJVuVAKAYOspaTBsvv3Uyw3SPi4xcVnq8u37z1LJnCZtpKaDglBrGwcK4+ 47DdIFhgZZLxJuL43pnXhi6PxHYZeR7WKkzxzsnOFY6R4MsUCjEaLnsRFTKs3yJ44qAluy7LJ24 X73mx4MLKR7Ng= X-Google-Smtp-Source: ALg8bN4RrHmdN6elXT1P4hqEGOBMC44648IfM71oVp2BsV6dMdm/sNnRvj4DD1AGfPA474mzLOoslQ== X-Received: by 2002:a1c:5984:: with SMTP id n126mr2981373wmb.62.1547553092137; Tue, 15 Jan 2019 03:51:32 -0800 (PST) Received: from [10.137.2.19] (ip-86-49-251-50.net.upcbroadband.cz. [86.49.251.50]) by smtp.gmail.com with ESMTPSA id d2sm64553002wrs.97.2019.01.15.03.51.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Jan 2019 03:51:31 -0800 (PST) Subject: Re: FETCH FIRST clause WITH TIES option To: Surafel Temesgen Cc: Robert Haas , andrew@tao11.riddles.org.uk, PostgreSQL Hackers References: <40c9e6c5-4fc7-1589-0cc5-8a857405b91a@2ndquadrant.com> <87r2g87py9.fsf@news-spur.riddles.org.uk> <942526de-1854-2bdc-ad17-e276c1efeb1d@2ndquadrant.com> <87muqw7luv.fsf@news-spur.riddles.org.uk> <65ecc991-97aa-50a2-7131-10c8b3991c8c@2ndquadrant.com> <3458d42b-6be2-7f69-440f-019e5c713360@2ndquadrant.com> From: Tomas Vondra Message-ID: Date: Tue, 15 Jan 2019 12:51:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/15/19 11:07 AM, Surafel Temesgen wrote: > > > On Wed, Jan 2, 2019 at 6:19 PM Tomas Vondra > > wrote: > > After looking at the "FETCH FIRST ... PERCENT" patch, I wonder if this > patch should tweak estimates in some way. Currently, the cardinality > estimate is the same as for plain LIMIT, using the requested number of > rows. But let's say there are very few large groups - that will > naturally increase the number of rows produced. > > As an example, let's say the subplan produces 1M rows, and there are > 1000 groups (when split according to the ORDER BY clause). > > >   > > can we use ORDER BY column raw statistic in limit node reliably? > because it seems to me it can be affected by other operation in a > subplan like filter condition > What do you mean by "raw statistic"? Using stats from the underlying table is not quite possible, because you might be operating on top of join or something like that. IMHO the best thing you can do is call estimate_num_groups() and combine that with the number of input rows. That shall benefit from ndistinct coefficients when available, etc. I've been thinking that considering the unreliability of grouping estimates we should use a multiple of the average size (because there may be much larger groups), but I think that's quite unprecipled and I'd much rather try without it first. But maybe we can do better when there really is a single table to consider, in which case we might look at MCV lists and estimate the largest group. That would give us a much better idea of the worst case. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services