Skip to content

Commit

Permalink
Merge #68245
Browse files Browse the repository at this point in the history
68245: sql implement the pg_db_role_setting table r=RichardJCai a=rafiss

touches #21151

Release note (sql change): The pg_db_role_setting table of the
pg_catalog is now implemented. When `ALTER ROLE ... SET var`
is used to configure per-role defaults, these default settings will be
populated in pg_db_role_setting. This table contains the same data no
matter which database the current session is using.

See https://www.postgresql.org/docs/13/catalog-pg-db-role-setting.html

Co-authored-by: Rafi Shamim <[email protected]>
  • Loading branch information
craig[bot] and rafiss committed Aug 5, 2021
2 parents 6b8d593 + 28b5534 commit 5a43a7e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
14 changes: 14 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_role_set
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 32 additions & 4 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/vtable/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down

0 comments on commit 5a43a7e

Please sign in to comment.