there isn't an exact analog to oracle's rownum in PG. so in this case you'd just have to keep a counter in your db_foreach that reads the rows returned.