@@ -106,6 +106,7 @@ static void explain_ExecutorRun(QueryDesc *queryDesc,
106106static void explain_ExecutorFinish (QueryDesc * queryDesc );
107107static void explain_ExecutorEnd (QueryDesc * queryDesc );
108108
109+ static void AddMultiColumnStatisticsForNode (PlanState * planstate , ExplainState * es );
109110
110111/*
111112 * Module load callback
@@ -395,9 +396,9 @@ explain_ExecutorFinish(QueryDesc *queryDesc)
395396 PG_END_TRY ();
396397}
397398
398- static void
399- AddMultiColumnStatisticsForNode ( PlanState * planstate , ExplainState * es );
400-
399+ /**
400+ * Try to add multicolumn statistics for specified subplans.
401+ */
401402static void
402403AddMultiColumnStatisticsForSubPlans (List * plans , ExplainState * es )
403404{
@@ -411,16 +412,22 @@ AddMultiColumnStatisticsForSubPlans(List *plans, ExplainState *es)
411412 }
412413}
413414
415+ /**
416+ * Try to add multicolumn statistics for plan subnodes.
417+ */
414418static void
415419AddMultiColumnStatisticsForMemberNodes (PlanState * * planstates , int nsubnodes ,
416- ExplainState * es )
420+ ExplainState * es )
417421{
418422 int j ;
419423
420424 for (j = 0 ; j < nsubnodes ; j ++ )
421425 AddMultiColumnStatisticsForNode (planstates [j ], es );
422426}
423427
428+ /**
429+ * Comparator used to sort Vars by name
430+ */
424431static int
425432vars_list_comparator (const ListCell * a , const ListCell * b )
426433{
@@ -429,26 +436,33 @@ vars_list_comparator(const ListCell *a, const ListCell *b)
429436 return strcmp (va , vb );
430437}
431438
439+ /**
440+ * Try to add multicolumn statistics for qual
441+ */
432442static void
433443AddMultiColumnStatisticsForQual (void * qual , ExplainState * es )
434444{
435445 List * vars = NULL ;
436446 ListCell * lc ;
437447
448+ /* Extract vars from all quals */
438449 foreach (lc , qual )
439450 {
440451 Node * node = (Node * )lfirst (lc );
441452 if (IsA (node , RestrictInfo ))
442453 node = (Node * )((RestrictInfo * )node )-> clause ;
443454 vars = list_concat (vars , pull_vars_of_level (node , 0 ));
444455 }
456+
457+ /* Loop until we considered all vars */
445458 while (vars != NULL )
446459 {
447460 ListCell * cell ;
448461 List * cols = NULL ;
449462 Index varno = 0 ;
450463 Bitmapset * colmap = NULL ;
451464
465+ /* Contruct list of unique vars */
452466 foreach (cell , vars )
453467 {
454468 Node * node = (Node * ) lfirst (cell );
@@ -460,7 +474,7 @@ AddMultiColumnStatisticsForQual(void* qual, ExplainState *es)
460474 varno = var -> varno ;
461475 if (var -> varattno > 0 &&
462476 !bms_is_member (var -> varattno , colmap ) &&
463- varno >= 1 &&
477+ varno >= 1 && /* not synthetic var */
464478 varno <= list_length (es -> rtable ) &&
465479 list_length (cols ) < STATS_MAX_DIMENSIONS )
466480 {
@@ -482,6 +496,7 @@ AddMultiColumnStatisticsForQual(void* qual, ExplainState *es)
482496 }
483497 vars = foreach_delete_current (vars , cell );
484498 }
499+ /* To create multicolumn statitics we need to have at least 2 columns */
485500 if (list_length (cols ) >= 2 )
486501 {
487502 RangeTblEntry * rte = rt_fetch (varno , es -> rtable );
@@ -498,7 +513,9 @@ AddMultiColumnStatisticsForQual(void* qual, ExplainState *es)
498513 size_t name_len ;
499514 TupleTableSlot * slot ;
500515
516+ /* Sort variables by name */
501517 list_sort (cols , vars_list_comparator );
518+
502519 /* Construct name for statistic by concatenating relation name with all columns */
503520 foreach (cell , cols )
504521 {
@@ -509,6 +526,7 @@ AddMultiColumnStatisticsForQual(void* qual, ExplainState *es)
509526 }
510527
511528 name_len = strlen (stat_name );
529+ /* Truncate name if it doesn't fit in NameData */
512530 if (name_len >= NAMEDATALEN )
513531 stat_name = psprintf ("%.*s_%08x" , NAMEDATALEN - 10 , stat_name , (unsigned )hash_any ((uint8 * )stat_name , name_len ));
514532
@@ -556,6 +574,9 @@ AddMultiColumnStatisticsForQual(void* qual, ExplainState *es)
556574 }
557575}
558576
577+ /**
578+ * Try to add multicolumn statistics for node
579+ */
559580static void
560581AddMultiColumnStatisticsForNode (PlanState * planstate , ExplainState * es )
561582{
0 commit comments