There are existing solutions for queues in Postgres, notably pgmq.