Skip to main content

mz_catalog/builtin/
pg_catalog.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Built-in catalog items for the `pg_catalog` schema.
11
12use std::collections::BTreeMap;
13use std::sync::LazyLock;
14
15use mz_pgrepr::oid;
16use mz_repr::namespaces::PG_CATALOG_SCHEMA;
17use mz_repr::{RelationDesc, SqlScalarType};
18use mz_sql::catalog::{
19    CatalogType, CatalogTypeDetails, CatalogTypePgMetadata, NameReference, ObjectType,
20};
21use mz_sql::rbac;
22use mz_sql::session::user::MZ_SYSTEM_ROLE_ID;
23
24use super::{BuiltinType, BuiltinView, PUBLIC_SELECT};
25
26// Builtin definitions below. Ensure you add new builtins to the `BUILTINS` map.
27//
28// You SHOULD NOT delete a builtin. If you do, you will break any downstream
29// user objects that depended on the builtin.
30//
31// Builtins are loaded in dependency order, so a builtin must appear in `BUILTINS`
32// before any items it depends upon.
33//
34// WARNING: if you change the definition of an existing builtin item, you must
35// be careful to maintain backwards compatibility! Adding new columns is safe.
36// Removing a column, changing the name of a column, or changing the type of a
37// column is not safe, as persisted user views may depend upon that column.
38
39// The following types are the list of builtin data types available
40// in Materialize. This list is derived from the `pg_type` table in PostgreSQL.
41//
42// Builtin types cannot be created, updated, or deleted. Their OIDs
43// are static, unlike other objects, to match the type OIDs defined by Postgres.
44
45pub const TYPE_BOOL: BuiltinType<NameReference> = BuiltinType {
46    name: "bool",
47    schema: PG_CATALOG_SCHEMA,
48    oid: oid::TYPE_BOOL_OID,
49    details: CatalogTypeDetails {
50        typ: CatalogType::Bool,
51        array_id: None,
52        pg_metadata: Some(CatalogTypePgMetadata {
53            typinput_oid: 1242,
54            typreceive_oid: 2436,
55        }),
56    },
57};
58
59pub const TYPE_BYTEA: BuiltinType<NameReference> = BuiltinType {
60    name: "bytea",
61    schema: PG_CATALOG_SCHEMA,
62    oid: oid::TYPE_BYTEA_OID,
63    details: CatalogTypeDetails {
64        typ: CatalogType::Bytes,
65        array_id: None,
66        pg_metadata: Some(CatalogTypePgMetadata {
67            typinput_oid: 1244,
68            typreceive_oid: 2412,
69        }),
70    },
71};
72
73pub const TYPE_INT8: BuiltinType<NameReference> = BuiltinType {
74    name: "int8",
75    schema: PG_CATALOG_SCHEMA,
76    oid: oid::TYPE_INT8_OID,
77    details: CatalogTypeDetails {
78        typ: CatalogType::Int64,
79        array_id: None,
80        pg_metadata: Some(CatalogTypePgMetadata {
81            typinput_oid: 460,
82            typreceive_oid: 2408,
83        }),
84    },
85};
86
87pub const TYPE_INT4: BuiltinType<NameReference> = BuiltinType {
88    name: "int4",
89    schema: PG_CATALOG_SCHEMA,
90    oid: oid::TYPE_INT4_OID,
91    details: CatalogTypeDetails {
92        typ: CatalogType::Int32,
93        array_id: None,
94        pg_metadata: Some(CatalogTypePgMetadata {
95            typinput_oid: 42,
96            typreceive_oid: 2406,
97        }),
98    },
99};
100
101pub const TYPE_TEXT: BuiltinType<NameReference> = BuiltinType {
102    name: "text",
103    schema: PG_CATALOG_SCHEMA,
104    oid: oid::TYPE_TEXT_OID,
105    details: CatalogTypeDetails {
106        typ: CatalogType::String,
107        array_id: None,
108        pg_metadata: Some(CatalogTypePgMetadata {
109            typinput_oid: 46,
110            typreceive_oid: 2414,
111        }),
112    },
113};
114
115pub const TYPE_OID: BuiltinType<NameReference> = BuiltinType {
116    name: "oid",
117    schema: PG_CATALOG_SCHEMA,
118    oid: oid::TYPE_OID_OID,
119    details: CatalogTypeDetails {
120        typ: CatalogType::Oid,
121        array_id: None,
122        pg_metadata: Some(CatalogTypePgMetadata {
123            typinput_oid: 1798,
124            typreceive_oid: 2418,
125        }),
126    },
127};
128
129pub const TYPE_FLOAT4: BuiltinType<NameReference> = BuiltinType {
130    name: "float4",
131    schema: PG_CATALOG_SCHEMA,
132    oid: oid::TYPE_FLOAT4_OID,
133    details: CatalogTypeDetails {
134        typ: CatalogType::Float32,
135        array_id: None,
136        pg_metadata: Some(CatalogTypePgMetadata {
137            typinput_oid: 200,
138            typreceive_oid: 2424,
139        }),
140    },
141};
142
143pub const TYPE_FLOAT8: BuiltinType<NameReference> = BuiltinType {
144    name: "float8",
145    schema: PG_CATALOG_SCHEMA,
146    oid: oid::TYPE_FLOAT8_OID,
147    details: CatalogTypeDetails {
148        typ: CatalogType::Float64,
149        array_id: None,
150        pg_metadata: Some(CatalogTypePgMetadata {
151            typinput_oid: 214,
152            typreceive_oid: 2426,
153        }),
154    },
155};
156
157pub const TYPE_BOOL_ARRAY: BuiltinType<NameReference> = BuiltinType {
158    name: "_bool",
159    schema: PG_CATALOG_SCHEMA,
160    oid: oid::TYPE_BOOL_ARRAY_OID,
161    details: CatalogTypeDetails {
162        typ: CatalogType::Array {
163            element_reference: TYPE_BOOL.name,
164        },
165        array_id: None,
166        pg_metadata: Some(CatalogTypePgMetadata {
167            typinput_oid: 750,
168            typreceive_oid: 2400,
169        }),
170    },
171};
172
173pub const TYPE_BYTEA_ARRAY: BuiltinType<NameReference> = BuiltinType {
174    name: "_bytea",
175    schema: PG_CATALOG_SCHEMA,
176    oid: oid::TYPE_BYTEA_ARRAY_OID,
177    details: CatalogTypeDetails {
178        typ: CatalogType::Array {
179            element_reference: TYPE_BYTEA.name,
180        },
181        array_id: None,
182        pg_metadata: Some(CatalogTypePgMetadata {
183            typinput_oid: 750,
184            typreceive_oid: 2400,
185        }),
186    },
187};
188
189pub const TYPE_INT4_ARRAY: BuiltinType<NameReference> = BuiltinType {
190    name: "_int4",
191    schema: PG_CATALOG_SCHEMA,
192    oid: oid::TYPE_INT4_ARRAY_OID,
193    details: CatalogTypeDetails {
194        typ: CatalogType::Array {
195            element_reference: TYPE_INT4.name,
196        },
197        array_id: None,
198        pg_metadata: Some(CatalogTypePgMetadata {
199            typinput_oid: 750,
200            typreceive_oid: 2400,
201        }),
202    },
203};
204
205pub const TYPE_TEXT_ARRAY: BuiltinType<NameReference> = BuiltinType {
206    name: "_text",
207    schema: PG_CATALOG_SCHEMA,
208    oid: oid::TYPE_TEXT_ARRAY_OID,
209    details: CatalogTypeDetails {
210        typ: CatalogType::Array {
211            element_reference: TYPE_TEXT.name,
212        },
213        array_id: None,
214        pg_metadata: Some(CatalogTypePgMetadata {
215            typinput_oid: 750,
216            typreceive_oid: 2400,
217        }),
218    },
219};
220
221pub const TYPE_INT8_ARRAY: BuiltinType<NameReference> = BuiltinType {
222    name: "_int8",
223    schema: PG_CATALOG_SCHEMA,
224    oid: oid::TYPE_INT8_ARRAY_OID,
225    details: CatalogTypeDetails {
226        typ: CatalogType::Array {
227            element_reference: TYPE_INT8.name,
228        },
229        array_id: None,
230        pg_metadata: Some(CatalogTypePgMetadata {
231            typinput_oid: 750,
232            typreceive_oid: 2400,
233        }),
234    },
235};
236
237pub const TYPE_FLOAT4_ARRAY: BuiltinType<NameReference> = BuiltinType {
238    name: "_float4",
239    schema: PG_CATALOG_SCHEMA,
240    oid: oid::TYPE_FLOAT4_ARRAY_OID,
241    details: CatalogTypeDetails {
242        typ: CatalogType::Array {
243            element_reference: TYPE_FLOAT4.name,
244        },
245        array_id: None,
246        pg_metadata: Some(CatalogTypePgMetadata {
247            typinput_oid: 750,
248            typreceive_oid: 2400,
249        }),
250    },
251};
252
253pub const TYPE_FLOAT8_ARRAY: BuiltinType<NameReference> = BuiltinType {
254    name: "_float8",
255    schema: PG_CATALOG_SCHEMA,
256    oid: oid::TYPE_FLOAT8_ARRAY_OID,
257    details: CatalogTypeDetails {
258        typ: CatalogType::Array {
259            element_reference: TYPE_FLOAT8.name,
260        },
261        array_id: None,
262        pg_metadata: Some(CatalogTypePgMetadata {
263            typinput_oid: 750,
264            typreceive_oid: 2400,
265        }),
266    },
267};
268
269pub const TYPE_OID_ARRAY: BuiltinType<NameReference> = BuiltinType {
270    name: "_oid",
271    schema: PG_CATALOG_SCHEMA,
272    oid: oid::TYPE_OID_ARRAY_OID,
273    details: CatalogTypeDetails {
274        typ: CatalogType::Array {
275            element_reference: TYPE_OID.name,
276        },
277        array_id: None,
278        pg_metadata: Some(CatalogTypePgMetadata {
279            typinput_oid: 750,
280            typreceive_oid: 2400,
281        }),
282    },
283};
284
285pub const TYPE_DATE: BuiltinType<NameReference> = BuiltinType {
286    name: "date",
287    schema: PG_CATALOG_SCHEMA,
288    oid: oid::TYPE_DATE_OID,
289    details: CatalogTypeDetails {
290        typ: CatalogType::Date,
291        array_id: None,
292        pg_metadata: Some(CatalogTypePgMetadata {
293            typinput_oid: 1084,
294            typreceive_oid: 2468,
295        }),
296    },
297};
298
299pub const TYPE_TIME: BuiltinType<NameReference> = BuiltinType {
300    name: "time",
301    schema: PG_CATALOG_SCHEMA,
302    oid: oid::TYPE_TIME_OID,
303    details: CatalogTypeDetails {
304        typ: CatalogType::Time,
305        array_id: None,
306        pg_metadata: Some(CatalogTypePgMetadata {
307            typinput_oid: 1143,
308            typreceive_oid: 2470,
309        }),
310    },
311};
312
313pub const TYPE_TIMESTAMP: BuiltinType<NameReference> = BuiltinType {
314    name: "timestamp",
315    schema: PG_CATALOG_SCHEMA,
316    oid: oid::TYPE_TIMESTAMP_OID,
317    details: CatalogTypeDetails {
318        typ: CatalogType::Timestamp,
319        array_id: None,
320        pg_metadata: Some(CatalogTypePgMetadata {
321            typinput_oid: 1312,
322            typreceive_oid: 2474,
323        }),
324    },
325};
326
327pub const TYPE_TIMESTAMP_ARRAY: BuiltinType<NameReference> = BuiltinType {
328    name: "_timestamp",
329    schema: PG_CATALOG_SCHEMA,
330    oid: oid::TYPE_TIMESTAMP_ARRAY_OID,
331    details: CatalogTypeDetails {
332        typ: CatalogType::Array {
333            element_reference: TYPE_TIMESTAMP.name,
334        },
335        array_id: None,
336        pg_metadata: Some(CatalogTypePgMetadata {
337            typinput_oid: 750,
338            typreceive_oid: 2400,
339        }),
340    },
341};
342
343pub const TYPE_DATE_ARRAY: BuiltinType<NameReference> = BuiltinType {
344    name: "_date",
345    schema: PG_CATALOG_SCHEMA,
346    oid: oid::TYPE_DATE_ARRAY_OID,
347    details: CatalogTypeDetails {
348        typ: CatalogType::Array {
349            element_reference: TYPE_DATE.name,
350        },
351        array_id: None,
352        pg_metadata: Some(CatalogTypePgMetadata {
353            typinput_oid: 750,
354            typreceive_oid: 2400,
355        }),
356    },
357};
358
359pub const TYPE_TIME_ARRAY: BuiltinType<NameReference> = BuiltinType {
360    name: "_time",
361    schema: PG_CATALOG_SCHEMA,
362    oid: oid::TYPE_TIME_ARRAY_OID,
363    details: CatalogTypeDetails {
364        typ: CatalogType::Array {
365            element_reference: TYPE_TIME.name,
366        },
367        array_id: None,
368        pg_metadata: Some(CatalogTypePgMetadata {
369            typinput_oid: 750,
370            typreceive_oid: 2400,
371        }),
372    },
373};
374
375pub const TYPE_TIMESTAMPTZ: BuiltinType<NameReference> = BuiltinType {
376    name: "timestamptz",
377    schema: PG_CATALOG_SCHEMA,
378    oid: oid::TYPE_TIMESTAMPTZ_OID,
379    details: CatalogTypeDetails {
380        typ: CatalogType::TimestampTz,
381        array_id: None,
382        pg_metadata: Some(CatalogTypePgMetadata {
383            typinput_oid: 1150,
384            typreceive_oid: 2476,
385        }),
386    },
387};
388
389pub const TYPE_TIMESTAMPTZ_ARRAY: BuiltinType<NameReference> = BuiltinType {
390    name: "_timestamptz",
391    schema: PG_CATALOG_SCHEMA,
392    oid: oid::TYPE_TIMESTAMPTZ_ARRAY_OID,
393    details: CatalogTypeDetails {
394        typ: CatalogType::Array {
395            element_reference: TYPE_TIMESTAMPTZ.name,
396        },
397        array_id: None,
398        pg_metadata: Some(CatalogTypePgMetadata {
399            typinput_oid: 750,
400            typreceive_oid: 2400,
401        }),
402    },
403};
404
405pub const TYPE_INTERVAL: BuiltinType<NameReference> = BuiltinType {
406    name: "interval",
407    schema: PG_CATALOG_SCHEMA,
408    oid: oid::TYPE_INTERVAL_OID,
409    details: CatalogTypeDetails {
410        typ: CatalogType::Interval,
411        array_id: None,
412        pg_metadata: Some(CatalogTypePgMetadata {
413            typinput_oid: 1160,
414            typreceive_oid: 2478,
415        }),
416    },
417};
418
419pub const TYPE_INTERVAL_ARRAY: BuiltinType<NameReference> = BuiltinType {
420    name: "_interval",
421    schema: PG_CATALOG_SCHEMA,
422    oid: oid::TYPE_INTERVAL_ARRAY_OID,
423    details: CatalogTypeDetails {
424        typ: CatalogType::Array {
425            element_reference: TYPE_INTERVAL.name,
426        },
427        array_id: None,
428        pg_metadata: Some(CatalogTypePgMetadata {
429            typinput_oid: 750,
430            typreceive_oid: 2400,
431        }),
432    },
433};
434
435pub const TYPE_NAME: BuiltinType<NameReference> = BuiltinType {
436    name: "name",
437    schema: PG_CATALOG_SCHEMA,
438    oid: oid::TYPE_NAME_OID,
439    details: CatalogTypeDetails {
440        typ: CatalogType::PgLegacyName,
441        array_id: None,
442        pg_metadata: Some(CatalogTypePgMetadata {
443            typinput_oid: 34,
444            typreceive_oid: 2422,
445        }),
446    },
447};
448
449pub const TYPE_NAME_ARRAY: BuiltinType<NameReference> = BuiltinType {
450    name: "_name",
451    schema: PG_CATALOG_SCHEMA,
452    oid: oid::TYPE_NAME_ARRAY_OID,
453    details: CatalogTypeDetails {
454        typ: CatalogType::Array {
455            element_reference: TYPE_NAME.name,
456        },
457        array_id: None,
458        pg_metadata: Some(CatalogTypePgMetadata {
459            typinput_oid: 750,
460            typreceive_oid: 2400,
461        }),
462    },
463};
464
465pub const TYPE_NUMERIC: BuiltinType<NameReference> = BuiltinType {
466    name: "numeric",
467    schema: PG_CATALOG_SCHEMA,
468    oid: oid::TYPE_NUMERIC_OID,
469    details: CatalogTypeDetails {
470        typ: CatalogType::Numeric,
471        array_id: None,
472        pg_metadata: Some(CatalogTypePgMetadata {
473            typinput_oid: 1701,
474            typreceive_oid: 2460,
475        }),
476    },
477};
478
479pub const TYPE_NUMERIC_ARRAY: BuiltinType<NameReference> = BuiltinType {
480    name: "_numeric",
481    schema: PG_CATALOG_SCHEMA,
482    oid: oid::TYPE_NUMERIC_ARRAY_OID,
483    details: CatalogTypeDetails {
484        typ: CatalogType::Array {
485            element_reference: TYPE_NUMERIC.name,
486        },
487        array_id: None,
488        pg_metadata: Some(CatalogTypePgMetadata {
489            typinput_oid: 750,
490            typreceive_oid: 2400,
491        }),
492    },
493};
494
495pub const TYPE_RECORD: BuiltinType<NameReference> = BuiltinType {
496    name: "record",
497    schema: PG_CATALOG_SCHEMA,
498    oid: oid::TYPE_RECORD_OID,
499    details: CatalogTypeDetails {
500        typ: CatalogType::Pseudo,
501        array_id: None,
502        pg_metadata: Some(CatalogTypePgMetadata {
503            typinput_oid: 2290,
504            typreceive_oid: 2402,
505        }),
506    },
507};
508
509pub const TYPE_RECORD_ARRAY: BuiltinType<NameReference> = BuiltinType {
510    name: "_record",
511    schema: PG_CATALOG_SCHEMA,
512    oid: oid::TYPE_RECORD_ARRAY_OID,
513    details: CatalogTypeDetails {
514        typ: CatalogType::Array {
515            element_reference: TYPE_RECORD.name,
516        },
517        array_id: None,
518        pg_metadata: Some(CatalogTypePgMetadata {
519            typinput_oid: 750,
520            typreceive_oid: 2400,
521        }),
522    },
523};
524
525pub const TYPE_UUID: BuiltinType<NameReference> = BuiltinType {
526    name: "uuid",
527    schema: PG_CATALOG_SCHEMA,
528    oid: oid::TYPE_UUID_OID,
529    details: CatalogTypeDetails {
530        typ: CatalogType::Uuid,
531        array_id: None,
532        pg_metadata: Some(CatalogTypePgMetadata {
533            typinput_oid: 2952,
534            typreceive_oid: 2961,
535        }),
536    },
537};
538
539pub const TYPE_UUID_ARRAY: BuiltinType<NameReference> = BuiltinType {
540    name: "_uuid",
541    schema: PG_CATALOG_SCHEMA,
542    oid: oid::TYPE_UUID_ARRAY_OID,
543    details: CatalogTypeDetails {
544        typ: CatalogType::Array {
545            element_reference: TYPE_UUID.name,
546        },
547        array_id: None,
548        pg_metadata: Some(CatalogTypePgMetadata {
549            typinput_oid: 750,
550            typreceive_oid: 2400,
551        }),
552    },
553};
554
555pub const TYPE_JSONB: BuiltinType<NameReference> = BuiltinType {
556    name: "jsonb",
557    schema: PG_CATALOG_SCHEMA,
558    oid: oid::TYPE_JSONB_OID,
559    details: CatalogTypeDetails {
560        typ: CatalogType::Jsonb,
561        array_id: None,
562        pg_metadata: Some(CatalogTypePgMetadata {
563            typinput_oid: 3806,
564            typreceive_oid: 3805,
565        }),
566    },
567};
568
569pub const TYPE_JSONB_ARRAY: BuiltinType<NameReference> = BuiltinType {
570    name: "_jsonb",
571    schema: PG_CATALOG_SCHEMA,
572    oid: oid::TYPE_JSONB_ARRAY_OID,
573    details: CatalogTypeDetails {
574        typ: CatalogType::Array {
575            element_reference: TYPE_JSONB.name,
576        },
577        array_id: None,
578        pg_metadata: Some(CatalogTypePgMetadata {
579            typinput_oid: 750,
580            typreceive_oid: 2400,
581        }),
582    },
583};
584
585pub const TYPE_ANY: BuiltinType<NameReference> = BuiltinType {
586    name: "any",
587    schema: PG_CATALOG_SCHEMA,
588    oid: oid::TYPE_ANY_OID,
589    details: CatalogTypeDetails {
590        typ: CatalogType::Pseudo,
591        array_id: None,
592        pg_metadata: Some(CatalogTypePgMetadata {
593            typinput_oid: 2294,
594            typreceive_oid: 0,
595        }),
596    },
597};
598
599pub const TYPE_ANYARRAY: BuiltinType<NameReference> = BuiltinType {
600    name: "anyarray",
601    schema: PG_CATALOG_SCHEMA,
602    oid: oid::TYPE_ANYARRAY_OID,
603    details: CatalogTypeDetails {
604        typ: CatalogType::Pseudo,
605        array_id: None,
606        pg_metadata: Some(CatalogTypePgMetadata {
607            typinput_oid: 2296,
608            typreceive_oid: 2502,
609        }),
610    },
611};
612
613pub const TYPE_ANYELEMENT: BuiltinType<NameReference> = BuiltinType {
614    name: "anyelement",
615    schema: PG_CATALOG_SCHEMA,
616    oid: oid::TYPE_ANYELEMENT_OID,
617    details: CatalogTypeDetails {
618        typ: CatalogType::Pseudo,
619        array_id: None,
620        pg_metadata: Some(CatalogTypePgMetadata {
621            typinput_oid: 2312,
622            typreceive_oid: 0,
623        }),
624    },
625};
626
627pub const TYPE_ANYNONARRAY: BuiltinType<NameReference> = BuiltinType {
628    name: "anynonarray",
629    schema: PG_CATALOG_SCHEMA,
630    oid: oid::TYPE_ANYNONARRAY_OID,
631    details: CatalogTypeDetails {
632        typ: CatalogType::Pseudo,
633        array_id: None,
634        pg_metadata: Some(CatalogTypePgMetadata {
635            typinput_oid: 2777,
636            typreceive_oid: 0,
637        }),
638    },
639};
640
641pub const TYPE_ANYRANGE: BuiltinType<NameReference> = BuiltinType {
642    name: "anyrange",
643    schema: PG_CATALOG_SCHEMA,
644    oid: oid::TYPE_ANYRANGE_OID,
645    details: CatalogTypeDetails {
646        typ: CatalogType::Pseudo,
647        array_id: None,
648        pg_metadata: Some(CatalogTypePgMetadata {
649            typinput_oid: 3832,
650            typreceive_oid: 0,
651        }),
652    },
653};
654
655pub const TYPE_CHAR: BuiltinType<NameReference> = BuiltinType {
656    name: "char",
657    schema: PG_CATALOG_SCHEMA,
658    oid: oid::TYPE_CHAR_OID,
659    details: CatalogTypeDetails {
660        typ: CatalogType::PgLegacyChar,
661        array_id: None,
662        pg_metadata: Some(CatalogTypePgMetadata {
663            typinput_oid: 1245,
664            typreceive_oid: 2434,
665        }),
666    },
667};
668
669pub const TYPE_VARCHAR: BuiltinType<NameReference> = BuiltinType {
670    name: "varchar",
671    schema: PG_CATALOG_SCHEMA,
672    oid: oid::TYPE_VARCHAR_OID,
673    details: CatalogTypeDetails {
674        typ: CatalogType::VarChar,
675        array_id: None,
676        pg_metadata: Some(CatalogTypePgMetadata {
677            typinput_oid: 1046,
678            typreceive_oid: 2432,
679        }),
680    },
681};
682
683pub const TYPE_INT2: BuiltinType<NameReference> = BuiltinType {
684    name: "int2",
685    schema: PG_CATALOG_SCHEMA,
686    oid: oid::TYPE_INT2_OID,
687    details: CatalogTypeDetails {
688        typ: CatalogType::Int16,
689        array_id: None,
690        pg_metadata: Some(CatalogTypePgMetadata {
691            typinput_oid: 38,
692            typreceive_oid: 2404,
693        }),
694    },
695};
696
697pub const TYPE_INT2_ARRAY: BuiltinType<NameReference> = BuiltinType {
698    name: "_int2",
699    schema: PG_CATALOG_SCHEMA,
700    oid: oid::TYPE_INT2_ARRAY_OID,
701    details: CatalogTypeDetails {
702        typ: CatalogType::Array {
703            element_reference: TYPE_INT2.name,
704        },
705        array_id: None,
706        pg_metadata: Some(CatalogTypePgMetadata {
707            typinput_oid: 750,
708            typreceive_oid: 2400,
709        }),
710    },
711};
712
713pub const TYPE_BPCHAR: BuiltinType<NameReference> = BuiltinType {
714    name: "bpchar",
715    schema: PG_CATALOG_SCHEMA,
716    oid: oid::TYPE_BPCHAR_OID,
717    details: CatalogTypeDetails {
718        typ: CatalogType::Char,
719        array_id: None,
720        pg_metadata: Some(CatalogTypePgMetadata {
721            typinput_oid: 1044,
722            typreceive_oid: 2430,
723        }),
724    },
725};
726
727pub const TYPE_CHAR_ARRAY: BuiltinType<NameReference> = BuiltinType {
728    name: "_char",
729    schema: PG_CATALOG_SCHEMA,
730    oid: oid::TYPE_CHAR_ARRAY_OID,
731    details: CatalogTypeDetails {
732        typ: CatalogType::Array {
733            element_reference: TYPE_CHAR.name,
734        },
735        array_id: None,
736        pg_metadata: Some(CatalogTypePgMetadata {
737            typinput_oid: 750,
738            typreceive_oid: 2400,
739        }),
740    },
741};
742
743pub const TYPE_VARCHAR_ARRAY: BuiltinType<NameReference> = BuiltinType {
744    name: "_varchar",
745    schema: PG_CATALOG_SCHEMA,
746    oid: oid::TYPE_VARCHAR_ARRAY_OID,
747    details: CatalogTypeDetails {
748        typ: CatalogType::Array {
749            element_reference: TYPE_VARCHAR.name,
750        },
751        array_id: None,
752        pg_metadata: Some(CatalogTypePgMetadata {
753            typinput_oid: 750,
754            typreceive_oid: 2400,
755        }),
756    },
757};
758
759pub const TYPE_BPCHAR_ARRAY: BuiltinType<NameReference> = BuiltinType {
760    name: "_bpchar",
761    schema: PG_CATALOG_SCHEMA,
762    oid: oid::TYPE_BPCHAR_ARRAY_OID,
763    details: CatalogTypeDetails {
764        typ: CatalogType::Array {
765            element_reference: TYPE_BPCHAR.name,
766        },
767        array_id: None,
768        pg_metadata: Some(CatalogTypePgMetadata {
769            typinput_oid: 750,
770            typreceive_oid: 2400,
771        }),
772    },
773};
774
775pub const TYPE_REGPROC: BuiltinType<NameReference> = BuiltinType {
776    name: "regproc",
777    schema: PG_CATALOG_SCHEMA,
778    oid: oid::TYPE_REGPROC_OID,
779    details: CatalogTypeDetails {
780        typ: CatalogType::RegProc,
781        array_id: None,
782        pg_metadata: Some(CatalogTypePgMetadata {
783            typinput_oid: 44,
784            typreceive_oid: 2444,
785        }),
786    },
787};
788
789pub const TYPE_REGPROC_ARRAY: BuiltinType<NameReference> = BuiltinType {
790    name: "_regproc",
791    schema: PG_CATALOG_SCHEMA,
792    oid: oid::TYPE_REGPROC_ARRAY_OID,
793    details: CatalogTypeDetails {
794        typ: CatalogType::Array {
795            element_reference: TYPE_REGPROC.name,
796        },
797        array_id: None,
798        pg_metadata: Some(CatalogTypePgMetadata {
799            typinput_oid: 750,
800            typreceive_oid: 2400,
801        }),
802    },
803};
804
805pub const TYPE_REGTYPE: BuiltinType<NameReference> = BuiltinType {
806    name: "regtype",
807    schema: PG_CATALOG_SCHEMA,
808    oid: oid::TYPE_REGTYPE_OID,
809    details: CatalogTypeDetails {
810        typ: CatalogType::RegType,
811        array_id: None,
812        pg_metadata: Some(CatalogTypePgMetadata {
813            typinput_oid: 2220,
814            typreceive_oid: 2454,
815        }),
816    },
817};
818
819pub const TYPE_REGTYPE_ARRAY: BuiltinType<NameReference> = BuiltinType {
820    name: "_regtype",
821    schema: PG_CATALOG_SCHEMA,
822    oid: oid::TYPE_REGTYPE_ARRAY_OID,
823    details: CatalogTypeDetails {
824        typ: CatalogType::Array {
825            element_reference: TYPE_REGTYPE.name,
826        },
827        array_id: None,
828        pg_metadata: Some(CatalogTypePgMetadata {
829            typinput_oid: 750,
830            typreceive_oid: 2400,
831        }),
832    },
833};
834
835pub const TYPE_REGCLASS: BuiltinType<NameReference> = BuiltinType {
836    name: "regclass",
837    schema: PG_CATALOG_SCHEMA,
838    oid: oid::TYPE_REGCLASS_OID,
839    details: CatalogTypeDetails {
840        typ: CatalogType::RegClass,
841        array_id: None,
842        pg_metadata: Some(CatalogTypePgMetadata {
843            typinput_oid: 2218,
844            typreceive_oid: 2452,
845        }),
846    },
847};
848
849pub const TYPE_REGCLASS_ARRAY: BuiltinType<NameReference> = BuiltinType {
850    name: "_regclass",
851    schema: PG_CATALOG_SCHEMA,
852    oid: oid::TYPE_REGCLASS_ARRAY_OID,
853    details: CatalogTypeDetails {
854        typ: CatalogType::Array {
855            element_reference: TYPE_REGCLASS.name,
856        },
857        array_id: None,
858        pg_metadata: Some(CatalogTypePgMetadata {
859            typinput_oid: 750,
860            typreceive_oid: 2400,
861        }),
862    },
863};
864
865pub const TYPE_INT2_VECTOR: BuiltinType<NameReference> = BuiltinType {
866    name: "int2vector",
867    schema: PG_CATALOG_SCHEMA,
868    oid: oid::TYPE_INT2_VECTOR_OID,
869    details: CatalogTypeDetails {
870        typ: CatalogType::Int2Vector,
871        array_id: None,
872        pg_metadata: Some(CatalogTypePgMetadata {
873            typinput_oid: 40,
874            typreceive_oid: 2410,
875        }),
876    },
877};
878
879pub const TYPE_INT2_VECTOR_ARRAY: BuiltinType<NameReference> = BuiltinType {
880    name: "_int2vector",
881    schema: PG_CATALOG_SCHEMA,
882    oid: oid::TYPE_INT2_VECTOR_ARRAY_OID,
883    details: CatalogTypeDetails {
884        typ: CatalogType::Array {
885            element_reference: TYPE_INT2_VECTOR.name,
886        },
887        array_id: None,
888        pg_metadata: Some(CatalogTypePgMetadata {
889            typinput_oid: 750,
890            typreceive_oid: 2400,
891        }),
892    },
893};
894
895pub const TYPE_ANYCOMPATIBLE: BuiltinType<NameReference> = BuiltinType {
896    name: "anycompatible",
897    schema: PG_CATALOG_SCHEMA,
898    oid: oid::TYPE_ANYCOMPATIBLE_OID,
899    details: CatalogTypeDetails {
900        typ: CatalogType::Pseudo,
901        array_id: None,
902        pg_metadata: Some(CatalogTypePgMetadata {
903            typinput_oid: 5086,
904            typreceive_oid: 0,
905        }),
906    },
907};
908
909pub const TYPE_ANYCOMPATIBLEARRAY: BuiltinType<NameReference> = BuiltinType {
910    name: "anycompatiblearray",
911    schema: PG_CATALOG_SCHEMA,
912    oid: oid::TYPE_ANYCOMPATIBLEARRAY_OID,
913    details: CatalogTypeDetails {
914        typ: CatalogType::Pseudo,
915        array_id: None,
916        pg_metadata: Some(CatalogTypePgMetadata {
917            typinput_oid: 5088,
918            typreceive_oid: 5090,
919        }),
920    },
921};
922
923pub const TYPE_ANYCOMPATIBLENONARRAY: BuiltinType<NameReference> = BuiltinType {
924    name: "anycompatiblenonarray",
925    schema: PG_CATALOG_SCHEMA,
926    oid: oid::TYPE_ANYCOMPATIBLENONARRAY_OID,
927    details: CatalogTypeDetails {
928        typ: CatalogType::Pseudo,
929        array_id: None,
930        pg_metadata: Some(CatalogTypePgMetadata {
931            typinput_oid: 5092,
932            typreceive_oid: 0,
933        }),
934    },
935};
936
937pub const TYPE_ANYCOMPATIBLERANGE: BuiltinType<NameReference> = BuiltinType {
938    name: "anycompatiblerange",
939    schema: PG_CATALOG_SCHEMA,
940    oid: oid::TYPE_ANYCOMPATIBLERANGE_OID,
941    details: CatalogTypeDetails {
942        typ: CatalogType::Pseudo,
943        array_id: None,
944        pg_metadata: Some(CatalogTypePgMetadata {
945            typinput_oid: 5094,
946            typreceive_oid: 0,
947        }),
948    },
949};
950
951pub const TYPE_INT4_RANGE: BuiltinType<NameReference> = BuiltinType {
952    name: "int4range",
953    schema: PG_CATALOG_SCHEMA,
954    oid: mz_pgrepr::oid::TYPE_INT4RANGE_OID,
955    details: CatalogTypeDetails {
956        typ: CatalogType::Range {
957            element_reference: TYPE_INT4.name,
958        },
959        array_id: None,
960        pg_metadata: Some(CatalogTypePgMetadata {
961            typinput_oid: 3834,
962            typreceive_oid: 3836,
963        }),
964    },
965};
966
967pub const TYPE_INT4_RANGE_ARRAY: BuiltinType<NameReference> = BuiltinType {
968    name: "_int4range",
969    schema: PG_CATALOG_SCHEMA,
970    oid: mz_pgrepr::oid::TYPE_INT4RANGE_ARRAY_OID,
971    details: CatalogTypeDetails {
972        typ: CatalogType::Array {
973            element_reference: TYPE_INT4_RANGE.name,
974        },
975        array_id: None,
976        pg_metadata: Some(CatalogTypePgMetadata {
977            typinput_oid: 750,
978            typreceive_oid: 2400,
979        }),
980    },
981};
982
983pub const TYPE_INT8_RANGE: BuiltinType<NameReference> = BuiltinType {
984    name: "int8range",
985    schema: PG_CATALOG_SCHEMA,
986    oid: mz_pgrepr::oid::TYPE_INT8RANGE_OID,
987    details: CatalogTypeDetails {
988        typ: CatalogType::Range {
989            element_reference: TYPE_INT8.name,
990        },
991        array_id: None,
992        pg_metadata: Some(CatalogTypePgMetadata {
993            typinput_oid: 3834,
994            typreceive_oid: 3836,
995        }),
996    },
997};
998
999pub const TYPE_INT8_RANGE_ARRAY: BuiltinType<NameReference> = BuiltinType {
1000    name: "_int8range",
1001    schema: PG_CATALOG_SCHEMA,
1002    oid: mz_pgrepr::oid::TYPE_INT8RANGE_ARRAY_OID,
1003    details: CatalogTypeDetails {
1004        typ: CatalogType::Array {
1005            element_reference: TYPE_INT8_RANGE.name,
1006        },
1007        array_id: None,
1008        pg_metadata: Some(CatalogTypePgMetadata {
1009            typinput_oid: 750,
1010            typreceive_oid: 2400,
1011        }),
1012    },
1013};
1014
1015pub const TYPE_DATE_RANGE: BuiltinType<NameReference> = BuiltinType {
1016    name: "daterange",
1017    schema: PG_CATALOG_SCHEMA,
1018    oid: mz_pgrepr::oid::TYPE_DATERANGE_OID,
1019    details: CatalogTypeDetails {
1020        typ: CatalogType::Range {
1021            element_reference: TYPE_DATE.name,
1022        },
1023        array_id: None,
1024        pg_metadata: Some(CatalogTypePgMetadata {
1025            typinput_oid: 3834,
1026            typreceive_oid: 3836,
1027        }),
1028    },
1029};
1030
1031pub const TYPE_DATE_RANGE_ARRAY: BuiltinType<NameReference> = BuiltinType {
1032    name: "_daterange",
1033    schema: PG_CATALOG_SCHEMA,
1034    oid: mz_pgrepr::oid::TYPE_DATERANGE_ARRAY_OID,
1035    details: CatalogTypeDetails {
1036        typ: CatalogType::Array {
1037            element_reference: TYPE_DATE_RANGE.name,
1038        },
1039        array_id: None,
1040        pg_metadata: Some(CatalogTypePgMetadata {
1041            typinput_oid: 750,
1042            typreceive_oid: 2400,
1043        }),
1044    },
1045};
1046
1047pub const TYPE_NUM_RANGE: BuiltinType<NameReference> = BuiltinType {
1048    name: "numrange",
1049    schema: PG_CATALOG_SCHEMA,
1050    oid: mz_pgrepr::oid::TYPE_NUMRANGE_OID,
1051    details: CatalogTypeDetails {
1052        typ: CatalogType::Range {
1053            element_reference: TYPE_NUMERIC.name,
1054        },
1055        array_id: None,
1056        pg_metadata: Some(CatalogTypePgMetadata {
1057            typinput_oid: 3834,
1058            typreceive_oid: 3836,
1059        }),
1060    },
1061};
1062
1063pub const TYPE_NUM_RANGE_ARRAY: BuiltinType<NameReference> = BuiltinType {
1064    name: "_numrange",
1065    schema: PG_CATALOG_SCHEMA,
1066    oid: mz_pgrepr::oid::TYPE_NUMRANGE_ARRAY_OID,
1067    details: CatalogTypeDetails {
1068        typ: CatalogType::Array {
1069            element_reference: TYPE_NUM_RANGE.name,
1070        },
1071        array_id: None,
1072        pg_metadata: Some(CatalogTypePgMetadata {
1073            typinput_oid: 750,
1074            typreceive_oid: 2400,
1075        }),
1076    },
1077};
1078
1079pub const TYPE_TS_RANGE: BuiltinType<NameReference> = BuiltinType {
1080    name: "tsrange",
1081    schema: PG_CATALOG_SCHEMA,
1082    oid: mz_pgrepr::oid::TYPE_TSRANGE_OID,
1083    details: CatalogTypeDetails {
1084        typ: CatalogType::Range {
1085            element_reference: TYPE_TIMESTAMP.name,
1086        },
1087        array_id: None,
1088        pg_metadata: Some(CatalogTypePgMetadata {
1089            typinput_oid: 3834,
1090            typreceive_oid: 3836,
1091        }),
1092    },
1093};
1094
1095pub const TYPE_TS_RANGE_ARRAY: BuiltinType<NameReference> = BuiltinType {
1096    name: "_tsrange",
1097    schema: PG_CATALOG_SCHEMA,
1098    oid: mz_pgrepr::oid::TYPE_TSRANGE_ARRAY_OID,
1099    details: CatalogTypeDetails {
1100        typ: CatalogType::Array {
1101            element_reference: TYPE_TS_RANGE.name,
1102        },
1103        array_id: None,
1104        pg_metadata: Some(CatalogTypePgMetadata {
1105            typinput_oid: 750,
1106            typreceive_oid: 2400,
1107        }),
1108    },
1109};
1110
1111pub const TYPE_TSTZ_RANGE: BuiltinType<NameReference> = BuiltinType {
1112    name: "tstzrange",
1113    schema: PG_CATALOG_SCHEMA,
1114    oid: mz_pgrepr::oid::TYPE_TSTZRANGE_OID,
1115    details: CatalogTypeDetails {
1116        typ: CatalogType::Range {
1117            element_reference: TYPE_TIMESTAMPTZ.name,
1118        },
1119        array_id: None,
1120        pg_metadata: Some(CatalogTypePgMetadata {
1121            typinput_oid: 3834,
1122            typreceive_oid: 3836,
1123        }),
1124    },
1125};
1126
1127pub const TYPE_TSTZ_RANGE_ARRAY: BuiltinType<NameReference> = BuiltinType {
1128    name: "_tstzrange",
1129    schema: PG_CATALOG_SCHEMA,
1130    oid: mz_pgrepr::oid::TYPE_TSTZRANGE_ARRAY_OID,
1131    details: CatalogTypeDetails {
1132        typ: CatalogType::Array {
1133            element_reference: TYPE_TSTZ_RANGE.name,
1134        },
1135        array_id: None,
1136        pg_metadata: Some(CatalogTypePgMetadata {
1137            typinput_oid: 750,
1138            typreceive_oid: 2400,
1139        }),
1140    },
1141};
1142
1143pub const TYPE_ACL_ITEM: BuiltinType<NameReference> = BuiltinType {
1144    name: "aclitem",
1145    schema: PG_CATALOG_SCHEMA,
1146    oid: 1033,
1147    details: CatalogTypeDetails {
1148        typ: CatalogType::AclItem,
1149        array_id: None,
1150        pg_metadata: Some(CatalogTypePgMetadata {
1151            typinput_oid: 1031,
1152            typreceive_oid: 0,
1153        }),
1154    },
1155};
1156
1157pub const TYPE_ACL_ITEM_ARRAY: BuiltinType<NameReference> = BuiltinType {
1158    name: "_aclitem",
1159    schema: PG_CATALOG_SCHEMA,
1160    oid: 1034,
1161    details: CatalogTypeDetails {
1162        typ: CatalogType::Array {
1163            element_reference: TYPE_ACL_ITEM.name,
1164        },
1165        array_id: None,
1166        pg_metadata: Some(CatalogTypePgMetadata {
1167            typinput_oid: 750,
1168            typreceive_oid: 2400,
1169        }),
1170    },
1171};
1172
1173pub const TYPE_INTERNAL: BuiltinType<NameReference> = BuiltinType {
1174    name: "internal",
1175    schema: PG_CATALOG_SCHEMA,
1176    oid: 2281,
1177    details: CatalogTypeDetails {
1178        typ: CatalogType::Pseudo,
1179        array_id: None,
1180        pg_metadata: Some(CatalogTypePgMetadata {
1181            typinput_oid: 2304,
1182            typreceive_oid: 0,
1183        }),
1184    },
1185};
1186
1187pub static PG_NAMESPACE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1188    name: "pg_namespace",
1189    schema: PG_CATALOG_SCHEMA,
1190    oid: oid::VIEW_PG_NAMESPACE_OID,
1191    desc: RelationDesc::builder()
1192        .with_column("oid", SqlScalarType::Oid.nullable(false))
1193        .with_column("nspname", SqlScalarType::String.nullable(false))
1194        .with_column("nspowner", SqlScalarType::Oid.nullable(false))
1195        .with_column(
1196            "nspacl",
1197            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(true),
1198        )
1199        .finish(),
1200    column_comments: BTreeMap::new(),
1201    sql: "
1202SELECT
1203    oid, nspname, nspowner, nspacl
1204FROM mz_internal.pg_namespace_all_databases
1205WHERE database_name IS NULL OR database_name = pg_catalog.current_database();",
1206    access: vec![PUBLIC_SELECT],
1207    ontology: None,
1208});
1209
1210pub static PG_CLASS: LazyLock<BuiltinView> = LazyLock::new(|| {
1211    BuiltinView {
1212    name: "pg_class",
1213    schema: PG_CATALOG_SCHEMA,
1214    oid: oid::VIEW_PG_CLASS_OID,
1215    desc: RelationDesc::builder()
1216        .with_column("oid", SqlScalarType::Oid.nullable(false))
1217        .with_column("relname", SqlScalarType::String.nullable(false))
1218        .with_column("relnamespace", SqlScalarType::Oid.nullable(false))
1219        .with_column("reloftype", SqlScalarType::Oid.nullable(false))
1220        .with_column("relowner", SqlScalarType::Oid.nullable(false))
1221        .with_column("relam", SqlScalarType::Oid.nullable(false))
1222        .with_column("reltablespace", SqlScalarType::Oid.nullable(false))
1223        .with_column("reltuples", SqlScalarType::Float32.nullable(false))
1224        .with_column("reltoastrelid", SqlScalarType::Oid.nullable(false))
1225        .with_column("relhasindex", SqlScalarType::Bool.nullable(false))
1226        .with_column("relpersistence", SqlScalarType::PgLegacyChar.nullable(false))
1227        .with_column("relkind", SqlScalarType::String.nullable(true))
1228        .with_column("relnatts", SqlScalarType::Int16.nullable(false))
1229        .with_column("relchecks", SqlScalarType::Int16.nullable(false))
1230        .with_column("relhasrules", SqlScalarType::Bool.nullable(false))
1231        .with_column("relhastriggers", SqlScalarType::Bool.nullable(false))
1232        .with_column("relhassubclass", SqlScalarType::Bool.nullable(false))
1233        .with_column("relrowsecurity", SqlScalarType::Bool.nullable(false))
1234        .with_column("relforcerowsecurity", SqlScalarType::Bool.nullable(false))
1235        .with_column("relreplident", SqlScalarType::PgLegacyChar.nullable(false))
1236        .with_column("relispartition", SqlScalarType::Bool.nullable(false))
1237        .with_column("relhasoids", SqlScalarType::Bool.nullable(false))
1238        .with_column(
1239            "reloptions",
1240            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(true),
1241        )
1242        .finish(),
1243    column_comments: BTreeMap::new(),
1244    sql: "
1245SELECT
1246    oid, relname, relnamespace, reloftype, relowner, relam, reltablespace, reltuples, reltoastrelid,
1247    relhasindex, relpersistence, relkind, relnatts, relchecks, relhasrules, relhastriggers, relhassubclass,
1248    relrowsecurity, relforcerowsecurity, relreplident, relispartition, relhasoids, reloptions
1249FROM mz_internal.pg_class_all_databases
1250WHERE database_name IS NULL OR database_name = pg_catalog.current_database();
1251",
1252    access: vec![PUBLIC_SELECT],
1253    ontology: None,
1254}
1255});
1256
1257pub static PG_DEPEND: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1258    name: "pg_depend",
1259    schema: PG_CATALOG_SCHEMA,
1260    oid: oid::VIEW_PG_DEPEND_OID,
1261    desc: RelationDesc::builder()
1262        .with_column("classid", SqlScalarType::Oid.nullable(true))
1263        .with_column("objid", SqlScalarType::Oid.nullable(false))
1264        .with_column("objsubid", SqlScalarType::Int32.nullable(false))
1265        .with_column("refclassid", SqlScalarType::Oid.nullable(true))
1266        .with_column("refobjid", SqlScalarType::Oid.nullable(false))
1267        .with_column("refobjsubid", SqlScalarType::Int32.nullable(false))
1268        .with_column("deptype", SqlScalarType::PgLegacyChar.nullable(false))
1269        .finish(),
1270    column_comments: BTreeMap::new(),
1271    sql: "
1272WITH class_objects AS (
1273    SELECT
1274        CASE
1275            WHEN type = 'table' THEN 'pg_tables'::pg_catalog.regclass::pg_catalog.oid
1276            WHEN type = 'source' THEN 'pg_tables'::pg_catalog.regclass::pg_catalog.oid
1277            WHEN type = 'view' THEN 'pg_views'::pg_catalog.regclass::pg_catalog.oid
1278            WHEN type = 'materialized-view' THEN 'pg_matviews'::pg_catalog.regclass::pg_catalog.oid
1279        END classid,
1280        id,
1281        oid,
1282        schema_id
1283    FROM mz_catalog.mz_relations
1284    UNION ALL
1285    SELECT
1286        'pg_index'::pg_catalog.regclass::pg_catalog.oid AS classid,
1287        i.id,
1288        i.oid,
1289        r.schema_id
1290    FROM mz_catalog.mz_indexes i
1291    JOIN mz_catalog.mz_relations r ON i.on_id = r.id
1292),
1293
1294current_objects AS (
1295    SELECT class_objects.*
1296    FROM class_objects
1297    JOIN mz_catalog.mz_schemas ON mz_schemas.id = class_objects.schema_id
1298    LEFT JOIN mz_catalog.mz_databases d ON d.id = mz_schemas.database_id
1299    -- This filter is tricky, as it filters out not just objects outside the
1300    -- database, but *dependencies* on objects outside this database. It's not
1301    -- clear that this is the right choice, but because PostgreSQL doesn't
1302    -- support cross-database references, it's not clear that the other choice
1303    -- is better.
1304    WHERE mz_schemas.database_id IS NULL OR d.name = pg_catalog.current_database()
1305)
1306
1307SELECT
1308    objects.classid::pg_catalog.oid,
1309    objects.oid::pg_catalog.oid AS objid,
1310    0::pg_catalog.int4 AS objsubid,
1311    dependents.classid::pg_catalog.oid AS refclassid,
1312    dependents.oid::pg_catalog.oid AS refobjid,
1313    0::pg_catalog.int4 AS refobjsubid,
1314    'n'::pg_catalog.char AS deptype
1315FROM mz_internal.mz_object_dependencies
1316JOIN current_objects objects ON object_id = objects.id
1317JOIN current_objects dependents ON referenced_object_id = dependents.id",
1318    access: vec![PUBLIC_SELECT],
1319    ontology: None,
1320});
1321
1322pub static PG_DATABASE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1323    name: "pg_database",
1324    schema: PG_CATALOG_SCHEMA,
1325    oid: oid::VIEW_PG_DATABASE_OID,
1326    desc: RelationDesc::builder()
1327        .with_column("oid", SqlScalarType::Oid.nullable(false))
1328        .with_column("datname", SqlScalarType::String.nullable(false))
1329        .with_column("datdba", SqlScalarType::Oid.nullable(false))
1330        .with_column("encoding", SqlScalarType::Int32.nullable(false))
1331        .with_column("datistemplate", SqlScalarType::Bool.nullable(false))
1332        .with_column("datallowconn", SqlScalarType::Bool.nullable(false))
1333        .with_column("datcollate", SqlScalarType::String.nullable(false))
1334        .with_column("datctype", SqlScalarType::String.nullable(false))
1335        .with_column(
1336            "datacl",
1337            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(true),
1338        )
1339        .with_key(vec![0])
1340        .finish(),
1341    column_comments: BTreeMap::new(),
1342    sql: "SELECT
1343    d.oid as oid,
1344    d.name as datname,
1345    role_owner.oid as datdba,
1346    6 as encoding,
1347    -- Materialize doesn't support database cloning.
1348    FALSE AS datistemplate,
1349    TRUE AS datallowconn,
1350    'C' as datcollate,
1351    'C' as datctype,
1352    NULL::pg_catalog.text[] as datacl
1353FROM mz_catalog.mz_databases d
1354JOIN mz_catalog.mz_roles role_owner ON role_owner.id = d.owner_id",
1355    access: vec![PUBLIC_SELECT],
1356    ontology: None,
1357});
1358
1359pub static PG_INDEX: LazyLock<BuiltinView> = LazyLock::new(|| {
1360    BuiltinView {
1361        name: "pg_index",
1362        schema: PG_CATALOG_SCHEMA,
1363        oid: oid::VIEW_PG_INDEX_OID,
1364        desc: RelationDesc::builder()
1365            .with_column("indexrelid", SqlScalarType::Oid.nullable(false))
1366            .with_column("indrelid", SqlScalarType::Oid.nullable(false))
1367            .with_column("indnatts", SqlScalarType::Int16.nullable(false))
1368            .with_column("indisunique", SqlScalarType::Bool.nullable(false))
1369            .with_column("indisprimary", SqlScalarType::Bool.nullable(false))
1370            .with_column("indimmediate", SqlScalarType::Bool.nullable(false))
1371            .with_column("indisclustered", SqlScalarType::Bool.nullable(false))
1372            .with_column("indisvalid", SqlScalarType::Bool.nullable(false))
1373            .with_column("indisreplident", SqlScalarType::Bool.nullable(false))
1374            .with_column("indkey", SqlScalarType::Int2Vector.nullable(false))
1375            .with_column("indoption", SqlScalarType::Int2Vector.nullable(false))
1376            .with_column("indexprs", SqlScalarType::String.nullable(true))
1377            .with_column("indpred", SqlScalarType::String.nullable(true))
1378            .with_key(vec![0, 1])
1379            .finish(),
1380        column_comments: BTreeMap::new(),
1381        sql: "SELECT
1382    mz_indexes.oid AS indexrelid,
1383    mz_relations.oid AS indrelid,
1384    count(mz_index_columns.index_position)::pg_catalog.int2 AS indnatts,
1385    -- MZ doesn't support creating unique indexes so indisunique is filled with false
1386    false::pg_catalog.bool AS indisunique,
1387    false::pg_catalog.bool AS indisprimary,
1388    -- MZ doesn't support unique indexes so indimmediate is filled with false
1389    false::pg_catalog.bool AS indimmediate,
1390    -- MZ doesn't support CLUSTER so indisclustered is filled with false
1391    false::pg_catalog.bool AS indisclustered,
1392    -- MZ never creates invalid indexes so indisvalid is filled with true
1393    true::pg_catalog.bool AS indisvalid,
1394    -- MZ doesn't support replication so indisreplident is filled with false
1395    false::pg_catalog.bool AS indisreplident,
1396    -- Return zero if the index attribute is not a simple column reference, column position otherwise
1397    pg_catalog.string_agg(coalesce(mz_index_columns.on_position::int8, 0)::pg_catalog.text, ' ' ORDER BY mz_index_columns.index_position::int8)::pg_catalog.int2vector AS indkey,
1398    -- MZ doesn't have per-column flags, so returning a 0 for each column in the index
1399    pg_catalog.string_agg('0', ' ')::pg_catalog.int2vector AS indoption,
1400    -- Index expressions are returned in MZ format
1401    CASE pg_catalog.string_agg(mz_index_columns.on_expression, ' ' ORDER BY mz_index_columns.index_position::int8)
1402    WHEN NULL THEN NULL
1403    ELSE '{' || pg_catalog.string_agg(mz_index_columns.on_expression, '}, {' ORDER BY mz_index_columns.index_position::int8) || '}'
1404    END AS indexprs,
1405    -- MZ doesn't support indexes with predicates
1406    NULL::pg_catalog.text AS indpred
1407FROM mz_catalog.mz_indexes
1408JOIN mz_catalog.mz_relations ON mz_indexes.on_id = mz_relations.id
1409JOIN mz_catalog.mz_index_columns ON mz_index_columns.index_id = mz_indexes.id
1410JOIN mz_catalog.mz_schemas ON mz_schemas.id = mz_relations.schema_id
1411LEFT JOIN mz_catalog.mz_databases d ON d.id = mz_schemas.database_id
1412WHERE mz_schemas.database_id IS NULL OR d.name = pg_catalog.current_database()
1413GROUP BY mz_indexes.oid, mz_relations.oid",
1414        access: vec![PUBLIC_SELECT],
1415        ontology: None,
1416    }
1417});
1418
1419pub static PG_INDEXES: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1420    name: "pg_indexes",
1421    schema: PG_CATALOG_SCHEMA,
1422    oid: oid::VIEW_PG_INDEXES_OID,
1423    desc: RelationDesc::builder()
1424        .with_column("table_catalog", SqlScalarType::String.nullable(false))
1425        .with_column("schemaname", SqlScalarType::String.nullable(false))
1426        .with_column("tablename", SqlScalarType::String.nullable(false))
1427        .with_column("indexname", SqlScalarType::String.nullable(false))
1428        .with_column("tablespace", SqlScalarType::String.nullable(true))
1429        .with_column("indexdef", SqlScalarType::String.nullable(true))
1430        .finish(),
1431    column_comments: BTreeMap::new(),
1432    sql: "SELECT
1433    current_database() as table_catalog,
1434    s.name AS schemaname,
1435    r.name AS tablename,
1436    i.name AS indexname,
1437    NULL::text AS tablespace,
1438    -- TODO(jkosh44) Fill in with actual index definition.
1439    NULL::text AS indexdef
1440FROM mz_catalog.mz_indexes i
1441JOIN mz_catalog.mz_relations r ON i.on_id = r.id
1442JOIN mz_catalog.mz_schemas s ON s.id = r.schema_id
1443LEFT JOIN mz_catalog.mz_databases d ON d.id = s.database_id
1444WHERE s.database_id IS NULL OR d.name = current_database()",
1445    access: vec![PUBLIC_SELECT],
1446    ontology: None,
1447});
1448
1449/// Note: Databases, Roles, Clusters, Cluster Replicas, Secrets, and Connections are excluded from
1450/// this view for Postgres compatibility. Specifically, there is no classoid for these objects,
1451/// which is required for this view.
1452pub static PG_DESCRIPTION: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1453    name: "pg_description",
1454    schema: PG_CATALOG_SCHEMA,
1455    oid: oid::VIEW_PG_DESCRIPTION_OID,
1456    desc: RelationDesc::builder()
1457        .with_column("objoid", SqlScalarType::Oid.nullable(false))
1458        .with_column("classoid", SqlScalarType::Oid.nullable(true))
1459        .with_column("objsubid", SqlScalarType::Int32.nullable(false))
1460        .with_column("description", SqlScalarType::String.nullable(false))
1461        .finish(),
1462    column_comments: BTreeMap::new(),
1463    sql: "
1464SELECT
1465    objoid,
1466    classoid,
1467    objsubid,
1468    description
1469FROM
1470    mz_internal.pg_description_all_databases
1471WHERE
1472    (oid_database_name IS NULL OR oid_database_name = pg_catalog.current_database()) AND
1473    (class_database_name IS NULL OR class_database_name = pg_catalog.current_database());",
1474    access: vec![PUBLIC_SELECT],
1475    ontology: None,
1476});
1477
1478pub static PG_TYPE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1479    name: "pg_type",
1480    schema: PG_CATALOG_SCHEMA,
1481    oid: oid::VIEW_PG_TYPE_OID,
1482    desc: RelationDesc::builder()
1483        .with_column("oid", SqlScalarType::Oid.nullable(false))
1484        .with_column("typname", SqlScalarType::String.nullable(false))
1485        .with_column("typnamespace", SqlScalarType::Oid.nullable(false))
1486        .with_column("typowner", SqlScalarType::Oid.nullable(false))
1487        .with_column("typlen", SqlScalarType::Int16.nullable(true))
1488        .with_column("typtype", SqlScalarType::PgLegacyChar.nullable(false))
1489        .with_column("typcategory", SqlScalarType::PgLegacyChar.nullable(true))
1490        .with_column("typdelim", SqlScalarType::PgLegacyChar.nullable(false))
1491        .with_column("typrelid", SqlScalarType::Oid.nullable(false))
1492        .with_column("typelem", SqlScalarType::Oid.nullable(false))
1493        .with_column("typarray", SqlScalarType::Oid.nullable(false))
1494        .with_column("typinput", SqlScalarType::RegProc.nullable(true))
1495        .with_column("typreceive", SqlScalarType::Oid.nullable(false))
1496        .with_column("typnotnull", SqlScalarType::Bool.nullable(false))
1497        .with_column("typbasetype", SqlScalarType::Oid.nullable(false))
1498        .with_column("typtypmod", SqlScalarType::Int32.nullable(false))
1499        .with_column("typcollation", SqlScalarType::Oid.nullable(false))
1500        .with_column("typdefault", SqlScalarType::String.nullable(true))
1501        .finish(),
1502    column_comments: BTreeMap::new(),
1503    sql: "SELECT
1504    oid, typname, typnamespace, typowner, typlen, typtype, typcategory, typdelim, typrelid, typelem,
1505    typarray, typinput, typreceive, typnotnull, typbasetype, typtypmod, typcollation, typdefault
1506FROM mz_internal.pg_type_all_databases
1507WHERE database_name IS NULL OR database_name = pg_catalog.current_database();",
1508    access: vec![PUBLIC_SELECT],
1509    ontology: None,
1510});
1511
1512/// <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
1513pub static PG_ATTRIBUTE: LazyLock<BuiltinView> = LazyLock::new(|| {
1514    BuiltinView {
1515        name: "pg_attribute",
1516        schema: PG_CATALOG_SCHEMA,
1517        oid: oid::VIEW_PG_ATTRIBUTE_OID,
1518        desc: RelationDesc::builder()
1519            .with_column("attrelid", SqlScalarType::Oid.nullable(false))
1520            .with_column("attname", SqlScalarType::String.nullable(false))
1521            .with_column("atttypid", SqlScalarType::Oid.nullable(false))
1522            .with_column("attlen", SqlScalarType::Int16.nullable(true))
1523            .with_column("attnum", SqlScalarType::Int16.nullable(false))
1524            .with_column("atttypmod", SqlScalarType::Int32.nullable(false))
1525            .with_column("attndims", SqlScalarType::Int16.nullable(false))
1526            .with_column("attnotnull", SqlScalarType::Bool.nullable(false))
1527            .with_column("atthasdef", SqlScalarType::Bool.nullable(false))
1528            .with_column("attidentity", SqlScalarType::PgLegacyChar.nullable(false))
1529            .with_column("attgenerated", SqlScalarType::PgLegacyChar.nullable(false))
1530            .with_column("attisdropped", SqlScalarType::Bool.nullable(false))
1531            .with_column("attcollation", SqlScalarType::Oid.nullable(false))
1532            .finish(),
1533        column_comments: BTreeMap::new(),
1534        sql: "
1535SELECT
1536    attrelid, attname, atttypid, attlen, attnum, atttypmod, attndims, attnotnull, atthasdef,
1537    attidentity, attgenerated, attisdropped, attcollation
1538FROM mz_internal.pg_attribute_all_databases
1539WHERE
1540  (database_name IS NULL OR database_name = pg_catalog.current_database()) AND
1541  (pg_type_database_name IS NULL OR pg_type_database_name = pg_catalog.current_database());",
1542        // Since this depends on pg_type, its id must be higher due to initialization
1543        // ordering.
1544        access: vec![PUBLIC_SELECT],
1545        ontology: None,
1546    }
1547});
1548
1549pub static PG_PROC: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1550    name: "pg_proc",
1551    schema: PG_CATALOG_SCHEMA,
1552    oid: oid::VIEW_PG_PROC_OID,
1553    desc: RelationDesc::builder()
1554        .with_column("oid", SqlScalarType::Oid.nullable(false))
1555        .with_column("proname", SqlScalarType::String.nullable(false))
1556        .with_column("pronamespace", SqlScalarType::Oid.nullable(false))
1557        .with_column("proowner", SqlScalarType::Oid.nullable(false))
1558        .with_column("proargdefaults", SqlScalarType::String.nullable(true))
1559        .with_column("prorettype", SqlScalarType::Oid.nullable(false))
1560        .finish(),
1561    column_comments: BTreeMap::new(),
1562    sql: "SELECT
1563    mz_functions.oid,
1564    mz_functions.name AS proname,
1565    mz_schemas.oid AS pronamespace,
1566    role_owner.oid AS proowner,
1567    NULL::pg_catalog.text AS proargdefaults,
1568    ret_type.oid AS prorettype
1569FROM mz_catalog.mz_functions
1570JOIN mz_catalog.mz_schemas ON mz_functions.schema_id = mz_schemas.id
1571LEFT JOIN mz_catalog.mz_databases d ON d.id = mz_schemas.database_id
1572JOIN mz_catalog.mz_types AS ret_type ON mz_functions.return_type_id = ret_type.id
1573JOIN mz_catalog.mz_roles role_owner ON role_owner.id = mz_functions.owner_id
1574WHERE mz_schemas.database_id IS NULL OR d.name = pg_catalog.current_database()",
1575    access: vec![PUBLIC_SELECT],
1576    ontology: None,
1577});
1578
1579pub static PG_OPERATOR: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1580    name: "pg_operator",
1581    schema: PG_CATALOG_SCHEMA,
1582    oid: oid::VIEW_PG_OPERATOR_OID,
1583    desc: RelationDesc::builder()
1584        .with_column("oid", SqlScalarType::Oid.nullable(false))
1585        .with_column("oprname", SqlScalarType::String.nullable(false))
1586        .with_column("oprresult", SqlScalarType::Oid.nullable(false))
1587        .with_column("oprleft", SqlScalarType::Oid.nullable(false))
1588        .with_column("oprright", SqlScalarType::Oid.nullable(false))
1589        .with_key(vec![0, 1, 2, 3, 4])
1590        .finish(),
1591    column_comments: BTreeMap::new(),
1592    sql: "SELECT
1593    mz_operators.oid,
1594    mz_operators.name AS oprname,
1595    ret_type.oid AS oprresult,
1596    left_type.oid as oprleft,
1597    right_type.oid as oprright
1598FROM mz_catalog.mz_operators
1599JOIN mz_catalog.mz_types AS ret_type ON mz_operators.return_type_id = ret_type.id
1600JOIN mz_catalog.mz_types AS left_type ON mz_operators.argument_type_ids[1] = left_type.id
1601JOIN mz_catalog.mz_types AS right_type ON mz_operators.argument_type_ids[2] = right_type.id
1602WHERE array_length(mz_operators.argument_type_ids, 1) = 2
1603UNION SELECT
1604    mz_operators.oid,
1605    mz_operators.name AS oprname,
1606    ret_type.oid AS oprresult,
1607    0 as oprleft,
1608    right_type.oid as oprright
1609FROM mz_catalog.mz_operators
1610JOIN mz_catalog.mz_types AS ret_type ON mz_operators.return_type_id = ret_type.id
1611JOIN mz_catalog.mz_types AS right_type ON mz_operators.argument_type_ids[1] = right_type.id
1612WHERE array_length(mz_operators.argument_type_ids, 1) = 1",
1613    access: vec![PUBLIC_SELECT],
1614    ontology: None,
1615});
1616
1617pub static PG_RANGE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1618    name: "pg_range",
1619    schema: PG_CATALOG_SCHEMA,
1620    oid: oid::VIEW_PG_RANGE_OID,
1621    desc: RelationDesc::builder()
1622        .with_column("rngtypid", SqlScalarType::Oid.nullable(false))
1623        .with_column("rngsubtype", SqlScalarType::Oid.nullable(false))
1624        .with_key(vec![])
1625        .finish(),
1626    column_comments: BTreeMap::new(),
1627    sql: "SELECT
1628    NULL::pg_catalog.oid AS rngtypid,
1629    NULL::pg_catalog.oid AS rngsubtype
1630WHERE false",
1631    access: vec![PUBLIC_SELECT],
1632    ontology: None,
1633});
1634
1635pub static PG_ENUM: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1636    name: "pg_enum",
1637    schema: PG_CATALOG_SCHEMA,
1638    oid: oid::VIEW_PG_ENUM_OID,
1639    desc: RelationDesc::builder()
1640        .with_column("oid", SqlScalarType::Oid.nullable(false))
1641        .with_column("enumtypid", SqlScalarType::Oid.nullable(false))
1642        .with_column("enumsortorder", SqlScalarType::Float32.nullable(false))
1643        .with_column("enumlabel", SqlScalarType::String.nullable(false))
1644        .with_key(vec![])
1645        .finish(),
1646    column_comments: BTreeMap::new(),
1647    sql: "SELECT
1648    NULL::pg_catalog.oid AS oid,
1649    NULL::pg_catalog.oid AS enumtypid,
1650    NULL::pg_catalog.float4 AS enumsortorder,
1651    NULL::pg_catalog.text AS enumlabel
1652WHERE false",
1653    access: vec![PUBLIC_SELECT],
1654    ontology: None,
1655});
1656
1657pub static PG_ATTRDEF: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1658    name: "pg_attrdef",
1659    schema: PG_CATALOG_SCHEMA,
1660    oid: oid::VIEW_PG_ATTRDEF_OID,
1661    desc: RelationDesc::builder()
1662        .with_column("oid", SqlScalarType::Oid.nullable(true))
1663        .with_column("adrelid", SqlScalarType::Oid.nullable(false))
1664        .with_column("adnum", SqlScalarType::Int64.nullable(false))
1665        .with_column("adbin", SqlScalarType::String.nullable(false))
1666        .with_column("adsrc", SqlScalarType::String.nullable(false))
1667        .finish(),
1668    column_comments: BTreeMap::new(),
1669    sql: "
1670SELECT
1671    pg_attrdef_all_databases.oid as oid,
1672    adrelid,
1673    adnum,
1674    adbin,
1675    adsrc
1676FROM mz_internal.pg_attrdef_all_databases
1677    JOIN mz_catalog.mz_databases d ON (d.id IS NULL OR d.name = pg_catalog.current_database());",
1678    access: vec![PUBLIC_SELECT],
1679    ontology: None,
1680});
1681
1682pub static PG_SETTINGS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1683    name: "pg_settings",
1684    schema: PG_CATALOG_SCHEMA,
1685    oid: oid::VIEW_PG_SETTINGS_OID,
1686    desc: RelationDesc::builder()
1687        .with_column("name", SqlScalarType::String.nullable(false))
1688        .with_column("setting", SqlScalarType::String.nullable(false))
1689        .with_key(vec![])
1690        .finish(),
1691    column_comments: BTreeMap::new(),
1692    sql: "SELECT
1693    name, setting
1694FROM (VALUES
1695    ('max_index_keys'::pg_catalog.text, '1000'::pg_catalog.text)
1696) AS _ (name, setting)",
1697    access: vec![PUBLIC_SELECT],
1698    ontology: None,
1699});
1700
1701pub static PG_AUTH_MEMBERS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1702    name: "pg_auth_members",
1703    schema: PG_CATALOG_SCHEMA,
1704    oid: oid::VIEW_PG_AUTH_MEMBERS_OID,
1705    desc: RelationDesc::builder()
1706        .with_column("roleid", SqlScalarType::Oid.nullable(false))
1707        .with_column("member", SqlScalarType::Oid.nullable(false))
1708        .with_column("grantor", SqlScalarType::Oid.nullable(false))
1709        .with_column("admin_option", SqlScalarType::Bool.nullable(false))
1710        .finish(),
1711    column_comments: BTreeMap::new(),
1712    sql: "SELECT
1713    role.oid AS roleid,
1714    member.oid AS member,
1715    grantor.oid AS grantor,
1716    -- Materialize hasn't implemented admin_option.
1717    false as admin_option
1718FROM mz_catalog.mz_role_members membership
1719JOIN mz_catalog.mz_roles role ON membership.role_id = role.id
1720JOIN mz_catalog.mz_roles member ON membership.member = member.id
1721JOIN mz_catalog.mz_roles grantor ON membership.grantor = grantor.id",
1722    access: vec![PUBLIC_SELECT],
1723    ontology: None,
1724});
1725
1726pub static PG_EVENT_TRIGGER: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1727    name: "pg_event_trigger",
1728    schema: PG_CATALOG_SCHEMA,
1729    oid: oid::VIEW_PG_EVENT_TRIGGER_OID,
1730    desc: RelationDesc::builder()
1731        .with_column("oid", SqlScalarType::Oid.nullable(false))
1732        .with_column("evtname", SqlScalarType::String.nullable(false))
1733        .with_column("evtevent", SqlScalarType::String.nullable(false))
1734        .with_column("evtowner", SqlScalarType::Oid.nullable(false))
1735        .with_column("evtfoid", SqlScalarType::Oid.nullable(false))
1736        .with_column("evtenabled", SqlScalarType::PgLegacyChar.nullable(false))
1737        .with_column(
1738            "evttags",
1739            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(false),
1740        )
1741        .with_key(vec![])
1742        .finish(),
1743    column_comments: BTreeMap::new(),
1744    sql: "SELECT
1745        NULL::pg_catalog.oid AS oid,
1746        NULL::pg_catalog.text AS evtname,
1747        NULL::pg_catalog.text AS evtevent,
1748        NULL::pg_catalog.oid AS evtowner,
1749        NULL::pg_catalog.oid AS evtfoid,
1750        NULL::pg_catalog.char AS evtenabled,
1751        NULL::pg_catalog.text[] AS evttags
1752    WHERE false",
1753    access: vec![PUBLIC_SELECT],
1754    ontology: None,
1755});
1756
1757pub static PG_LANGUAGE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1758    name: "pg_language",
1759    schema: PG_CATALOG_SCHEMA,
1760    oid: oid::VIEW_PG_LANGUAGE_OID,
1761    desc: RelationDesc::builder()
1762        .with_column("oid", SqlScalarType::Oid.nullable(false))
1763        .with_column("lanname", SqlScalarType::String.nullable(false))
1764        .with_column("lanowner", SqlScalarType::Oid.nullable(false))
1765        .with_column("lanispl", SqlScalarType::Bool.nullable(false))
1766        .with_column("lanpltrusted", SqlScalarType::Bool.nullable(false))
1767        .with_column("lanplcallfoid", SqlScalarType::Oid.nullable(false))
1768        .with_column("laninline", SqlScalarType::Oid.nullable(false))
1769        .with_column("lanvalidator", SqlScalarType::Oid.nullable(false))
1770        .with_column(
1771            "lanacl",
1772            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(false),
1773        )
1774        .with_key(vec![])
1775        .finish(),
1776    column_comments: BTreeMap::new(),
1777    sql: "SELECT
1778        NULL::pg_catalog.oid  AS oid,
1779        NULL::pg_catalog.text AS lanname,
1780        NULL::pg_catalog.oid  AS lanowner,
1781        NULL::pg_catalog.bool AS lanispl,
1782        NULL::pg_catalog.bool AS lanpltrusted,
1783        NULL::pg_catalog.oid  AS lanplcallfoid,
1784        NULL::pg_catalog.oid  AS laninline,
1785        NULL::pg_catalog.oid  AS lanvalidator,
1786        NULL::pg_catalog.text[] AS lanacl
1787    WHERE false",
1788    access: vec![PUBLIC_SELECT],
1789    ontology: None,
1790});
1791
1792pub static PG_SHDESCRIPTION: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1793    name: "pg_shdescription",
1794    schema: PG_CATALOG_SCHEMA,
1795    oid: oid::VIEW_PG_SHDESCRIPTION_OID,
1796    desc: RelationDesc::builder()
1797        .with_column("objoid", SqlScalarType::Oid.nullable(false))
1798        .with_column("classoid", SqlScalarType::Oid.nullable(false))
1799        .with_column("description", SqlScalarType::String.nullable(false))
1800        .with_key(vec![])
1801        .finish(),
1802    column_comments: BTreeMap::new(),
1803    sql: "SELECT
1804        NULL::pg_catalog.oid AS objoid,
1805        NULL::pg_catalog.oid AS classoid,
1806        NULL::pg_catalog.text AS description
1807    WHERE false",
1808    access: vec![PUBLIC_SELECT],
1809    ontology: None,
1810});
1811
1812pub static PG_TIMEZONE_ABBREVS: LazyLock<BuiltinView> = LazyLock::new(|| {
1813    BuiltinView {
1814        name: "pg_timezone_abbrevs",
1815        schema: PG_CATALOG_SCHEMA,
1816        oid: oid::VIEW_PG_TIMEZONE_ABBREVS_OID,
1817        desc: RelationDesc::builder()
1818            .with_column("abbrev", SqlScalarType::String.nullable(false))
1819            .with_column("utc_offset", SqlScalarType::Interval.nullable(true))
1820            .with_column("is_dst", SqlScalarType::Bool.nullable(true))
1821            .with_key(vec![0])
1822            .finish(),
1823        column_comments: BTreeMap::new(),
1824        sql: "SELECT
1825    abbreviation AS abbrev,
1826    COALESCE(utc_offset, timezone_offset(timezone_name, now()).base_utc_offset + timezone_offset(timezone_name, now()).dst_offset)
1827        AS utc_offset,
1828    COALESCE(dst, timezone_offset(timezone_name, now()).dst_offset <> INTERVAL '0')
1829        AS is_dst
1830FROM mz_catalog.mz_timezone_abbreviations",
1831        access: vec![PUBLIC_SELECT],
1832        ontology: None,
1833    }
1834});
1835
1836pub static PG_TIMEZONE_NAMES: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1837    name: "pg_timezone_names",
1838    schema: PG_CATALOG_SCHEMA,
1839    oid: oid::VIEW_PG_TIMEZONE_NAMES_OID,
1840    desc: RelationDesc::builder()
1841        .with_column("name", SqlScalarType::String.nullable(false))
1842        .with_column("abbrev", SqlScalarType::String.nullable(true))
1843        .with_column("utc_offset", SqlScalarType::Interval.nullable(true))
1844        .with_column("is_dst", SqlScalarType::Bool.nullable(true))
1845        .with_key(vec![0])
1846        .finish(),
1847    column_comments: BTreeMap::new(),
1848    sql: "SELECT
1849    name,
1850    timezone_offset(name, now()).abbrev AS abbrev,
1851    timezone_offset(name, now()).base_utc_offset + timezone_offset(name, now()).dst_offset
1852        AS utc_offset,
1853    timezone_offset(name, now()).dst_offset <> INTERVAL '0'
1854        AS is_dst
1855FROM mz_catalog.mz_timezone_names",
1856    access: vec![PUBLIC_SELECT],
1857    ontology: None,
1858});
1859
1860// NOTE: If you add real data to this implementation, then please update
1861// the related `pg_` function implementations (like `pg_get_constraintdef`)
1862pub static PG_CONSTRAINT: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1863    name: "pg_constraint",
1864    schema: PG_CATALOG_SCHEMA,
1865    oid: oid::VIEW_PG_CONSTRAINT_OID,
1866    desc: RelationDesc::builder()
1867        .with_column("oid", SqlScalarType::Oid.nullable(false))
1868        .with_column("conname", SqlScalarType::String.nullable(false))
1869        .with_column("connamespace", SqlScalarType::Oid.nullable(false))
1870        .with_column("contype", SqlScalarType::PgLegacyChar.nullable(false))
1871        .with_column("condeferrable", SqlScalarType::Bool.nullable(false))
1872        .with_column("condeferred", SqlScalarType::Bool.nullable(false))
1873        .with_column("convalidated", SqlScalarType::Bool.nullable(false))
1874        .with_column("conrelid", SqlScalarType::Oid.nullable(false))
1875        .with_column("contypid", SqlScalarType::Oid.nullable(false))
1876        .with_column("conindid", SqlScalarType::Oid.nullable(false))
1877        .with_column("conparentid", SqlScalarType::Oid.nullable(false))
1878        .with_column("confrelid", SqlScalarType::Oid.nullable(false))
1879        .with_column("confupdtype", SqlScalarType::PgLegacyChar.nullable(false))
1880        .with_column("confdeltype", SqlScalarType::PgLegacyChar.nullable(false))
1881        .with_column("confmatchtype", SqlScalarType::PgLegacyChar.nullable(false))
1882        .with_column("conislocal", SqlScalarType::Bool.nullable(false))
1883        .with_column("coninhcount", SqlScalarType::Int32.nullable(false))
1884        .with_column("connoinherit", SqlScalarType::Bool.nullable(false))
1885        .with_column(
1886            "conkey",
1887            SqlScalarType::Array(Box::new(SqlScalarType::Int16)).nullable(false),
1888        )
1889        .with_column(
1890            "confkey",
1891            SqlScalarType::Array(Box::new(SqlScalarType::Int16)).nullable(false),
1892        )
1893        .with_column(
1894            "conpfeqop",
1895            SqlScalarType::Array(Box::new(SqlScalarType::Oid)).nullable(false),
1896        )
1897        .with_column(
1898            "conppeqop",
1899            SqlScalarType::Array(Box::new(SqlScalarType::Oid)).nullable(false),
1900        )
1901        .with_column(
1902            "conffeqop",
1903            SqlScalarType::Array(Box::new(SqlScalarType::Oid)).nullable(false),
1904        )
1905        .with_column(
1906            "conexclop",
1907            SqlScalarType::Array(Box::new(SqlScalarType::Oid)).nullable(false),
1908        )
1909        .with_column("conbin", SqlScalarType::String.nullable(false))
1910        .with_key(vec![])
1911        .finish(),
1912    column_comments: BTreeMap::new(),
1913    sql: "SELECT
1914    NULL::pg_catalog.oid as oid,
1915    NULL::pg_catalog.text as conname,
1916    NULL::pg_catalog.oid as connamespace,
1917    NULL::pg_catalog.\"char\" as contype,
1918    NULL::pg_catalog.bool as condeferrable,
1919    NULL::pg_catalog.bool as condeferred,
1920    NULL::pg_catalog.bool as convalidated,
1921    NULL::pg_catalog.oid as conrelid,
1922    NULL::pg_catalog.oid as contypid,
1923    NULL::pg_catalog.oid as conindid,
1924    NULL::pg_catalog.oid as conparentid,
1925    NULL::pg_catalog.oid as confrelid,
1926    NULL::pg_catalog.\"char\" as confupdtype,
1927    NULL::pg_catalog.\"char\" as confdeltype,
1928    NULL::pg_catalog.\"char\" as confmatchtype,
1929    NULL::pg_catalog.bool as conislocal,
1930    NULL::pg_catalog.int4 as coninhcount,
1931    NULL::pg_catalog.bool as connoinherit,
1932    NULL::pg_catalog.int2[] as conkey,
1933    NULL::pg_catalog.int2[] as confkey,
1934    NULL::pg_catalog.oid[] as conpfeqop,
1935    NULL::pg_catalog.oid[] as conppeqop,
1936    NULL::pg_catalog.oid[] as conffeqop,
1937    NULL::pg_catalog.oid[] as conexclop,
1938    NULL::pg_catalog.text as conbin
1939WHERE false",
1940    access: vec![PUBLIC_SELECT],
1941    ontology: None,
1942});
1943
1944pub static PG_TABLES: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1945    name: "pg_tables",
1946    schema: PG_CATALOG_SCHEMA,
1947    oid: oid::VIEW_PG_TABLES_OID,
1948    desc: RelationDesc::builder()
1949        .with_column("schemaname", SqlScalarType::String.nullable(true))
1950        .with_column("tablename", SqlScalarType::String.nullable(false))
1951        .with_column("tableowner", SqlScalarType::String.nullable(false))
1952        .finish(),
1953    column_comments: BTreeMap::new(),
1954    sql: "
1955SELECT n.nspname AS schemaname,
1956    c.relname AS tablename,
1957    pg_catalog.pg_get_userbyid(c.relowner) AS tableowner
1958FROM pg_catalog.pg_class c
1959LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
1960WHERE c.relkind IN ('r', 'p')",
1961    access: vec![PUBLIC_SELECT],
1962    ontology: None,
1963});
1964
1965pub static PG_TABLESPACE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1966    name: "pg_tablespace",
1967    schema: PG_CATALOG_SCHEMA,
1968    oid: oid::VIEW_PG_TABLESPACE_OID,
1969    desc: RelationDesc::builder()
1970        .with_column("oid", SqlScalarType::Oid.nullable(false))
1971        .with_column("spcname", SqlScalarType::String.nullable(false))
1972        .with_column("spcowner", SqlScalarType::Oid.nullable(true))
1973        .with_column(
1974            "spcacl",
1975            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(true),
1976        )
1977        .with_column(
1978            "spcoptions",
1979            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(true),
1980        )
1981        .with_key(vec![])
1982        .finish(),
1983    column_comments: BTreeMap::new(),
1984    sql: "
1985    SELECT oid, spcname, spcowner, spcacl, spcoptions
1986    FROM (
1987        VALUES (
1988            --These are the same defaults CockroachDB uses.
1989            0::pg_catalog.oid,
1990            'pg_default'::pg_catalog.text,
1991            NULL::pg_catalog.oid,
1992            NULL::pg_catalog.text[],
1993            NULL::pg_catalog.text[]
1994        )
1995    ) AS _ (oid, spcname, spcowner, spcacl, spcoptions)
1996",
1997    access: vec![PUBLIC_SELECT],
1998    ontology: None,
1999});
2000
2001pub static PG_ACCESS_METHODS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2002    name: "pg_am",
2003    schema: PG_CATALOG_SCHEMA,
2004    oid: oid::VIEW_PG_AM_OID,
2005    desc: RelationDesc::builder()
2006        .with_column("oid", SqlScalarType::Oid.nullable(false))
2007        .with_column("amname", SqlScalarType::String.nullable(false))
2008        .with_column("amhandler", SqlScalarType::RegProc.nullable(false))
2009        .with_column("amtype", SqlScalarType::PgLegacyChar.nullable(false))
2010        .with_key(vec![])
2011        .finish(),
2012    column_comments: BTreeMap::new(),
2013    sql: "
2014SELECT NULL::pg_catalog.oid AS oid,
2015    NULL::pg_catalog.text AS amname,
2016    NULL::pg_catalog.regproc AS amhandler,
2017    NULL::pg_catalog.\"char\" AS amtype
2018WHERE false",
2019    access: vec![PUBLIC_SELECT],
2020    ontology: None,
2021});
2022
2023pub static PG_ROLES: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2024    name: "pg_roles",
2025    schema: PG_CATALOG_SCHEMA,
2026    oid: oid::VIEW_PG_ROLES_OID,
2027    desc: RelationDesc::builder()
2028        .with_column("rolname", SqlScalarType::String.nullable(false))
2029        .with_column("rolsuper", SqlScalarType::Bool.nullable(true))
2030        .with_column("rolinherit", SqlScalarType::Bool.nullable(false))
2031        .with_column("rolcreaterole", SqlScalarType::Bool.nullable(true))
2032        .with_column("rolcreatedb", SqlScalarType::Bool.nullable(true))
2033        .with_column("rolcanlogin", SqlScalarType::Bool.nullable(false))
2034        .with_column("rolreplication", SqlScalarType::Bool.nullable(false))
2035        .with_column("rolconnlimit", SqlScalarType::Int32.nullable(false))
2036        .with_column("rolpassword", SqlScalarType::String.nullable(false))
2037        .with_column(
2038            "rolvaliduntil",
2039            SqlScalarType::TimestampTz { precision: None }.nullable(true),
2040        )
2041        .with_column("rolbypassrls", SqlScalarType::Bool.nullable(false))
2042        .with_column(
2043            "rolconfig",
2044            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(true),
2045        )
2046        .with_column("oid", SqlScalarType::Oid.nullable(false))
2047        .finish(),
2048    column_comments: BTreeMap::new(),
2049    sql: "SELECT
2050    rolname,
2051    rolsuper,
2052    rolinherit,
2053    rolcreaterole,
2054    rolcreatedb,
2055    COALESCE(rolcanlogin, false) AS rolcanlogin,
2056    rolreplication,
2057    rolconnlimit,
2058    '********' as rolpassword,
2059    rolvaliduntil,
2060    rolbypassrls,
2061    (
2062        SELECT array_agg(parameter_name || '=' || parameter_value)
2063        FROM mz_catalog.mz_role_parameters rp
2064        JOIN mz_catalog.mz_roles r ON r.id = rp.role_id
2065        WHERE ai.oid = r.oid
2066    ) AS rolconfig,
2067    oid
2068FROM pg_catalog.pg_authid ai",
2069    access: vec![PUBLIC_SELECT],
2070    ontology: None,
2071});
2072
2073pub static PG_USER: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2074    name: "pg_user",
2075    schema: PG_CATALOG_SCHEMA,
2076    oid: oid::VIEW_PG_USER_OID,
2077    desc: RelationDesc::builder()
2078        .with_column("usename", SqlScalarType::String.nullable(false))
2079        .with_column("usesysid", SqlScalarType::Oid.nullable(false))
2080        .with_column("usecreatedb", SqlScalarType::Bool.nullable(true))
2081        .with_column("usesuper", SqlScalarType::Bool.nullable(true))
2082        .with_column("userepl", SqlScalarType::Bool.nullable(false))
2083        .with_column("usebypassrls", SqlScalarType::Bool.nullable(false))
2084        .with_column("passwd", SqlScalarType::String.nullable(true))
2085        .with_column(
2086            "valuntil",
2087            SqlScalarType::TimestampTz { precision: None }.nullable(true),
2088        )
2089        .with_column(
2090            "useconfig",
2091            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(true),
2092        )
2093        .finish(),
2094    column_comments: BTreeMap::new(),
2095    sql: "
2096SELECT
2097    rolname as usename,
2098    ai.oid as usesysid,
2099    rolcreatedb AS usecreatedb,
2100    rolsuper AS usesuper,
2101    rolreplication AS userepl,
2102    rolbypassrls AS usebypassrls,
2103    rolpassword as passwd,
2104    rolvaliduntil as valuntil,
2105    (
2106        SELECT array_agg(parameter_name || '=' || parameter_value)
2107        FROM mz_catalog.mz_role_parameters rp
2108        JOIN mz_catalog.mz_roles r ON r.id = rp.role_id
2109        WHERE ai.oid = r.oid
2110    ) AS useconfig
2111FROM pg_catalog.pg_authid ai
2112WHERE rolcanlogin",
2113    access: vec![PUBLIC_SELECT],
2114    ontology: None,
2115});
2116
2117pub static PG_VIEWS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2118    name: "pg_views",
2119    schema: PG_CATALOG_SCHEMA,
2120    oid: oid::VIEW_PG_VIEWS_OID,
2121    desc: RelationDesc::builder()
2122        .with_column("schemaname", SqlScalarType::String.nullable(true))
2123        .with_column("viewname", SqlScalarType::String.nullable(false))
2124        .with_column("viewowner", SqlScalarType::Oid.nullable(false))
2125        .with_column("definition", SqlScalarType::String.nullable(false))
2126        .finish(),
2127    column_comments: BTreeMap::new(),
2128    sql: "SELECT
2129    s.name AS schemaname,
2130    v.name AS viewname,
2131    role_owner.oid AS viewowner,
2132    v.definition AS definition
2133FROM mz_catalog.mz_views v
2134LEFT JOIN mz_catalog.mz_schemas s ON s.id = v.schema_id
2135LEFT JOIN mz_catalog.mz_databases d ON d.id = s.database_id
2136JOIN mz_catalog.mz_roles role_owner ON role_owner.id = v.owner_id
2137WHERE s.database_id IS NULL OR d.name = current_database()",
2138    access: vec![PUBLIC_SELECT],
2139    ontology: None,
2140});
2141
2142pub static PG_MATVIEWS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2143    name: "pg_matviews",
2144    schema: PG_CATALOG_SCHEMA,
2145    oid: oid::VIEW_PG_MATVIEWS_OID,
2146    desc: RelationDesc::builder()
2147        .with_column("schemaname", SqlScalarType::String.nullable(true))
2148        .with_column("matviewname", SqlScalarType::String.nullable(false))
2149        .with_column("matviewowner", SqlScalarType::Oid.nullable(false))
2150        .with_column("definition", SqlScalarType::String.nullable(false))
2151        .finish(),
2152    column_comments: BTreeMap::new(),
2153    sql: "SELECT
2154    s.name AS schemaname,
2155    m.name AS matviewname,
2156    role_owner.oid AS matviewowner,
2157    m.definition AS definition
2158FROM mz_catalog.mz_materialized_views m
2159LEFT JOIN mz_catalog.mz_schemas s ON s.id = m.schema_id
2160LEFT JOIN mz_catalog.mz_databases d ON d.id = s.database_id
2161JOIN mz_catalog.mz_roles role_owner ON role_owner.id = m.owner_id
2162WHERE s.database_id IS NULL OR d.name = current_database()",
2163    access: vec![PUBLIC_SELECT],
2164    ontology: None,
2165});
2166
2167// MZ doesn't support COLLATE so the table is filled with NULLs and made empty. pg_database hard
2168// codes a collation of 'C' for every database, so we could copy that here.
2169pub static PG_COLLATION: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2170    name: "pg_collation",
2171    schema: PG_CATALOG_SCHEMA,
2172    oid: oid::VIEW_PG_COLLATION_OID,
2173    desc: RelationDesc::builder()
2174        .with_column("oid", SqlScalarType::Oid.nullable(false))
2175        .with_column("collname", SqlScalarType::String.nullable(false))
2176        .with_column("collnamespace", SqlScalarType::Oid.nullable(false))
2177        .with_column("collowner", SqlScalarType::Oid.nullable(false))
2178        .with_column("collprovider", SqlScalarType::PgLegacyChar.nullable(false))
2179        .with_column("collisdeterministic", SqlScalarType::Bool.nullable(false))
2180        .with_column("collencoding", SqlScalarType::Int32.nullable(false))
2181        .with_column("collcollate", SqlScalarType::String.nullable(false))
2182        .with_column("collctype", SqlScalarType::String.nullable(false))
2183        .with_column("collversion", SqlScalarType::String.nullable(false))
2184        .with_key(vec![])
2185        .finish(),
2186    column_comments: BTreeMap::new(),
2187    sql: "
2188SELECT
2189    NULL::pg_catalog.oid AS oid,
2190    NULL::pg_catalog.text AS collname,
2191    NULL::pg_catalog.oid AS collnamespace,
2192    NULL::pg_catalog.oid AS collowner,
2193    NULL::pg_catalog.\"char\" AS collprovider,
2194    NULL::pg_catalog.bool AS collisdeterministic,
2195    NULL::pg_catalog.int4 AS collencoding,
2196    NULL::pg_catalog.text AS collcollate,
2197    NULL::pg_catalog.text AS collctype,
2198    NULL::pg_catalog.text AS collversion
2199WHERE false",
2200    access: vec![PUBLIC_SELECT],
2201    ontology: None,
2202});
2203
2204// MZ doesn't support row level security policies so the table is filled in with NULLs and made empty.
2205pub static PG_POLICY: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2206    name: "pg_policy",
2207    schema: PG_CATALOG_SCHEMA,
2208    oid: oid::VIEW_PG_POLICY_OID,
2209    desc: RelationDesc::builder()
2210        .with_column("oid", SqlScalarType::Oid.nullable(false))
2211        .with_column("polname", SqlScalarType::String.nullable(false))
2212        .with_column("polrelid", SqlScalarType::Oid.nullable(false))
2213        .with_column("polcmd", SqlScalarType::PgLegacyChar.nullable(false))
2214        .with_column("polpermissive", SqlScalarType::Bool.nullable(false))
2215        .with_column(
2216            "polroles",
2217            SqlScalarType::Array(Box::new(SqlScalarType::Oid)).nullable(false),
2218        )
2219        .with_column("polqual", SqlScalarType::String.nullable(false))
2220        .with_column("polwithcheck", SqlScalarType::String.nullable(false))
2221        .with_key(vec![])
2222        .finish(),
2223    column_comments: BTreeMap::new(),
2224    sql: "
2225SELECT
2226    NULL::pg_catalog.oid AS oid,
2227    NULL::pg_catalog.text AS polname,
2228    NULL::pg_catalog.oid AS polrelid,
2229    NULL::pg_catalog.\"char\" AS polcmd,
2230    NULL::pg_catalog.bool AS polpermissive,
2231    NULL::pg_catalog.oid[] AS polroles,
2232    NULL::pg_catalog.text AS polqual,
2233    NULL::pg_catalog.text AS polwithcheck
2234WHERE false",
2235    access: vec![PUBLIC_SELECT],
2236    ontology: None,
2237});
2238
2239// MZ doesn't support table inheritance so the table is filled in with NULLs and made empty.
2240pub static PG_INHERITS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2241    name: "pg_inherits",
2242    schema: PG_CATALOG_SCHEMA,
2243    oid: oid::VIEW_PG_INHERITS_OID,
2244    desc: RelationDesc::builder()
2245        .with_column("inhrelid", SqlScalarType::Oid.nullable(false))
2246        .with_column("inhparent", SqlScalarType::Oid.nullable(false))
2247        .with_column("inhseqno", SqlScalarType::Int32.nullable(false))
2248        .with_column("inhdetachpending", SqlScalarType::Bool.nullable(false))
2249        .with_key(vec![])
2250        .finish(),
2251    column_comments: BTreeMap::new(),
2252    sql: "
2253SELECT
2254    NULL::pg_catalog.oid AS inhrelid,
2255    NULL::pg_catalog.oid AS inhparent,
2256    NULL::pg_catalog.int4 AS inhseqno,
2257    NULL::pg_catalog.bool AS inhdetachpending
2258WHERE false",
2259    access: vec![PUBLIC_SELECT],
2260    ontology: None,
2261});
2262
2263pub static PG_LOCKS: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2264    name: "pg_locks",
2265    schema: PG_CATALOG_SCHEMA,
2266    oid: oid::VIEW_PG_LOCKS_OID,
2267    desc: RelationDesc::builder()
2268        .with_column("locktype", SqlScalarType::String.nullable(false))
2269        .with_column("database", SqlScalarType::Oid.nullable(false))
2270        .with_column("relation", SqlScalarType::Oid.nullable(false))
2271        .with_column("page", SqlScalarType::Int32.nullable(false))
2272        .with_column("tuple", SqlScalarType::Int16.nullable(false))
2273        .with_column("virtualxid", SqlScalarType::String.nullable(false))
2274        .with_column("transactionid", SqlScalarType::String.nullable(false))
2275        .with_column("classid", SqlScalarType::Oid.nullable(false))
2276        .with_column("objid", SqlScalarType::Oid.nullable(false))
2277        .with_column("objsubid", SqlScalarType::Int16.nullable(false))
2278        .with_column("virtualtransaction", SqlScalarType::String.nullable(false))
2279        .with_column("pid", SqlScalarType::Int32.nullable(false))
2280        .with_column("mode", SqlScalarType::String.nullable(false))
2281        .with_column("granted", SqlScalarType::Bool.nullable(false))
2282        .with_column("fastpath", SqlScalarType::Bool.nullable(false))
2283        .with_column(
2284            "waitstart",
2285            SqlScalarType::TimestampTz { precision: None }.nullable(false),
2286        )
2287        .with_key(vec![])
2288        .finish(),
2289    column_comments: BTreeMap::new(),
2290    sql: "
2291SELECT
2292-- While there exist locks in Materialize, we don't expose them, so all of these fields are NULL.
2293    NULL::pg_catalog.text AS locktype,
2294    NULL::pg_catalog.oid AS database,
2295    NULL::pg_catalog.oid AS relation,
2296    NULL::pg_catalog.int4 AS page,
2297    NULL::pg_catalog.int2 AS tuple,
2298    NULL::pg_catalog.text AS virtualxid,
2299    NULL::pg_catalog.text AS transactionid,
2300    NULL::pg_catalog.oid AS classid,
2301    NULL::pg_catalog.oid AS objid,
2302    NULL::pg_catalog.int2 AS objsubid,
2303    NULL::pg_catalog.text AS virtualtransaction,
2304    NULL::pg_catalog.int4 AS pid,
2305    NULL::pg_catalog.text AS mode,
2306    NULL::pg_catalog.bool AS granted,
2307    NULL::pg_catalog.bool AS fastpath,
2308    NULL::pg_catalog.timestamptz AS waitstart
2309WHERE false",
2310    access: vec![PUBLIC_SELECT],
2311    ontology: None,
2312});
2313
2314pub static PG_AUTHID: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2315    name: "pg_authid",
2316    schema: PG_CATALOG_SCHEMA,
2317    oid: oid::VIEW_PG_AUTHID_OID,
2318    desc: RelationDesc::builder()
2319        .with_column("oid", SqlScalarType::Oid.nullable(false))
2320        .with_column("rolname", SqlScalarType::String.nullable(false))
2321        .with_column("rolsuper", SqlScalarType::Bool.nullable(true))
2322        .with_column("rolinherit", SqlScalarType::Bool.nullable(false))
2323        .with_column("rolcreaterole", SqlScalarType::Bool.nullable(true))
2324        .with_column("rolcreatedb", SqlScalarType::Bool.nullable(true))
2325        .with_column("rolcanlogin", SqlScalarType::Bool.nullable(false))
2326        .with_column("rolreplication", SqlScalarType::Bool.nullable(false))
2327        .with_column("rolbypassrls", SqlScalarType::Bool.nullable(false))
2328        .with_column("rolconnlimit", SqlScalarType::Int32.nullable(false))
2329        .with_column("rolpassword", SqlScalarType::String.nullable(true))
2330        .with_column(
2331            "rolvaliduntil",
2332            SqlScalarType::TimestampTz { precision: None }.nullable(true),
2333        )
2334        .finish(),
2335    column_comments: BTreeMap::new(),
2336    // The `has_system_privilege` invocations for `rolcreaterole` and `rolcreatedb` get expanded
2337    // into very complex subqueries. If we put them into the SELECT clause directly, decorrelation
2338    // produces a very complex plan that the optimizer has a hard time dealing with. In particular,
2339    // the optimizer fails to reduce a query like `SELECT oid FROM pg_authid` to a simple lookup on
2340    // the `pg_authid_core` index and instead produces a large plan that contains a bunch of
2341    // expensive joins and arrangements.
2342    //
2343    // The proper fix is likely to implement `has_system_privileges` in Rust, but for now we work
2344    // around the issue by manually decorrelating `rolcreaterole` and `rolcreatedb`. Note that to
2345    // get the desired behavior we need to make sure that the join with `extra` doesn't change the
2346    // cardinality of `pg_authid_core` (otherwise it can never be optimized away). We ensure this
2347    // by:
2348    //  * using a `LEFT JOIN`, so the optimizer knows that left elements are never filtered
2349    //  * applying a `DISTINCT ON` to the CTE, so the optimizer knows that left elements are never
2350    //    duplicated
2351    sql: r#"
2352WITH extra AS (
2353    SELECT
2354        DISTINCT ON (oid)
2355        oid,
2356        mz_catalog.has_system_privilege(oid, 'CREATEROLE') AS rolcreaterole,
2357        mz_catalog.has_system_privilege(oid, 'CREATEDB') AS rolcreatedb
2358    FROM mz_internal.pg_authid_core
2359)
2360SELECT
2361    oid,
2362    rolname,
2363    rolsuper,
2364    rolinherit,
2365    extra.rolcreaterole,
2366    extra.rolcreatedb,
2367    rolcanlogin,
2368    rolreplication,
2369    rolbypassrls,
2370    rolconnlimit,
2371    rolpassword,
2372    rolvaliduntil
2373FROM mz_internal.pg_authid_core
2374LEFT JOIN extra USING (oid)"#,
2375    access: vec![rbac::owner_privilege(ObjectType::Table, MZ_SYSTEM_ROLE_ID)],
2376    ontology: None,
2377});
2378
2379pub static PG_AGGREGATE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2380    name: "pg_aggregate",
2381    schema: PG_CATALOG_SCHEMA,
2382    oid: oid::VIEW_PG_AGGREGATE_OID,
2383    desc: RelationDesc::builder()
2384        .with_column("aggfnoid", SqlScalarType::Oid.nullable(false))
2385        .with_column("aggkind", SqlScalarType::String.nullable(false))
2386        .with_column("aggnumdirectargs", SqlScalarType::Int16.nullable(false))
2387        .with_column("aggtransfn", SqlScalarType::RegProc.nullable(true))
2388        .with_column("aggfinalfn", SqlScalarType::RegProc.nullable(false))
2389        .with_column("aggcombinefn", SqlScalarType::RegProc.nullable(false))
2390        .with_column("aggserialfn", SqlScalarType::RegProc.nullable(false))
2391        .with_column("aggdeserialfn", SqlScalarType::RegProc.nullable(false))
2392        .with_column("aggmtransfn", SqlScalarType::RegProc.nullable(false))
2393        .with_column("aggminvtransfn", SqlScalarType::RegProc.nullable(false))
2394        .with_column("aggmfinalfn", SqlScalarType::RegProc.nullable(false))
2395        .with_column("aggfinalextra", SqlScalarType::Bool.nullable(false))
2396        .with_column("aggmfinalextra", SqlScalarType::Bool.nullable(false))
2397        .with_column("aggfinalmodify", SqlScalarType::PgLegacyChar.nullable(true))
2398        .with_column(
2399            "aggmfinalmodify",
2400            SqlScalarType::PgLegacyChar.nullable(true),
2401        )
2402        .with_column("aggsortop", SqlScalarType::Oid.nullable(false))
2403        .with_column("aggtranstype", SqlScalarType::Oid.nullable(true))
2404        .with_column("aggtransspace", SqlScalarType::Int32.nullable(true))
2405        .with_column("aggmtranstype", SqlScalarType::Oid.nullable(false))
2406        .with_column("aggmtransspace", SqlScalarType::Int32.nullable(true))
2407        .with_column("agginitval", SqlScalarType::String.nullable(true))
2408        .with_column("aggminitval", SqlScalarType::String.nullable(true))
2409        .finish(),
2410    column_comments: BTreeMap::new(),
2411    sql: "SELECT
2412    a.oid as aggfnoid,
2413    -- Currently Materialize only support 'normal' aggregate functions.
2414    a.agg_kind as aggkind,
2415    a.agg_num_direct_args as aggnumdirectargs,
2416    -- Materialize doesn't support these fields.
2417    NULL::pg_catalog.regproc as aggtransfn,
2418    '0'::pg_catalog.regproc as aggfinalfn,
2419    '0'::pg_catalog.regproc as aggcombinefn,
2420    '0'::pg_catalog.regproc as aggserialfn,
2421    '0'::pg_catalog.regproc as aggdeserialfn,
2422    '0'::pg_catalog.regproc as aggmtransfn,
2423    '0'::pg_catalog.regproc as aggminvtransfn,
2424    '0'::pg_catalog.regproc as aggmfinalfn,
2425    false as aggfinalextra,
2426    false as aggmfinalextra,
2427    NULL::pg_catalog.\"char\" AS aggfinalmodify,
2428    NULL::pg_catalog.\"char\" AS aggmfinalmodify,
2429    '0'::pg_catalog.oid as aggsortop,
2430    NULL::pg_catalog.oid as aggtranstype,
2431    NULL::pg_catalog.int4 as aggtransspace,
2432    '0'::pg_catalog.oid as aggmtranstype,
2433    NULL::pg_catalog.int4 as aggmtransspace,
2434    NULL::pg_catalog.text as agginitval,
2435    NULL::pg_catalog.text as aggminitval
2436FROM mz_internal.mz_aggregates a",
2437    access: vec![PUBLIC_SELECT],
2438    ontology: None,
2439});
2440
2441pub static PG_TRIGGER: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2442    name: "pg_trigger",
2443    schema: PG_CATALOG_SCHEMA,
2444    oid: oid::VIEW_PG_TRIGGER_OID,
2445    desc: RelationDesc::builder()
2446        .with_column("oid", SqlScalarType::Oid.nullable(false))
2447        .with_column("tgrelid", SqlScalarType::Oid.nullable(false))
2448        .with_column("tgparentid", SqlScalarType::Oid.nullable(false))
2449        .with_column("tgname", SqlScalarType::String.nullable(false))
2450        .with_column("tgfoid", SqlScalarType::Oid.nullable(false))
2451        .with_column("tgtype", SqlScalarType::Int16.nullable(false))
2452        .with_column("tgenabled", SqlScalarType::PgLegacyChar.nullable(false))
2453        .with_column("tgisinternal", SqlScalarType::Bool.nullable(false))
2454        .with_column("tgconstrrelid", SqlScalarType::Oid.nullable(false))
2455        .with_column("tgconstrindid", SqlScalarType::Oid.nullable(false))
2456        .with_column("tgconstraint", SqlScalarType::Oid.nullable(false))
2457        .with_column("tgdeferrable", SqlScalarType::Bool.nullable(false))
2458        .with_column("tginitdeferred", SqlScalarType::Bool.nullable(false))
2459        .with_column("tgnargs", SqlScalarType::Int16.nullable(false))
2460        .with_column("tgattr", SqlScalarType::Int2Vector.nullable(false))
2461        .with_column("tgargs", SqlScalarType::Bytes.nullable(false))
2462        .with_column("tgqual", SqlScalarType::String.nullable(false))
2463        .with_column("tgoldtable", SqlScalarType::String.nullable(false))
2464        .with_column("tgnewtable", SqlScalarType::String.nullable(false))
2465        .with_key(vec![])
2466        .finish(),
2467    column_comments: BTreeMap::new(),
2468    sql: "SELECT
2469    -- MZ doesn't support triggers so all of these fields are NULL.
2470    NULL::pg_catalog.oid AS oid,
2471    NULL::pg_catalog.oid AS tgrelid,
2472    NULL::pg_catalog.oid AS tgparentid,
2473    NULL::pg_catalog.text AS tgname,
2474    NULL::pg_catalog.oid AS tgfoid,
2475    NULL::pg_catalog.int2 AS tgtype,
2476    NULL::pg_catalog.\"char\" AS tgenabled,
2477    NULL::pg_catalog.bool AS tgisinternal,
2478    NULL::pg_catalog.oid AS tgconstrrelid,
2479    NULL::pg_catalog.oid AS tgconstrindid,
2480    NULL::pg_catalog.oid AS tgconstraint,
2481    NULL::pg_catalog.bool AS tgdeferrable,
2482    NULL::pg_catalog.bool AS tginitdeferred,
2483    NULL::pg_catalog.int2 AS tgnargs,
2484    NULL::pg_catalog.int2vector AS tgattr,
2485    NULL::pg_catalog.bytea AS tgargs,
2486    -- NOTE: The tgqual column is actually type `pg_node_tree` which we don't support. CockroachDB
2487    -- uses text as a placeholder, so we'll follow their lead here.
2488    NULL::pg_catalog.text AS tgqual,
2489    NULL::pg_catalog.text AS tgoldtable,
2490    NULL::pg_catalog.text AS tgnewtable
2491WHERE false
2492    ",
2493    access: vec![PUBLIC_SELECT],
2494    ontology: None,
2495});
2496
2497pub static PG_REWRITE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2498    name: "pg_rewrite",
2499    schema: PG_CATALOG_SCHEMA,
2500    oid: oid::VIEW_PG_REWRITE_OID,
2501    desc: RelationDesc::builder()
2502        .with_column("oid", SqlScalarType::Oid.nullable(false))
2503        .with_column("rulename", SqlScalarType::String.nullable(false))
2504        .with_column("ev_class", SqlScalarType::Oid.nullable(false))
2505        .with_column("ev_type", SqlScalarType::PgLegacyChar.nullable(false))
2506        .with_column("ev_enabled", SqlScalarType::PgLegacyChar.nullable(false))
2507        .with_column("is_instead", SqlScalarType::Bool.nullable(false))
2508        .with_column("ev_qual", SqlScalarType::String.nullable(false))
2509        .with_column("ev_action", SqlScalarType::String.nullable(false))
2510        .with_key(vec![])
2511        .finish(),
2512    column_comments: BTreeMap::new(),
2513    sql: "SELECT
2514    -- MZ doesn't support rewrite rules so all of these fields are NULL.
2515    NULL::pg_catalog.oid AS oid,
2516    NULL::pg_catalog.text AS rulename,
2517    NULL::pg_catalog.oid AS ev_class,
2518    NULL::pg_catalog.\"char\" AS ev_type,
2519    NULL::pg_catalog.\"char\" AS ev_enabled,
2520    NULL::pg_catalog.bool AS is_instead,
2521    -- NOTE: The ev_qual and ev_action columns are actually type `pg_node_tree` which we don't
2522    -- support. CockroachDB uses text as a placeholder, so we'll follow their lead here.
2523    NULL::pg_catalog.text AS ev_qual,
2524    NULL::pg_catalog.text AS ev_action
2525WHERE false
2526    ",
2527    access: vec![PUBLIC_SELECT],
2528    ontology: None,
2529});
2530
2531pub static PG_EXTENSION: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
2532    name: "pg_extension",
2533    schema: PG_CATALOG_SCHEMA,
2534    oid: oid::VIEW_PG_EXTENSION_OID,
2535    desc: RelationDesc::builder()
2536        .with_column("oid", SqlScalarType::Oid.nullable(false))
2537        .with_column("extname", SqlScalarType::String.nullable(false))
2538        .with_column("extowner", SqlScalarType::Oid.nullable(false))
2539        .with_column("extnamespace", SqlScalarType::Oid.nullable(false))
2540        .with_column("extrelocatable", SqlScalarType::Bool.nullable(false))
2541        .with_column("extversion", SqlScalarType::String.nullable(false))
2542        .with_column(
2543            "extconfig",
2544            SqlScalarType::Array(Box::new(SqlScalarType::Oid)).nullable(false),
2545        )
2546        .with_column(
2547            "extcondition",
2548            SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(false),
2549        )
2550        .with_key(vec![])
2551        .finish(),
2552    column_comments: BTreeMap::new(),
2553    sql: "SELECT
2554    -- MZ doesn't support extensions so all of these fields are NULL.
2555    NULL::pg_catalog.oid AS oid,
2556    NULL::pg_catalog.text AS extname,
2557    NULL::pg_catalog.oid AS extowner,
2558    NULL::pg_catalog.oid AS extnamespace,
2559    NULL::pg_catalog.bool AS extrelocatable,
2560    NULL::pg_catalog.text AS extversion,
2561    NULL::pg_catalog.oid[] AS extconfig,
2562    NULL::pg_catalog.text[] AS extcondition
2563WHERE false
2564    ",
2565    access: vec![PUBLIC_SELECT],
2566    ontology: None,
2567});