create table if not exists public.usage_operations ( id varchar(40) primary key, user_id varchar(40) not null references public.users(id) on delete cascade, operation varchar(48) not null, period_key varchar(16) not null, idempotency_key varchar(180) not null, status varchar(20) not null default 'reserved', units integer not null default 1 check (units > 0), request_id varchar(100), resource_id varchar(80), created_at timestamptz not null default now(), updated_at timestamptz not null default now(), completed_at timestamptz, constraint uq_usage_operations_user_operation_key unique (user_id, operation, idempotency_key), constraint ck_usage_operations_status check (status in ('reserved', 'completed', 'released')) ); create index if not exists ix_usage_operations_user_period_operation on public.usage_operations(user_id, period_key, operation); alter table public.usage_operations enable row level security; revoke all on table public.usage_operations from anon, authenticated;