diff --git a/pkg/sql/logictest/testdata/logic_test/alter_role_set b/pkg/sql/logictest/testdata/logic_test/alter_role_set index e0def5a9e55e..eb933100af7a 100644 --- a/pkg/sql/logictest/testdata/logic_test/alter_role_set +++ b/pkg/sql/logictest/testdata/logic_test/alter_role_set @@ -22,6 +22,20 @@ database_id role_name settings 53 ยท {application_name=c} 53 test_set_role {application_name=b} +# Defaults should be in pg_catalog too. +query OOTTT colnames +SELECT setdatabase, setrole, d.datname, r.rolname, setconfig +FROM pg_catalog.pg_db_role_setting +LEFT JOIN pg_catalog.pg_database d ON setdatabase = d.oid +LEFT JOIN pg_catalog.pg_roles r ON setrole = r.oid +ORDER BY 1, 2 +---- +setdatabase setrole datname rolname setconfig +0 0 NULL NULL {application_name=d} +0 265380634 NULL test_set_role {application_name=a} +53 0 test_set_db NULL {application_name=c} +53 265380634 test_set_db test_set_role {application_name=b} + statement ok ALTER ROLE test_set_role SET backslash_quote = 'safe_encoding' diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index a5d7537eb74e..8a851b67c57e 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -3642,7 +3642,7 @@ objoid classoid objsubid description 4294967133 4294967137 0 encoding conversions (empty - unimplemented) 4294967132 4294967137 0 pg_cursors was created for compatibility and is currently unimplemented 4294967131 4294967137 0 available databases (incomplete) -4294967130 4294967137 0 pg_db_role_setting was created for compatibility and is currently unimplemented +4294967130 4294967137 0 contains the default values that have been configured for session variables 4294967129 4294967137 0 default ACLs; these are the privileges that will be assigned to newly created objects 4294967128 4294967137 0 dependency relationships (incomplete) 4294967127 4294967137 0 object comments diff --git a/pkg/sql/pg_catalog.go b/pkg/sql/pg_catalog.go index 12afc4d2fa24..fa8c7518a808 100644 --- a/pkg/sql/pg_catalog.go +++ b/pkg/sql/pg_catalog.go @@ -37,6 +37,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" + "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/sql/vtable" @@ -2835,12 +2836,39 @@ var pgCatalogShmemAllocationsTable = virtualSchemaTable{ } var pgCatalogDbRoleSettingTable = virtualSchemaTable{ - comment: "pg_db_role_setting was created for compatibility and is currently unimplemented", - schema: vtable.PgCatalogDbRoleSetting, - populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error { + comment: `contains the default values that have been configured for session variables +https://www.postgresql.org/docs/13/catalog-pg-db-role-setting.html`, + schema: vtable.PgCatalogDbRoleSetting, + populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error { + rows, err := p.extendedEvalCtx.ExecCfg.InternalExecutor.QueryBufferedEx( + ctx, + "select-db-role-settings", + p.EvalContext().Txn, + sessiondata.InternalExecutorOverride{User: security.RootUserName()}, + `SELECT database_id, role_name, settings FROM system.public.database_role_settings`, + ) + if err != nil { + return err + } + h := makeOidHasher() + for _, row := range rows { + databaseID := tree.MustBeDOid(row[0]) + roleName := tree.MustBeDString(row[1]) + roleID := oidZero + if roleName != "" { + roleID = h.UserOid(security.MakeSQLUsernameFromPreNormalizedString(string(roleName))) + } + settings := tree.MustBeDArray(row[2]) + if err := addRow( + settings, + databaseID, + roleID, + ); err != nil { + return err + } + } return nil }, - unimplemented: true, } var pgCatalogTimezoneNamesTable = virtualSchemaTable{ diff --git a/pkg/sql/vtable/pg_catalog.go b/pkg/sql/vtable/pg_catalog.go index ae508f5276de..ce3a5177b13c 100644 --- a/pkg/sql/vtable/pg_catalog.go +++ b/pkg/sql/vtable/pg_catalog.go @@ -989,8 +989,9 @@ CREATE TABLE pg_catalog.pg_shmem_allocations ( allocated_size INT )` -// PgCatalogDbRoleSetting is an empty table created by pg_catalog_test -// and is currently unimplemented. +// PgCatalogDbRoleSetting contains the default values that have been configured +// for session variables, for each role and database combination. This table +// contains the same data no matter which database the current session is using. const PgCatalogDbRoleSetting = ` CREATE TABLE pg_catalog.pg_db_role_setting ( setconfig STRING[],