Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tNqu9-002Znm-GQ for pgsql-sql@arkaria.postgresql.org; Wed, 18 Dec 2024 10:03:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tNqu8-00D2zO-Qh for pgsql-sql@arkaria.postgresql.org; Wed, 18 Dec 2024 10:03:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tN8E3-005E1l-6a for pgsql-sql@lists.postgresql.org; Mon, 16 Dec 2024 10:21:20 +0000 Received: from mail-oo1-xc31.google.com ([2607:f8b0:4864:20::c31]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tN8E0-0037fT-Na for pgsql-sql@lists.postgresql.org; Mon, 16 Dec 2024 10:21:18 +0000 Received: by mail-oo1-xc31.google.com with SMTP id 006d021491bc7-5f2cf57ca52so282124eaf.3 for ; Mon, 16 Dec 2024 02:21:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734344474; x=1734949274; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=rrwC10Kzmm2F6AhzSI+kCTUyft2g3ZKdoMONYNRS6io=; b=Mu23+EwSYUgAV0DoQv6yBlBl3CtpoaVIq4EXXe8gFQ7YiGNTGdTxOnfPCN+rjMjpr1 eRG/hXcEKv0bn4WVqhsBtYsI09ziEU7dJrPwyPCVXaCl2SJkkZ9N9JCZL8kLoBksPvM7 l0GsuzX++gIZh3R0MMsHvi4oJPs51brOc2ZsSHmkUMUeoDhBrwOgleYWiXzfVeG5Iqxf 8oQH1aJkc5yX0KKM3PF2Lgo3E8YDrx1WgTfSsHahj1IBEl7SuFFhe0L4H+BXevf6+Ym4 GcyGto4tldffjbslbmaZS8jt6FU/TTI968nZjblDddKMaZ7UmQA/NwKShZ0+qyTC4LKu 48UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734344474; x=1734949274; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=rrwC10Kzmm2F6AhzSI+kCTUyft2g3ZKdoMONYNRS6io=; b=lqbQGU0Blx3eCmGV4xSKMfW51C+9EdJ45SuDQXvBNyssCBt9VX/b0Hb4rnVJhugfjB aEYhPSW0sCIfE/0KG8ErDml/FsrihKCR88FfBs0OYb4qxUUyjnLbf9LkPlY2xn9O8Apt 72w2cvHlt0wu10ZUEzyqp2u6HP+HPqp+kXck1J6bXQ3veGPZbxczPVE6QlteMKIrfiZe FpL58XrB5isb8911VcyZVn7nCZLewjs3kTp0ysZ/CmyfjkywH88kbU37kCEJrBdXVm4R ax+H7ijtnySZAVDtMndm9d8mG6zYNMMBXnKfDalMg1RJbqn5XGbDn7OABm9hHOtO++LE DJcw== X-Gm-Message-State: AOJu0YwoTpEJBeL/Upn7CsyQdrG0C/oKatzGEamRRgAgjua+f3u8PoFr qwal0UIqV+VQ3CEzFLdNIFJNUe7dVIIsxoobmtjmy7ZKAUrireU6k8tfJ0GIannGZCkY9oTn6pB pSKsdydAh2aGnYSoXIo7Y8V6FQ5JWj5Yl X-Gm-Gg: ASbGncuuLh9fcJcb6+Ap28fe4gqe1FpdFC5y4oh7jJQcWvEthRL5mu0VDFMUW8Xslll blvvyw71iUc83lhSqKmfNj1xgY25+SmRG5WUzh6aB X-Google-Smtp-Source: AGHT+IGu04GP8kFEyoTwr7SZnxp11WTze5QWuaiIIAlP7si2okoOw8ssmRgaAY91hbAxJqXXK3GSiYiZJ0BdguqSbOk= X-Received: by 2002:a05:6870:9a20:b0:29e:1875:222d with SMTP id 586e51a60fabf-2a3ac4a071cmr1927448fac.1.1734344474168; Mon, 16 Dec 2024 02:21:14 -0800 (PST) MIME-Version: 1.0 From: sulfinu@gmail.com Date: Mon, 16 Dec 2024 12:21:03 +0200 Message-ID: Subject: Abitity to identify the current iteration in a recursive SELECT (feature request) To: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000004d1e940629608a93" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000004d1e940629608a93 Content-Type: text/plain; charset="UTF-8" Hi, the algorithm present here for the resolution of WITH RECURSIVE queries is, broadly speaking, iterative, with the iteration sequence number indicated in the first row item of the field generated by the SEARCH BREADTH FIRST clause, i.e. *0*, *1*, *2* etc. Is there a way to obtain directly this iteration sequence number within the SELECT statement following the UNION keyword in a recursive construction? I know it can by obtained by maintaining its value in a working table column, but that's suboptimal - I need it as a "magic" variable, akin, for example, the excluded variable available inside the ON CONFLICT DO UPDATE clause of an INSERT statement. *Background* I'm using the a recursive SELECT in order to join iteratively several (virtual) tables computed dinamically based on the iteration number. Why? In order to implement a set intersection, with each set extracted from a jsonb column of a table. If the iteration number was a distinct column of the working table, the table joined in each iteration would need use LATERAL, thus building it for as many times as the number of rows in the working table instead of *just once*. *Workaround* I simulated the said missing variable with a sequence, which is cumbersome because; - the sequence is single-use, so it must be named randomly, created before and dropped after the WITH RECURSIVE ... SELECT statement; - nextval('sequence_name') *cannot* be used directly within WHERE clauses, because it is re-evaluated for each row; the next sequence value must be reached through a phrase like join (select nextval('sequence_name')) in order to ensure a single evaluation per iteration. --0000000000004d1e940629608a93 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

the algorithm present here for the resolution of WITH RECURSIVE queries is, broadly speaking, iterative, wi= th the iteration sequence number indicated in the first row item of the fie= ld generated by the SEARCH BREADTH FI= RST clause, i.e. 0, 1, 2 etc.

Is there a way to obtain directly this iteration sequence number wi= thin the SELECT statement foll= owing the UNION keyword in a r= ecursive construction? I know it can by obtained by maintaining its value i= n a working table column, but that's suboptimal - I need it as a "= magic" variable, akin, for example, the excluded variable available inside the ON CONFLICT DO UPDATE clause of an INSERT statement.

= Background
I'm using the a recursive SELECT in order to join iteratively several (virtu= al) tables computed dinamically based on the iteration number. Why? In orde= r to implement a set intersection, with each set extracted from a jsonb column of a table.
If t= he iteration number was a distinct column of the working table, the table j= oined in each iteration would need use LATERAL, thus building it for as many times as the number of rows = in the working table instead of just once.

<= div>Workaround
I simulated the said missing variable with = a sequence, which is cumbersome because;
  • the sequence is = single-use, so it must be named randomly, created before and dropped after = the WITH RECURSIVE ... SELECT = statement;
  • nextval('seque= nce_name') cannot be used directly within WHERE clauses, because it is re-evaluated for= each row; the next sequence value must be reached through a phrase like join (select nextval('sequence_name= ')) in order to ensure a single evaluation per iteration.
--0000000000004d1e940629608a93--