Skip to content

Conversation

@spilchen
Copy link
Contributor

@spilchen spilchen commented Oct 8, 2025

Complete the implementation of the row_security session variable, which was previously defined but non-functional.

When set to 'off', non-admin queries that would be affected by row-level security (RLS) fail with an error instead of silently filtering rows. This matches postgres' behavior.

Admin users and table owners (without FORCE) remain exempt from RLS regardless of this setting.

The variable defaults to 'on' and is tracked in the memo to invalidate cached plans when changed.

Closes #138916.

Epic: none

Release note (sql change): The row_security session variable now behaves like in postgres, allowing users to detect when RLS is applied.

@spilchen spilchen self-assigned this Oct 8, 2025
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@spilchen spilchen force-pushed the gh-138916/251008/1708/rls-row-security/pr-ready branch from 25385a4 to 906f4fc Compare October 9, 2025 15:23
@spilchen spilchen closed this Oct 9, 2025
@spilchen spilchen reopened this Oct 9, 2025
@spilchen spilchen requested a review from a team October 9, 2025 17:16
@spilchen spilchen marked this pull request as ready for review October 9, 2025 17:16
@spilchen spilchen requested a review from a team as a code owner October 9, 2025 17:16
@spilchen spilchen requested review from rytaft and removed request for a team October 9, 2025 17:16
@spilchen
Copy link
Contributor Author

spilchen commented Oct 9, 2025

Note: this is separate from the INSPECT work I have been doing recently. @shghasemi and I worked through part of this in our 1-on-1 pair programming session. So, just finishing this off from that session.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Just a few nits from me, :lgtm:

@yuzefovich reviewed 9 of 9 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rytaft)


pkg/sql/opt/memo/memo.go line 498 at r1 (raw file):

		m.disableSlowCascadeFastPathForRBRTables != evalCtx.SessionData().OptimizerDisableCrossRegionCascadeFastPathForRBRTables ||
		m.useImprovedHoistJoinProject != evalCtx.SessionData().OptimizerUseImprovedHoistJoinProject ||
		m.rowSecurity != evalCtx.SessionData().RowSecurity ||

nit: we should extend TestMemoIsStale for this addition.


pkg/sql/logictest/testdata/logic_test/row_level_security line 5766 at r1 (raw file):

SET SESSION row_security = 'off';

statement error pq: query would be affected by row-level security policy for table "accounts"

nit: it's nice to also check the PG error code (if we match postgres).


pkg/sql/vars.go line 1688 at r1 (raw file):

	`row_security`: {
		Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {

nit: I don't immediately see why / what the difference is, but we also usually set GetStringVal.

Complete the implementation of the `row_security` session variable,
which was previously defined but non-functional.

When set to `'off'`, non-admin queries that would be affected by
row-level security (RLS) fail with an error instead of silently
filtering rows. This matches postgres' behavior.

Admin users and table owners (without FORCE) remain exempt from RLS
regardless of this setting.

The variable defaults to `'on'` and is tracked in the memo to invalidate
cached plans when changed.

Closes cockroachdb#138916.

Epic: none

Release note (sql change): The `row_security` session variable now
behaves like in postgres, allowing users to detect when RLS is applied.
@spilchen spilchen force-pushed the gh-138916/251008/1708/rls-row-security/pr-ready branch from 906f4fc to 7e43381 Compare October 9, 2025 18:17
Copy link
Contributor Author

@spilchen spilchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rytaft and @yuzefovich)


pkg/sql/vars.go line 1688 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: I don't immediately see why / what the difference is, but we also usually set GetStringVal.

Done


pkg/sql/opt/memo/memo.go line 498 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: we should extend TestMemoIsStale for this addition.

Done


pkg/sql/logictest/testdata/logic_test/row_level_security line 5766 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: it's nice to also check the PG error code (if we match postgres).

Done. Yes, we match postgres.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuzefovich reviewed 3 of 3 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rytaft)

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! it's nice that we're able to add this

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rytaft)

@spilchen
Copy link
Contributor Author

spilchen commented Oct 9, 2025

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 10, 2025

@craig craig bot merged commit 8b90335 into cockroachdb:master Oct 10, 2025
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: support row_security session parameter

4 participants