Consider this CTE:

WITH division_by_zero AS (
  SELECT 1/0
)
SELECT 42

It returns 42 instead of raising an error. How can I force PostgreSQL to evaluate the SELECT 1/0?

有帮助吗?

解决方案

That is because the main query doesn't reference division_by_zero anywhere.

The documentation says:

[...] PostgreSQL's implementation evaluates only as many rows of a WITH query as are actually fetched by the parent query.

That is zero in your case.

If you want the CTE to be executed, you could for example add a WHERE condition like

WHERE EXISTS (SELECT 1 FROM division_by_zero)
许可以下: CC-BY-SA归因
不隶属于 dba.stackexchange
scroll top