Ad
  • Default User Avatar

    It's a thing that only works for simple queries.

    The docs says:

    This works because PostgreSQL's implementation evaluates only as many rows of a WITH query as are actually fetched by the parent query.
    Using this trick in production is not recommended, because other systems might work differently.
    Also, it usually won't work if you make the outer query sort the recursive query's results or join them to some other table,
    because in such cases the outer query will usually try to fetch all of the WITH query's output anyway.
    
    

    https://www.postgresql.org/docs/13/queries-with.html