Re: [HACKERS] UPDATE of partition key

Amit Khandekar <amitdkhan.pg@gmail.com>

From: Amit Khandekar <amitdkhan.pg@gmail.com>
To: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
Cc: Robert Haas <robertmhaas@gmail.com>, pgsql-hackers <pgsql-hackers@postgresql.org>, Thomas Munro <thomas.munro@enterprisedb.com>
Date: 2017-12-01T11:54:25Z
Lists: pgsql-hackers

Attachments

On 30 November 2017 at 18:56, Amit Khandekar <amitdkhan.pg@gmail.com> wrote:
> While addressing Thomas's point about test scenarios not yet covered,
> I observed the following ...
>
> Suppose an UPDATE RLS policy with a WITH CHECK clause is defined on
> the target table. Now In ExecUpdate(), the corresponding WCO qual gets
> executed *before* the partition constraint check, as per existing
> behaviour. And the qual succeeds. And then because of partition-key
> updated, the row is moved to another partition. Here, suppose there is
> a BR INSERT trigger which modifies the row, and the resultant row
> actually would *not* pass the UPDATE RLS policy. But for this
> partition, since it is an INSERT, only INSERT RLS WCO quals are
> executed.
>
> So effectively, with a user-perspective, an RLS WITH CHECK policy that
> was defined to reject an updated row, is getting updated successfully.
> This is because the policy is not checked *after* a row trigger in the
> new partition is executed.
>
> Attached is a test case that reproduces this issue.
>
> I think, in case of row-movement, we should defer calling
> ExecWithCheckOptions() until the row is being inserted using
> ExecInsert(). And then in ExecInsert(), ExecWithCheckOptions() should
> be called using WCO_RLS_UPDATE_CHECK rather than WCO_RLS_INSERT_CHECK
> (I recall Amit Langote was of this opinion) as below :
>
> --- a/src/backend/executor/nodeModifyTable.c
> +++ b/src/backend/executor/nodeModifyTable.c
> @@ -510,7 +510,9 @@ ExecInsert(ModifyTableState *mtstate,
>   * we are looking for at this point.
>   */
>   if (resultRelInfo->ri_WithCheckOptions != NIL)
> -     ExecWithCheckOptions(WCO_RLS_INSERT_CHECK,
> +        ExecWithCheckOptions((mtstate->operation == CMD_UPDATE ?
> +                             WCO_RLS_UPDATE_CHECK : WCO_RLS_INSERT_CHECK),
>                               resultRelInfo, slot, estate);

Attached is v28 patch which has the fix for this issue as described
above. In ExecUpdate(), if partition constraint fails, we skip
ExecWithCheckOptions (), and later in ExecInsert() it gets called with
WCO_RLS_UPDATE_CHECK.

Added a few test scenarios for the same, in regress/sql/update.sql.

-- 
Thanks,
-Amit Khandekar
EnterpriseDB Corporation
The Postgres Database Company

Commits

  1. Avoid referencing off the end of subplan_partition_offsets.

  2. Allow UPDATE to move rows between partitions.

  3. Remove useless lookup of root partitioned rel in ExecInitModifyTable().

  4. Factor error generation out of ExecPartitionCheck.

  5. Minor preparatory refactoring for UPDATE row movement.

  6. Simplify and encapsulate tuple routing support code.

  7. Avoid coercing a whole-row variable that is already coerced.

  8. Use ResultRelInfo ** rather than ResultRelInfo * for tuple routing.

  9. Make RelationGetPartitionDispatchInfo expand depth-first.

  10. Expand partitioned tables in PartDesc order.

  11. Use a real RT index when setting up partition tuple routing.

  12. Fix transition tables for partition/inheritance.

  13. Fix confusion about number of subplans in partitioned INSERT setup.

  14. Prevent BEFORE triggers from violating partitioning constraints.

  15. Fire per-statement triggers on partitioned tables.

  16. Fix reporting of violations in ExecConstraints, again.

  17. Don't scan partitioned tables.

  18. Allow FDWs to push down quals without breaking EvalPlanQual rechecks.