Skip to content

Commit 55c09cb

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 55c09cb

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

src/catalog/src/builtin.rs

Lines changed: 57 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,55 @@ 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+
SELECT
10642+
oid,
10643+
rolname,
10644+
rolsuper,
10645+
rolinherit,
10646+
mz_catalog.has_system_privilege(oid, 'CREATEROLE') AS rolcreaterole,
10647+
mz_catalog.has_system_privilege(oid, 'CREATEDB') AS rolcreatedb,
10648+
rolcanlogin,
10649+
rolreplication,
10650+
rolbypassrls,
10651+
rolconnlimit,
10652+
rolpassword,
10653+
rolvaliduntil
10654+
FROM mz_internal.pg_authid_core"#,
10655+
access: vec![rbac::owner_privilege(ObjectType::Table, MZ_SYSTEM_ROLE_ID)],
10656+
});
10657+
1061110658
pub static PG_AGGREGATE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
1061210659
name: "pg_aggregate",
1061310660
schema: PG_CATALOG_SCHEMA,
@@ -13967,6 +14014,8 @@ pub static BUILTINS_STATIC: LazyLock<Vec<Builtin<NameReference>>> = LazyLock::ne
1396714014
Builtin::View(&PG_TABLESPACE),
1396814015
Builtin::View(&PG_ACCESS_METHODS),
1396914016
Builtin::View(&PG_LOCKS),
14017+
Builtin::View(&PG_AUTHID_CORE),
14018+
Builtin::Index(&PG_AUTHID_CORE_IND),
1397014019
Builtin::View(&PG_AUTHID),
1397114020
Builtin::View(&PG_ROLES),
1397214021
Builtin::View(&PG_USER),

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;

0 commit comments

Comments
 (0)