title: How do I cap number of rows in based on category using postgres policies?
body: <p>I have a database of <code>orders</code> and each order has a <code>time_slot</code> (of type <code>TIME</code>).</p>
  <pre class="lang-sql prettyprint-override"><code>select id, time_slot from orders limit 5;
  
   10 | 13:00:00
   11 | 12:00:00
   13 | 11:00:00
   14 | 12:30:00
   15 | 11:30:00
  </code></pre>
  <p>I want to make sure that only a certain number of orders can be placed in a time slot; for example, let's say I only want 8 orders per time slot.</p>
  <p>I am using Supabase, so I would like to implement using RLS policies.</p>
  <p>I've been through several iterations, but none of them have worked. Some complain of infinite recursion. My current approach is the following: I have created a view of time slot load.</p>
  <pre class="lang-sql prettyprint-override"><code>create or replace view time_slot_load as 
    select time_slot, count(*) 
    from orders
    group by time_slot;
  select * from time_slot_load limit 5;
  
   11:00:00  |     1
   12:30:00  |     1
   11:30:00  |     1
   13:00:00  |     1
   12:00:00  |     7
  </code></pre>
  <p>I can then create a policy that checks against this view.</p>
  <pre class="lang-sql prettyprint-override"><code>ALTER POLICY &quot;Only 8 orders per time slot&quot;
    ON public.orders
    WITH CHECK (
      (SELECT (load.count &lt;= 8)
         FROM time_slot_load load
         WHERE (load.time_slot = orders.time_slot))
      );
  </code></pre>
  <p>But this is not working.</p>
  <p>Is there some way I can do this using constraints or RLS policies?
  Any help is appreciated.</p>