pg_tviews 0.1.0-beta.22

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
# Development Guide

This guide covers setting up the development environment, running tests, and contributing to pg_tviews.

## Prerequisites

- **Rust**: 1.70+ with rustup
- **PostgreSQL**: 15, 16, or 17
- **pgrx**: 0.12.8+ for PostgreSQL extension development
- **jsonb_delta**: Required extension for JSONB operations

## Environment Setup

### 1. Install Rust

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
```

The toolchain is pinned in `rust-toolchain.toml` (rustup installs it on first
`cargo` call). Local runs and CI use the same compiler; bump the pin in a
dedicated PR.

### 2. Install PostgreSQL

**Ubuntu/Debian:**
```bash
sudo apt-get update
sudo apt-get install postgresql-17 postgresql-server-dev-17
```

**macOS (Homebrew):**
```bash
brew install postgresql@17
```

**Arch Linux:**
```bash
sudo pacman -S postgresql
```

### 3. Install pgrx

```bash
cargo install --locked cargo-pgrx
```

### 4. Initialize pgrx

```bash
# Initialize with your PostgreSQL version
cargo pgrx init

# Or specify a specific version
cargo pgrx init --pg17 /usr/lib/postgresql/17/bin/pg_config
```

### 5. Install jsonb_delta

```bash
# Clone and build jsonb_delta
git clone https://github.com/evoludigit/jsonb_delta.git
cd jsonb_delta
make && sudo make install
```

### 6. Install SBOM Tools (Optional)

For generating Software Bill of Materials (SBOM) in compliance with international standards:

```bash
# SBOM generation for Rust (SPDX format)
cargo install cargo-sbom

# CycloneDX generator (CycloneDX format)
cargo install cargo-cyclonedx

# Optional: Validation and scanning tools
npm install -g @cyclonedx/cyclonedx-cli  # CycloneDX validation
pip install spdx-tools                    # SPDX validation

# Container and filesystem vulnerability scanning
# Trivy is used in CI/CD workflows for automated scanning
```

**SBOM Standards Compliance:**
- **SPDX 2.3**: ISO/IEC 5962:2021 (International standard)
- **CycloneDX 1.5**: OWASP security-focused format
- **NTIA Minimum Elements**: US Federal requirements
- **EU Cyber Resilience Act**: European requirements
- **PCI-DSS 4.0**: Payment card industry requirements

### 7. Install Signing Tools (For Releases)

For cryptographic signing of release artifacts:

```bash
# Sigstore Cosign (keyless signing)
# macOS
brew install cosign

# Linux
wget "https://github.com/sigstore/cosign/releases/download/v2.2.2/cosign-linux-amd64"
sudo mv cosign-linux-amd64 /usr/local/bin/cosign
sudo chmod +x /usr/local/bin/cosign

# GPG (traditional signing)
# Usually pre-installed on Linux/macOS
gpg --version

# GitHub CLI (for attestations)
# macOS
brew install gh

# Ubuntu/Debian
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update && sudo apt install gh
```

**Signing Standards Compliance:**
- **Sigstore**: Keyless signing with transparency logs
- **GPG**: OpenPGP standard for maintainer signatures
- **SLSA Level 3**: Supply chain provenance
- **ISO 27001**: Cryptographic signing requirements

## Building

### Development Build

```bash
# Build the extension
cargo pgrx install

# Build with release optimizations
cargo pgrx install --release
```

### Testing Build

```bash
# Run all tests for PostgreSQL 17
cargo pgrx test pg17

# Run tests for every supported version (16, 17, 18)
cargo pgrx test pg16
cargo pgrx test pg17
cargo pgrx test pg18
```

## Testing

### Test Types

1. **Rust Unit Tests**: Test individual functions without PostgreSQL
2. **pgrx Integration Tests**: Test with PostgreSQL using `#[pg_test]`
3. **SQL Integration Tests**: Test complete workflows with SQL files

### Running Tests

```bash
# Run the pure-Rust unit tests (what CI's `unit` job runs). PostgreSQL symbols
# only resolve inside a server, so let the linker ignore them.
RUSTFLAGS="-C link-arg=-Wl,--unresolved-symbols=ignore-all" \
  cargo test --lib --no-default-features --features pg18 -- --skip pg_test_
# Always keep `--skip pg_test_`: a matching #[pg_test] makes pgrx reinstall a
# pg_test build of the extension, after which CREATE EXTENSION fails until you
# re-run `cargo pgrx install`. Use a separate CARGO_TARGET_DIR to keep the main
# build cache valid.

# Lint and format (both enforced in CI)
cargo clippy --no-default-features --features pg18 --all-targets -- -D warnings
cargo fmt --check

# Run pgrx integration tests (requires PostgreSQL)
cargo pgrx test pg17

# Run specific test
cargo pgrx test pg17 -- --test test_metadata_tables_creation

# Run SQL integration tests manually
psql -d test_db -f test/sql/00_extension_loading.sql
```

### Writing Tests

#### Rust Unit Tests

```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_my_function() {
        assert_eq!(my_function(2), 4);
    }
}
```

#### pgrx Integration Tests

```rust
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
    use pgrx::prelude::*;

    #[pg_test]
    fn test_with_postgres() {
        let result = Spi::get_one::<i32>("SELECT 1 + 1");
        assert_eq!(result, Ok(Some(2)));
    }
}
```

#### SQL Integration Tests

Create files in `test/sql/` with SQL commands and expected output.

#### Test Feature Configuration

**Important**: All `#[pg_test]` functions must be conditional on the `pg_test` feature:

```rust
#[cfg(any(test, feature = "pg_test"))]
#[pg_test]
fn test_my_function() {
    // Test code here
}
```

This allows the code to compile with `--no-default-features` for CI/CD pipelines that don't need PostgreSQL integration tests.

```bash
# Compile without PostgreSQL integration tests
cargo check --no-default-features --features pg17

# Run with full test suite
cargo pgrx test pg17
```

### Test Database Setup

```bash
# Create test database
createdb pg_tviews_test

# Enable required extensions
psql -d pg_tviews_test -c "CREATE EXTENSION jsonb_delta;"

# Install pg_tviews
cargo pgrx install --release
psql -d pg_tviews_test -c "CREATE EXTENSION pg_tviews;"
```

## Debugging

### Logging

Use pgrx logging macros:

```rust
use pgrx::prelude::*;

info!("Info message: {}", value);
debug!("Debug message: {:?}", data);
warning!("Warning message");
error!("Error message: {}", err);
```

### PostgreSQL Logs

Check PostgreSQL logs for extension errors:

```bash
# View PostgreSQL logs
tail -f /var/log/postgresql/postgresql-17-main.log

# Or check systemd logs
journalctl -u postgresql -f
```

### SPI Debugging

Debug SPI queries:

```rust
// Log the query before execution
info!("Executing query: {}", query);

// Execute and check result
let result = Spi::get_one::<String>(&query);
info!("Query result: {:?}", result);
```

## Code Organization

```
src/
├── lib.rs              # Extension entry point and exports
├── error/              # Error types and testing utilities
├── metadata.rs         # Metadata table management
├── catalog.rs          # PostgreSQL catalog queries
├── trigger.rs          # Trigger installation logic
├── refresh.rs          # Incremental refresh implementation
├── propagate.rs        # Cascade propagation logic
└── utils.rs            # Shared utility functions

test/
└── sql/                # SQL integration tests

.github/
└── workflows/          # CI/CD configuration
```

## Development Workflow

### 1. Write Tests First (RED)

```bash
# Create failing tests
cargo test --lib  # Should fail initially
```

### 3. Implement Code (GREEN)

```bash
# Implement minimal code to pass tests
cargo test --lib  # Should pass now
```

### 4. Refactor (REFACTOR)

```bash
# Improve code quality while maintaining tests
cargo test --lib
cargo pgrx test pg17
```

### 5. Integration Test (QA)

```bash
# Run full test suite
cargo pgrx test pg17
psql -d test_db -f test/sql/*.sql
```

## Contributing

### Commit Messages

Follow conventional commit format:

```bash
feat(error): add TViewError enum with SQLSTATE mapping
fix(deps): correct pg_depend query direction
test(refresh): add cascade propagation tests
docs(readme): update installation instructions
```

### Pull Requests

1. Create a feature branch from `develop`
2. Implement changes with tests
3. Ensure CI passes
4. Update documentation if needed
5. Request review

### Code Style

- Use `rustfmt` for formatting: `cargo fmt`
- Use `clippy` for linting: `cargo clippy`
- Follow Rust naming conventions
- Add documentation comments to public APIs
- Use `TViewResult<T>` for all fallible operations

## Troubleshooting

### Common Issues

**pgrx init fails:**
```bash
# Check PostgreSQL is installed and running
pg_config --version
sudo systemctl status postgresql

# Try specifying pg_config path explicitly
cargo pgrx init --pg17 /usr/lib/postgresql/17/bin/pg_config
```

**Extension fails to load:**
```bash
# Check PostgreSQL logs
tail -f /var/log/postgresql/postgresql-17-main.log

# Verify jsonb_delta is installed
psql -c "SELECT * FROM pg_extension WHERE extname = 'jsonb_delta';"
```

**Tests fail:**
```bash
# Clean and rebuild
cargo clean
cargo pgrx install --release

# Check test database setup
psql -d pg_tviews_test -c "SELECT version();"
```

### Getting Help

- Check existing issues on GitHub
- Look at pgrx documentation: https://github.com/pgcentralfoundation/pgrx
- PostgreSQL extension development: https://www.postgresql.org/docs/17/extend.html