Picking up new rows in a DB

I’ve been playing around a little with postgresql-simple in combination with streaming for picking up inserted rows in a DB table. What I came up with was

eventStreamer :: Connection -> Int -> Stream (Of (Int, Int)) IO ()
eventStreamer conn ver = do
  res <- liftIO $ query conn  "SELECT * FROM events WHERE id > (?) ORDER BY id" (Only ver)
  case res of
    [] -> do
      liftIO $ threadDelay 1000000
      eventStreamer conn ver
    _ -> do
      let ver' = maximum $ map fst res
      each res
      eventStreamer conn ver'

It seems almost too simple so I’m suspecting I missed something.

  • Am I missing something?
  • Should I be using streaming, or is pipes or conduit better (in case they are, in what way)?
Leave a comment