pg_tviews 0.1.0-beta.27

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
416
417
418
419
//! What a utility statement changes that TVIEWs follow, read before it runs.

use super::{CStr, TViewResult, pg_sys};

/// An `ALTER … RENAME COLUMN` statement, captured before it runs.
pub(super) struct ColumnRename {
    pub(super) relation: *const pg_sys::RangeVar,
    pub(super) old_name: String,
    pub(super) new_name: String,
}

impl ColumnRename {
    /// The renamed relation's OID and the old/new names, once the rename has run.
    /// `None` if the relation is gone (`IF EXISTS` on a missing table).
    pub(super) fn resolve(self) -> Option<(pg_sys::Oid, String, String)> {
        // SAFETY: `relation` points into the statement's parse tree, which lives
        // until the utility statement finishes.
        let relid = unsafe {
            pg_sys::RangeVarGetRelidExtended(
                self.relation,
                pg_sys::NoLock.cast_signed(),
                pg_sys::RVROption::RVR_MISSING_OK,
                None,
                std::ptr::null_mut(),
            )
        };
        (relid != pg_sys::InvalidOid).then_some((relid, self.old_name, self.new_name))
    }
}

/// Whether `pg_tviews` is installed in the current database. A syscache lookup:
/// no SPI, and no catalog query that could fail when the extension is absent.
pub(super) fn extension_installed() -> bool {
    // SAFETY: both calls only read backend state; the syscache lookup runs only
    // inside a transaction, where it is valid.
    unsafe {
        pg_sys::IsTransactionState()
            && pg_sys::get_extension_oid(c"pg_tviews".as_ptr(), true) != pg_sys::InvalidOid
    }
}

/// The column rename carried by `pstmt`, if it is one.
///
/// SAFETY: `pstmt` must be null or a valid `PlannedStmt` from the `ProcessUtility` hook.
pub(super) unsafe fn column_rename_of(pstmt: *const pg_sys::PlannedStmt) -> Option<ColumnRename> {
    // SAFETY: every pointer is null-checked before it is dereferenced.
    unsafe {
        if pstmt.is_null() || (*pstmt).utilityStmt.is_null() {
            return None;
        }
        let node = (*pstmt).utilityStmt;
        if (*node).type_ != pg_sys::NodeTag::T_RenameStmt {
            return None;
        }
        #[allow(clippy::cast_ptr_alignment)] // Reason: PostgreSQL Node* → RenameStmt* cast
        let stmt = &*node.cast::<pg_sys::RenameStmt>();
        if stmt.renameType != pg_sys::ObjectType::OBJECT_COLUMN
            || stmt.relation.is_null()
            || stmt.subname.is_null()
            || stmt.newname.is_null()
        {
            return None;
        }
        Some(ColumnRename {
            relation: stmt.relation,
            old_name: CStr::from_ptr(stmt.subname).to_string_lossy().into_owned(),
            new_name: CStr::from_ptr(stmt.newname).to_string_lossy().into_owned(),
        })
    }
}

/// An `ALTER TABLE` that renames, drops or retypes columns, captured before it
/// runs.
pub(super) struct ColumnChange {
    pub(super) relation: *const pg_sys::RangeVar,
    /// `RENAME COLUMN` or `DROP COLUMN`, when the statement does either.
    pub(super) removes: Option<&'static str>,
    /// Each `ALTER COLUMN … TYPE`: the column and its new type.
    pub(super) retypes: Vec<(String, *const pg_sys::TypeName)>,
}

/// The column change carried by the utility statement `node`, if it is one.
///
/// SAFETY: `node` must be a valid, non-null `Node*`.
pub(super) unsafe fn column_change_of(node: *mut pg_sys::Node) -> Option<ColumnChange> {
    // SAFETY: each cast follows the node's tag; the cells of an AlterTableStmt
    // are AlterTableCmds, an AT_AlterColumnType's def a ColumnDef, null-checked.
    unsafe {
        #[allow(clippy::cast_ptr_alignment)] // Reason: PostgreSQL Node* → statement cast by tag
        let change = match (*node).type_ {
            pg_sys::NodeTag::T_RenameStmt => {
                let stmt = &*node.cast::<pg_sys::RenameStmt>();
                if stmt.renameType != pg_sys::ObjectType::OBJECT_COLUMN {
                    return None;
                }
                ColumnChange {
                    relation: stmt.relation,
                    removes: Some("RENAME COLUMN"),
                    retypes: Vec::new(),
                }
            }
            pg_sys::NodeTag::T_AlterTableStmt => {
                let stmt = &*node.cast::<pg_sys::AlterTableStmt>();
                let mut change = ColumnChange {
                    relation: stmt.relation,
                    removes: None,
                    retypes: Vec::new(),
                };
                for i in 0..pg_sys::list_length(stmt.cmds) {
                    let cmd = pg_sys::list_nth(stmt.cmds, i).cast::<pg_sys::AlterTableCmd>();
                    if cmd.is_null() {
                        continue;
                    }
                    match (*cmd).subtype {
                        pg_sys::AlterTableType::AT_DropColumn => {
                            change.removes = Some("DROP COLUMN");
                        }
                        pg_sys::AlterTableType::AT_AlterColumnType
                            if !(*cmd).name.is_null() && !(*cmd).def.is_null() =>
                        {
                            let def = (*cmd).def.cast::<pg_sys::ColumnDef>();
                            change.retypes.push((
                                CStr::from_ptr((*cmd).name).to_string_lossy().into_owned(),
                                (*def).typeName,
                            ));
                        }
                        _ => {}
                    }
                }
                change
            }
            _ => return None,
        };
        (!change.relation.is_null() && (change.removes.is_some() || !change.retypes.is_empty()))
            .then_some(change)
    }
}

/// The table an `ALTER TABLE … RENAME TO` or `ALTER TABLE … SET SCHEMA` renames or
/// moves, resolved before the statement runs.
///
/// SAFETY: `pstmt` must be null or a valid `PlannedStmt` from the `ProcessUtility` hook.
pub(super) unsafe fn table_move_of(pstmt: *const pg_sys::PlannedStmt) -> Option<pg_sys::Oid> {
    // SAFETY: every pointer is null-checked before it is dereferenced.
    unsafe {
        if pstmt.is_null() || (*pstmt).utilityStmt.is_null() {
            return None;
        }
        let node = (*pstmt).utilityStmt;
        #[allow(clippy::cast_ptr_alignment)] // Reason: PostgreSQL Node* → statement cast by tag
        let relation = match (*node).type_ {
            pg_sys::NodeTag::T_RenameStmt => {
                let stmt = &*node.cast::<pg_sys::RenameStmt>();
                (stmt.renameType == pg_sys::ObjectType::OBJECT_TABLE).then_some(stmt.relation)
            }
            pg_sys::NodeTag::T_AlterObjectSchemaStmt => {
                let stmt = &*node.cast::<pg_sys::AlterObjectSchemaStmt>();
                (stmt.objectType == pg_sys::ObjectType::OBJECT_TABLE).then_some(stmt.relation)
            }
            _ => None,
        }?;
        if relation.is_null() {
            return None;
        }
        let relid = pg_sys::RangeVarGetRelidExtended(
            relation,
            pg_sys::NoLock.cast_signed(),
            pg_sys::RVROption::RVR_MISSING_OK,
            None,
            std::ptr::null_mut(),
        );
        (relid != pg_sys::InvalidOid).then_some(relid)
    }
}

/// The materialized view a `REFRESH MATERIALIZED VIEW` fills (not `WITH NO DATA`,
/// which leaves nothing to read), resolved before the statement runs.
///
/// SAFETY: `pstmt` is null or a valid `PlannedStmt`.
pub(super) unsafe fn matview_refresh_of(pstmt: *const pg_sys::PlannedStmt) -> Option<pg_sys::Oid> {
    // SAFETY: every pointer is null-checked before it is dereferenced.
    unsafe {
        if pstmt.is_null() || (*pstmt).utilityStmt.is_null() {
            return None;
        }
        let node = (*pstmt).utilityStmt;
        if (*node).type_ != pg_sys::NodeTag::T_RefreshMatViewStmt {
            return None;
        }
        #[allow(clippy::cast_ptr_alignment)] // Reason: PostgreSQL Node* → statement cast by tag
        let stmt = &*node.cast::<pg_sys::RefreshMatViewStmt>();
        if stmt.skipData || stmt.relation.is_null() {
            return None;
        }
        let relid = pg_sys::RangeVarGetRelidExtended(
            stmt.relation,
            pg_sys::NoLock.cast_signed(),
            pg_sys::RVROption::RVR_MISSING_OK,
            None,
            std::ptr::null_mut(),
        );
        (relid != pg_sys::InvalidOid).then_some(relid)
    }
}

/// A statement after which the backing views must follow their tables' grants,
/// and their owners when `owners`.
pub(super) struct PrivilegesChange {
    pub(super) owners: bool,
}

/// `GRANT` / `REVOKE` on tables (by name or `ALL TABLES IN SCHEMA`), `ALTER TABLE
/// … OWNER TO` and `REASSIGN OWNED`.
///
/// SAFETY: `pstmt` must be null or a valid `PlannedStmt` from the `ProcessUtility` hook.
pub(super) unsafe fn privileges_change_of(
    pstmt: *const pg_sys::PlannedStmt,
) -> Option<PrivilegesChange> {
    // SAFETY: every pointer is null-checked before it is dereferenced.
    unsafe {
        if pstmt.is_null() || (*pstmt).utilityStmt.is_null() {
            return None;
        }
        let node = (*pstmt).utilityStmt;
        #[allow(clippy::cast_ptr_alignment)] // Reason: PostgreSQL Node* → statement cast by tag
        match (*node).type_ {
            pg_sys::NodeTag::T_GrantStmt => {
                let stmt = &*node.cast::<pg_sys::GrantStmt>();
                (stmt.objtype == pg_sys::ObjectType::OBJECT_TABLE)
                    .then_some(PrivilegesChange { owners: false })
            }
            pg_sys::NodeTag::T_AlterTableStmt => {
                let stmt = &*node.cast::<pg_sys::AlterTableStmt>();
                let cmds = stmt.cmds;
                let changes_owner = stmt.objtype == pg_sys::ObjectType::OBJECT_TABLE
                    && (0..pg_sys::list_length(cmds)).any(|i| {
                        let cmd = pg_sys::list_nth(cmds, i).cast::<pg_sys::AlterTableCmd>();
                        !cmd.is_null() && (*cmd).subtype == pg_sys::AlterTableType::AT_ChangeOwner
                    });
                changes_owner.then_some(PrivilegesChange { owners: true })
            }
            pg_sys::NodeTag::T_ReassignOwnedStmt => Some(PrivilegesChange { owners: true }),
            _ => None,
        }
    }
}

/// The tables a `CREATE TABLE … PARTITION OF`, `ALTER TABLE … ATTACH PARTITION` or
/// `… DETACH PARTITION` (also `CONCURRENTLY` and `FINALIZE`) adds to or removes
/// from a partition tree, resolved once `PostgreSQL` has run the statement.
pub(super) struct PartitionDdl {
    pub(super) tables: Vec<*mut pg_sys::RangeVar>,
    /// The partitioned table an `ATTACH` or `DETACH` changes the rows of.
    pub(super) changed_rows_of: Option<*mut pg_sys::RangeVar>,
}

impl PartitionDdl {
    /// Give each added partition the triggers its tree's TVIEWs need, and take
    /// ours off each removed one.
    ///
    /// SAFETY: the `RangeVar`s come from the statement's parse tree, which outlives
    /// the statement.
    pub(super) unsafe fn apply(self) -> TViewResult<()> {
        // Partition roots are cached per backend.
        crate::cache::invalidate_all();
        for rv in self.tables {
            // SAFETY: see above.
            let oid = unsafe { resolve_relation_oid(rv) };
            if oid != pg_sys::InvalidOid {
                crate::dependency::triggers::ensure_partition_triggers(oid)?;
            }
        }
        // The rows of an attached or detached partition enter or leave the
        // partitioned table with no row written: refresh its TVIEWs in full.
        // SAFETY: see above.
        let parent = self
            .changed_rows_of
            .map_or(pg_sys::InvalidOid, |rv| unsafe { resolve_relation_oid(rv) });
        if parent != pg_sys::InvalidOid {
            crate::delta::refresh_tviews_over(parent)?;
        }
        Ok(())
    }
}

/// The partition change carried by `pstmt`, if it is one.
///
/// SAFETY: `pstmt` must be null or a valid `PlannedStmt` from the `ProcessUtility` hook.
pub(super) unsafe fn partition_ddl_of(pstmt: *const pg_sys::PlannedStmt) -> Option<PartitionDdl> {
    // SAFETY: every pointer is null-checked before it is dereferenced.
    unsafe {
        if pstmt.is_null() || (*pstmt).utilityStmt.is_null() {
            return None;
        }
        let node = (*pstmt).utilityStmt;
        let mut tables = Vec::new();
        let mut changed_rows_of = None;
        match (*node).type_ {
            pg_sys::NodeTag::T_CreateStmt => {
                #[allow(clippy::cast_ptr_alignment)] // Reason: PostgreSQL Node* → CreateStmt* cast
                let stmt = &*node.cast::<pg_sys::CreateStmt>();
                if !stmt.partbound.is_null() && !stmt.relation.is_null() {
                    tables.push(stmt.relation);
                }
            }
            pg_sys::NodeTag::T_AlterTableStmt => {
                #[allow(clippy::cast_ptr_alignment)]
                // Reason: PostgreSQL Node* → AlterTableStmt* cast
                let stmt = &*node.cast::<pg_sys::AlterTableStmt>();
                for i in 0..pg_sys::list_length(stmt.cmds) {
                    let cmd = pg_sys::list_nth(stmt.cmds, i).cast::<pg_sys::AlterTableCmd>();
                    if cmd.is_null()
                        || !matches!(
                            (*cmd).subtype,
                            pg_sys::AlterTableType::AT_AttachPartition
                                | pg_sys::AlterTableType::AT_DetachPartition
                                | pg_sys::AlterTableType::AT_DetachPartitionFinalize
                        )
                        || (*cmd).def.is_null()
                    {
                        continue;
                    }
                    #[allow(clippy::cast_ptr_alignment)]
                    // Reason: PostgreSQL Node* → PartitionCmd* cast
                    let partition = &*(*cmd).def.cast::<pg_sys::PartitionCmd>();
                    if !partition.name.is_null() {
                        tables.push(partition.name);
                        changed_rows_of = Some(stmt.relation);
                    }
                }
            }
            _ => {}
        }
        (!tables.is_empty()).then_some(PartitionDdl {
            tables,
            changed_rows_of,
        })
    }
}

/// If `node` is `CREATE EXTENSION` or `DROP EXTENSION`, the extension name(s) it names.
///
/// SAFETY: `node` must be a valid, non-null `Node*`.
pub(super) unsafe fn extension_statement_names(node: *mut pg_sys::Node) -> Option<Vec<String>> {
    // SAFETY: the caller's node; each cast follows its tag, and the lists hold
    // the node types PostgreSQL's grammar puts there.
    unsafe {
        let tag = (*node).type_;
        if tag == pg_sys::NodeTag::T_CreateExtensionStmt {
            #[allow(clippy::cast_ptr_alignment)]
            // Reason: PostgreSQL Node* → CreateExtensionStmt* cast
            let stmt = node.cast::<pg_sys::CreateExtensionStmt>();
            let name = if (*stmt).extname.is_null() {
                String::new()
            } else {
                CStr::from_ptr((*stmt).extname)
                    .to_string_lossy()
                    .into_owned()
            };
            return Some(vec![name]);
        }
        if tag == pg_sys::NodeTag::T_DropStmt {
            #[allow(clippy::cast_ptr_alignment)] // Reason: PostgreSQL Node* → DropStmt* cast
            let stmt = node.cast::<pg_sys::DropStmt>();
            if (*stmt).removeType != pg_sys::ObjectType::OBJECT_EXTENSION {
                return None;
            }
            let mut names = Vec::new();
            let objects = (*stmt).objects;
            for i in 0..pg_sys::list_length(objects) {
                let item = pg_sys::list_nth(objects, i).cast::<pg_sys::String>();
                if !item.is_null() && !(*item).sval.is_null() {
                    names.push(CStr::from_ptr((*item).sval).to_string_lossy().into_owned());
                }
            }
            return Some(names);
        }
        None
    }
}

/// Whether `pstmt` is a `DROP EXTENSION` naming `pg_tviews`.
///
/// SAFETY: `pstmt` must be null or a valid `PlannedStmt*`.
pub(super) unsafe fn drops_pg_tviews(pstmt: *const pg_sys::PlannedStmt) -> bool {
    // SAFETY: the caller's statement and its utility node, both checked for null.
    unsafe {
        if pstmt.is_null() || (*pstmt).utilityStmt.is_null() {
            return false;
        }
        let node = (*pstmt).utilityStmt;
        (*node).type_ == pg_sys::NodeTag::T_DropStmt
            && extension_statement_names(node)
                .is_some_and(|names| names.iter().any(|n| n == "pg_tviews"))
    }
}

/// Resolve a `RangeVar` to a relation OID without locking or raising.
///
/// Returns `InvalidOid` when the relation (or its schema) doesn't exist. This is a
/// direct catalog lookup: no SPI, so it is safe inside the hook's `catch_unwind`.
///
/// SAFETY: `rv` must be null or a valid `RangeVar*`.
pub(super) unsafe fn resolve_relation_oid(rv: *const pg_sys::RangeVar) -> pg_sys::Oid {
    if rv.is_null() {
        return pg_sys::InvalidOid;
    }
    // SAFETY: the caller's RangeVar, checked for null; NoLock with MISSING_OK
    // neither locks nor raises for a missing relation.
    unsafe {
        pg_sys::RangeVarGetRelidExtended(
            rv,
            pg_sys::NoLock.cast_signed(),
            pg_sys::RVROption::RVR_MISSING_OK,
            None,
            std::ptr::null_mut(),
        )
    }
}