|
| 1 | +package catalog |
| 2 | + |
| 3 | +type InternalView struct { |
| 4 | + Schema string |
| 5 | + Name string |
| 6 | + DDL string |
| 7 | +} |
| 8 | + |
| 9 | +func (v *InternalView) QualifiedName() string { |
| 10 | + return v.Schema + "." + v.Name |
| 11 | +} |
| 12 | + |
| 13 | +var InternalViews = []InternalView{ |
| 14 | + { |
| 15 | + Schema: "__sys__", |
| 16 | + Name: "pg_stat_user_tables", |
| 17 | + DDL: `SELECT |
| 18 | + t.table_schema || '.' || t.table_name AS relid, -- Create a unique ID for the table |
| 19 | + t.table_schema AS schemaname, -- Schema name |
| 20 | + t.table_name AS relname, -- Table name |
| 21 | + 0 AS seq_scan, -- Default to 0 (DuckDB doesn't track this) |
| 22 | + NULL AS last_seq_scan, -- Placeholder (DuckDB doesn't track this) |
| 23 | + 0 AS seq_tup_read, -- Default to 0 |
| 24 | + 0 AS idx_scan, -- Default to 0 |
| 25 | + NULL AS last_idx_scan, -- Placeholder |
| 26 | + 0 AS idx_tup_fetch, -- Default to 0 |
| 27 | + 0 AS n_tup_ins, -- Default to 0 (inserted tuples not tracked) |
| 28 | + 0 AS n_tup_upd, -- Default to 0 (updated tuples not tracked) |
| 29 | + 0 AS n_tup_del, -- Default to 0 (deleted tuples not tracked) |
| 30 | + 0 AS n_tup_hot_upd, -- Default to 0 (HOT updates not tracked) |
| 31 | + 0 AS n_tup_newpage_upd, -- Default to 0 (new page updates not tracked) |
| 32 | + 0 AS n_live_tup, -- Default to 0 (live tuples not tracked) |
| 33 | + 0 AS n_dead_tup, -- Default to 0 (dead tuples not tracked) |
| 34 | + 0 AS n_mod_since_analyze, -- Default to 0 |
| 35 | + 0 AS n_ins_since_vacuum, -- Default to 0 |
| 36 | + NULL AS last_vacuum, -- Placeholder |
| 37 | + NULL AS last_autovacuum, -- Placeholder |
| 38 | + NULL AS last_analyze, -- Placeholder |
| 39 | + NULL AS last_autoanalyze, -- Placeholder |
| 40 | + 0 AS vacuum_count, -- Default to 0 |
| 41 | + 0 AS autovacuum_count, -- Default to 0 |
| 42 | + 0 AS analyze_count, -- Default to 0 |
| 43 | + 0 AS autoanalyze_count -- Default to 0 |
| 44 | +FROM |
| 45 | + information_schema.tables t |
| 46 | +WHERE |
| 47 | + t.table_type = 'BASE TABLE'; -- Include only base tables (not views)`, |
| 48 | + }, |
| 49 | + { |
| 50 | + Schema: "__sys__", |
| 51 | + Name: "pg_index", |
| 52 | + DDL: `SELECT |
| 53 | + ROW_NUMBER() OVER () AS indexrelid, -- Simulated unique ID for the index |
| 54 | + t.table_oid AS indrelid, -- OID of the table |
| 55 | + COUNT(k.column_name) AS indnatts, -- Number of columns included in the index |
| 56 | + COUNT(k.column_name) AS indnkeyatts, -- Number of key columns in the index (same as indnatts here) |
| 57 | + CASE |
| 58 | + WHEN c.constraint_type = 'UNIQUE' THEN TRUE |
| 59 | + ELSE FALSE |
| 60 | + END AS indisunique, -- Indicates if the index is unique |
| 61 | + CASE |
| 62 | + WHEN c.constraint_type = 'PRIMARY KEY' THEN TRUE |
| 63 | + ELSE FALSE |
| 64 | + END AS indisprimary, -- Indicates if the index is a primary key |
| 65 | + ARRAY_AGG(k.ordinal_position ORDER BY k.ordinal_position) AS indkey, -- Array of column positions |
| 66 | + ARRAY[]::BIGINT[] AS indcollation, -- DuckDB does not support collation, set to default |
| 67 | + ARRAY[]::BIGINT[] AS indclass, -- DuckDB does not support index class, set to default |
| 68 | + ARRAY[]::INTEGER[] AS indoption, -- DuckDB does not support index options, set to default |
| 69 | + NULL AS indexprs, -- DuckDB does not support expression indexes, set to NULL |
| 70 | + NULL AS indpred -- DuckDB does not support partial indexes, set to NULL |
| 71 | +FROM |
| 72 | + information_schema.key_column_usage k |
| 73 | +JOIN |
| 74 | + information_schema.table_constraints c |
| 75 | + ON k.constraint_name = c.constraint_name |
| 76 | + AND k.table_name = c.table_name |
| 77 | +JOIN |
| 78 | + duckdb_tables() t |
| 79 | + ON k.table_name = t.table_name |
| 80 | + AND k.table_schema = t.schema_name |
| 81 | +WHERE |
| 82 | + c.constraint_type IN ('PRIMARY KEY', 'UNIQUE') -- Only select primary key and unique constraints |
| 83 | +GROUP BY |
| 84 | + t.table_oid, c.constraint_type, c.constraint_name |
| 85 | +ORDER BY |
| 86 | + t.table_oid;`, |
| 87 | + }, |
| 88 | +} |
0 commit comments