DO $$
BEGIN
WITH oauthDelete AS (
	DELETE FROM oauthaccessdata o
	WHERE NOT EXISTS (
		SELECT p.* FROM preferences p
		WHERE o.clientid = p.name AND o.userid = p.userid AND p.category = 'oauth_app'
	)
	RETURNING o.token
)
DELETE FROM sessions s WHERE s.token in (select oauthDelete.token from oauthDelete);
END $$;
