You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
In pg_dump, avoid doing per-table queries for RLS policies.
For no particularly good reason, getPolicies() queried pg_policyseparately for each table. We can collect all the policies ina single query instead, and attach them to the correct TableInfoobjects using findTableByOid() lookups. On the regressiondatabase, this reduces the number of queries substantially, andprovides a visible savings even when running against a localserver.Per complaint from Hubert Depesz Lubaczewski. Since this is sucha simple fix and can have a visible performance benefit, back-patchto all supported branches.Discussion:https://postgr.es/m/20210826084430.GA26282@depesz.com
" pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
3735
-
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3736
-
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3737
-
"FROM pg_catalog.pg_policy pol "
3738
-
"WHERE polrelid = '%u'",
3739
-
tbinfo->dobj.catId.oid);
3740
-
else
3741
-
appendPQExpBuffer(query,
3742
-
"SELECT oid, tableoid, pol.polname, pol.polcmd, 't' as polpermissive, "
3743
-
"CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3744
-
" pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
3745
-
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3746
-
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3747
-
"FROM pg_catalog.pg_policy pol "
3748
-
"WHERE polrelid = '%u'",
3749
-
tbinfo->dobj.catId.oid);
3750
-
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3720
+
/*
3721
+
* Now, read all RLS policies, and create PolicyInfo objects for all those
appendPQExpBuffer(query, "'t' as polpermissive, ");
3732
+
appendPQExpBuffer(query,
3733
+
"CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3734
+
" pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
3735
+
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3736
+
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3737
+
"FROM pg_catalog.pg_policy pol");
3753
3738
3754
-
if (ntups == 0)
3755
-
{
3756
-
/*
3757
-
* No explicit policies to handle (only the default-deny policy,
3758
-
* which is handled as part of the table definition). Clean up
3759
-
* and return.
3760
-
*/
3761
-
PQclear(res);
3762
-
continue;
3763
-
}
3739
+
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);