xzw's recent activity

  1. Comment on A PostgreSQL planner semi-join gotcha with CTE, LIMIT, and RETURNING in ~comp

    xzw
    (edited )
    Link
    I don't agree with this. In my opinion the key takeaway should be that if your conditional expression may return different results on different runs or contains side-effects, do not gamble with...
    • Exemplary

    I don't agree with this. In my opinion the key takeaway should be that if your conditional expression may return different results on different runs or contains side-effects, do not gamble with whether the optimizer runs it once or multiple times. Ironically it is materialized CTE that can help you preventing that, see https://www.postgresql.org/docs/17/queries-with.html#QUERIES-WITH-CTE-MATERIALIZATION

    A useful property of WITH queries is that they are normally evaluated only once per execution of the parent query, even if they are referred to more than once by the parent query or sibling WITH queries. Thus, expensive calculations that are needed in multiple places can be placed within a WITH query to avoid redundant work. Another possible application is to prevent unwanted multiple evaluations of functions with side-effects.

    However, if a WITH query is non-recursive and side-effect-free (that is, it is a SELECT containing no volatile functions) then it can be folded into the parent query, allowing joint optimization of the two query levels. By default, this happens if the parent query references the WITH query just once, but not if it references the WITH query more than once. You can override that decision by specifying MATERIALIZED to force separate calculation of the WITH query, or by specifying NOT MATERIALIZED to force it to be merged into the parent query.

    So something like this should work:

    with rows_to_be_deleted as materialized (
        select * from task_queue
        where queue_group_id = 15
        fetch first 1 rows only
        for update skip locked 
    )
    delete from task_queue 
    where id in (select id from rows_to_be_deleted)
    returning item_id;
    

    The point is, put the subquery with side-effects in CTE, not the whole delete expression, and use the materialized keyword to prevent the planner from optimizing it out. I'm not sure the materialized keyword is really needed there, as PostgreSQL should know that locking is a side-effect, and would materialize the CTE either way, but it is a good indicator for fellow developers.

    As a side note: limit is not part of the SQL standard, fetch is.

    5 votes
  2. Comment on Fiction with great “plot devices” in ~misc

    xzw
    Link Parent
    I think The Truman Show was inspired by The Prisoner (1967) series. They have a similar thematic. I loved both.

    I think The Truman Show was inspired by The Prisoner (1967) series. They have a similar thematic. I loved both.

    1 vote
  3. Comment on Anyone interested in trying out Kagi? (trial giveaway: round #2) in ~tech

    xzw
    Link
    I'm also interested in trying it out. Thanks.

    I'm also interested in trying it out. Thanks.

  4. Comment on <deleted topic> in ~tech

    xzw
    Link Parent
    Is it really free if the content creator earns ad revenue for the clicks for example? Or AI just steals money by copying the content and making the information available elsewhere?

    Is it really free if the content creator earns ad revenue for the clicks for example? Or AI just steals money by copying the content and making the information available elsewhere?

    6 votes