How Can an Always-False PostgreSQL Query Run for Hours?

Viewed 57

I've been trying to debug a slow query in another part of our system, and saw this query is active:

SELECT * FROM xdmdf.hematoma AS "zzz4" WHERE (0 = 1)

It has apparently been active for > 8 hours. With that WHERE clause, logically, this query should return zero rows. Why would a SQL engine even bother to evaluate it? Would a query like this be useful for anything, and if so, what could it be?

(xdmdf.hematoma is a view and I would expect SELECT * on it to take ~30 minutes under non-locky conditions.)

This statement:

explain select 1 from xdmdf.hematoma limit 1

(no analyze) has been running for about 10 minutes now.

1 Answers

There are two possibilities:

  1. It takes forever to plan the statement, because you changed some planner settings and the view definition is so complicated (partitioned table?).

    This is the unlikely explanation.

  2. A concurrent transaction is holding an ACCESS EXCLUSIVE lock on a table involved in the view definition.

    Terminate any such concurrent transactions.

Related