Re: Row pattern recognition

From: Tatsuo Ishii <ishii(at)postgresql(dot)org>
To: assam258(at)gmail(dot)com
Cc: jian(dot)universality(at)gmail(dot)com, zsolt(dot)parragi(at)percona(dot)com, sjjang112233(at)gmail(dot)com, vik(at)postgresfriends(dot)org, er(at)xs4all(dot)nl, jacob(dot)champion(at)enterprisedb(dot)com, david(dot)g(dot)johnston(at)gmail(dot)com, peter(at)eisentraut(dot)org, li(dot)evan(dot)chao(at)gmail(dot)com, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Row pattern recognition
Date: 2026-11-23 06:29:11
Message-ID: 20261123.152911.1208560970857183923.ishii@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Henson,

> Before I send the fixes, I would like to agree on one design point:
> whether, and how, planning keeps the argument of a navigation such as
> PREV() from raising an error at plan time. Section 2 asks you to
> choose between two options. The fix changes how this is implemented,
> so I want to explain the change, and the other ways I tried, before
> you see the patch.
>
> The problem:
>
> WITH t(id, v) AS (VALUES (1, 10))
> SELECT count(*) OVER w AS cnt
> FROM t
> WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
> PATTERN (A) DEFINE A AS PREV(v / 0) > 0);
>
> A navigation evaluates its argument only at the row it moves to, and
> not at all when there is no such row. Here the only row has no
> previous row, so the query should return 0. But pull-up replaces v
> with 10, and constant folding then raises division by zero at plan
> time.

With v55 I don't see the error?

test=#\i test.sql
WITH t(id, v) AS (VALUES (1, 10))
SELECT count(*) OVER w AS cnt
FROM t
WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
PATTERN (A) DEFINE A AS PREV(v / 0) > 0);
cnt
-----
0
(1 row)

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Henson Choi 2026-11-23 06:41:00 Re: Row pattern recognition
Previous Message Sho Ito 2026-11-23 05:58:24 Re: [PATCH] psql: Fix REPACK index completion after a column list