- Notifications
You must be signed in to change notification settings - Fork4.9k
Commit525392d
committed
Don't lock partitions pruned by initial pruning
Before executing a cached generic plan, AcquireExecutorLocks() inplancache.c locks all relations in a plan's range table to ensure theplan is safe for execution. However, this locks runtime-prunablerelations that will later be pruned during "initial" runtime pruning,introducing unnecessary overhead.This commit defers locking for such relations to executor startup andensures that if the CachedPlan is invalidated due to concurrent DDLduring this window, replanning is triggered. Deferring these locksavoids unnecessary locking overhead for pruned partitions, resultingin significant speedup, particularly when many partitions are prunedduring initial runtime pruning.* Changes to locking when executing generic plans:AcquireExecutorLocks() now locks only unprunable relations, that is,those found in PlannedStmt.unprunableRelids (introduced in commitcbc1279), to avoid locking runtime-prunable partitionsunnecessarily. The remaining locks are taken byExecDoInitialPruning(), which acquires them only for partitions thatsurvive pruning.This deferral does not affect the locks required for permissionchecking in InitPlan(), which takes place before initial pruning.ExecCheckPermissions() now includes an Assert to verify that allrelations undergoing permission checks, none of which can be in theset of runtime-prunable relations, are properly locked.* Plan invalidation handling:Deferring locks introduces a window where prunable relations may bealtered by concurrent DDL, invalidating the plan. A new function,ExecutorStartCachedPlan(), wraps ExecutorStart() to detect and handleinvalidation caused by deferred locking. If invalidation occurs,ExecutorStartCachedPlan() updates CachedPlan using the newUpdateCachedPlan() function and retries execution with the updatedplan. To ensure all code paths that may be affected by this handleinvalidation properly, all callers of ExecutorStart that may execute aPlannedStmt from a CachedPlan have been updated to useExecutorStartCachedPlan() instead.UpdateCachedPlan() replaces stale plans in CachedPlan.stmt_list. A newCachedPlan.stmt_context, created as a child of CachedPlan.context,allows freeing old PlannedStmts while preserving the CachedPlanstructure and its statement list. This ensures that loops overstatements in upstream callers of ExecutorStartCachedPlan() remainintact.ExecutorStart() and ExecutorStart_hook implementations now return aboolean value indicating whether plan initialization succeeded with avalid PlanState tree in QueryDesc.planstate, or false otherwise, inwhich case QueryDesc.planstate is NULL. Hook implementations arerequired to call standard_ExecutorStart() at the beginning, and if itreturns false, they should do the same without proceeding.* Testing:To verify these changes, the delay_execution module tests scenarioswhere cached plans become invalid due to changes in prunable relationsafter deferred locks.* Note to extension authors:ExecutorStart_hook implementations must verify plan validity aftercalling standard_ExecutorStart(), as explained earlier. For example: if (prev_ExecutorStart) plan_valid = prev_ExecutorStart(queryDesc, eflags); else plan_valid = standard_ExecutorStart(queryDesc, eflags); if (!plan_valid) return false; <extension-code> return true;Extensions accessing child relations, especially prunable partitions,via ExecGetRangeTableRelation() must now ensure their RT indexes arepresent in es_unpruned_relids (introduced in commitcbc1279), orthey will encounter an error. This is a strict requirement after thischange, as only relations in that set are locked.The idea of deferring some locks to executor startup, allowing locksfor prunable partitions to be skipped, was first proposed by Tom Lane.Reviewed-by: Robert Haas <robertmhaas@gmail.com> (earlier versions)Reviewed-by: David Rowley <dgrowleyml@gmail.com> (earlier versions)Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> (earlier versions)Reviewed-by: Tomas Vondra <tomas@vondra.me>Reviewed-by: Junwang Zhao <zhjwpku@gmail.com>Discussion:https://postgr.es/m/CA+HiwqFGkMSge6TgC9KQzde0ohpAycLQuV7ooitEEpbKB0O_mg@mail.gmail.com1 parent4aa6fa3 commit525392d
File tree
33 files changed
+1014
-95
lines changed- contrib
- auto_explain
- pg_stat_statements
- src
- backend
- commands
- executor
- tcop
- utils
- cache
- mmgr
- include
- commands
- executor
- nodes
- utils
- test/modules/delay_execution
- expected
- specs
33 files changed
+1014
-95
lines changedLines changed: 12 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
76 | 76 |
| |
77 | 77 |
| |
78 | 78 |
| |
79 |
| - | |
| 79 | + | |
80 | 80 |
| |
81 | 81 |
| |
82 | 82 |
| |
| |||
256 | 256 |
| |
257 | 257 |
| |
258 | 258 |
| |
259 |
| - | |
| 259 | + | |
260 | 260 |
| |
261 | 261 |
| |
| 262 | + | |
| 263 | + | |
262 | 264 |
| |
263 | 265 |
| |
264 | 266 |
| |
| |||
294 | 296 |
| |
295 | 297 |
| |
296 | 298 |
| |
297 |
| - | |
| 299 | + | |
298 | 300 |
| |
299 |
| - | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
300 | 306 |
| |
301 | 307 |
| |
302 | 308 |
| |
| |||
314 | 320 |
| |
315 | 321 |
| |
316 | 322 |
| |
| 323 | + | |
| 324 | + | |
317 | 325 |
| |
318 | 326 |
| |
319 | 327 |
| |
|
Lines changed: 12 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
333 | 333 |
| |
334 | 334 |
| |
335 | 335 |
| |
336 |
| - | |
| 336 | + | |
337 | 337 |
| |
338 | 338 |
| |
339 | 339 |
| |
| |||
987 | 987 |
| |
988 | 988 |
| |
989 | 989 |
| |
990 |
| - | |
| 990 | + | |
991 | 991 |
| |
992 | 992 |
| |
| 993 | + | |
| 994 | + | |
993 | 995 |
| |
994 |
| - | |
| 996 | + | |
995 | 997 |
| |
996 |
| - | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
997 | 1003 |
| |
998 | 1004 |
| |
999 | 1005 |
| |
| |||
1016 | 1022 |
| |
1017 | 1023 |
| |
1018 | 1024 |
| |
| 1025 | + | |
| 1026 | + | |
1019 | 1027 |
| |
1020 | 1028 |
| |
1021 | 1029 |
| |
|
Lines changed: 3 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
556 | 556 |
| |
557 | 557 |
| |
558 | 558 |
| |
559 |
| - | |
| 559 | + | |
560 | 560 |
| |
561 | 561 |
| |
562 | 562 |
| |
| |||
566 | 566 |
| |
567 | 567 |
| |
568 | 568 |
| |
569 |
| - | |
| 569 | + | |
| 570 | + | |
570 | 571 |
| |
571 | 572 |
| |
572 | 573 |
| |
|
Lines changed: 3 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
332 | 332 |
| |
333 | 333 |
| |
334 | 334 |
| |
335 |
| - | |
| 335 | + | |
336 | 336 |
| |
337 | 337 |
| |
338 | 338 |
| |
339 | 339 |
| |
340 |
| - | |
| 340 | + | |
| 341 | + | |
341 | 342 |
| |
342 | 343 |
| |
343 | 344 |
| |
|
Lines changed: 17 additions & 5 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
519 | 519 |
| |
520 | 520 |
| |
521 | 521 |
| |
522 |
| - | |
| 522 | + | |
| 523 | + | |
523 | 524 |
| |
524 | 525 |
| |
525 | 526 |
| |
| |||
641 | 642 |
| |
642 | 643 |
| |
643 | 644 |
| |
644 |
| - | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
645 | 648 |
| |
646 | 649 |
| |
647 | 650 |
| |
| |||
697 | 700 |
| |
698 | 701 |
| |
699 | 702 |
| |
700 |
| - | |
| 703 | + | |
701 | 704 |
| |
702 | 705 |
| |
703 | 706 |
| |
| |||
711 | 714 |
| |
712 | 715 |
| |
713 | 716 |
| |
714 |
| - | |
715 |
| - | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
716 | 728 |
| |
717 | 729 |
| |
718 | 730 |
| |
|
Lines changed: 3 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
907 | 907 |
| |
908 | 908 |
| |
909 | 909 |
| |
| 910 | + | |
910 | 911 |
| |
911 | 912 |
| |
912 | 913 |
| |
913 | 914 |
| |
914 |
| - | |
| 915 | + | |
| 916 | + | |
915 | 917 |
| |
916 | 918 |
| |
917 | 919 |
| |
|
Lines changed: 3 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
438 | 438 |
| |
439 | 439 |
| |
440 | 440 |
| |
441 |
| - | |
| 441 | + | |
442 | 442 |
| |
443 | 443 |
| |
444 | 444 |
| |
445 | 445 |
| |
446 |
| - | |
| 446 | + | |
| 447 | + | |
447 | 448 |
| |
448 | 449 |
| |
449 | 450 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
117 | 117 |
| |
118 | 118 |
| |
119 | 119 |
| |
| 120 | + | |
120 | 121 |
| |
121 | 122 |
| |
122 | 123 |
| |
|
Lines changed: 7 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
202 | 202 |
| |
203 | 203 |
| |
204 | 204 |
| |
205 |
| - | |
| 205 | + | |
| 206 | + | |
206 | 207 |
| |
207 | 208 |
| |
208 | 209 |
| |
| |||
582 | 583 |
| |
583 | 584 |
| |
584 | 585 |
| |
| 586 | + | |
585 | 587 |
| |
586 | 588 |
| |
587 | 589 |
| |
| |||
654 | 656 |
| |
655 | 657 |
| |
656 | 658 |
| |
657 |
| - | |
| 659 | + | |
| 660 | + | |
658 | 661 |
| |
659 | 662 |
| |
660 | 663 |
| |
| |||
665 | 668 |
| |
666 | 669 |
| |
667 | 670 |
| |
| 671 | + | |
| 672 | + | |
668 | 673 |
| |
669 | 674 |
| |
670 | 675 |
| |
|
Lines changed: 15 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
5057 | 5057 |
| |
5058 | 5058 |
| |
5059 | 5059 |
| |
| 5060 | + | |
| 5061 | + | |
| 5062 | + | |
| 5063 | + | |
| 5064 | + | |
| 5065 | + | |
| 5066 | + | |
| 5067 | + | |
| 5068 | + | |
| 5069 | + | |
| 5070 | + | |
| 5071 | + | |
| 5072 | + | |
| 5073 | + | |
| 5074 | + | |
5060 | 5075 |
| |
5061 | 5076 |
| |
5062 | 5077 |
| |
|
Lines changed: 32 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
280 | 280 |
| |
281 | 281 |
| |
282 | 282 |
| |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
283 | 305 |
| |
284 | 306 |
| |
285 | 307 |
| |
| |||
288 | 310 |
| |
289 | 311 |
| |
290 | 312 |
| |
291 |
| - | |
| 313 | + | |
292 | 314 |
| |
293 | 315 |
| |
294 |
| - | |
| 316 | + | |
295 | 317 |
| |
| 318 | + | |
| 319 | + | |
296 | 320 |
| |
297 | 321 |
| |
298 | 322 |
| |
| |||
316 | 340 |
| |
317 | 341 |
| |
318 | 342 |
| |
319 |
| - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
320 | 349 |
| |
321 | 350 |
| |
322 | 351 |
| |
|
0 commit comments
Comments
(0)