|
| 1 | +------------------------------------------------------------------------------- |
| 2 | +-- _vectorizer_create_async_batch__table |
| 3 | +create or replace function ai._vectorizer_create_async_batch_tables( |
| 4 | + schema_name name, |
| 5 | + async_batch_queue_table name, |
| 6 | + async_batch_chunks_table name, |
| 7 | + source_pk pg_catalog.jsonb, |
| 8 | + grant_to name [] |
| 9 | +) returns void as |
| 10 | +$func$ |
| 11 | +declare |
| 12 | + _sql text; |
| 13 | + _index_name text; |
| 14 | + _pk_cols pg_catalog.text; |
| 15 | +begin |
| 16 | + -- create the batches table |
| 17 | + select pg_catalog.format |
| 18 | + ( $sql$create table %I.%I( |
| 19 | + id VARCHAR(255) PRIMARY KEY, |
| 20 | + created_at TIMESTAMP(0) NOT NULL DEFAULT NOW(), |
| 21 | + status TEXT NOT NULL, |
| 22 | + errors JSONB, |
| 23 | + metadata JSONB, |
| 24 | + next_attempt_after TIMESTAMPTZ NOT NULL, |
| 25 | + total_attempts INT NOT NULL DEFAULT 0 |
| 26 | + )$sql$ |
| 27 | + , schema_name |
| 28 | + , async_batch_queue_table |
| 29 | + ) into strict _sql |
| 30 | + ; |
| 31 | + execute _sql; |
| 32 | + |
| 33 | + select pg_catalog.format |
| 34 | + ( $sql$create index on %I.%I (status)$sql$ |
| 35 | + , schema_name |
| 36 | + , async_batch_queue_table |
| 37 | + ) into strict _sql |
| 38 | + ; |
| 39 | + execute _sql; |
| 40 | + |
| 41 | + select pg_catalog.string_agg(pg_catalog.format('%I', x.attname), ', ' order by x.pknum) |
| 42 | + into strict _pk_cols |
| 43 | + from pg_catalog.jsonb_to_recordset(source_pk) x(pknum int, attname name) |
| 44 | + ; |
| 45 | + |
| 46 | + -- create the batch chunks table. The chunk content needs to be stored |
| 47 | + -- because when retrieving the batches, we need to map each embedding to |
| 48 | + -- the chunk so that we can save them in the embeddings store table. |
| 49 | + select pg_catalog.format( |
| 50 | + $sql$ |
| 51 | + create table %I.%I( |
| 52 | + %s, |
| 53 | + chunk_seq int not null, |
| 54 | + created_at timestamptz not null default now(), |
| 55 | + async_batch_id text not null references %I.%I (id) on delete cascade, |
| 56 | + chunk text not null, |
| 57 | + unique (%s, chunk_seq) |
| 58 | + )$sql$, |
| 59 | + schema_name, |
| 60 | + async_batch_chunks_table, |
| 61 | + ( |
| 62 | + select pg_catalog.string_agg( |
| 63 | + pg_catalog.format('%I %s not null' , x.attname , x.typname), |
| 64 | + ', ' |
| 65 | + order by x.attnum |
| 66 | + ) |
| 67 | + from pg_catalog.jsonb_to_recordset(source_pk) x(attnum int, attname name, typname name) |
| 68 | + ), |
| 69 | + schema_name, |
| 70 | + async_batch_queue_table, |
| 71 | + _pk_cols |
| 72 | + ) into strict _sql |
| 73 | + ; |
| 74 | + execute _sql; |
| 75 | + |
| 76 | + if grant_to is not null then |
| 77 | + -- grant select, update, delete on batches table to grant_to roles |
| 78 | + select pg_catalog.format( |
| 79 | + $sql$grant select, insert, update, delete on %I.%I to %s$sql$, |
| 80 | + schema_name, |
| 81 | + async_batch_queue_table, |
| 82 | + ( |
| 83 | + select pg_catalog.string_agg(pg_catalog.quote_ident(x), ', ') |
| 84 | + from pg_catalog.unnest(grant_to) x |
| 85 | + ) |
| 86 | + ) into strict _sql; |
| 87 | + execute _sql; |
| 88 | + |
| 89 | + -- grant select, update, delete on batch chunks table to grant_to roles |
| 90 | + select pg_catalog.format( |
| 91 | + $sql$grant select, insert, update, delete on %I.%I to %s$sql$, |
| 92 | + schema_name, |
| 93 | + async_batch_chunks_table, |
| 94 | + ( |
| 95 | + select pg_catalog.string_agg(pg_catalog.quote_ident(x), ', ') |
| 96 | + from pg_catalog.unnest(grant_to) x |
| 97 | + ) |
| 98 | + ) into strict _sql; |
| 99 | + execute _sql; |
| 100 | + end if; |
| 101 | +end; |
| 102 | +$func$ |
| 103 | +language plpgsql volatile security invoker |
| 104 | +set search_path to pg_catalog, pg_temp; |
| 105 | + |
| 106 | +------------------------------------------------------------------------------- |
| 107 | +-- vectorizer_enable_async_batches |
| 108 | +create or replace function ai.vectorizer_enable_async_batches( |
| 109 | + vectorizer_id pg_catalog.int4 |
| 110 | +) returns void |
| 111 | +as $func$ |
| 112 | +declare |
| 113 | + _config pg_catalog.jsonb; |
| 114 | +begin |
| 115 | + select config into _config |
| 116 | + from ai.vectorizers |
| 117 | + where id = vectorizer_id; |
| 118 | + |
| 119 | + if _config is null then |
| 120 | + raise exception 'vectorizer with id % not found', vectorizer_id; |
| 121 | + end if; |
| 122 | + |
| 123 | + if not _config ? 'use_async_batch_api' then |
| 124 | + raise exception 'vectorizer configuration does not support async batch api'; |
| 125 | + end if; |
| 126 | + |
| 127 | + update ai.vectorizers |
| 128 | + set config = jsonb_set(config, '{async_batch_enabled}', 'true'::jsonb) |
| 129 | + where id = vectorizer_id; |
| 130 | + |
| 131 | + perform |
| 132 | +end |
| 133 | +$func$ language plpgsql security definer |
| 134 | +set search_path to pg_catalog, pg_temp; |
| 135 | + |
| 136 | +------------------------------------------------------------------------------- |
| 137 | +-- vectorizer_disable_async_batches |
| 138 | +create or replace function ai.vectorizer_disable_async_batches( |
| 139 | + vectorizer_id pg_catalog.int4 |
| 140 | +) returns void |
| 141 | +as $func$ |
| 142 | +declare |
| 143 | + _config pg_catalog.jsonb; |
| 144 | +begin |
| 145 | + select config into _config |
| 146 | + from ai.vectorizers |
| 147 | + where id = vectorizer_id; |
| 148 | + |
| 149 | + if _config is null then |
| 150 | + raise exception 'vectorizer with id % not found', vectorizer_id; |
| 151 | + end if; |
| 152 | + |
| 153 | + if not _config ? 'use_async_batch_api' then |
| 154 | + raise exception 'vectorizer configuration does not support async batch api'; |
| 155 | + end if; |
| 156 | + |
| 157 | + update ai.vectorizers |
| 158 | + set config = jsonb_set(config, '{async_batch_enabled}', 'false'::jsonb) |
| 159 | + where id = vectorizer_id; |
| 160 | + |
| 161 | + perform |
| 162 | +end |
| 163 | +$func$ language plpgsql security definer |
| 164 | +set search_path to pg_catalog, pg_temp; |
0 commit comments