forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit48a6592
committed
Improve speed of aggregates that use array_append as transition function.
In the previous coding, if an aggregate's transition function returned anexpanded array, nodeAgg.c and nodeWindowAgg.c would always copy it and thusforce it into the flat representation. This led to ping-ponging betweenflat and expanded formats, which costs a lot. For an aggregate usingarray_append as transition function, I measured about a 15X slowdowncompared to the pre-9.5 code, when working on simple int[] arrays.Of course, the old code was already O(N^2) in this usage due to copyingflat arrays all the time, but it wasn't quite this inefficient.To fix, teach nodeAgg.c and nodeWindowAgg.c to allow expanded transitionvalues without copying, so long as the transition function takes care toreturn the transition value already properly parented under the aggcontext.That puts a bit of extra responsibility on the transition function, butdoing it this way allows us to not need any extra logic in the fast pathof advance_transition_function (ie, with a pass-by-value transition value,or with a modified-in-place pass-by-reference value). We already knowthat that's a hot spot so I'm loath to add any cycles at all there. Also,while only array_append currently knows how to follow this convention,this solution allows other transition functions to opt-in without needingto have a whitelist in the core aggregation code.(The reason we would need a whitelist is that currently, if you pass aR/W expanded-object pointer to an arbitrary function, it's allowed to doanything with it including deleting it; that breaks the core agg code'sassumption that it should free discarded values. Returning a value underaggcontext is the transition function's signal that it knows it is anaggregate transition function and will play nice. Possibly the API rulesfor expanded objects should be refined, but that would not be aback-patchable change.)With this fix, an aggregate using array_append is no longer O(N^2), so it'smuch faster than pre-9.5 code rather than much slower. It's still a bitslower than the bespoke infrastructure for array_agg, but the differentialseems to be only about 10%-20% rather than orders of magnitude.Discussion: <6315.1477677885@sss.pgh.pa.us>1 parent4a43a62 commit48a6592
File tree
5 files changed
+142
-33
lines changed- doc/src/sgml
- src/backend
- executor
- optimizer/util
- utils/adt
5 files changed
+142
-33
lines changedLines changed: 15 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
626 | 626 |
| |
627 | 627 |
| |
628 | 628 |
| |
629 |
| - | |
| 629 | + | |
630 | 630 |
| |
631 | 631 |
| |
632 | 632 |
| |
633 | 633 |
| |
634 | 634 |
| |
635 | 635 |
| |
636 | 636 |
| |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
637 | 651 |
| |
638 | 652 |
| |
639 | 653 |
| |
|
Lines changed: 58 additions & 18 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
91 | 91 |
| |
92 | 92 |
| |
93 | 93 |
| |
94 |
| - | |
95 |
| - | |
96 |
| - | |
97 |
| - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
98 | 101 |
| |
99 | 102 |
| |
100 | 103 |
| |
| |||
805 | 808 |
| |
806 | 809 |
| |
807 | 810 |
| |
808 |
| - | |
809 |
| - | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
810 | 815 |
| |
811 | 816 |
| |
812 | 817 |
| |
813 | 818 |
| |
814 | 819 |
| |
815 | 820 |
| |
816 | 821 |
| |
817 |
| - | |
818 |
| - | |
819 |
| - | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
820 | 831 |
| |
821 | 832 |
| |
822 |
| - | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
823 | 841 |
| |
824 | 842 |
| |
825 | 843 |
| |
| |||
1067 | 1085 |
| |
1068 | 1086 |
| |
1069 | 1087 |
| |
1070 |
| - | |
1071 |
| - | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
1072 | 1093 |
| |
1073 | 1094 |
| |
1074 | 1095 |
| |
1075 | 1096 |
| |
1076 | 1097 |
| |
1077 | 1098 |
| |
1078 | 1099 |
| |
1079 |
| - | |
1080 |
| - | |
1081 |
| - | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
1082 | 1109 |
| |
1083 | 1110 |
| |
1084 |
| - | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
1085 | 1119 |
| |
1086 | 1120 |
| |
1087 | 1121 |
| |
| |||
1347 | 1381 |
| |
1348 | 1382 |
| |
1349 | 1383 |
| |
1350 |
| - | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
1351 | 1387 |
| |
1352 | 1388 |
| |
1353 | 1389 |
| |
| |||
1374 | 1410 |
| |
1375 | 1411 |
| |
1376 | 1412 |
| |
| 1413 | + | |
1377 | 1414 |
| |
1378 | 1415 |
| |
1379 | 1416 |
| |
| |||
1424 | 1461 |
| |
1425 | 1462 |
| |
1426 | 1463 |
| |
1427 |
| - | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
1428 | 1467 |
| |
1429 | 1468 |
| |
1430 | 1469 |
| |
| |||
1433 | 1472 |
| |
1434 | 1473 |
| |
1435 | 1474 |
| |
| 1475 | + | |
1436 | 1476 |
| |
1437 | 1477 |
| |
1438 | 1478 |
| |
|
Lines changed: 46 additions & 13 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
362 | 362 |
| |
363 | 363 |
| |
364 | 364 |
| |
365 |
| - | |
366 |
| - | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
367 | 369 |
| |
368 | 370 |
| |
369 | 371 |
| |
370 | 372 |
| |
371 | 373 |
| |
372 | 374 |
| |
373 | 375 |
| |
374 |
| - | |
375 |
| - | |
376 |
| - | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
377 | 385 |
| |
378 | 386 |
| |
379 |
| - | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
380 | 395 |
| |
381 | 396 |
| |
382 | 397 |
| |
| |||
513 | 528 |
| |
514 | 529 |
| |
515 | 530 |
| |
516 |
| - | |
517 |
| - | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
518 | 535 |
| |
519 | 536 |
| |
520 | 537 |
| |
| |||
525 | 542 |
| |
526 | 543 |
| |
527 | 544 |
| |
528 |
| - | |
529 |
| - | |
530 |
| - | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
531 | 554 |
| |
532 | 555 |
| |
533 |
| - | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
534 | 564 |
| |
535 | 565 |
| |
536 | 566 |
| |
| |||
568 | 598 |
| |
569 | 599 |
| |
570 | 600 |
| |
571 |
| - | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
572 | 604 |
| |
573 | 605 |
| |
574 | 606 |
| |
| |||
596 | 628 |
| |
597 | 629 |
| |
598 | 630 |
| |
| 631 | + | |
599 | 632 |
| |
600 | 633 |
| |
601 | 634 |
| |
|
Lines changed: 10 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
646 | 646 |
| |
647 | 647 |
| |
648 | 648 |
| |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
649 | 659 |
| |
650 | 660 |
| |
651 | 661 |
| |
|
Lines changed: 13 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
32 | 32 |
| |
33 | 33 |
| |
34 | 34 |
| |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
35 | 39 |
| |
36 | 40 |
| |
37 | 41 |
| |
38 | 42 |
| |
39 | 43 |
| |
40 | 44 |
| |
41 | 45 |
| |
| 46 | + | |
42 | 47 |
| |
43 | 48 |
| |
44 | 49 |
| |
| |||
51 | 56 |
| |
52 | 57 |
| |
53 | 58 |
| |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
54 | 63 |
| |
55 | 64 |
| |
56 | 65 |
| |
| 66 | + | |
| 67 | + | |
57 | 68 |
| |
| 69 | + | |
58 | 70 |
| |
59 | 71 |
| |
60 | 72 |
| |
| |||
72 | 84 |
| |
73 | 85 |
| |
74 | 86 |
| |
75 |
| - | |
| 87 | + | |
76 | 88 |
| |
77 | 89 |
| |
78 | 90 |
| |
|
0 commit comments
Comments
(0)