Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lGTT5-0000hR-9B for pgsql-hackers@arkaria.postgresql.org; Sun, 28 Feb 2021 21:19:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lGTT2-0008N8-85 for pgsql-hackers@arkaria.postgresql.org; Sun, 28 Feb 2021 21:19:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lGTT1-0008N0-OS for pgsql-hackers@lists.postgresql.org; Sun, 28 Feb 2021 21:19:08 +0000 Received: from mail-il1-x12b.google.com ([2607:f8b0:4864:20::12b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lGTSr-0003mY-VZ for pgsql-hackers@lists.postgresql.org; Sun, 28 Feb 2021 21:19:06 +0000 Received: by mail-il1-x12b.google.com with SMTP id e2so12933275ilu.0 for ; Sun, 28 Feb 2021 13:18:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=do6bYjrfEvkbzLS7tCItLiojg3q3xKeyzomhLSAY6BU=; b=RETzv9jH0RtvS40prbkXVG0Ih7XiXDt4XwHQm0iXRf4azgRLUw5CjwpSNOIk7Tle/h joRHy3hctmmbYqmV9TVpmXOIkAm9c4lvIwsbt7R2HVe6nh65TIbVpufryKf+Va3iQPzJ 6yaF/PmX/NXCx/ccAVBDmMBKXhGhMrpkrCeJkrp0BuAikYI+ZytVVLVDr0OUqvqwCkG8 BUbMaylTwZF6snVz6jbLge2zG53nADyxjNBJjaPKsUtb4nKGU+NZCG9wMoVXkFGXyxV3 bE0fU7xfj+I90YrJgqkEkjd84xAEBaU9Cu/QOC+8bMsHoyOHX4lWg2Oo1XXHlCys9zHf LLWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=do6bYjrfEvkbzLS7tCItLiojg3q3xKeyzomhLSAY6BU=; b=PtYbeJCjB4tHTuz0GOjLWgEu53OLeZ8/72eCS6FuCoW6qqdoK/CsjvK6A8llfc7Vfl CIGFWSA6li7jvd3QdXHJseQ6f5meUp2bZQ0Y+SBkc9AsgRwXOpV+GxS3dwsjt7AxMWQR NPwFM0/GZGq5sVUQY8/XdwyWJo5YgUVmc0ivOkhBNrm4hKAweFAyJIsj3f1Uo71Bhgfn 0tL42m4tPD0qhkydPn6w5TNt5+7m50WwcOSfYfugIh8EFt6+soEuMpfsj19s2m13UShf 3uuytaNb6nIUnK7L/mSASMNOx7dFobbfv9dhwJ4NiPKTLJiDn7vvktg+sG4Shv99J3zt 0U8w== X-Gm-Message-State: AOAM5301cKwXTIUPrkai9eP/vjuTuwUH/QQ/Dype1uQFMF5EeBrsIvAQ rPVl1ibzxP7lBXdDJ0o4dV5MGGLGX7fWew== X-Google-Smtp-Source: ABdhPJwvD+mTSJiyktRgDfLPpkG2KfPbgtGSqZvGh0pwZHI/HGYXrQnOul43pq6Xqyvra40kjQilVw== X-Received: by 2002:a92:6511:: with SMTP id z17mr10587696ilb.232.1614547136662; Sun, 28 Feb 2021 13:18:56 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id d12sm7954646ila.71.2021.02.28.13.18.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Feb 2021 13:18:55 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 1884A80145F; Sun, 28 Feb 2021 15:18:55 -0600 (CST) Date: Sun, 28 Feb 2021 15:18:54 -0600 From: Justin Pryzby To: Stephen Frost Cc: pgsql-hackers@lists.postgresql.org Subject: [PATCH] refactor ATExec{En,Dis}ableRowSecurity Message-ID: <20210228211854.GC20769@telsasoft.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nhYGnrYv1PEJ5gA2" Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --nhYGnrYv1PEJ5gA2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline tablecmds.c is 17k lines long, this makes it ~30 lines shorter. --nhYGnrYv1PEJ5gA2 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Refactor-ATExec-En-Dis-ableRowSecurity-in-the-style-.patch" From 2e9500227d45142eb00e9e1ebee001642a834518 Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Sat, 27 Feb 2021 20:39:10 -0600 Subject: [PATCH 1/2] Refactor ATExec{En,Dis}ableRowSecurity in the style of ATExecForceNoForceRowSecurity commit 088c83363a11200f2225f279d4a5c6cc6f9db3d2 ALTER TABLE .. FORCE ROW LEVEL SECURITY commit 491c029dbc4206779cf659aa0ff986af7831d2ff Row-Level Security Policies (RLS) --- src/backend/commands/tablecmds.c | 34 +++++--------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 7adeaedd0e..69e3184c86 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -526,8 +526,7 @@ static ObjectAddress ATExecAddOf(Relation rel, const TypeName *ofTypename, LOCKM static void ATExecDropOf(Relation rel, LOCKMODE lockmode); static void ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode); static void ATExecGenericOptions(Relation rel, List *options); -static void ATExecEnableRowSecurity(Relation rel); -static void ATExecDisableRowSecurity(Relation rel); +static void ATExecSetRowSecurity(Relation rel, bool rls); static void ATExecForceNoForceRowSecurity(Relation rel, bool force_rls); static void index_copy_data(Relation rel, RelFileNode newrnode); @@ -4865,10 +4864,10 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel, ATExecReplicaIdentity(rel, (ReplicaIdentityStmt *) cmd->def, lockmode); break; case AT_EnableRowSecurity: - ATExecEnableRowSecurity(rel); + ATExecSetRowSecurity(rel, true); break; case AT_DisableRowSecurity: - ATExecDisableRowSecurity(rel); + ATExecSetRowSecurity(rel, false); break; case AT_ForceRowSecurity: ATExecForceNoForceRowSecurity(rel, true); @@ -14883,30 +14882,7 @@ ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode * ALTER TABLE ENABLE/DISABLE ROW LEVEL SECURITY */ static void -ATExecEnableRowSecurity(Relation rel) -{ - Relation pg_class; - Oid relid; - HeapTuple tuple; - - relid = RelationGetRelid(rel); - - pg_class = table_open(RelationRelationId, RowExclusiveLock); - - tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid)); - - if (!HeapTupleIsValid(tuple)) - elog(ERROR, "cache lookup failed for relation %u", relid); - - ((Form_pg_class) GETSTRUCT(tuple))->relrowsecurity = true; - CatalogTupleUpdate(pg_class, &tuple->t_self, tuple); - - table_close(pg_class, RowExclusiveLock); - heap_freetuple(tuple); -} - -static void -ATExecDisableRowSecurity(Relation rel) +ATExecSetRowSecurity(Relation rel, bool rls) { Relation pg_class; Oid relid; @@ -14922,7 +14898,7 @@ ATExecDisableRowSecurity(Relation rel) if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for relation %u", relid); - ((Form_pg_class) GETSTRUCT(tuple))->relrowsecurity = false; + ((Form_pg_class) GETSTRUCT(tuple))->relrowsecurity = rls; CatalogTupleUpdate(pg_class, &tuple->t_self, tuple); table_close(pg_class, RowExclusiveLock); -- 2.17.0 --nhYGnrYv1PEJ5gA2 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-Further-refactoring.patch" From bf200702203eebe08011eaa007e93cae46a63ccb Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Sat, 27 Feb 2021 20:52:35 -0600 Subject: [PATCH 2/2] Further refactoring --- src/backend/commands/tablecmds.c | 46 +++++++++----------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 69e3184c86..2e322e78ae 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -526,8 +526,7 @@ static ObjectAddress ATExecAddOf(Relation rel, const TypeName *ofTypename, LOCKM static void ATExecDropOf(Relation rel, LOCKMODE lockmode); static void ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode); static void ATExecGenericOptions(Relation rel, List *options); -static void ATExecSetRowSecurity(Relation rel, bool rls); -static void ATExecForceNoForceRowSecurity(Relation rel, bool force_rls); +static void ATExecSetRowSecurity(Relation rel, int rls, int force_rls); static void index_copy_data(Relation rel, RelFileNode newrnode); static const char *storage_name(char c); @@ -4864,16 +4863,16 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel, ATExecReplicaIdentity(rel, (ReplicaIdentityStmt *) cmd->def, lockmode); break; case AT_EnableRowSecurity: - ATExecSetRowSecurity(rel, true); + ATExecSetRowSecurity(rel, true, -1); break; case AT_DisableRowSecurity: - ATExecSetRowSecurity(rel, false); + ATExecSetRowSecurity(rel, false, -1); break; case AT_ForceRowSecurity: - ATExecForceNoForceRowSecurity(rel, true); + ATExecSetRowSecurity(rel, -1, true); break; case AT_NoForceRowSecurity: - ATExecForceNoForceRowSecurity(rel, false); + ATExecSetRowSecurity(rel, -1, false); break; case AT_GenericOptions: ATExecGenericOptions(rel, (List *) cmd->def); @@ -14880,13 +14879,15 @@ ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode /* * ALTER TABLE ENABLE/DISABLE ROW LEVEL SECURITY + * ALTER TABLE FORCE/NO FORCE ROW LEVEL SECURITY */ static void -ATExecSetRowSecurity(Relation rel, bool rls) +ATExecSetRowSecurity(Relation rel, int rls, int force_rls) { Relation pg_class; Oid relid; HeapTuple tuple; + Form_pg_class classform; relid = RelationGetRelid(rel); @@ -14898,33 +14899,12 @@ ATExecSetRowSecurity(Relation rel, bool rls) if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for relation %u", relid); - ((Form_pg_class) GETSTRUCT(tuple))->relrowsecurity = rls; - CatalogTupleUpdate(pg_class, &tuple->t_self, tuple); - - table_close(pg_class, RowExclusiveLock); - heap_freetuple(tuple); -} - -/* - * ALTER TABLE FORCE/NO FORCE ROW LEVEL SECURITY - */ -static void -ATExecForceNoForceRowSecurity(Relation rel, bool force_rls) -{ - Relation pg_class; - Oid relid; - HeapTuple tuple; - - relid = RelationGetRelid(rel); - - pg_class = table_open(RelationRelationId, RowExclusiveLock); - - tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid)); - - if (!HeapTupleIsValid(tuple)) - elog(ERROR, "cache lookup failed for relation %u", relid); + classform = (Form_pg_class) GETSTRUCT(tuple); + if (rls != -1) + classform->relrowsecurity = (bool) rls; + if (force_rls != -1) + classform->relforcerowsecurity = (bool) force_rls; - ((Form_pg_class) GETSTRUCT(tuple))->relforcerowsecurity = force_rls; CatalogTupleUpdate(pg_class, &tuple->t_self, tuple); table_close(pg_class, RowExclusiveLock); -- 2.17.0 --nhYGnrYv1PEJ5gA2--