pg_tviews 0.1.0-beta.31

Transactional materialized views with incremental refresh for PostgreSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Declared JSON-path indexes (option `indexes`, #226): expression btrees over
//! `data` written the way `FraiseQL` writes its filters and sorts, so the planner
//! matches them: `data->>'k'`, `data->'a'->>'b'`, `(data->>'k')::bigint`.
//!
//! An index on `data` makes every refresh of a row a non-HOT update (the
//! refresh rewrites `data`): a declared index costs writes for the reads it
//! serves.

use super::ManagedIndex;
use crate::error::{TViewError, TViewResult};
use pgrx::pg_sys;
use serde_json::Value;

/// The type a key is cast to, as `FraiseQL` casts a typed field. Only immutable
/// casts: `text → timestamptz` (and `date`, `time`) depend on settings, so no
/// index can be built over them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyType {
    Text,
    Bigint,
    Numeric,
    Boolean,
}

impl KeyType {
    fn parse(name: &str) -> TViewResult<Self> {
        match name {
            "text" => Ok(Self::Text),
            "bigint" => Ok(Self::Bigint),
            "numeric" => Ok(Self::Numeric),
            "boolean" => Ok(Self::Boolean),
            "timestamptz" | "date" | "time" => Err(invalid(format!(
                "a cast to {name} is not immutable, so no index can serve it: sort the ISO \
                 8601 text instead (COLLATE \"C\"), or keep the value in a column of its own"
            ))),
            other => Err(invalid(format!(
                "unknown type \"{other}\" (known: text, bigint, numeric, boolean)"
            ))),
        }
    }

    const fn name(self) -> &'static str {
        match self {
            Self::Text => "text",
            Self::Bigint => "bigint",
            Self::Numeric => "numeric",
            Self::Boolean => "boolean",
        }
    }
}

/// One key of an index: a path into `data`, its type and its order.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct JsonKey {
    path: Vec<String>,
    key_type: KeyType,
    desc: bool,
}

/// A declared index: its keys, in order.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct JsonIndex {
    keys: Vec<JsonKey>,
}

fn invalid(reason: impl Into<String>) -> TViewError {
    TViewError::InvalidInput {
        parameter: "indexes".to_string(),
        reason: reason.into(),
    }
}

/// PostgreSQL's limit on the keys of an index.
const MAX_KEYS: usize = 32;

/// Parse the `indexes` option: an array of indexes, each a non-empty array of
/// keys, no index twice.
///
/// # Errors
/// [`TViewError::InvalidInput`] naming what is wrong.
pub fn parse(value: &Value) -> TViewResult<Vec<JsonIndex>> {
    let shape = || {
        invalid(
            "must be an array of indexes, each an array of keys: a path (\"author.id\") or \
             {\"path\": …, \"type\": \"text\"|\"bigint\"|\"numeric\"|\"boolean\", \"desc\": …}",
        )
    };
    let Value::Array(indexes) = value else {
        return Err(shape());
    };
    let mut parsed: Vec<JsonIndex> = Vec::new();
    for index in indexes {
        let Value::Array(keys) = index else {
            return Err(shape());
        };
        if keys.is_empty() || keys.len() > MAX_KEYS {
            return Err(invalid(format!(
                "an index has 1 to {MAX_KEYS} keys, not {}",
                keys.len()
            )));
        }
        let index = JsonIndex {
            keys: keys.iter().map(parse_key).collect::<TViewResult<_>>()?,
        };
        if parsed.contains(&index) {
            return Err(invalid(format!(
                "index {} is declared twice",
                index.canonical()
            )));
        }
        parsed.push(index);
    }
    Ok(parsed)
}

fn parse_key(key: &Value) -> TViewResult<JsonKey> {
    match key {
        Value::String(path) => Ok(JsonKey {
            path: parse_path(path)?,
            key_type: KeyType::Text,
            desc: false,
        }),
        Value::Object(fields) => {
            let mut path = None;
            let mut key_type = KeyType::Text;
            let mut desc = false;
            for (field, value) in fields {
                match (field.as_str(), value) {
                    ("path", Value::String(p)) => path = Some(parse_path(p)?),
                    ("type", Value::String(t)) => key_type = KeyType::parse(t)?,
                    ("desc", Value::Bool(d)) => desc = *d,
                    ("path" | "type" | "desc", _) => {
                        return Err(invalid(format!(
                            "a key's \"{field}\" must be a {}",
                            if field == "desc" { "boolean" } else { "string" }
                        )));
                    }
                    (other, _) => {
                        return Err(invalid(format!(
                            "unknown key field \"{other}\" (known: path, type, desc)"
                        )));
                    }
                }
            }
            Ok(JsonKey {
                path: path.ok_or_else(|| invalid("a key object needs a \"path\""))?,
                key_type,
                desc,
            })
        }
        _ => Err(invalid(
            "a key is a path string or an object with a \"path\"",
        )),
    }
}

fn parse_path(path: &str) -> TViewResult<Vec<String>> {
    let segments: Vec<String> = path.split('.').map(str::to_string).collect();
    if segments.iter().any(String::is_empty) {
        return Err(invalid(format!(
            "path \"{path}\" has an empty segment: name the keys from data down, \
             separated by dots"
        )));
    }
    Ok(segments)
}

impl JsonKey {
    /// The key as declared, shortest form: the path alone for an ascending text key.
    fn canonical(&self) -> Value {
        let path = self.path.join(".");
        if self.key_type == KeyType::Text && !self.desc {
            return Value::String(path);
        }
        let mut key = serde_json::Map::new();
        key.insert("path".into(), Value::String(path));
        if self.key_type != KeyType::Text {
            key.insert("type".into(), Value::String(self.key_type.name().into()));
        }
        if self.desc {
            key.insert("desc".into(), Value::Bool(true));
        }
        Value::Object(key)
    }

    /// The expression, as `pg_get_indexdef()` writes it, so a dump restores the
    /// same parse tree: `(data ->> 'k'::text)`, `((data -> 'a'::text) ->> 'b'::text)`,
    /// `((data ->> 'k'::text))::bigint`.
    fn expression(&self) -> String {
        let literal = |segment: &str| format!("{}::text", crate::utils::quote_literal(segment));
        let (last, objects) = self.path.split_last().unwrap_or((&self.path[0], &[]));
        let object = objects.iter().fold("data".to_string(), |e, s| {
            format!("({e} -> {})", literal(s))
        });
        let text = format!("({object} ->> {})", literal(last));
        match self.key_type {
            KeyType::Text => text,
            typed => format!("({text})::{}", typed.name()),
        }
    }

    /// Its part of the index name.
    fn label(&self) -> String {
        let mut label = self.path.join("_");
        if self.key_type != KeyType::Text {
            label = format!("{label}_{}", self.key_type.name());
        }
        if self.desc {
            label.push_str("_desc");
        }
        label
    }
}

impl JsonIndex {
    /// The `indexes` option ([`parse`]).
    ///
    /// # Errors
    /// [`TViewError::InvalidInput`] naming what is wrong.
    pub fn parse_option(value: &Value) -> TViewResult<Vec<Self>> {
        parse(value)
    }

    /// The index as declared, shortest form (`tviews.registry` publishes it).
    #[must_use]
    pub fn canonical(&self) -> Value {
        Value::Array(self.keys.iter().map(JsonKey::canonical).collect())
    }

    /// The index `pg_tviews` creates for it on `tview_name`:
    /// `idx_<tview>_data_<keys>`.
    #[must_use]
    pub fn managed(&self, tview_name: &str) -> ManagedIndex {
        let mut label = self
            .keys
            .iter()
            .map(JsonKey::label)
            .collect::<Vec<_>>()
            .join("_");
        // The data GIN index is idx_<tview>_data_gin.
        if label == "gin" {
            label.push_str("_text");
        }
        let elements = self
            .keys
            .iter()
            .map(|k| format!("({}){}", k.expression(), if k.desc { " DESC" } else { "" }))
            .collect::<Vec<_>>()
            .join(", ");
        ManagedIndex::expressions(tview_name, format!("data_{label}"), elements)
    }
}

/// The declared indexes as `tviews.registry` publishes them.
#[must_use]
pub fn canonical(indexes: &[JsonIndex]) -> Value {
    Value::Array(indexes.iter().map(JsonIndex::canonical).collect())
}

/// The declared indexes of `entity` that are on its table under their names, as
/// `pg_tviews`' own: what [`apply`] compares a declaration with.
///
/// # Errors
/// A failed catalog read.
pub(crate) fn present(entity: &str, table: pg_sys::Oid) -> TViewResult<Vec<JsonIndex>> {
    let tview = format!("tv_{entity}");
    let on_table = names_on(table)?;
    let recorded = crate::catalog::indexes::recorded(table)?;
    Ok(declared(entity)?
        .into_iter()
        .filter(|index| {
            let name = index.managed(&tview).name;
            on_table.contains(&name) && recorded.contains(&name)
        })
        .collect())
}

/// Bring `entity`'s table (`table`, in `schema`) to the `desired` indexes: drop
/// the declared ones no longer desired, create the missing ones, and store the
/// declaration. The others are left as they are.
///
/// # Errors
/// A TVIEW without a `data` column to index, a name another relation holds, a
/// failed `CREATE` / `DROP INDEX` or catalog write.
pub(crate) fn apply(
    entity: &str,
    schema: &str,
    table: pg_sys::Oid,
    desired: &[JsonIndex],
) -> TViewResult<()> {
    let tview = format!("tv_{entity}");
    if !desired.is_empty()
        && !crate::utils::column_types(table)?
            .iter()
            .any(|(c, _)| c == "data")
    {
        return Err(invalid(format!("{tview} has no data column to index")));
    }
    let present = present(entity, table)?;
    let wanted: Vec<ManagedIndex> = desired.iter().map(|i| i.managed(&tview)).collect();
    let mut dropped = Vec::new();
    for index in present.iter().map(|i| i.managed(&tview)) {
        if !wanted.iter().any(|w| w.name == index.name) {
            crate::utils::spi::run_ddl(&format!(
                "DROP INDEX {}.{}",
                crate::utils::ident::quoted(schema),
                crate::utils::ident::quoted(&index.name)
            ))?;
            dropped.push(index.name);
        }
    }
    crate::catalog::indexes::forget(table, &dropped)?;
    let present: Vec<String> = present.iter().map(|i| i.managed(&tview).name).collect();
    let mut created = Vec::new();
    for index in wanted.iter().filter(|w| !present.contains(&w.name)) {
        match index.create(schema, &tview)? {
            Some(name) => created.push(name),
            None => {
                return Err(TViewError::IndexNameReserved {
                    table: crate::utils::qualified_relname_from_oid(table)?,
                    index: index.name.clone(),
                });
            }
        }
    }
    crate::catalog::indexes::record(table, &created)?;
    crate::catalog::indexes::declare(entity, canonical(desired))
}

/// The `indexes` option `entity` declared.
///
/// # Errors
/// A failed catalog read, or a stored declaration this release cannot read.
pub(crate) fn declared(entity: &str) -> TViewResult<Vec<JsonIndex>> {
    parse(&crate::catalog::indexes::declared(entity)?)
}

/// The names of the indexes on `table`.
fn names_on(table: pg_sys::Oid) -> TViewResult<Vec<String>> {
    crate::utils::spi::strings(
        "SELECT c.relname::pg_catalog.text FROM pg_catalog.pg_index i \
         JOIN pg_catalog.pg_class c ON c.oid = i.indexrelid WHERE i.indrelid = $1",
        &[crate::utils::spi::oid(table)],
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn one(value: &Value) -> JsonIndex {
        parse(&json!([value])).unwrap().remove(0)
    }

    #[test]
    fn expressions_are_written_as_pg_get_indexdef_writes_them() {
        let index =
            one(&json!(["status", "author.id", {"path": "age", "type": "bigint", "desc": true}]));
        let managed = index.managed("tv_item");
        assert_eq!(
            managed.name,
            "idx_tv_item_data_status_author_id_age_bigint_desc"
        );
        assert_eq!(
            managed.ddl("public", "tv_item"),
            "CREATE INDEX IF NOT EXISTS \"idx_tv_item_data_status_author_id_age_bigint_desc\" \
             ON \"public\".\"tv_item\" (((data ->> 'status'::text)), \
             (((data -> 'author'::text) ->> 'id'::text)), \
             (((data ->> 'age'::text))::bigint) DESC)"
        );
    }

    #[test]
    fn canonical_round_trips() {
        let declared = json!([["status"], [{"path": "price", "type": "numeric", "desc": true}],
                              [{"path": "a.b", "type": "text"}]]);
        let parsed = parse(&declared).unwrap();
        let published = canonical(&parsed);
        assert_eq!(
            published,
            json!([["status"], [{"path": "price", "type": "numeric", "desc": true}], ["a.b"]])
        );
        assert_eq!(parse(&published).unwrap(), parsed);
    }

    #[test]
    fn refusals() {
        assert!(parse(&json!(["status"])).is_err());
        assert!(parse(&json!([[]])).is_err());
        assert!(parse(&json!([["a..b"]])).is_err());
        assert!(parse(&json!([["a"], ["a"]])).is_err());
        assert!(parse(&json!([[{"path": "t", "type": "timestamptz"}]])).is_err());
        assert!(parse(&json!([[{"path": "t", "kind": "x"}]])).is_err());
        assert!(parse(&json!([[{"type": "bigint"}]])).is_err());
    }

    #[test]
    fn the_gin_label_is_kept_free() {
        assert_eq!(
            one(&json!(["gin"])).managed("tv_x").name,
            "idx_tv_x_data_gin_text"
        );
    }
}