Skip to content

Commit ddbe176

Browse files
committed
catalog: index most of pg_authid
pgbouncer by default queries `pg_authid` to authenticate users. This query is slow because it reads from unindexed sources and computes an expensive dataflow. Ideally we would fix this by indexing it, but that's not possible because two columns are computed using unmaterializable functions. So instead we introduce a new `pg_authid_core` view that excludes these columns and can be indexed. `pg_authid` is then defined on top of this view.
1 parent c3b5b58 commit ddbe176

File tree

13 files changed

+156
-41
lines changed

13 files changed

+156
-41
lines changed

doc/user/content/sql/system-catalog/mz_internal.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,10 +1374,10 @@ The `mz_webhook_sources` table contains a row for each webhook source in the sys
13741374
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_objects_id_namespace_types -->
13751375
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.mz_console_cluster_utilization_overview -->
13761376

1377-
1378-
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_class_all_databases -->
1379-
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_type_all_databases -->
1380-
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_namespace_all_databases -->
1381-
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_description_all_databases -->
13821377
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_attrdef_all_databases -->
13831378
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_attribute_all_databases -->
1379+
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_authid_core -->
1380+
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_class_all_databases -->
1381+
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_description_all_databases -->
1382+
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_namespace_all_databases -->
1383+
<!-- RELATION_SPEC_UNDOCUMENTED mz_internal.pg_type_all_databases -->

src/catalog/src/builtin.rs

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10564,17 +10564,17 @@ WHERE false",
1056410564
access: vec![PUBLIC_SELECT],
1056510565
});
1056610566

10567-
pub static PG_AUTHID: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
10568-
name: "pg_authid",
10569-
schema: PG_CATALOG_SCHEMA,
10570-
oid: oid::VIEW_PG_AUTHID_OID,
10567+
/// Peeled version of `PG_AUTHID`: Excludes the columns rolcreaterole and rolcreatedb, to make this
10568+
/// view indexable.
10569+
pub static PG_AUTHID_CORE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
10570+
name: "pg_authid_core",
10571+
schema: MZ_INTERNAL_SCHEMA,
10572+
oid: oid::VIEW_PG_AUTHID_CORE_OID,
1057110573
desc: RelationDesc::builder()
1057210574
.with_column("oid", SqlScalarType::Oid.nullable(false))
1057310575
.with_column("rolname", SqlScalarType::String.nullable(false))
1057410576
.with_column("rolsuper", SqlScalarType::Bool.nullable(true))
1057510577
.with_column("rolinherit", SqlScalarType::Bool.nullable(false))
10576-
.with_column("rolcreaterole", SqlScalarType::Bool.nullable(true))
10577-
.with_column("rolcreatedb", SqlScalarType::Bool.nullable(true))
1057810578
.with_column("rolcanlogin", SqlScalarType::Bool.nullable(false))
1057910579
.with_column("rolreplication", SqlScalarType::Bool.nullable(false))
1058010580
.with_column("rolbypassrls", SqlScalarType::Bool.nullable(false))
@@ -10592,8 +10592,6 @@ SELECT
1059210592
r.name AS rolname,
1059310593
rolsuper,
1059410594
inherit AS rolinherit,
10595-
mz_catalog.has_system_privilege(r.oid, 'CREATEROLE') AS rolcreaterole,
10596-
mz_catalog.has_system_privilege(r.oid, 'CREATEDB') AS rolcreatedb,
1059710595
COALESCE(r.rolcanlogin, false) AS rolcanlogin,
1059810596
-- MZ doesn't support replication in the same way Postgres does
1059910597
false AS rolreplication,
@@ -10608,6 +10606,64 @@ LEFT JOIN mz_catalog.mz_role_auth a ON r.oid = a.role_oid"#,
1060810606
access: vec![rbac::owner_privilege(ObjectType::Table, MZ_SYSTEM_ROLE_ID)],
1060910607
});
1061010608

10609+
pub const PG_AUTHID_CORE_IND: BuiltinIndex = BuiltinIndex {
10610+
name: "pg_authid_core_ind",
10611+
schema: MZ_INTERNAL_SCHEMA,
10612+
oid: oid::INDEX_PG_AUTHID_CORE_IND_OID,
10613+
sql: "IN CLUSTER mz_catalog_server
10614+
ON mz_internal.pg_authid_core (rolname)",
10615+
is_retained_metrics_object: false,
10616+
};
10617+
10618+
pub static PG_AUTHID: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
10619+
name: "pg_authid",
10620+
schema: PG_CATALOG_SCHEMA,
10621+
oid: oid::VIEW_PG_AUTHID_OID,
10622+
desc: RelationDesc::builder()
10623+
.with_column("oid", SqlScalarType::Oid.nullable(false))
10624+
.with_column("rolname", SqlScalarType::String.nullable(false))
10625+
.with_column("rolsuper", SqlScalarType::Bool.nullable(true))
10626+
.with_column("rolinherit", SqlScalarType::Bool.nullable(false))
10627+
.with_column("rolcreaterole", SqlScalarType::Bool.nullable(true))
10628+
.with_column("rolcreatedb", SqlScalarType::Bool.nullable(true))
10629+
.with_column("rolcanlogin", SqlScalarType::Bool.nullable(false))
10630+
.with_column("rolreplication", SqlScalarType::Bool.nullable(false))
10631+
.with_column("rolbypassrls", SqlScalarType::Bool.nullable(false))
10632+
.with_column("rolconnlimit", SqlScalarType::Int32.nullable(false))
10633+
.with_column("rolpassword", SqlScalarType::String.nullable(true))
10634+
.with_column(
10635+
"rolvaliduntil",
10636+
SqlScalarType::TimestampTz { precision: None }.nullable(true),
10637+
)
10638+
.finish(),
10639+
column_comments: BTreeMap::new(),
10640+
sql: r#"
10641+
WITH extra AS (
10642+
SELECT
10643+
DISTINCT ON (oid)
10644+
oid,
10645+
mz_catalog.has_system_privilege(oid, 'CREATEROLE') AS rolcreaterole,
10646+
mz_catalog.has_system_privilege(oid, 'CREATEDB') AS rolcreatedb
10647+
FROM mz_internal.pg_authid_core
10648+
)
10649+
SELECT
10650+
oid,
10651+
rolname,
10652+
rolsuper,
10653+
rolinherit,
10654+
extra.rolcreaterole,
10655+
extra.rolcreatedb,
10656+
rolcanlogin,
10657+
rolreplication,
10658+
rolbypassrls,
10659+
rolconnlimit,
10660+
rolpassword,
10661+
rolvaliduntil
10662+
FROM mz_internal.pg_authid_core
10663+
LEFT JOIN extra USING (oid)"#,
10664+
access: vec![rbac::owner_privilege(ObjectType::Table, MZ_SYSTEM_ROLE_ID)],
10665+
});
10666+
1061110667
pub static PG_AGGREGATE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1061210668
name: "pg_aggregate",
1061310669
schema: PG_CATALOG_SCHEMA,
@@ -13967,6 +14023,8 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne
1396714023
Builtin::View(&PG_TABLESPACE),
1396814024
Builtin::View(&PG_ACCESS_METHODS),
1396914025
Builtin::View(&PG_LOCKS),
14026+
Builtin::View(&PG_AUTHID_CORE),
14027+
Builtin::Index(&PG_AUTHID_CORE_IND),
1397014028
Builtin::View(&PG_AUTHID),
1397114029
Builtin::View(&PG_ROLES),
1397214030
Builtin::View(&PG_USER),

src/environmentd/tests/testdata/http/ws

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

src/pgrepr-consts/src/oid.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,5 @@ pub const TABLE_MZ_ROLE_AUTH_OID: u32 = 17059;
783783
pub const TABLE_MZ_ICEBERG_SINKS_OID: u32 = 17060;
784784
pub const VIEW_MZ_OBJECT_GLOBAL_IDS_OID: u32 = 17061;
785785
pub const TABLE_MZ_REPLACEMENTS_OID: u32 = 17062;
786+
pub const VIEW_PG_AUTHID_CORE_OID: u32 = 17063;
787+
pub const INDEX_PG_AUTHID_CORE_IND_OID: u32 = 17064;

test/sqllogictest/autogenerated/mz_internal.slt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ mz_wallclock_lag_history
825825
mz_webhook_sources
826826
pg_attrdef_all_databases
827827
pg_attribute_all_databases
828+
pg_authid_core
828829
pg_class_all_databases
829830
pg_description_all_databases
830831
pg_namespace_all_databases

test/sqllogictest/cluster.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,15 @@ CREATE CLUSTER test REPLICAS (foo (SIZE 'scale=1,workers=1'));
417417
query I
418418
SELECT COUNT(name) FROM mz_indexes;
419419
----
420-
295
420+
296
421421

422422
statement ok
423423
DROP CLUSTER test CASCADE
424424

425425
query T
426426
SELECT COUNT(name) FROM mz_indexes;
427427
----
428-
263
428+
264
429429

430430
simple conn=mz_system,user=mz_system
431431
ALTER CLUSTER quickstart OWNER TO materialize

test/sqllogictest/distinct_arrangements.slt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,12 +1019,13 @@ ArrangeBy[[Column(0,␠"objoid"),␠Column(1,␠"classoid"),␠Column(2,␠"objs
10191019
ArrangeBy[[Column(0,␠"objoid"),␠Column(1,␠"classoid"),␠Column(2,␠"objsubid"),␠Column(3,␠"description"),␠Column(4,␠"oid_database_name"),␠Column(5,␠"class_database_name")]]-errors 1
10201020
ArrangeBy[[Column(0,␠"oid"),␠Column(1,␠"adrelid"),␠Column(2,␠"adnum"),␠Column(3,␠"adbin"),␠Column(4,␠"adsrc")]] 1
10211021
ArrangeBy[[Column(0,␠"oid"),␠Column(1,␠"adrelid"),␠Column(2,␠"adnum"),␠Column(3,␠"adbin"),␠Column(4,␠"adsrc")]]-errors 1
1022-
ArrangeBy[[Column(0,␠"oid")]] 2
1022+
ArrangeBy[[Column(0,␠"oid")]] 3
10231023
ArrangeBy[[Column(0,␠"oid")]]-errors 1
10241024
ArrangeBy[[Column(0,␠"replica_id"),␠Column(3,␠"bucket_start")]] 5
10251025
ArrangeBy[[Column(0,␠"replica_id"),␠Column(4,␠"bucket_start")]] 1
10261026
ArrangeBy[[Column(0,␠"replica_id")]] 12
10271027
ArrangeBy[[Column(0,␠"replica_id")]]-errors 5
1028+
ArrangeBy[[Column(0,␠"role_oid")]] 1
10281029
ArrangeBy[[Column(0,␠"schema_id")]] 6
10291030
ArrangeBy[[Column(0,␠"schema_id")]]-errors 6
10301031
ArrangeBy[[Column(0,␠"self")]] 1
@@ -1059,6 +1060,8 @@ ArrangeBy[[Column(1,␠"referenced_object_id")]] 2
10591060
ArrangeBy[[Column(1,␠"relname")]] 1
10601061
ArrangeBy[[Column(1,␠"relname")]]-errors 1
10611062
ArrangeBy[[Column(1,␠"replica_id")]] 1
1063+
ArrangeBy[[Column(1,␠"rolname")]] 1
1064+
ArrangeBy[[Column(1,␠"rolname")]]-errors 1
10621065
ArrangeBy[[Column(1,␠"session_id")]] 1
10631066
ArrangeBy[[Column(1,␠"shard_id"),␠Column(2,␠"collection_timestamp")]] 1
10641067
ArrangeBy[[Column(1,␠"shard_id")]] 1
@@ -1103,13 +1106,13 @@ ArrangeBy[[Column(6,␠"schema_id")]]-errors 1
11031106
ArrangeBy[[Column(7,␠"database_id")]] 1
11041107
ArrangeBy[[Column(9,␠"database_id")]] 1
11051108
ArrangeBy[[]] 11
1106-
Arranged␠DistinctBy 49
1109+
Arranged␠DistinctBy 50
11071110
Arranged␠MinsMaxesHierarchical␠input 14
11081111
Arranged␠ReduceInaccumulable 3
11091112
Arranged␠TopK␠input 111
11101113
Distinct␠recursive␠err 3
1111-
DistinctBy 49
1112-
DistinctByErrorCheck 49
1114+
DistinctBy 50
1115+
DistinctByErrorCheck 50
11131116
ReduceAccumulable 11
11141117
ReduceInaccumulable 3
11151118
ReduceInaccumulable␠Error␠Check 3

test/sqllogictest/information_schema_tables.slt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ pg_attribute_all_databases
769769
VIEW
770770
materialize
771771
mz_internal
772+
pg_authid_core
773+
VIEW
774+
materialize
775+
mz_internal
772776
pg_class_all_databases
773777
VIEW
774778
materialize

0 commit comments

Comments
 (0)