Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2019-07-16 15:51:44 +0000
committerTom Lane2019-07-16 15:51:44 +0000
commit569ed7f48312c70ed4a79daec1d7688fda4e74ac (patch)
tree0c99570181049379f9daf351980829f4e5517f25 /src/backend/rewrite/rowsecurity.c
parent0896ae561b6c799d45cb61d8a3b18fbb442130a7 (diff)
Redesign the API for list sorting (list_qsort becomes list_sort).
In the wake of commit 1cff1b95a, the obvious way to sort a List is to apply qsort() directly to the array of ListCells. list_qsort was building an intermediate array of pointers-to-ListCells, which we no longer need, but getting rid of it forces an API change: the comparator functions need to do one less level of indirection. Since we're having to touch the callers anyway, let's do two additional changes: sort the given list in-place rather than making a copy (as none of the existing callers have any use for the copying behavior), and rename list_qsort to list_sort. It was argued that the old name exposes more about the implementation than it should, which I find pretty questionable, but a better reason to rename it is to be sure we get the attention of any external callers about the need to fix their comparator functions. While we're at it, change four existing callers of qsort() to use list_sort instead; previously, they all had local reinventions of list_qsort, ie build-an-array-from-a-List-and-qsort-it. (There are some other places where changing to list_sort perhaps would be worthwhile, but they're less obviously wins.) Discussion: https://postgr.es/m/29361.1563220190@sss.pgh.pa.us
Diffstat (limited to 'src/backend/rewrite/rowsecurity.c')
-rw-r--r--src/backend/rewrite/rowsecurity.c43
1 files changed, 10 insertions, 33 deletions
diff --git a/src/backend/rewrite/rowsecurity.c b/src/backend/rewrite/rowsecurity.c
index 300af6f06f2..1c44714589b 100644
--- a/src/backend/rewrite/rowsecurity.c
+++ b/src/backend/rewrite/rowsecurity.c
@@ -63,9 +63,9 @@ static void get_policies_for_relation(Relation relation,
List **permissive_policies,
List **restrictive_policies);
-static List *sort_policies_by_name(List *policies);
+static void sort_policies_by_name(List *policies);
-static int row_security_policy_cmp(const void *a, const void *b);
+static int row_security_policy_cmp(const ListCell *a, const ListCell *b);
static void add_security_quals(int rt_index,
List *permissive_policies,
@@ -470,7 +470,7 @@ get_policies_for_relation(Relation relation, CmdType cmd, Oid user_id,
* We sort restrictive policies by name so that any WCOs they generate are
* checked in a well-defined order.
*/
- *restrictive_policies = sort_policies_by_name(*restrictive_policies);
+ sort_policies_by_name(*restrictive_policies);
/*
* Then add any permissive or restrictive policies defined by extensions.
@@ -488,7 +488,7 @@ get_policies_for_relation(Relation relation, CmdType cmd, Oid user_id,
* always check all built-in restrictive policies, in name order,
* before checking restrictive policies added by hooks, in name order.
*/
- hook_policies = sort_policies_by_name(hook_policies);
+ sort_policies_by_name(hook_policies);
foreach(item, hook_policies)
{
@@ -522,43 +522,20 @@ get_policies_for_relation(Relation relation, CmdType cmd, Oid user_id,
* This is not necessary for permissive policies, since they are all combined
* together using OR into a single WithCheckOption check.
*/
-static List *
+static void
sort_policies_by_name(List *policies)
{
- int npol = list_length(policies);
- RowSecurityPolicy *pols;
- ListCell *item;
- int ii = 0;
-
- if (npol <= 1)
- return policies;
-
- pols = (RowSecurityPolicy *) palloc(sizeof(RowSecurityPolicy) * npol);
-
- foreach(item, policies)
- {
- RowSecurityPolicy *policy = (RowSecurityPolicy *) lfirst(item);
-
- pols[ii++] = *policy;
- }
-
- qsort(pols, npol, sizeof(RowSecurityPolicy), row_security_policy_cmp);
-
- policies = NIL;
- for (ii = 0; ii < npol; ii++)
- policies = lappend(policies, &pols[ii]);
-
- return policies;
+ list_sort(policies, row_security_policy_cmp);
}
/*
- * qsort comparator to sort RowSecurityPolicy entries by name
+ * list_sort comparator to sort RowSecurityPolicy entries by name
*/
static int
-row_security_policy_cmp(const void *a, const void *b)
+row_security_policy_cmp(const ListCell *a, const ListCell *b)
{
- const RowSecurityPolicy *pa = (const RowSecurityPolicy *) a;
- const RowSecurityPolicy *pb = (const RowSecurityPolicy *) b;
+ const RowSecurityPolicy *pa = (const RowSecurityPolicy *) lfirst(a);
+ const RowSecurityPolicy *pb = (const RowSecurityPolicy *) lfirst(b);
/* Guard against NULL policy names from extensions */
if (pa->policy_name == NULL)