diff --git a/src/backend/executor/tqueue.c b/src/backend/executor/tqueue.c
index ee4e03e..8a6c6f3 100644
--- a/src/backend/executor/tqueue.c
+++ b/src/backend/executor/tqueue.c
@@ -234,6 +234,8 @@ TupleQueueFunnelNext(TupleQueueFunnel *funnel, bool nowait, bool *done)
 				   &funnel->queue[funnel->nextqueue + 1],
 				   sizeof(shm_mq_handle *)
 						* (funnel->nqueues - funnel->nextqueue));
+
+			funnel->nextqueue = (funnel->nextqueue + 1) % funnel->nqueues;
 			if (funnel->nextqueue < waitpos)
 				--waitpos;
 		}
@@ -260,7 +262,7 @@ TupleQueueFunnelNext(TupleQueueFunnel *funnel, bool nowait, bool *done)
 		 * and return NULL (if we're in non-blocking mode) or wait for the
 		 * process latch to be set (otherwise).
 		 */
-		if (funnel->nextqueue == waitpos)
+		if (result != SHM_MQ_DETACHED && funnel->nextqueue == waitpos)
 		{
 			if (nowait)
 				return NULL;
