pg_tviews 0.1.0-beta.11

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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# pg_tviews Error Reference

**Version**: 0.1.0-alpha
**Last Updated**: December 10, 2025

## Overview

This document provides comprehensive reference for all error types that can occur in pg_tviews. Each error includes its SQLSTATE code, common causes, and resolution steps.

## Error Categories

- [Metadata Errors]#metadata-errors - TVIEW definition and catalog issues
- [Dependency Errors]#dependency-errors - Relationship and cycle detection
- [SQL Parsing Errors]#sql-parsing-errors - Query validation and syntax issues
- [Extension Dependency Errors]#extension-dependency-errors - Required extension issues
- [Concurrency Errors]#concurrency-errors - Locking and deadlock issues
- [Refresh Errors]#refresh-errors - Runtime refresh operation failures
- [System Errors]#system-errors - Internal and infrastructure issues

## Metadata Errors

### MetadataNotFound

**SQLSTATE**: P0001
**Description**: TVIEW metadata not found for the specified entity.

**Common Causes**:
- TVIEW was dropped but code still references it
- TVIEW creation failed partway through
- Database corruption or inconsistent state

**Example**:
```sql
-- Error occurs when trying to refresh a non-existent TVIEW
SELECT pg_tviews_cascade('unknown_entity'::regclass::oid, 123);
-- ERROR: TVIEW metadata not found for entity 'unknown_entity'
```

**Resolution**:
1. Verify TVIEW exists: `SELECT * FROM pg_tview_meta WHERE entity = 'entity_name';`
2. Recreate TVIEW if missing: `SELECT pg_tviews_create('entity_name', 'SELECT ...');`
3. Check for typos in entity name

**When to Report**: If TVIEW should exist but metadata is missing.

### TViewAlreadyExists

**SQLSTATE**: 42710
**Description**: Attempting to create a TVIEW that already exists.

**Common Causes**:
- Duplicate TVIEW creation attempts
- Migration scripts running multiple times
- Race conditions in application code

**Example**:
```sql
SELECT pg_tviews_create('tv_post', 'SELECT tb_post.pk_post, tb_post.id, jsonb_build_object(''id'', tb_post.id, ''data'', tb_post.data) as data FROM tb_post');
-- First call succeeds
SELECT pg_tviews_create('tv_post', 'SELECT tb_post.pk_post, tb_post.id, jsonb_build_object(''id'', tb_post.id, ''data'', tb_post.data) as data FROM tb_post');
-- ERROR: TVIEW 'posts' already exists
```

**Resolution**:
1. Check if TVIEW exists: `SELECT * FROM pg_tview_meta WHERE entity = 'posts';`
2. Use conditional creation: `SELECT pg_tviews_drop('posts', true);` then recreate
3. Implement idempotent creation logic in application code

### InvalidTViewName

**SQLSTATE**: 42602
**Description**: TVIEW name doesn't follow required naming convention.

**Common Causes**:
- TVIEW name doesn't start with `tv_`
- Invalid characters in name
- Name conflicts with PostgreSQL reserved words

**Example**:
```sql
SELECT pg_tviews_create('posts', 'SELECT ...');  -- Missing tv_ prefix
-- ERROR: Invalid TVIEW name 'posts': must start with 'tv_'
```

**Resolution**:
1. Use correct naming: `tv_<entity>` format
2. Check for reserved words: `SELECT word FROM pg_get_keywords() WHERE word = 'name';`
3. Use underscores for multi-word entities: `tv_user_posts`

## Dependency Errors

### CircularDependency

**SQLSTATE**: 55P03
**Description**: Circular dependency detected between TVIEWs.

**Common Causes**:
- TVIEW A depends on TVIEW B which depends on TVIEW A
- Complex dependency chains creating loops
- Incorrect foreign key relationships

**Example**:
```sql
-- tv_post depends on users, tv_user depends on posts
CREATE TABLE tv_post AS SELECT tb_post.pk_post, tb_post.id, jsonb_build_object('id', tb_post.id, 'user_name', tb_user.name) as data FROM tb_post JOIN tb_user ON tb_post.fk_user = tb_user.pk_user;
CREATE TABLE tv_user AS SELECT tb_user.pk_user, tb_user.id, jsonb_build_object('id', tb_user.id, 'post_title', tb_post.title) as data FROM tb_user JOIN tb_post ON tb_user.pk_user = tb_post.fk_user;
-- ERROR: Circular dependency detected: posts → users → posts
```

**Resolution**:
1. Redesign schema to eliminate circular dependencies
2. Use computed columns instead of joins where possible
3. Restructure data model to use one-way relationships

**When to Report**: If circular dependency is legitimate and should be supported.

### DependencyDepthExceeded

**SQLSTATE**: 54001
**Description**: Dependency chain exceeds maximum allowed depth.

**Common Causes**:
- Deep dependency hierarchies
- Recursive relationships
- Overly complex data models

**Example**:
```sql
-- Chain: tv_a -> tv_b -> tv_c -> ... -> tv_z (26 levels deep)
-- ERROR: Dependency depth 26 exceeds maximum 10
```

**Resolution**:
1. Simplify dependency chain by denormalizing intermediate levels
2. Use direct table access instead of cascading through multiple TVIEWs
3. Increase `max_propagation_depth` if absolutely necessary (not recommended)

### DependencyResolutionFailed

**SQLSTATE**: 55000
**Description**: Failed to resolve dependencies for a TVIEW.

**Common Causes**:
- Referenced tables don't exist
- Foreign key relationships not properly defined
- Schema changes after TVIEW creation

**Example**:
```sql
CREATE TABLE tv_post AS SELECT tb_post.pk_post, tb_post.id, jsonb_build_object('id', tb_post.id, 'user_name', tb_user.name) as data FROM tb_post JOIN tb_user ON tb_post.fk_user = tb_user.pk_user;
-- Later: DROP TABLE users;
-- Then refresh: ERROR: Failed to resolve dependencies for 'tv_post': table 'users' does not exist
```

**Resolution**:
1. Verify all referenced tables exist
2. Check foreign key constraints are intact
3. Recreate TVIEW after schema changes: `DROP TABLE tv_post; CREATE TABLE tv_post AS ...;`

## SQL Parsing Errors

### InvalidSelectStatement

**SQLSTATE**: 42601
**Description**: The SELECT statement provided is invalid or unsupported.

**Common Causes**:
- Syntax errors in SQL
- Unsupported SQL features (UNION, CTEs, window functions)
- Missing required columns (pk_*, data)

**Example**:
```sql
SELECT pg_tviews_create('posts', 'SELECT id FROM posts');  -- Missing pk_post and data columns
-- ERROR: Invalid SELECT statement: Required column 'pk_post' missing
```

**Resolution**:
1. Ensure SELECT includes required columns:
   - `pk_<entity>` as primary key column
   - `data` as JSONB column
2. Check SQL syntax with `EXPLAIN SELECT ...;`
3. Remove unsupported features (see DDL Reference for supported SQL)

### RequiredColumnMissing

**SQLSTATE**: 42703
**Description**: Required column is missing from SELECT statement.

**Common Causes**:
- Missing `pk_<entity>` column
- Missing `data` column
- Incorrect column naming

**Example**:
```sql
SELECT pg_tviews_create('tv_post', 'SELECT tb_post.id, tb_post.title FROM tb_post');
-- ERROR: Required column 'pk_post' missing in SELECT statement
```

**Resolution**:
1. Add primary key column: `SELECT id as pk_post, ...`
2. Add data column: `SELECT ..., jsonb_build_object(...) as data`
3. Verify column names match entity: `pk_<entity>`

### TypeInferenceFailed

**SQLSTATE**: 42804
**Description**: Failed to infer PostgreSQL column types.

**Common Causes**:
- Complex expressions that can't be typed
- User-defined types not recognized
- Table doesn't exist or has no rows

**Example**:
```sql
SELECT pg_tviews_create('posts', 'SELECT complex_function(id) as pk_post, data FROM posts');
-- ERROR: Failed to infer type for column 'pk_post': complex_function not recognized
```

**Resolution**:
1. Use explicit type casts: `SELECT id::bigint as pk_post`
2. Simplify expressions or use subqueries
3. Ensure referenced tables exist and have data

## Extension Dependency Errors

### JsonbIvmNotInstalled

**SQLSTATE**: 58P01
**Description**: Required jsonb_delta extension is not installed.

**Common Causes**:
- jsonb_delta extension not installed
- Performance optimization disabled
- Fresh PostgreSQL installation

**Example**:
```sql
-- During TVIEW creation or refresh
-- WARNING: jsonb_delta extension not detected
-- → Performance: Basic (full document replacement)
-- → To enable 1.5-3× faster cascades, install jsonb_delta
```

**Resolution**:
1. Install jsonb_delta: `CREATE EXTENSION jsonb_delta;`
2. Download from: https://github.com/fraiseql/jsonb_delta
3. Restart pg_tviews extension if needed

**Note**: This is a warning, not an error. pg_tviews works without jsonb_delta but slower.

### ExtensionVersionMismatch

**SQLSTATE**: 58P01
**Description**: Extension version incompatibility detected.

**Common Causes**:
- Outdated extension version
- Incompatible PostgreSQL version
- Mixed extension versions in cluster

**Example**:
```sql
-- During extension load
-- ERROR: Extension 'pg_tviews' version mismatch: required 0.1.0, found 0.0.9
```

**Resolution**:
1. Update extension: `ALTER EXTENSION pg_tviews UPDATE;`
2. Reinstall if needed: `DROP EXTENSION pg_tviews; CREATE EXTENSION pg_tviews;`
3. Check PostgreSQL compatibility

## Concurrency Errors

### LockTimeout

**SQLSTATE**: 40P01
**Description**: Lock acquisition timed out.

**Common Causes**:
- Long-running transactions blocking TVIEW operations
- High concurrency causing lock contention
- Deadlock prevention mechanisms

**Example**:
```sql
-- During high concurrency
-- ERROR: Lock timeout on resource 'pg_tview_meta' after 30000ms
```

**Resolution**:
1. Reduce transaction length
2. Use shorter lock timeouts: `SET lock_timeout = '10s';`
3. Implement retry logic with exponential backoff
4. Check for long-running transactions: `SELECT * FROM pg_stat_activity WHERE state = 'active';`

### DeadlockDetected

**SQLSTATE**: 40P01
**Description**: Deadlock detected between concurrent operations.

**Common Causes**:
- Multiple TVIEWs modifying each other
- Complex dependency chains with concurrent updates
- Poor transaction ordering

**Example**:
```sql
-- Two transactions updating interdependent TVIEWs
-- Transaction 1: UPDATE posts SET ... WHERE id = 1;
-- Transaction 2: UPDATE users SET ... WHERE id = 1;
-- ERROR: Deadlock detected in TVIEW refresh operation
```

**Resolution**:
1. Reorder operations to avoid deadlocks
2. Use shorter transactions
3. Implement deadlock retry logic
4. Review dependency graph for cycles

## Refresh Errors

### CascadeDepthExceeded

**SQLSTATE**: 54001
**Description**: Cascade refresh exceeded maximum depth limit.

**Common Causes**:
- Deep dependency chains
- Infinite loops in refresh logic
- Recursive relationships

**Example**:
```sql
-- Deep dependency chain causes cascade to exceed limit
-- ERROR: Cascade depth 15 exceeds maximum 10. Possible infinite cascade loop.
```

**Resolution**:
1. Simplify dependency chain
2. Increase `max_propagation_depth` if needed (not recommended)
3. Check for infinite loops in business logic
4. Use direct table access instead of cascading

### RefreshFailed

**SQLSTATE**: XX000
**Description**: Individual TVIEW refresh operation failed.

**Common Causes**:
- Data corruption in base tables
- Constraint violations during refresh
- Permission issues on TVIEW tables

**Example**:
```sql
-- During automatic refresh
-- ERROR: Failed to refresh TVIEW 'post' row 123: permission denied for table tv_post
```

**Resolution**:
1. Check permissions on TVIEW tables
2. Verify data integrity in base tables
3. Check for constraint violations
4. Review TVIEW definition for errors

### BatchTooLarge

**SQLSTATE**: 54000
**Description**: Batch operation exceeds size limits.

**Common Causes**:
- Large bulk operations
- Configuration limits too low
- Memory constraints

**Example**:
```sql
-- Large bulk insert
-- ERROR: Batch size 5000 exceeds maximum 1000
```

**Resolution**:
1. Process in smaller batches
2. Increase batch size limits if hardware allows
3. Use statement-level triggers for bulk operations

### DependencyCycle

**SQLSTATE**: 55P03
**Description**: Dependency cycle detected in entity graph.

**Common Causes**:
- Circular relationships in data model
- Incorrect foreign key setup
- Complex many-to-many relationships

**Example**:
```sql
-- ERROR: Dependency cycle detected in entity graph: posts -> comments -> posts
```

**Resolution**:
1. Redesign schema to eliminate cycles
2. Use computed columns or triggers instead of circular TVIEWs
3. Implement cycle detection in application logic

### PropagationDepthExceeded

**SQLSTATE**: 54001
**Description**: Propagation exceeded maximum depth (possible infinite loop).

**Common Causes**:
- Deep dependency chains
- Recursive business logic
- Infinite loops in refresh propagation

**Example**:
```sql
-- ERROR: Propagation exceeded maximum depth of 50 iterations (100 entities processed)
```

**Resolution**:
1. Simplify dependency graph
2. Check for infinite loops in business rules
3. Increase depth limit if necessary (not recommended)
4. Use direct updates instead of cascading

## System Errors

### CatalogError

**SQLSTATE**: XX000
**Description**: PostgreSQL catalog operation failed.

**Common Causes**:
- Database corruption
- Permission issues on system catalogs
- PostgreSQL internal errors

**Example**:
```sql
-- ERROR: Catalog operation 'get_table_oid' failed: permission denied for table pg_class
```

**Resolution**:
1. Check database permissions
2. Run `ANALYZE;` to update statistics
3. Check PostgreSQL logs for catalog corruption
4. Consider `REINDEX SYSTEM;` for catalog issues

**When to Report**: Usually indicates PostgreSQL issues, not pg_tviews bugs.

### SpiError

**SQLSTATE**: XX000
**Description**: Server Programming Interface (SPI) operation failed.

**Common Causes**:
- SQL syntax errors in generated queries
- Permission issues
- Resource constraints

**Example**:
```sql
-- ERROR: SPI query failed: permission denied for table tv_post
-- Query: UPDATE tv_post SET data = $1 WHERE pk_post = $2
```

**Resolution**:
1. Check permissions on TVIEW tables
2. Verify generated SQL syntax
3. Check resource limits (work_mem, etc.)
4. Review PostgreSQL error logs

### SerializationError

**SQLSTATE**: XX000
**Description**: Data serialization/deserialization failed.

**Common Causes**:
- JSONB data corruption
- Binary data format changes
- Memory allocation failures

**Example**:
```sql
-- ERROR: Serialization error: JSON parse error: invalid character
```

**Resolution**:
1. Check data integrity in base tables
2. Validate JSONB data: `SELECT jsonb_typeof(data) FROM tv_table;`
3. Repair corrupted data
4. Check available memory

### ConfigError

**SQLSTATE**: XX000
**Description**: Configuration setting error.

**Common Causes**:
- Invalid GUC values
- Configuration conflicts
- Environment variable issues

**Example**:
```sql
-- ERROR: Configuration error for 'pg_tviews.max_propagation_depth': invalid value 'abc' (value: abc)
```

**Resolution**:
1. Check configuration syntax: `SHOW pg_tviews.max_propagation_depth;`
2. Use valid values: `SET pg_tviews.max_propagation_depth = 10;`
3. Check postgresql.conf for invalid settings

### CacheError

**SQLSTATE**: XX000
**Description**: Cache operation failed.

**Common Causes**:
- Memory corruption
- Concurrent access issues
- Cache size limits exceeded

**Example**:
```sql
-- ERROR: Cache 'graph_cache' error: poisoned mutex
```

**Resolution**:
1. Restart PostgreSQL to clear corrupted cache
2. Check memory usage and limits
3. Reduce cache sizes if needed
4. Check for concurrent access issues

### CallbackError

**SQLSTATE**: XX000
**Description**: FFI callback operation failed.

**Common Causes**:
- Panics in Rust code
- Memory corruption in FFI boundary
- Threading issues

**Example**:
```sql
-- ERROR: FFI callback 'trigger_handler' failed: panicked at 'index out of bounds'
```

**Resolution**:
1. Check PostgreSQL logs for panic details
2. Restart PostgreSQL
3. Report as bug with full error context

**When to Report**: Always report callback errors as they indicate Rust panics.

### MetricsError

**SQLSTATE**: XX000
**Description**: Metrics collection operation failed.

**Common Causes**:
- Metrics table corruption
- Permission issues on metrics
- Disk space issues

**Example**:
```sql
-- ERROR: Metrics operation 'record_timing' failed: disk full
```

**Resolution**:
1. Check disk space: `df -h`
2. Clean old metrics: `SELECT pg_tviews_cleanup_metrics(30);`
3. Check metrics table permissions
4. Repair corrupted metrics table if needed

### InternalError

**SQLSTATE**: XX000
**Description**: Internal error indicating a bug in pg_tviews.

**Common Causes**:
- Programming errors in extension
- Unexpected state transitions
- Logic errors in refresh algorithms

**Example**:
```sql
-- ERROR: Internal error at src/refresh/main.rs:123: assertion failed: queue.is_empty()
-- Please report this bug.
```

**Resolution**:
1. Note the file and line number
2. Gather context: TVIEW definition, data sample, operation performed
3. Report as bug with full error details

**When to Report**: Always report internal errors as they indicate bugs in the extension.

## Error Handling Best Practices

### Application-Level Error Handling

```sql
-- Use try-catch style error handling
CREATE OR REPLACE FUNCTION safe_tview_operation()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
    -- Attempt operation
    PERFORM pg_tviews_create('tv_post', 'SELECT ...');
EXCEPTION
    WHEN sqlstate '42710' THEN  -- Duplicate object
        RAISE NOTICE 'TVIEW already exists, skipping';
    WHEN sqlstate 'P0001' THEN  -- TVIEW metadata not found
        RAISE NOTICE 'TVIEW missing, recreating';
        PERFORM pg_tviews_create('tv_post', 'SELECT ...');
    WHEN OTHERS THEN
        RAISE;  -- Re-raise unexpected errors
END;
$$;
```

### Logging and Monitoring

```sql
-- Log errors for monitoring
CREATE OR REPLACE FUNCTION log_tview_errors()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
    IF TG_OP = 'ERROR' THEN
        INSERT INTO error_log (error_time, error_message, context)
        VALUES (now(), TG_ERROR_MESSAGE, TG_ARGV[0]);
    END IF;
    RETURN NULL;
END;
$$;
```

### Error Recovery Strategies

1. **Idempotent Operations**: Design operations that can be safely retried
2. **Graceful Degradation**: Fall back to direct table access when TVIEWs fail
3. **Circuit Breakers**: Temporarily disable TVIEWs during extended outages
4. **Automated Recovery**: Implement health checks and automatic recreation

## See Also

- [Debugging Guide]operations/debugging.md - Troubleshooting procedures
- [API Reference]API_REFERENCE.md - Function documentation
- [Monitoring Guide]MONITORING.md - Health checking and metrics