SELECT .. FOR UPDATE?:https://www.postgresql.org/docs/10/static/sql-select.html#SQL-FOR-UPDATE-SHAREhttps://www.postgresql.org/docs/10/static/explicit-locking.html#LOCKING-ROWS
It says: "Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be specified with GROUP BY."
Regards, Vitaliy