Which should I use for `DELETE ... RETURNING *` @SqlUpdate or @SqlQuery?

530 views
Skip to first unread message

Stephen Connolly

unread,
Jul 11, 2018, 9:47:10 AM7/11/18
to jDBI
I'm using PostgreSQL's SKIP LOCKED... e.g.

DELETE FROM task_queue 
WHERE id = (SELECT id FROM task_queue ORDER BY id LIMIT 1 FOR UPDATE SKIP LOCKED) 
RETURNING *

So in my interface... do I do:

@SqlUpdate("DELETE FROM task_queue "
+ "WHERE id = ( "
+ " SELECT id "
+ " FROM task_queue "
+ " ORDER BY id "
+ " LIMIT 1 "
+ " FOR UPDATE SKIP LOCKED "
+ ") "
+ "RETURNING *;")
Optional<Task> next();

or

@SqlQuery("DELETE FROM task_queue "
+ "WHERE id = ( "
+ " SELECT id "
+ " FROM task_queue "
+ " ORDER BY id "
+ " LIMIT 1 "
+ " FOR UPDATE SKIP LOCKED "
+ ") "
+ "RETURNING *;")
Optional<Task> next();

And yes, i'll suck it and see... but would really like the official guidance

Stephen Connolly

unread,
Jul 11, 2018, 9:48:24 AM7/11/18
to jDBI
jDBI version 3+

Matthew Hall

unread,
Jul 11, 2018, 12:12:26 PM7/11/18
to jd...@googlegroups.com
Try using @GetGeneratedKeys without naming any columns in the annotation. Postgres returns the entire affected row as generated keys.

I've used this on inserts and updates, but never tried it on a delete--let us know how it goes.

-Matthew

--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen Connolly

unread,
Jul 12, 2018, 10:19:32 AM7/12/18
to jd...@googlegroups.com
I ended up using @SqlQuery because (apart from the case of my query where i have LIMIT 1) there is the potential to return multiple rows and I don't think @GetGeneratedKeys would handle multi-row returns... seems to be working
Reply all
Reply to author
Forward
0 new messages