[Postgres] Add checks for RLS affecting replication #275
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
Originally, Supabase only supported logical replication by using the built-in
postgres
role. In the meantime, it is possible to create a custom role withREPLICATION
, and we recommend it in our docs.The caveat is that using RLS is very common and encouraged on Supabase, but RLS affects every non-superuser user by default. So when switching from
postgres
to a custom role, you could easily lose access to read the tables. And worse, this does not show up as an error - we simply get 0 results. And this often does not show up immediately, since streaming replication still works - you only notice when updating sync rules and the data is suddenly missing.The fix
The fix is to make sure that the custom role has the
BYPASSRLS
attribute set.The check
This adds a check on whether RLS is enabled for every replicated table, and logs a warning if RLS is enabled and the role is not super-user or has BYPASSRLS set.
This logs a warning, and also reports the same info via the diagnostics api. We don't block replication in this case, since there are scenarios not covered by this check, for example explicitly adding a RLS policy giving access to the custom role.
Sample log output: