diff --git a/api/migrations/migrations.go b/api/migrations/migrations.go index 8915b51..66fa461 100644 --- a/api/migrations/migrations.go +++ b/api/migrations/migrations.go @@ -85,4 +85,23 @@ var Migrations = []migrate.Migration{ SET NOT NULL; `, }, + { + Name: "2022-08-31.0.root-func-index.sql", + SQL: ` + CREATE OR REPLACE FUNCTION proofs_array (data jsonb) + RETURNS text[] + AS $CODE$ + BEGIN + RETURN ARRAY ( + SELECT + jsonb_array_elements(data) ->> 'proof'); + END + $CODE$ + LANGUAGE plpgsql + IMMUTABLE; + + CREATE INDEX proofs_arr_idx ON trees USING GIN ((proofs_array(proofs))); + DROP INDEX "trees_proofs_idx"; + `, + }, } diff --git a/api/root.go b/api/root.go index b558628..03023c7 100644 --- a/api/root.go +++ b/api/root.go @@ -45,7 +45,7 @@ func (s *Server) GetRoot(w http.ResponseWriter, r *http.Request) { const q = ` SELECT root FROM trees - WHERE proofs @> $1 + WHERE proofs_array(proofs) @> proofs_array($1) LIMIT 1 `