Dave's half-right - you've nested four deep and ran out of pools.
Where he's wrong is that all you need to do is to add another pool to your configuration.
The bad news is that nesting db_foreach three deep is a very expensive way to calculate dynamic data. If each db_foreach process 10 rows, the inner query will be executed 100 times. If each process 50 rows ... oh boy.
Are you sure you can't combine the nested loops?