我在查詢中遇到問題,其中一個CTE沒有返回任何行。但是這很難被注意到,並且在很短的時間內調試了一段時間。如何在PostgreSQL中調試公用表表達式?
是否有可能在Postgres中輸出所有CTE,而不會註釋掉主要查詢?
create or replace function generate_grid(
poly geometry, step double precision)
returns setof geometry as
$$ */
with
initial as (select st_xmin(poly) x0, st_ymin(poly) y0),
...(another 3 CTE skipped here)...
grid as (select point from points where st_intersects(point, poly)),
centroid as (select st_centroid(poly) point from grid where (select count(*)=0 from grid))
select * from grid
union all
select * from centroid;
$$ language sql;
在該示例中,CTE centroid
要逐步增量添加到之前運作良好的功能。它應該返回行,以防grid
爲空。該錯誤(,我已經修復)是它沒有,因爲它從空CTE grid
中選擇。現在,當我描述問題時,很明顯它爲什麼會失敗,但是當您編寫它並進行調試時,可能會發生各種各樣的事情,例如混合幾何體SRID,錯誤的SRID等。
你爲什麼會寫這樣的事情擺在首位,如果你不知道是什麼它呢? – joop
@joop當你不小心發出錯誤時,你總是知道你的程序在做什麼。這是一個小的細節,你失蹤,使事情無法正常工作。我添加了與之合作的真實查詢。 –
'...從網格哪裏(選擇計數(*)= 0從網格))'只是不好的風格,這是可以避免的,恕我直言。 [我甚至不知道它是否是有效的語法;我會在這裏使用'NOT EXISTS()'構造IIUC這個片段] – joop