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 1tNuhx-002vtu-0e for pgsql-sql@arkaria.postgresql.org; Wed, 18 Dec 2024 14:07:25 +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 1tNuhv-00FBQw-2g for pgsql-sql@arkaria.postgresql.org; Wed, 18 Dec 2024 14:07:22 +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 1tNuhu-00FBQo-PX for pgsql-sql@lists.postgresql.org; Wed, 18 Dec 2024 14:07:22 +0000 Received: from mail-il1-x12e.google.com ([2607:f8b0:4864:20::12e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tNuhr-000PkA-RM for pgsql-sql@lists.postgresql.org; Wed, 18 Dec 2024 14:07:21 +0000 Received: by mail-il1-x12e.google.com with SMTP id e9e14a558f8ab-3ad272538e8so2932935ab.1 for ; Wed, 18 Dec 2024 06:07:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734530838; x=1735135638; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=8eeTbPbCBsU6VGG19dRoRWoyYWGJYF9mjrw1Q/S+wGM=; b=XaIavfR2uKHYSmz3OISRxvIXJs6vSqgtBBGvSj5PHqnXPcWLXIsxOIiBISVcKdGRMd HLQSc8BmvaVaGhubFqDJAIcHt0tYmW4M15q/dPgLXmY/KtCDOcv6WhLPdZbA/f8BmEjT Tf+rWwvG4PfwB+yOECs6O0JTsy8uJe6byc8ATKweNzYpLxnCDDOmAwCRiD8nJ5OSLAih v16qGFL3XTkR3b7+o9+jaTehwcWqLHgbaEk7Asroxu9hz95FxC8eYhxLQI8j6pRO+et0 u5jwye67nWkJ634bUu6mET+4V+Yy/mSotJEXXOZlYND1DQ/oHLccktoV8Dqv5ZoxR9Ap qetA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734530838; x=1735135638; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8eeTbPbCBsU6VGG19dRoRWoyYWGJYF9mjrw1Q/S+wGM=; b=hhEFqMhTH8HqhdViBOzmgdBzS/m8n2amkxH9bXSuqYpQAhVs0f9yfoC2rnUy8AWEo/ fUaOW6bUD/AOPp0PQEJYneHkMUNRdtCwf47AdUul7271iubZzVbJ10I1uLQj98mOiO9m PkBh5bMLkVcbQRz5fuCimY0Yaty4V1x2C9EvWu3J4kfiwn61RXPKzWUPLWe6dpNTz7Xp jSBPYC1qVmUeiHI+XuES1Tzd3Q8Fa+9J3KqU4GjNZcqu7Nj3Qi7Hpx2ipu+wXQZJEvx9 9YUetwa61B1qZ5XdAWbdupmSfvhiqhjaanB+Rzx99ugVfBoFixjIPL7Wb1C8mA5EtGPI D2Pw== X-Gm-Message-State: AOJu0YxC5mOtDs/K3DmIgqVbL6PtMv/tNOeVZUTKKMLAWUDO9zegbkVZ caLnKwOYzgbYytpIdmM2PM2UkTsGznRyxsmYjshBRIf8rjwI6+PvaNjt5SJPI9wrFv26oQ+AWIr YeWTdufeaSrAc9OMi601J19uuZkM= X-Gm-Gg: ASbGnctJpGbZEpfac6LwEBHtN5UIvFg5cp3gCq1Dwi+RIzBneSg7Zficdf2dgNoeepj MJHtzYS3sce+R5JjwyG1Ha/QcHQ7zeoNbJAvPPQ== X-Google-Smtp-Source: AGHT+IEp8u2WCePPeJ5WqVILrSXMk2kLyocdBcnEAskrmg31m+/zZCwee+YgL9Ieqx0lDIOwGoywuGyyMWzAbI5pBrU= X-Received: by 2002:a92:ca09:0:b0:3a7:e8df:3fcb with SMTP id e9e14a558f8ab-3bde3b213f6mr20537805ab.7.1734530838249; Wed, 18 Dec 2024 06:07:18 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Greg Sabino Mullane Date: Wed, 18 Dec 2024 09:06:40 -0500 Message-ID: Subject: Re: Abitity to identify the current iteration in a recursive SELECT (feature request) To: sulfinu@gmail.com Cc: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000077568506298bee2b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000077568506298bee2b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Dec 18, 2024 at 5:00=E2=80=AFAM wrote: > 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 O= N > CONFLICT DO UPDATE clause of an INSERT statement. > Do you mean something like "... WHERE pg_magic_iteration_number < 10"? Looking at the source code, I don't see a trivial way to accomplish that. Maintaining the count as a column in your select is still the canonical way. As someone who writes a lot of recursive CTEs (especially each December!), I'm not sure how useful this feature would be, as the number of loops is rarely the criteria for ending the iterations. I'm using the a recursive SELECT in order to join iteratively several > (virtual) tables computed dinamically based on the iteration number. > Certainly the best solution is to use pl/pgsql, which gets you iterative loops, lots of introspection and ways to break out of the loop, and even true recursion. Cheers, Greg --00000000000077568506298bee2b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Wed, Dec 18, 2024 at 5:00=E2=80=AFAM &= lt;sulfinu@gmail.com> wrote:
Is there a way to obtain d= irectly this iteration sequence number within the SELECT statement following the UNION keyword in a recursive construction? I know it c= an by obtained by maintaining its value in a working table column, but that= 's suboptimal - I need it as a "magic" variable, akin, for ex= ample, the excluded variable a= vailable inside the ON CONFLICT DO UP= DATE clause of an INSERT statement.

Do you mean someth= ing like "... WHERE pg_magic_iteration_number < 10"? Looking a= t the source code, I don't see a trivial way to accomplish that. Mainta= ining=C2=A0the count as a column in your select is still the canonical way.= As someone who writes a lot of recursive CTEs (especially each December!),= I'm not sure how useful this feature would be, as the number of loops = is rarely the criteria for ending the iterations.

I'm = using the a recursive SELECT i= n order to join iteratively several (virtual) tables computed dinamically b= ased on the iteration number.

= Certainly the best solution is to use pl/pgsql, which gets you iterative lo= ops, lots of introspection=C2=A0and ways to break out of the loop, and even= true recursion.

Cheers,
Greg
=
--00000000000077568506298bee2b--