Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XPIpW-00048R-L0 for pgsql-bugs@arkaria.postgresql.org; Wed, 03 Sep 2014 22:15:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XPIpV-00035P-DZ for pgsql-bugs@arkaria.postgresql.org; Wed, 03 Sep 2014 22:15:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XPIpU-00035G-MQ for pgsql-bugs@postgresql.org; Wed, 03 Sep 2014 22:15:04 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XPIpQ-00035u-82 for pgsql-bugs@postgresql.org; Wed, 03 Sep 2014 22:15:03 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1XPIpN-0007Ma-Lp; Wed, 03 Sep 2014 18:14:57 -0400 Date: Wed, 3 Sep 2014 18:14:57 -0400 From: Bruce Momjian To: David G Johnston Cc: pgsql-bugs@postgresql.org Subject: Re: Re: BUG #10256: COUNT(*) behaves sort of like RANK() when used over a window containing an ORDER BY Message-ID: <20140903221457.GD13008@momjian.us> References: <20140507181702.1397.66739@wrigleys.postgresql.org> <5812.1399498939@sss.pgh.pa.us> <1399504103854-5803103.post@n5.nabble.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="6WlEvdN9Dv0WHSBl" Content-Disposition: inline In-Reply-To: <1399504103854-5803103.post@n5.nabble.com> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -3.6 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --6WlEvdN9Dv0WHSBl Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, May 7, 2014 at 04:08:23PM -0700, David G Johnston wrote: > Tom's quote is pretty much an abbreviation of the following taken directly > from: > http://www.postgresql.org/docs/9.3/interactive/sql-select.html > > "The default framing option is RANGE UNBOUNDED PRECEDING, which is the same > as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY, this > sets the frame to be all rows from the partition start up through the > current row's last peer. Without ORDER BY, all rows of the partition are > included in the window frame, since all rows become peers of the current > row." > > A more useful description and example is found in the tutorial (about > halfway down, the sum(*) example): > > http://www.postgresql.org/docs/9.3/interactive/tutorial-window.html > > The term "peer" in the first quotation is confusing to me. My understanding > is that "PARTITION BY" defines which rows are "peers" - even if that isn't > the wording used. So now using "peers" in relation to the FRAME clause > confuses the issue. IMO the "since all rows..." is really superfluous - I > would understand: > > "In the presence of an ORDER BY only rows up to the current row (including > all [partition] peers) are considered. Omitting an ORDER BY causes the > entire PARTITION to be considered the FRAME. Other frame definitions can be > requested by supplying an explicit FRAME clause." > > I knew the answer but in the interest of looking through the user's eyes I > wanted to go take a look (again, done this before) and really am not > surprised that these kinds of questions are being asked. Just having to > scan through 5 sections of the documentation is difficult. That said, the > tutorial section and inline examples make it quite clear how the > with/without ORDER BY behavior of window functions (sum in the example) > cause different results. > > At minimum the top of 9.3.4 could provide links to, and > descriptions/summaries of, what the other 4 sections cover and why things > are broken out the way they are. The other cross-references could point > back to that section-subsection as a kind of launch point: "Please see > section 3.5.1 for an overview of, and links to, other related sections." Based on this discussion, I developed the attached patch which qualifies "peer" in every place it is mentioned --- this should reduce future confusion. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --6WlEvdN9Dv0WHSBl Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="peer.diff" diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml new file mode 100644 index daa56e9..e50408c *** a/doc/src/sgml/func.sgml --- b/doc/src/sgml/func.sgml *************** SELECT xmlagg(x) FROM (SELECT x FROM tes *** 13050,13056 **** Window functions provide the ability to perform calculations across sets of rows that are related to the current query row. See for an introduction to this ! feature. --- 13050,13057 ---- Window functions provide the ability to perform calculations across sets of rows that are related to the current query row. See for an introduction to this ! feature, and for syntax ! details. diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml new file mode 100644 index b69b634..940d1aa *** a/doc/src/sgml/ref/select.sgml --- b/doc/src/sgml/ref/select.sgml *************** UNBOUNDED FOLLOWING *** 792,799 **** The default framing option is RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW; it sets the frame to be all rows from the partition start ! up through the current row's last peer in the ORDER BY ! ordering (which means all rows if there is no ORDER BY). In general, UNBOUNDED PRECEDING means that the frame starts with the first row of the partition, and similarly UNBOUNDED FOLLOWING means that the frame ends with the last --- 792,800 ---- The default framing option is RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW; it sets the frame to be all rows from the partition start ! up through the current row's last peer (a row that ORDER ! BY considers equivalent to the current row, or all rows if there ! is no ORDER BY). In general, UNBOUNDED PRECEDING means that the frame starts with the first row of the partition, and similarly UNBOUNDED FOLLOWING means that the frame ends with the last *************** UNBOUNDED FOLLOWING *** 817,823 **** results if the ORDER BY ordering does not order the rows uniquely. The RANGE options are designed to ensure that rows that are peers in the ORDER BY ordering are treated ! alike; any two peer rows will be both in or both not in the frame. --- 818,824 ---- results if the ORDER BY ordering does not order the rows uniquely. The RANGE options are designed to ensure that rows that are peers in the ORDER BY ordering are treated ! alike; all peer rows will be in the same frame. diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml new file mode 100644 index 434a894..2f0680f *** a/doc/src/sgml/syntax.sgml --- b/doc/src/sgml/syntax.sgml *************** UNBOUNDED FOLLOWING *** 1877,1883 **** first peer row (a row that ORDER BY considers equivalent to the current row), while a frame_end of CURRENT ROW means the frame ends with the last equivalent ! peer. In ROWS mode, CURRENT ROW simply means the current row. --- 1877,1883 ---- first peer row (a row that ORDER BY considers equivalent to the current row), while a frame_end of CURRENT ROW means the frame ends with the last equivalent ! ORDER BY peer. In ROWS mode, CURRENT ROW simply means the current row. *************** UNBOUNDED FOLLOWING *** 1897,1903 **** which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY, this sets the frame to be all rows from the partition start up through the current row's last ! peer. Without ORDER BY, all rows of the partition are included in the window frame, since all rows become peers of the current row. --- 1897,1903 ---- which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY, this sets the frame to be all rows from the partition start up through the current row's last ! ORDER BY peer. Without ORDER BY, all rows of the partition are included in the window frame, since all rows become peers of the current row. --6WlEvdN9Dv0WHSBl Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --6WlEvdN9Dv0WHSBl--