     FROM select_project_collection_plots(_project_id)
     LEFT JOIN plot_locks pl
         ON plot_id = pl.plot_rid
-        AND localtimestamp > pl.lock_end
     WHERE plotId < _plot_id
         AND user_id IS NULL
-        AND pl.lock_end IS NULL
+        AND (pl.lock_end IS NULL
+            OR localtimestamp > pl.lock_end)
     ORDER BY plotId DESC
     LIMIT 1
 

    FROM select_project_collection_plots(_project_id) as spp
    LEFT JOIN plot_locks pl
        ON plot_id = pl.plot_rid
        AND localtimestamp > pl.lock_end
    WHERE plotId = _plot_id
        AND user_id IS NULL
        AND pl.lock_end IS NULL

$$ LANGUAGE SQL;


    FROM select_project_collection_plots(_project_id)
    LEFT JOIN plot_locks pl
        ON plot_id = pl.plot_rid
    WHERE plotId > _plot_id
        AND user_id IS NULL
        AND (pl.lock_end IS NULL
            OR localtimestamp > pl.lock_end)
    ORDER BY plotId ASC
    LIMIT 1

