pg_tviews 0.1.0-beta.30

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
406
407
408
409
410
411
412
413
414
415
//! The `options` of `pg_tviews_create_or_replace()`, and the names they hold.

use crate::config::UncascadedPolicy;
use crate::ddl::aggregate::GroupKeys;
use crate::ddl::create::{Compression, DataStorage, JsonIndex, Storage, TOAST_TUPLE_TARGET};
use crate::ddl::uncascaded::{Declarations, TimeRefresh};
use crate::error::{TViewError, TViewResult};
use pgrx::prelude::*;

/// The `options` of `pg_tviews_create_or_replace()` and `pg_tviews_create()`, as
/// passed. `None`: not passed, so the default (ADR 0220).
#[derive(Debug, Default)]
pub(crate) struct Options {
    pub(super) logged: Option<bool>,
    pub(super) fillfactor: Option<i32>,
    pub(super) data_gin_index: Option<bool>,
    pub(super) data_compression: Option<Compression>,
    pub(super) data_storage: Option<DataStorage>,
    pub(super) toast_tuple_target: Option<i32>,
    /// An aggregate TVIEW's group keys.
    pub(super) group_keys: Option<GroupKeys>,
    /// What a write to a table no cascade reaches does.
    pub(super) uncascaded_policy: Option<UncascadedPolicy>,
    /// Tables with a policy of their own, as named: resolved when used.
    pub(super) uncascaded_tables: Option<Vec<(String, UncascadedPolicy)>>,
    /// Functions the definition calls, each with the tables it reads, as
    /// named: resolved when used.
    pub(super) function_reads: Option<Vec<(String, Vec<String>)>>,
    /// How the TVIEW is brought up to date when it reads the current time.
    pub(super) time_refresh: Option<TimeRefresh>,
    /// The GraphQL type name `pg_tviews_flush_and_report()` reports.
    pub(super) typename: Option<String>,
    /// Declared JSON-path indexes.
    pub(super) indexes: Option<Vec<JsonIndex>>,
}

/// A TVIEW as its options declare it: every option, those not passed at their
/// defaults (ADR 0220).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Declared {
    pub storage: Storage,
    pub group_keys: Option<GroupKeys>,
    pub declarations: Declarations,
    /// `None`: `PascalCase(entity)`.
    pub typename: Option<String>,
    pub indexes: Vec<JsonIndex>,
}

impl Options {
    /// Options of a `CREATE [UNLOGGED] TABLE tv_* [WITH (fillfactor = n,
    /// toast_tuple_target = n)] AS`.
    pub(crate) fn storage(
        logged: Option<bool>,
        fillfactor: Option<i32>,
        toast_tuple_target: Option<i32>,
    ) -> Self {
        Self {
            logged,
            fillfactor,
            toast_tuple_target,
            ..Self::default()
        }
    }

    /// What these options declare, every option not passed at its default.
    ///
    /// # Errors
    /// Returns an error naming a table or function that does not resolve.
    pub(crate) fn resolve(self) -> TViewResult<Declared> {
        let defaults = Storage::DEFAULT;
        Ok(Declared {
            storage: Storage {
                logged: self.logged.unwrap_or(defaults.logged),
                fillfactor: self.fillfactor.unwrap_or(defaults.fillfactor),
                data_gin_index: self.data_gin_index.unwrap_or(defaults.data_gin_index),
                data_compression: match self.data_compression {
                    Some(declared) => declared,
                    None => Compression::server_default()?,
                },
                data_storage: self.data_storage.unwrap_or(defaults.data_storage),
                toast_tuple_target: self.toast_tuple_target.or(defaults.toast_tuple_target),
            },
            group_keys: self.group_keys,
            declarations: Declarations::new(
                self.uncascaded_policy.unwrap_or_default(),
                match &self.uncascaded_tables {
                    Some(declared) => resolve_tables(declared)?,
                    None => Vec::new(),
                },
                match &self.function_reads {
                    Some(declared) => resolve_function_reads(declared)?,
                    None => Vec::new(),
                },
                self.time_refresh.unwrap_or(TimeRefresh::None),
            ),
            typename: self.typename,
            indexes: self.indexes.unwrap_or_default(),
        })
    }
}

pub(super) fn invalid(parameter: &str, reason: impl Into<String>) -> TViewError {
    TViewError::InvalidInput {
        parameter: parameter.to_string(),
        reason: reason.into(),
    }
}

/// Parse the `options` object: an unknown key or a value of the wrong type is an
/// error.
///
/// # Errors
/// Returns an error naming the offending key.
pub(crate) fn parse_options(value: &serde_json::Value) -> TViewResult<Options> {
    let serde_json::Value::Object(map) = value else {
        return Err(invalid("options", "must be a JSON object"));
    };
    let mut options = Options::default();
    for (key, value) in map {
        match key.as_str() {
            "logged" => options.logged = Some(boolean(key, value)?),
            "data_gin_index" => options.data_gin_index = Some(boolean(key, value)?),
            "fillfactor" => options.fillfactor = Some(fillfactor(key, value)?),
            // `null`, as tviews.registry publishes them where there is no `data`
            // column or no target: the default.
            "data_compression" => {
                options.data_compression = nullable(value, || {
                    named(key, value, Compression::parse, Compression::NAMES)
                })?;
            }
            "data_storage" => {
                options.data_storage = nullable(value, || {
                    named(key, value, DataStorage::parse, DataStorage::NAMES)
                })?;
            }
            "toast_tuple_target" => {
                options.toast_tuple_target = nullable(value, || toast_tuple_target(key, value))?;
            }
            "uncascaded_policy" => options.uncascaded_policy = Some(policy(key, value, "")?),
            "uncascaded_tables" => options.uncascaded_tables = Some(uncascaded_tables(key, value)?),
            "function_reads" => options.function_reads = Some(function_reads(key, value)?),
            "time_refresh" => options.time_refresh = Some(time_refresh(key, value)?),
            "group_keys" => options.group_keys = group_keys(key, value)?,
            "typename" => options.typename = typename(key, value)?,
            "indexes" => {
                options.indexes = Some(crate::ddl::create::JsonIndex::parse_option(value)?);
            }
            other => {
                return Err(invalid(
                    "options",
                    format!(
                        "unknown option \"{other}\" (known: logged, fillfactor, \
                         data_gin_index, data_compression, data_storage, toast_tuple_target, \
                         group_keys, uncascaded_policy, uncascaded_tables, \
                         function_reads, time_refresh, typename, indexes)"
                    ),
                ));
            }
        }
    }
    Ok(options)
}

fn boolean(key: &str, value: &serde_json::Value) -> TViewResult<bool> {
    value
        .as_bool()
        .ok_or_else(|| invalid(key, "must be a boolean"))
}

fn fillfactor(key: &str, value: &serde_json::Value) -> TViewResult<i32> {
    let fillfactor = value
        .as_i64()
        .ok_or_else(|| invalid(key, "must be an integer"))?;
    if !(10..=100).contains(&fillfactor) {
        return Err(invalid(key, "must be between 10 and 100"));
    }
    i32::try_from(fillfactor).map_err(|_| invalid(key, "must be between 10 and 100"))
}

/// `None` for JSON `null`, else what `parse` reads.
fn nullable<T>(
    value: &serde_json::Value,
    parse: impl FnOnce() -> TViewResult<T>,
) -> TViewResult<Option<T>> {
    if value.is_null() {
        Ok(None)
    } else {
        parse().map(Some)
    }
}

/// One of the names `parse` knows, listed in `names`.
fn named<T>(
    key: &str,
    value: &serde_json::Value,
    parse: fn(&str) -> Option<T>,
    names: &str,
) -> TViewResult<T> {
    value
        .as_str()
        .and_then(parse)
        .ok_or_else(|| invalid(key, format!("must be {names}")))
}

fn toast_tuple_target(key: &str, value: &serde_json::Value) -> TViewResult<i32> {
    let range = || {
        invalid(
            key,
            format!(
                "must be an integer from {} to {}",
                TOAST_TUPLE_TARGET.start(),
                TOAST_TUPLE_TARGET.end()
            ),
        )
    };
    value
        .as_i64()
        .and_then(|v| i32::try_from(v).ok())
        .filter(|v| TOAST_TUPLE_TARGET.contains(v))
        .ok_or_else(range)
}

/// A policy name; `of` names what it applies to in the error ("" for the option).
fn policy(key: &str, value: &serde_json::Value, of: &str) -> TViewResult<UncascadedPolicy> {
    value
        .as_str()
        .and_then(UncascadedPolicy::parse)
        .ok_or_else(|| invalid(key, format!("{of}{POLICIES}")))
}

fn uncascaded_tables(
    key: &str,
    value: &serde_json::Value,
) -> TViewResult<Vec<(String, UncascadedPolicy)>> {
    let serde_json::Value::Object(tables) = value else {
        return Err(invalid(
            key,
            "must be an object mapping each table to its policy, e.g. \
             {\"public.tb_locale\": \"full_refresh\"}",
        ));
    };
    tables
        .iter()
        .map(|(table, p)| {
            Ok((
                table.clone(),
                policy(key, p, &format!("the policy of {table} "))?,
            ))
        })
        .collect()
}

fn function_reads(key: &str, value: &serde_json::Value) -> TViewResult<Vec<(String, Vec<String>)>> {
    let shape = || {
        invalid(
            key,
            "must be an object mapping each function, with its argument types, \
             to the tables it reads, e.g. \
             {\"public.label_suffix()\": [\"public.tb_setting\"]}",
        )
    };
    let serde_json::Value::Object(functions) = value else {
        return Err(shape());
    };
    functions
        .iter()
        .map(|(function, tables)| {
            let tables = tables
                .as_array()
                .ok_or_else(shape)?
                .iter()
                .map(|t| t.as_str().map(str::to_string).ok_or_else(shape))
                .collect::<TViewResult<Vec<_>>>()?;
            Ok((function.clone(), tables))
        })
        .collect()
}

fn time_refresh(key: &str, value: &serde_json::Value) -> TViewResult<TimeRefresh> {
    match value {
        serde_json::Value::Null => Ok(TimeRefresh::None),
        serde_json::Value::String(s) if s == "external" => {
            Ok(TimeRefresh::External { declared: true })
        }
        _ => Err(invalid(
            key,
            "must be \"external\" (pg_tviews_refresh_time_dependent() is \
             called at the boundary) or null",
        )),
    }
}

fn group_keys(key: &str, value: &serde_json::Value) -> TViewResult<Option<GroupKeys>> {
    match value {
        serde_json::Value::Null => Ok(None),
        serde_json::Value::Object(keys) if !keys.is_empty() => Ok(Some(
            serde_json::from_value::<GroupKeys>(value.clone())
                .map_err(|_| invalid(key, "must map source table names to column names"))?,
        )),
        _ => Err(invalid(
            key,
            "must be null or an object mapping each source table to its \
             group key column, e.g. {\"tb_order\": \"fk_user\"}",
        )),
    }
}

fn typename(key: &str, value: &serde_json::Value) -> TViewResult<Option<String>> {
    match value {
        serde_json::Value::Null => Ok(None),
        serde_json::Value::String(name) if is_graphql_name(name) => Ok(Some(name.clone())),
        _ => Err(invalid(
            key,
            "must be null (PascalCase of the entity) or a GraphQL name ([_A-Za-z][_0-9A-Za-z]*)",
        )),
    }
}

/// Whether `name` is a GraphQL name: `[_A-Za-z][_0-9A-Za-z]*`.
pub(crate) fn is_graphql_name(name: &str) -> bool {
    name.chars()
        .next()
        .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
        && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

pub(super) const POLICIES: &str = "must be \"error\", \"full_refresh\" or \"warn\"";

/// The tables of the `uncascaded_tables` option, by OID.
///
/// # Errors
/// Returns an error naming a table that does not exist or is not a table.
pub(super) fn resolve_tables(
    declared: &[(String, UncascadedPolicy)],
) -> TViewResult<Vec<(pg_sys::Oid, UncascadedPolicy)>> {
    declared
        .iter()
        .map(|(name, policy)| Ok((resolve_table(name, "uncascaded_tables")?, *policy)))
        .collect()
}

/// The table an option names, by OID.
///
/// # Errors
/// Returns an error if it does not exist or is not a table.
pub(super) fn resolve_table(name: &str, option: &str) -> TViewResult<pg_sys::Oid> {
    let found = Spi::connect(|client| {
        client
            .select(
                "SELECT c.oid, c.relkind IN ('r', 'p', 'm', 'f') \
                 FROM (SELECT pg_catalog.to_regclass($1) AS relation) r \
                 LEFT JOIN pg_catalog.pg_class c ON c.oid = r.relation",
                None,
                &[crate::utils::spi::text(name)],
            )?
            .first()
            .get_two::<pg_sys::Oid, bool>()
    })
    .map_err(|e| crate::utils::spi::catalog_error(&format!("Look up a table of {option}"), &e))?;
    match found {
        (Some(oid), Some(true)) => Ok(oid),
        (Some(_), _) => Err(invalid(
            option,
            format!("{name} is not a table: name the tables a view reads"),
        )),
        _ => Err(invalid(option, format!("relation {name} does not exist"))),
    }
}

/// The functions of the `function_reads` option, as `schema.name(argument
/// types)`, with their tables by OID.
///
/// # Errors
/// Returns an error naming a function or table that does not exist.
pub(super) fn resolve_function_reads(
    declared: &[(String, Vec<String>)],
) -> TViewResult<Vec<(String, Vec<pg_sys::Oid>)>> {
    let mut reads = Vec::new();
    for (function, tables) in declared {
        let signature = Spi::connect(|client| {
            client
                .select(
                    &format!(
                        "SELECT {} FROM pg_catalog.pg_proc p \
                         WHERE p.oid = pg_catalog.to_regprocedure($1)",
                        crate::lineage::FUNCTION_SIGNATURE
                    ),
                    None,
                    &[crate::utils::spi::text(function)],
                )?
                .first()
                .get_one::<String>()
        })
        .or_else(|e| match e {
            spi::Error::InvalidPosition => Ok(None),
            e => Err(e),
        })
        .map_err(|e| crate::utils::spi::catalog_error("Look up a function of function_reads", &e))?
        .ok_or_else(|| {
            invalid(
                "function_reads",
                format!(
                    "function {function} does not exist: name it with its argument types, \
                     e.g. public.label_suffix() or public.price(bigint, date)"
                ),
            )
        })?;
        let tables = tables
            .iter()
            .map(|t| resolve_table(t, "function_reads"))
            .collect::<TViewResult<Vec<_>>>()?;
        reads.push((signature, tables));
    }
    Ok(reads)
}