mirror of
https://github.com/aljazceru/khatru.git
synced 2026-01-07 23:44:25 +01:00
use generated columns and gin array indexes to query tags better.
This commit is contained in:
@@ -13,6 +13,12 @@ func initDB(dburl string) (*sqlx.DB, error) {
|
||||
}
|
||||
|
||||
_, err = db.Exec(`
|
||||
CREATE FUNCTION tags_to_tagvalues(jsonb) RETURNS text[]
|
||||
AS 'SELECT array_agg(t->>1) FROM (SELECT jsonb_array_elements($1) AS t)s;'
|
||||
LANGUAGE SQL
|
||||
IMMUTABLE
|
||||
RETURNS NULL ON NULL INPUT;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS event (
|
||||
id text NOT NULL,
|
||||
pubkey text NOT NULL,
|
||||
@@ -20,14 +26,15 @@ CREATE TABLE IF NOT EXISTS event (
|
||||
kind integer NOT NULL,
|
||||
tags jsonb NOT NULL,
|
||||
content text NOT NULL,
|
||||
sig text NOT NULL
|
||||
sig text NOT NULL,
|
||||
|
||||
tagvalues text[] GENERATED ALWAYS AS (tags_to_tagvalues(tags)) STORED
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ididx ON event (id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS pubkeytimeidx ON event (pubkey, created_at);
|
||||
CREATE INDEX IF NOT EXISTS arbitrarytagvalues ON event USING gin (tagvalues);
|
||||
`)
|
||||
log.Print(err)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
const tagConditions = `jsonb_path_match(tags, '$[*][1] == $value', jsonb_build_object('value', ?::text))`
|
||||
|
||||
Reference in New Issue
Block a user