Re: [COMMITTERS] pgsql: Apply RLS policies to partitioned tables. - Mailing list pgsql-committers

From Amit Langote
Subject Re: [COMMITTERS] pgsql: Apply RLS policies to partitioned tables.
Date
Msg-id 89e6ae66-96ee-b6eb-7195-d88dbb988f2f@lab.ntt.co.jp
Whole thread Raw
In response to Re: [COMMITTERS] pgsql: Apply RLS policies to partitioned tables.  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [COMMITTERS] pgsql: Apply RLS policies to partitioned tables.  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-committers
On 2017/06/12 11:51, Tom Lane wrote:
> Amit Langote <Langote_Amit_f8@lab.ntt.co.jp> writes:
>> On 2017/06/12 11:09, Joe Conway wrote:
>>> On 06/11/2017 05:35 PM, Tom Lane wrote:
>>>> First guess is that map_partition_varattnos has forgotten to handle
>>>> WithCheckOption.qual.
>
>>> Drat. I'll take a look, but it would probably be good if someone
>>> generally familiar with the partitioned tables patches have a look as well.
>
>> I am looking too.  Commit 587cda35ca3 which added that code did add a test
>> in updatable_views.sql, but tests added by this commit have perhaps
>> exposed something not previously covered.
>
> My first guess above is wrong -- the undefined reference is actually
> "mtstate->mt_plans[i]".  This is because mtstate->mt_num_partitions is
> more than mtstate->mt_nplans in the failing case, so that the blithe
> assumption that we can use the partition number to index into
> mtstate->mt_plans is certainly wrong.

Yes, the code should never access mtstate->mt_plans[i] for i > 0, because
in this case (the INSERT case), there is only one plan.  Unlike
UPDATE/DELETE cases, we don't build plans for every partition in the
INSERT case.

> Don't know how this should
> be corrected, offhand, but do we really have more than one plan
> for a partitioned query?

There can't be, as mentioned, in the INSERT case.

How about something like the attached? The patch makes
mtstate->mt_plans[0] to be passed for all invocations of ExecInitQual() on
WithCheckOption.qual that is being initialized for each partition.  Note
that we are passing a PlanState that's based on the root parent table's
properties to initialize WITH CHECK OPTION quals to be checked against
rows that will be inserted into partitions (that is, after tuple-routing).

I tried to come up with a test that would cause the existing (unpatched)
code to crash, but couldn't.  How would one define a policy or WITH CHECK
OPTIONS view or something else, such that ExecInitQual() called on
WithCheckOptions.qual would try to access parent planstate and would crash
once parent becomes an invalid pointer (mtstate->mt_plans[i] for i > 0)?
After looking at the code downstream to ExecInitQual(), it seems that the
following cases would cases would cause parent planstate to be accessed:

* Qual references a whole row var
* Qual references an Aggref or a GroupingFunc or a WindowFunc
* Qual references a (Alternative) SubPlan

Thanks,
Amit

Attachment

pgsql-committers by date:

Previous
From: Tatsuo Ishii
Date:
Subject: [COMMITTERS] pgsql: Fix ALTER TABLE doc examples.
Next
From: Peter Eisentraut
Date:
Subject: [COMMITTERS] pgsql: Stop table sync workers when subscription relation entry isremo