Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eYx00-0002dL-PP for pgsql-general@arkaria.postgresql.org; Tue, 09 Jan 2018 16:43:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eYwzz-0002oF-WF for pgsql-general@arkaria.postgresql.org; Tue, 09 Jan 2018 16:43:40 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eYwzz-0002o5-Hn for pgsql-general@lists.postgresql.org; Tue, 09 Jan 2018 16:43:39 +0000 Received: from mail-yb0-x243.google.com ([2607:f8b0:4002:c09::243]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eYwzw-00017m-Si for pgsql-general@postgresql.org; Tue, 09 Jan 2018 16:43:38 +0000 Received: by mail-yb0-x243.google.com with SMTP id v17so4765083ybl.10 for ; Tue, 09 Jan 2018 08:43:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=fteWwaqKuyPmLQbj561tKN4a+ebekNb4ZD/lV4BbQiM=; b=KU+oSPS5ChGsjCw5avMbQP8QCx2odktHV5OBZx90VKzoB9BaVY5eSOOjRufkrJkK24 /XsxFZJoMWcmQyB+j2/2+AbW5zftvB19CqbCJNKuQ3UFgW5bTziMBeFpAenPTN9AZwUy osUdoww5+cGkYHul1GP0St5rNdxy8uEq+IvGgbZVW+FIh/3CL6qeJRniFXtr1dkDHFxI /MFdWFebsUCL1mMOMhkpMzRFdkJ0OnTIUxtcZFZhmkXUNM9Jbwl/2R3iq1BHh1crql5v ZvX43amf+mYFVLj28rBAw/d88n9YDl42MKIGJEPTMsegjFUerYZ7V3SzhgI+dTFZIO08 mCAA== 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-transfer-encoding :content-language; bh=fteWwaqKuyPmLQbj561tKN4a+ebekNb4ZD/lV4BbQiM=; b=WSlrsNa9WHQLWSNl+TVxWwUzAdqUTAVqFV/IfJ8XIy/5c9FwP130zl5xKURoPL/Lml X8rmU7MQEs5rZL/h5p3bF17fqRbTZ89snUUNRkURchbkTeFeDVbITpSWJYs2CE8Lvsvq BonRM/5g8kkJ79b1kABNfcjoCFu10n/ozNt8npP1OcSNePKs7C7Kp72BL8L6OaWsbVK4 grP0I4834156mduF3RyakNdUlvKDrf+LEq1QPfqs9x0Zhf5VDTE4Tp1OCP7llphE08ST E6SYGpXAy4JIShSHw0pDiPPYLLwkDDBIsOCl97rX+HGurMcGwRMgdeIYfNUcIdl7vyJo 2hkQ== X-Gm-Message-State: AKGB3mJiaX9bJVr/RtbCdJb+9Mf7FMeuW1CdoaM1qyOzulZHYqL4iB3g A7ZJvmQuskhEX5IAtB7jws2k/g7+ X-Google-Smtp-Source: ACJfBosYwvhQbRVwXEVXUBQaAsR3gFfER65rWjYBIPnxmSSfVvATdAxR3FkhST2EzwlfHi42M0Q0ow== X-Received: by 10.37.76.201 with SMTP id z192mr14594582yba.466.1515516214983; Tue, 09 Jan 2018 08:43:34 -0800 (PST) Received: from [192.168.1.183] ([193.93.78.27]) by smtp.gmail.com with ESMTPSA id d13sm6236670ywd.50.2018.01.09.08.43.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jan 2018 08:43:34 -0800 (PST) Subject: Re: Number of rows returned by Sort node To: Tom Lane Cc: pgsql-general@postgresql.org References: <6979dc4e-a7d1-993d-f2a4-bcdaff05106d@gmail.com> <28041.1515511410@sss.pgh.pa.us> From: Vitaliy Garnashevich Message-ID: Date: Tue, 9 Jan 2018 18:43:31 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <28041.1515511410@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thanks, Tom! I've found some more information in PostgreSQL source code: For example, consider the following relations:         outer: (0 ^1 1 2 5 5 5 6 6 7)    current tuple: 1         inner: (1 ^3 5 5 5 5 6)          current tuple: 3 ...suppose that the executor has just joined the first outer "5" with the last inner "5". The next step is of course to join the second outer "5" with all the inner "5's". This requires repositioning the inner "cursor" to point at the first inner "5". This is done by "marking" the first inner 5 so we can restore the "cursor" to it before joining with the second outer 5. The access method interface provides routines to mark and restore to a tuple. Essential operation of the merge join algorithm is as follows: Join {     get initial outer and inner tuples INITIALIZE     do forever {         while (outer != inner) { SKIP_TEST             if (outer < inner)                 advance outer SKIPOUTER_ADVANCE             else                 advance inner SKIPINNER_ADVANCE         }         mark inner position SKIP_TEST         do forever {             while (outer == inner) {                 join tuples JOINTUPLES                 advance inner position NEXTINNER             }             advance outer position NEXTOUTER             if (outer == mark) TESTOUTER                 restore inner position to mark TESTOUTER             else                 break    // return to top of outer loop         }     } } So, even that the Sort node was returning unique values, the join algorithm still had to do a lot of mark/restore, which were reflected in EXPLAIN's row count. Anyway, that's clear now. Regards, Vitaliy On 2018-01-09 17:23, Tom Lane wrote: > Vitaliy Garnashevich writes: >> How, according to EXPLAIN ANALYZE, the Sort node could return more rows >> as output (rows=767662), than it had for input from its child node >> (rows=135627)? > Unsurprising when it's the inner input of a merge join --- that reflects > the merge join rescanning parts of the inner input. It has to do that > whenever the outer input has duplicate keys. > > regards, tom lane >