Re: UPDATE of partition key
Amit Khandekar <amitdkhan.pg@gmail.com>
On 28 July 2017 at 20:10, Robert Haas <robertmhaas@gmail.com> wrote: > On Wed, Jul 26, 2017 at 2:13 AM, Amit Langote > <Langote_Amit_f8@lab.ntt.co.jp> wrote: >> Sorry to be responding this late to the Amit's make_resultrel_ordered >> patch itself, but I agree that we should teach the planner to *always* >> expand partitioned tables in the partition bound order. > > Sounds like we have unanimous agreement on that point. I too agree. > >> I checked that we get the same result relation order with both the >> patches, but I would like to highlight a notable difference here between >> the approaches taken by our patches. In my patch, I have now taught >> RelationGetPartitionDispatchInfo() to lock *only* the partitioned tables >> in the tree, because we need to look at its partition descriptor to >> collect partition OIDs and bounds. We can defer locking (and opening the >> relation descriptor of) leaf partitions to a point where planner has >> determined that the partition will be accessed after all (not pruned), >> which will be done in a separate patch of course. With Amit Langote's patch, we can very well do the locking beforehand by find_all_inheritors(), and then run RelationGetPartitionDispatchInfo() with noLock, so as to remove the deadlock problem. But I think we should keep these two tasks separate, i.e. expanding the partition tree in bound order, and making RelationGetPartitionDispatchInfo() work for the planner. Regarding building the PartitionDispatchInfo in the planner, we should do that only after it is known that partition columns are updated, so it can't be done in expand_inherited_rtentry() because it would be too soon. For planner setup, RelationGetPartitionDispatchInfo() should just build the tupmap for each partitioned table, and then initialize the rest of the fields like tuplslot, reldesc , etc later during execution. So for now, I feel we should just do the changes for making sure the order is same, and then over that, separately modify RelationGetPartitionDispatchInfo() for planner. > > That's very desirable, but I believe it introduces a deadlock risk > which Amit's patch avoids. A transaction using the code you've > written here is eventually going to lock all partitions, BUT it's > going to move the partitioned ones to the front of the locking order > vs. what find_all_inheritors would do. So, when multi-level > partitioning is in use, I think it could happen that some other > transaction is accessing the table using a different code path that > uses the find_all_inheritors order without modification. If those > locks conflict (e.g. query vs. DROP) then there's a deadlock risk. Yes, I agree. Even with single-level partitioning, the leaf partitions ordered by find_all_inheritors() is by oid values, so that's also going to be differently ordered. > > Unfortunately I don't see any easy way around that problem, but maybe > somebody else has an idea. One approach I had considered was to have find_inheritance_children() itself lock the children in bound order, so that everyone will have bound-ordered oids, but that would be too expensive since it requires opening all partitioned tables to initialize partition descriptors. In find_inheritance_children(), we get all oids without opening any tables. But now that I think more of it, it's only the partitioned tables that we have to open, not the leaf partitions; and furthermore, I didn't see calls to find_inheritance_children() and find_all_inheritors() in performance-critical code, except in expand_inherited_rtentry(). All of them are in DDL commands; but yes, that can change in the future. Regarding dynamically locking specific partitions as and when needed, I think this method inherently has the issue of deadlock because the order would be random. So it feels like there is no way around other than to lock all partitions beforehand. ---------------- Regarding using first resultrel for mapping RETURNING and WCO, I think we can use (a renamed) getASTriggerResultRelInfo() to get the root result relation, and use WCO and RETURNING expressions of this relation to do the mapping for child rels. This way, there won't be insert/update specific code, and we don't need to use first result relation. While checking the whole-row bug on the other thread [1] , I noticed that the RETURNING/WCO expressions for the per-subplan result rels are formed by considering not just simple vars, but also whole row vars and other nodes. So for update-tuple-routing, there would be some result-rels WCOs formed using adjust_appendrel_attrs(), while for others, they would be built using map_partition_varattnos() which only considers simple vars. So the bug in [1] would be there for update-partition-key as well, when the tuple is routed into a newly built resultrel. May be, while fixing the bug in [1] , this might be automatically solved. ---------------- Below are the TODOS at this point : Fix for bug reported by Rajkumar about update with join. Do something about two separate mapping tables for Transition tables and update tuple-routing. GetUpdatedColumns() to be moved to header file. More test scenarios in regression tests. Need to check/test whether we are correctly applying insert policies (ant not update) while inserting a routed tuple. Use getASTriggerResultRelInfo() for attrno mapping, rather than first resultrel, for generating child WCO/RETURNING expression. Address Robert's review comments on make_resultrel_ordered.patch. pgindent. [1] https://www.postgresql.org/message-id/d86d27ea-cc9d-5dbe-b131-e7dec4017983%40lab.ntt.co.jp Thanks, -Amit Khandekar EnterpriseDB Corporation The Postgres Database Company
Commits
-
Avoid referencing off the end of subplan_partition_offsets.
- 945f71db8452 11.0 landed
-
Allow UPDATE to move rows between partitions.
- 2f178441044b 11.0 landed
-
Remove useless lookup of root partitioned rel in ExecInitModifyTable().
- dca48d145e0e 11.0 cited
-
Factor error generation out of ExecPartitionCheck.
- 19c47e7c8202 11.0 landed
-
Minor preparatory refactoring for UPDATE row movement.
- ef6087ee5fa8 11.0 landed
-
Simplify and encapsulate tuple routing support code.
- cc6337d2fed5 11.0 landed
-
Avoid coercing a whole-row variable that is already coerced.
- 1c497fa72df7 11.0 landed
-
Use ResultRelInfo ** rather than ResultRelInfo * for tuple routing.
- 60f7c0abef03 11.0 landed
-
Make RelationGetPartitionDispatchInfo expand depth-first.
- 77b6b5e9ceca 11.0 cited
-
Expand partitioned tables in PartDesc order.
- 30833ba154e0 11.0 cited
-
Use a real RT index when setting up partition tuple routing.
- f81a91db4d1c 10.0 cited
-
Fix transition tables for partition/inheritance.
- 501ed02cf6f4 10.0 cited
-
Fix confusion about number of subplans in partitioned INSERT setup.
- 78a030a44196 10.0 cited
-
Prevent BEFORE triggers from violating partitioning constraints.
- 15ce775faa42 10.0 cited
-
Fire per-statement triggers on partitioned tables.
- e180c8aa8caf 10.0 cited
-
Fix reporting of violations in ExecConstraints, again.
- c0a8ae7be392 10.0 cited
-
Don't scan partitioned tables.
- d3cc37f1d801 10.0 cited
-
Allow FDWs to push down quals without breaking EvalPlanQual rechecks.
- 5fc4c26db512 9.6.0 cited