|
| 1 | +//! Virtual table row generators for each pg_catalog table. |
| 2 | +
|
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use futures::stream; |
| 6 | +use pgwire::api::results::{DataRowEncoder, QueryResponse, Response}; |
| 7 | +use pgwire::error::PgWireResult; |
| 8 | + |
| 9 | +use crate::control::security::identity::AuthenticatedIdentity; |
| 10 | +use crate::control::server::pgwire::types::{bool_field, int4_field, int8_field, text_field}; |
| 11 | +use crate::control::state::SharedState; |
| 12 | + |
| 13 | +/// `pg_database` — one row: the current database. |
| 14 | +pub fn pg_database() -> PgWireResult<Vec<Response>> { |
| 15 | + let schema = Arc::new(vec![ |
| 16 | + int8_field("oid"), |
| 17 | + text_field("datname"), |
| 18 | + text_field("datdba"), |
| 19 | + text_field("encoding"), |
| 20 | + ]); |
| 21 | + let mut encoder = DataRowEncoder::new(schema.clone()); |
| 22 | + encoder.encode_field(&1i64)?; |
| 23 | + encoder.encode_field(&"nodedb")?; |
| 24 | + encoder.encode_field(&"nodedb")?; |
| 25 | + encoder.encode_field(&"UTF8")?; |
| 26 | + let rows = vec![Ok(encoder.take_row())]; |
| 27 | + Ok(vec![Response::Query(QueryResponse::new( |
| 28 | + schema, |
| 29 | + stream::iter(rows), |
| 30 | + ))]) |
| 31 | +} |
| 32 | + |
| 33 | +/// `pg_namespace` — schemas: `public` + `pg_catalog`. |
| 34 | +pub fn pg_namespace() -> PgWireResult<Vec<Response>> { |
| 35 | + let schema = Arc::new(vec![ |
| 36 | + int8_field("oid"), |
| 37 | + text_field("nspname"), |
| 38 | + int8_field("nspowner"), |
| 39 | + ]); |
| 40 | + let mut encoder = DataRowEncoder::new(schema.clone()); |
| 41 | + let mut rows = Vec::new(); |
| 42 | + |
| 43 | + encoder.encode_field(&11i64)?; |
| 44 | + encoder.encode_field(&"pg_catalog")?; |
| 45 | + encoder.encode_field(&10i64)?; |
| 46 | + rows.push(Ok(encoder.take_row())); |
| 47 | + |
| 48 | + encoder.encode_field(&2200i64)?; |
| 49 | + encoder.encode_field(&"public")?; |
| 50 | + encoder.encode_field(&10i64)?; |
| 51 | + rows.push(Ok(encoder.take_row())); |
| 52 | + |
| 53 | + Ok(vec![Response::Query(QueryResponse::new( |
| 54 | + schema, |
| 55 | + stream::iter(rows), |
| 56 | + ))]) |
| 57 | +} |
| 58 | + |
| 59 | +/// `pg_type` — common Postgres type OIDs that client drivers need. |
| 60 | +pub fn pg_type() -> PgWireResult<Vec<Response>> { |
| 61 | + let schema = Arc::new(vec![ |
| 62 | + int8_field("oid"), |
| 63 | + text_field("typname"), |
| 64 | + int8_field("typnamespace"), |
| 65 | + int4_field("typlen"), |
| 66 | + text_field("typtype"), |
| 67 | + ]); |
| 68 | + |
| 69 | + let types: &[(i64, &str, i32, &str)] = &[ |
| 70 | + (16, "bool", 1, "b"), |
| 71 | + (20, "int8", 8, "b"), |
| 72 | + (21, "int2", 2, "b"), |
| 73 | + (23, "int4", 4, "b"), |
| 74 | + (25, "text", -1, "b"), |
| 75 | + (114, "json", -1, "b"), |
| 76 | + (700, "float4", 4, "b"), |
| 77 | + (701, "float8", 8, "b"), |
| 78 | + (1043, "varchar", -1, "b"), |
| 79 | + (1082, "date", 4, "b"), |
| 80 | + (1114, "timestamp", 8, "b"), |
| 81 | + (1184, "timestamptz", 8, "b"), |
| 82 | + (2950, "uuid", 16, "b"), |
| 83 | + (3802, "jsonb", -1, "b"), |
| 84 | + ]; |
| 85 | + |
| 86 | + let mut rows = Vec::with_capacity(types.len()); |
| 87 | + let mut encoder = DataRowEncoder::new(schema.clone()); |
| 88 | + |
| 89 | + for &(oid, name, len, typtype) in types { |
| 90 | + encoder.encode_field(&oid)?; |
| 91 | + encoder.encode_field(&name)?; |
| 92 | + encoder.encode_field(&11i64)?; |
| 93 | + encoder.encode_field(&len)?; |
| 94 | + encoder.encode_field(&typtype)?; |
| 95 | + rows.push(Ok(encoder.take_row())); |
| 96 | + } |
| 97 | + |
| 98 | + Ok(vec![Response::Query(QueryResponse::new( |
| 99 | + schema, |
| 100 | + stream::iter(rows), |
| 101 | + ))]) |
| 102 | +} |
| 103 | + |
| 104 | +/// `pg_class` — one row per active collection (mapped as relation). |
| 105 | +pub fn pg_class( |
| 106 | + state: &SharedState, |
| 107 | + identity: &AuthenticatedIdentity, |
| 108 | +) -> PgWireResult<Vec<Response>> { |
| 109 | + let schema = Arc::new(vec![ |
| 110 | + int8_field("oid"), |
| 111 | + text_field("relname"), |
| 112 | + int8_field("relnamespace"), |
| 113 | + text_field("relkind"), |
| 114 | + int8_field("relowner"), |
| 115 | + ]); |
| 116 | + |
| 117 | + let collections = load_collections(state, identity); |
| 118 | + |
| 119 | + let mut rows = Vec::with_capacity(collections.len()); |
| 120 | + let mut encoder = DataRowEncoder::new(schema.clone()); |
| 121 | + |
| 122 | + for (i, coll) in collections.iter().enumerate() { |
| 123 | + let oid = 16384i64 + i as i64; |
| 124 | + encoder.encode_field(&oid)?; |
| 125 | + encoder.encode_field(&coll.name.as_str())?; |
| 126 | + encoder.encode_field(&2200i64)?; |
| 127 | + encoder.encode_field(&"r")?; |
| 128 | + encoder.encode_field(&10i64)?; |
| 129 | + rows.push(Ok(encoder.take_row())); |
| 130 | + } |
| 131 | + |
| 132 | + Ok(vec![Response::Query(QueryResponse::new( |
| 133 | + schema, |
| 134 | + stream::iter(rows), |
| 135 | + ))]) |
| 136 | +} |
| 137 | + |
| 138 | +/// `pg_attribute` — one row per field in strict-schema collections. |
| 139 | +pub fn pg_attribute( |
| 140 | + state: &SharedState, |
| 141 | + identity: &AuthenticatedIdentity, |
| 142 | +) -> PgWireResult<Vec<Response>> { |
| 143 | + let schema = Arc::new(vec![ |
| 144 | + int8_field("attrelid"), |
| 145 | + text_field("attname"), |
| 146 | + int8_field("atttypid"), |
| 147 | + int4_field("attnum"), |
| 148 | + int4_field("attlen"), |
| 149 | + bool_field("attnotnull"), |
| 150 | + ]); |
| 151 | + |
| 152 | + let collections = load_collections(state, identity); |
| 153 | + |
| 154 | + let mut rows = Vec::new(); |
| 155 | + let mut encoder = DataRowEncoder::new(schema.clone()); |
| 156 | + |
| 157 | + for (i, coll) in collections.iter().enumerate() { |
| 158 | + let rel_oid = 16384i64 + i as i64; |
| 159 | + for (col_num, (field_name, field_type)) in coll.fields.iter().enumerate() { |
| 160 | + let type_oid = field_type_to_oid(field_type); |
| 161 | + encoder.encode_field(&rel_oid)?; |
| 162 | + encoder.encode_field(&field_name.as_str())?; |
| 163 | + encoder.encode_field(&type_oid)?; |
| 164 | + encoder.encode_field(&((col_num + 1) as i32))?; |
| 165 | + encoder.encode_field(&(-1i32))?; |
| 166 | + encoder.encode_field(&false)?; |
| 167 | + rows.push(Ok(encoder.take_row())); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + Ok(vec![Response::Query(QueryResponse::new( |
| 172 | + schema, |
| 173 | + stream::iter(rows), |
| 174 | + ))]) |
| 175 | +} |
| 176 | + |
| 177 | +/// `pg_index` — secondary indexes. |
| 178 | +/// |
| 179 | +/// Returns an empty result set with the correct schema. Structured |
| 180 | +/// index metadata is not yet surfaced through `StoredCollection`; |
| 181 | +/// once it is, this function will take `(state, identity)` and |
| 182 | +/// populate rows from the catalog. |
| 183 | +pub fn pg_index() -> PgWireResult<Vec<Response>> { |
| 184 | + let schema = Arc::new(vec![ |
| 185 | + int8_field("indexrelid"), |
| 186 | + int8_field("indrelid"), |
| 187 | + bool_field("indisunique"), |
| 188 | + bool_field("indisprimary"), |
| 189 | + ]); |
| 190 | + |
| 191 | + let rows: Vec<Result<_, pgwire::error::PgWireError>> = Vec::new(); |
| 192 | + |
| 193 | + Ok(vec![Response::Query(QueryResponse::new( |
| 194 | + schema, |
| 195 | + stream::iter(rows), |
| 196 | + ))]) |
| 197 | +} |
| 198 | + |
| 199 | +/// `pg_authid` — users / roles. |
| 200 | +pub fn pg_authid( |
| 201 | + state: &SharedState, |
| 202 | + identity: &AuthenticatedIdentity, |
| 203 | +) -> PgWireResult<Vec<Response>> { |
| 204 | + let schema = Arc::new(vec![ |
| 205 | + int8_field("oid"), |
| 206 | + text_field("rolname"), |
| 207 | + bool_field("rolsuper"), |
| 208 | + bool_field("rolcanlogin"), |
| 209 | + ]); |
| 210 | + |
| 211 | + let mut rows = Vec::new(); |
| 212 | + let mut encoder = DataRowEncoder::new(schema.clone()); |
| 213 | + |
| 214 | + let users = state.credentials.list_users(); |
| 215 | + for (i, user) in users.iter().enumerate() { |
| 216 | + let oid = 10i64 + i as i64; |
| 217 | + let is_super = identity.is_superuser && user == &identity.username; |
| 218 | + encoder.encode_field(&oid)?; |
| 219 | + encoder.encode_field(&user.as_str())?; |
| 220 | + encoder.encode_field(&is_super)?; |
| 221 | + encoder.encode_field(&true)?; |
| 222 | + rows.push(Ok(encoder.take_row())); |
| 223 | + } |
| 224 | + |
| 225 | + Ok(vec![Response::Query(QueryResponse::new( |
| 226 | + schema, |
| 227 | + stream::iter(rows), |
| 228 | + ))]) |
| 229 | +} |
| 230 | + |
| 231 | +fn load_collections( |
| 232 | + state: &SharedState, |
| 233 | + identity: &AuthenticatedIdentity, |
| 234 | +) -> Vec<crate::control::security::catalog::types::StoredCollection> { |
| 235 | + let Some(catalog) = state.credentials.catalog() else { |
| 236 | + return Vec::new(); |
| 237 | + }; |
| 238 | + if identity.is_superuser { |
| 239 | + catalog |
| 240 | + .load_all_collections() |
| 241 | + .unwrap_or_default() |
| 242 | + .into_iter() |
| 243 | + .filter(|c| c.is_active) |
| 244 | + .collect() |
| 245 | + } else { |
| 246 | + catalog |
| 247 | + .load_collections_for_tenant(identity.tenant_id.as_u32()) |
| 248 | + .unwrap_or_default() |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +fn field_type_to_oid(field_type: &str) -> i64 { |
| 253 | + match field_type.to_lowercase().as_str() { |
| 254 | + "bool" | "boolean" => 16, |
| 255 | + "int" | "integer" | "int4" => 23, |
| 256 | + "bigint" | "int8" => 20, |
| 257 | + "smallint" | "int2" => 21, |
| 258 | + "float" | "float4" | "real" => 700, |
| 259 | + "double" | "float8" => 701, |
| 260 | + "text" | "string" => 25, |
| 261 | + "varchar" => 1043, |
| 262 | + "json" => 114, |
| 263 | + "jsonb" => 3802, |
| 264 | + "uuid" => 2950, |
| 265 | + "date" => 1082, |
| 266 | + "timestamp" => 1114, |
| 267 | + "timestamptz" => 1184, |
| 268 | + _ => 25, |
| 269 | + } |
| 270 | +} |
0 commit comments