create extension if not exists pgcrypto; create schema if not exists billing; create table if not exists billing.reading ( id bigserial primary key, serial text not null check (btrim(serial) <> '' and length(serial) <= 128), kind text not null, metrics jsonb not null check (jsonb_typeof(metrics) = 'object'), created_at timestamptz not null default now(), n integer not null, h bit varying[] not null, y bit varying not null, w integer not null, parameters text not null ); create index if not exists reading_created_at_idx on billing.reading (created_at); create unique index if not exists reading_parameters_uniq on billing.reading (digest(parameters, 'sha256'));