88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.99 2006/10/04 00:29:50 momjian Exp $
11+ * $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.100 2006/10/05 17:57:40 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -1294,11 +1294,12 @@ typedef struct
12941294int first ;/* values[] index of first occurrence */
12951295}ScalarMCVItem ;
12961296
1297-
1298- /* context information for compare_scalars() */
1299- static FmgrInfo * datumCmpFn ;
1300- static SortFunctionKind datumCmpFnKind ;
1301- static int * datumCmpTupnoLink ;
1297+ typedef struct
1298+ {
1299+ FmgrInfo * cmpFn ;
1300+ SortFunctionKind cmpFnKind ;
1301+ int * tupnoLink ;
1302+ }CompareScalarsContext ;
13021303
13031304
13041305static void compute_minimal_stats (VacAttrStatsP stats ,
@@ -1309,7 +1310,7 @@ static void compute_scalar_stats(VacAttrStatsP stats,
13091310AnalyzeAttrFetchFunc fetchfunc ,
13101311int samplerows ,
13111312double totalrows );
1312- static int compare_scalars (const void * a ,const void * b );
1313+ static int compare_scalars (const void * a ,const void * b , void * arg );
13131314static int compare_mcvs (const void * a ,const void * b );
13141315
13151316
@@ -1828,13 +1829,14 @@ compute_scalar_stats(VacAttrStatsP stats,
18281829num_hist ,
18291830dups_cnt ;
18301831int slot_idx = 0 ;
1832+ CompareScalarsContext cxt ;
18311833
18321834/* Sort the collected values */
1833- datumCmpFn = & f_cmpfn ;
1834- datumCmpFnKind = cmpFnKind ;
1835- datumCmpTupnoLink = tupnoLink ;
1836- qsort ((void * )values ,values_cnt ,
1837- sizeof ( ScalarItem ), compare_scalars );
1835+ cxt . cmpFn = & f_cmpfn ;
1836+ cxt . cmpFnKind = cmpFnKind ;
1837+ cxt . tupnoLink = tupnoLink ;
1838+ qsort_arg ((void * )values ,values_cnt , sizeof ( ScalarItem ) ,
1839+ compare_scalars , ( void * ) & cxt );
18381840
18391841/*
18401842 * Now scan the values in order, find the most common ones, and also
@@ -2183,35 +2185,36 @@ compute_scalar_stats(VacAttrStatsP stats,
21832185}
21842186
21852187/*
2186- *qsort comparator for sorting ScalarItems
2188+ *qsort_arg comparator for sorting ScalarItems
21872189 *
2188- * Aside from sorting the items, we update thedatumCmpTupnoLink [] array
2190+ * Aside from sorting the items, we update thetupnoLink [] array
21892191 * whenever two ScalarItems are found to contain equal datums.The array
21902192 * is indexed by tupno; for each ScalarItem, it contains the highest
21912193 * tupno that that item's datum has been found to be equal to. This allows
21922194 * us to avoid additional comparisons in compute_scalar_stats().
21932195 */
21942196static int
2195- compare_scalars (const void * a ,const void * b )
2197+ compare_scalars (const void * a ,const void * b , void * arg )
21962198{
21972199Datum da = ((ScalarItem * )a )-> value ;
21982200int ta = ((ScalarItem * )a )-> tupno ;
21992201Datum db = ((ScalarItem * )b )-> value ;
22002202int tb = ((ScalarItem * )b )-> tupno ;
2203+ CompareScalarsContext * cxt = (CompareScalarsContext * )arg ;
22012204int32 compare ;
22022205
2203- compare = ApplySortFunction (datumCmpFn , datumCmpFnKind ,
2206+ compare = ApplySortFunction (cxt -> cmpFn , cxt -> cmpFnKind ,
22042207da , false,db , false);
22052208if (compare != 0 )
22062209return compare ;
22072210
22082211/*
2209- * The two datums are equal, so updatedatumCmpTupnoLink [].
2212+ * The two datums are equal, so updatecxt->tupnoLink [].
22102213 */
2211- if (datumCmpTupnoLink [ta ]< tb )
2212- datumCmpTupnoLink [ta ]= tb ;
2213- if (datumCmpTupnoLink [tb ]< ta )
2214- datumCmpTupnoLink [tb ]= ta ;
2214+ if (cxt -> tupnoLink [ta ]< tb )
2215+ cxt -> tupnoLink [ta ]= tb ;
2216+ if (cxt -> tupnoLink [tb ]< ta )
2217+ cxt -> tupnoLink [tb ]= ta ;
22152218
22162219/*
22172220 * For equal datums, sort by tupno