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.92) (envelope-from ) id 1jEjCK-0006iD-40 for pgsql-docs@arkaria.postgresql.org; Thu, 19 Mar 2020 00:38:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jEjCH-0007Ht-4h for pgsql-docs@arkaria.postgresql.org; Thu, 19 Mar 2020 00:38:05 +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 1jEjCG-0007Hm-T4 for pgsql-docs@lists.postgresql.org; Thu, 19 Mar 2020 00:38:04 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jEjCD-0001Ht-Ay for pgsql-docs@lists.postgresql.org; Thu, 19 Mar 2020 00:38:04 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1jEjCA-0002YP-UV; Wed, 18 Mar 2020 20:37:58 -0400 Date: Wed, 18 Mar 2020 20:37:58 -0400 From: Bruce Momjian To: Erik Tews Cc: pgsql-docs@lists.postgresql.org Subject: Re: Better documentation for row_number() combined with set returning functions Message-ID: <20200319003758.GD28341@momjian.us> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 On Tue, Feb 25, 2020 at 08:24:13PM +0100, Erik Tews wrote: > Hi > > The current documentation of Postgresql sounds like row_number() over > () can be used to number the rows returned by postgres. However, that Yes, row_number() is a member of the set of window functions. > doesn't work when the query also uses set returning functions such as > json_array_elements. In this case, row_number() will be the same for > every element of the set. Yes, the issue is that a set-returning function in the target list generates multiple rows while the other target list values are duplicated, e.g.: CREATE TABLE test (x int); INSERT INTO test VALUES (1), (2), (3); SELECT x, generate_series(4,6) AS y, row_number() OVER () AS z FROM test; x | y | z ---+---+--- 1 | 4 | 1 1 | 5 | 1 1 | 6 | 1 2 | 4 | 2 2 | 5 | 2 2 | 6 | 2 3 | 4 | 3 3 | 5 | 3 3 | 6 | 3 Notice the x=1 value is duplicated for the y values of 4,5,6, and the row_number is duplicated too. One way to avoid that is to do the expansion of the set-returning function in a WITH query, and then apply row_number(): WITH z AS (select x, generate_series(4,6) as y from test) select x, y, row_number() OVER () FROM z; x | y | row_number ---+---+------------ 1 | 4 | 1 1 | 5 | 2 1 | 6 | 3 2 | 4 | 4 2 | 5 | 5 2 | 6 | 6 3 | 4 | 7 3 | 5 | 8 3 | 6 | 9 > I suggest to add a paragraph to the description of row_number() that > states that this is the behavior and maybe also reference the "with > ordinality" feature that can be used instead. I can't see how ordinality could be used: SELECT x, z, ord FROM test, generate_series(4,6) WITH ORDINALITY AS a(z, ord); x | z | ord ---+---+----- 1 | 4 | 1 2 | 4 | 1 3 | 4 | 1 1 | 5 | 2 2 | 5 | 2 3 | 5 | 2 1 | 6 | 3 2 | 6 | 3 3 | 6 | 3 -- Bruce Momjian https://momjian.us EnterpriseDB https://enterprisedb.com + As you are, so once was I. As I am, so you will be. + + Ancient Roman grave inscription +