Improve last_id behavior using pgsql