In our application, we check at startup,

if a postgresql table exists if it doesn't, we create it, and then enable row level security

If it does exist, we still aren't certain if row level security is true or not

So, we always need to check if row level security is enabled, and if not, we need to enable it.

Now there are two ways of doing about this (there could be more, but these are the two ways we could think of):

  1. ALTER TABLE <tname> enable row level security;

Or, we can do

  1. SELECT rowsecurity FROM pg_tables WHERE tablename = <tname> and only if that returns false, run the ALTER TABLE from (1).

The question we have is, is option (2) more performant than option (1) or is it just making things more complicated without improving the performance for subsequent startups?

This check will happen upon every application startup.

1 Answer 1


I think both options are fine from a performance point of view.

I would still go for the second option, because that ALTER TABLE statement takes an ACCESS EXCLUSIVE lock on the table. That lock will conflict with all concurrent statements that access the table, and it will disrupt autovacuum processing on the table if it runs too often. So it is preferable to run the ALTER TABLE statement no more often than necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.