]> granicus.if.org Git - postgresql/blob - src/backend/tsearch/ts_typanalyze.c
Update copyright for 2009.
[postgresql] / src / backend / tsearch / ts_typanalyze.c
1 /*-------------------------------------------------------------------------
2  *
3  * ts_typanalyze.c
4  *        functions for gathering statistics from tsvector columns
5  *
6  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  *        $PostgreSQL: pgsql/src/backend/tsearch/ts_typanalyze.c,v 1.6 2009/01/01 17:23:48 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15
16 #include "access/hash.h"
17 #include "catalog/pg_operator.h"
18 #include "commands/vacuum.h"
19 #include "tsearch/ts_type.h"
20 #include "utils/builtins.h"
21 #include "utils/hsearch.h"
22
23
24 /* A hash key for lexemes */
25 typedef struct
26 {
27         char       *lexeme;                     /* lexeme (not NULL terminated!) */
28         int                     length;                 /* its length in bytes */
29 } LexemeHashKey;
30
31 /* A hash table entry for the Lossy Counting algorithm */
32 typedef struct
33 {
34         LexemeHashKey   key;            /* This is 'e' from the LC algorithm. */
35         int                             frequency;      /* This is 'f'. */
36         int                             delta;          /* And this is 'delta'. */
37 } TrackItem;
38
39 static void compute_tsvector_stats(VacAttrStats *stats,
40                                                                    AnalyzeAttrFetchFunc fetchfunc,
41                                                                    int samplerows,
42                                                                    double totalrows);
43 static void prune_lexemes_hashtable(HTAB *lexemes_tab, int b_current);
44 static uint32 lexeme_hash(const void *key, Size keysize);
45 static int lexeme_match(const void *key1, const void *key2, Size keysize);
46 static int lexeme_compare(const void *key1, const void *key2);
47 static int trackitem_compare_frequencies_desc(const void *e1, const void *e2);
48 static int trackitem_compare_lexemes(const void *e1, const void *e2);
49
50
51 /*
52  *      ts_typanalyze -- a custom typanalyze function for tsvector columns
53  */
54 Datum
55 ts_typanalyze(PG_FUNCTION_ARGS)
56 {
57         VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0);
58         Form_pg_attribute attr = stats->attr;
59
60         /* If the attstattarget column is negative, use the default value */
61         /* NB: it is okay to scribble on stats->attr since it's a copy */
62         if (attr->attstattarget < 0)
63                 attr->attstattarget = default_statistics_target;
64
65         stats->compute_stats = compute_tsvector_stats;
66         /* see comment about the choice of minrows in commands/analyze.c */
67         stats->minrows = 300 * attr->attstattarget;
68
69         PG_RETURN_BOOL(true);
70 }
71
72 /*
73  *      compute_tsvector_stats() -- compute statistics for a tsvector column
74  *
75  *      This functions computes statistics that are useful for determining @@
76  *      operations' selectivity, along with the fraction of non-null rows and
77  *      average width.
78  *
79  *      Instead of finding the most common values, as we do for most datatypes,
80  *      we're looking for the most common lexemes. This is more useful, because
81  *      there most probably won't be any two rows with the same tsvector and thus
82  *      the notion of a MCV is a bit bogus with this datatype. With a list of the
83  *      most common lexemes we can do a better job at figuring out @@ selectivity.
84  *
85  *      For the same reasons we assume that tsvector columns are unique when
86  *      determining the number of distinct values.
87  *
88  *      The algorithm used is Lossy Counting, as proposed in the paper "Approximate
89  *      frequency counts over data streams" by G. S. Manku and R. Motwani, in
90  *      Proceedings of the 28th International Conference on Very Large Data Bases,
91  *      Hong Kong, China, August 2002, section 4.2. The paper is available at
92  *      http://www.vldb.org/conf/2002/S10P03.pdf
93  *
94  *      The Lossy Counting (aka LC) algorithm goes like this:
95  *      Let D be a set of triples (e, f, d), where e is an element value, f is
96  *      that element's frequency (occurrence count) and d is the maximum error in
97  *      f.  We start with D empty and process the elements in batches of size
98  *      w. (The batch size is also known as "bucket size".) Let the current batch
99  *      number be b_current, starting with 1. For each element e we either
100  *      increment its f count, if it's already in D, or insert a new triple into D
101  *      with values (e, 1, b_current - 1). After processing each batch we prune D,
102  *      by removing from it all elements with f + d <= b_current. Finally, we
103  *      gather elements with largest f.  The LC paper proves error bounds on f
104  *      dependent on the batch size w, and shows that the required table size
105  *      is no more than a few times w.
106  *
107  *      We use a hashtable for the D structure and a bucket width of
108  *      statistics_target * 10, where 10 is an arbitrarily chosen constant,
109  *      meant to approximate the number of lexemes in a single tsvector.
110  */
111 static void
112 compute_tsvector_stats(VacAttrStats *stats,
113                                            AnalyzeAttrFetchFunc fetchfunc,
114                                            int samplerows,
115                                            double totalrows)
116 {
117         int                             num_mcelem;
118         int                             null_cnt = 0;
119         double                  total_width = 0;
120         /* This is D from the LC algorithm. */
121         HTAB                    *lexemes_tab;
122         HASHCTL                 hash_ctl;
123         HASH_SEQ_STATUS scan_status;
124         /* This is the current bucket number from the LC algorithm */
125         int                             b_current;
126         /* This is 'w' from the LC algorithm */
127         int                             bucket_width;
128         int vector_no,
129                 lexeme_no;
130         LexemeHashKey   hash_key;
131         TrackItem               *item;
132
133         /* We want statistics_target * 10 lexemes in the MCELEM array */
134         num_mcelem = stats->attr->attstattarget * 10;
135
136         /*
137          * We set bucket width equal to the target number of result lexemes.
138          * This is probably about right but perhaps might need to be scaled
139          * up or down a bit?
140          */
141         bucket_width = num_mcelem;
142
143         /*
144          * Create the hashtable. It will be in local memory, so we don't need to
145          * worry about initial size too much. Also we don't need to pay any
146          * attention to locking and memory management.
147          */
148         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
149         hash_ctl.keysize = sizeof(LexemeHashKey);
150         hash_ctl.entrysize = sizeof(TrackItem);
151         hash_ctl.hash = lexeme_hash;
152         hash_ctl.match = lexeme_match;
153         hash_ctl.hcxt = CurrentMemoryContext;
154         lexemes_tab = hash_create("Analyzed lexemes table",
155                                                           bucket_width * 4,
156                                                           &hash_ctl,
157                                                           HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);
158
159         /* Initialize counters. */
160         b_current = 1;
161         lexeme_no = 1;
162
163         /* Loop over the tsvectors. */
164         for (vector_no = 0; vector_no < samplerows; vector_no++)
165         {
166                 Datum           value;
167                 bool            isnull;
168                 TSVector        vector;
169                 WordEntry       *curentryptr;
170                 char            *lexemesptr;
171                 int                     j;
172
173                 vacuum_delay_point();
174
175                 value = fetchfunc(stats, vector_no, &isnull);
176
177                 /*
178                  * Check for null/nonnull.
179                  */
180                 if (isnull)
181                 {
182                         null_cnt++;
183                         continue;
184                 }
185
186                 /*
187                  * Add up widths for average-width calculation.  Since it's a
188                  * tsvector, we know it's varlena.  As in the regular
189                  * compute_minimal_stats function, we use the toasted width for this
190                  * calculation.
191                  */
192                 total_width += VARSIZE_ANY(DatumGetPointer(value));
193
194                 /*
195                  * Now detoast the tsvector if needed.
196                  */
197                 vector = DatumGetTSVector(value);
198
199                 /*
200                  * We loop through the lexemes in the tsvector and add them to our
201                  * tracking hashtable.  Note: the hashtable entries will point into
202                  * the (detoasted) tsvector value, therefore we cannot free that
203                  * storage until we're done.
204                  */
205                 lexemesptr = STRPTR(vector);
206                 curentryptr = ARRPTR(vector);
207                 for (j = 0; j < vector->size; j++)
208                 {
209                         bool                    found;
210
211                         /* Construct a hash key */
212                         hash_key.lexeme = lexemesptr + curentryptr->pos;
213                         hash_key.length = curentryptr->len;
214
215                         /* Lookup current lexeme in hashtable, adding it if new */
216                         item = (TrackItem *) hash_search(lexemes_tab,
217                                                                                          (const void *) &hash_key,
218                                                                                          HASH_ENTER, &found);
219
220                         if (found)
221                         {
222                                 /* The lexeme is already on the tracking list */
223                                 item->frequency++;
224                         }
225                         else
226                         {
227                                 /* Initialize new tracking list element */
228                                 item->frequency = 1;
229                                 item->delta = b_current - 1;
230                         }
231
232                         /* We prune the D structure after processing each bucket */
233                         if (lexeme_no % bucket_width == 0)
234                         {
235                                 prune_lexemes_hashtable(lexemes_tab, b_current);
236                                 b_current++;
237                         }
238
239                         /* Advance to the next WordEntry in the tsvector */
240                         lexeme_no++;
241                         curentryptr++;
242                 }
243         }
244
245         /* We can only compute real stats if we found some non-null values. */
246         if (null_cnt < samplerows)
247         {
248                 int                     nonnull_cnt = samplerows - null_cnt;
249                 int                     i;
250                 TrackItem       **sort_table;
251                 int                     track_len;
252                 int                     minfreq, maxfreq;
253
254                 stats->stats_valid = true;
255                 /* Do the simple null-frac and average width stats */
256                 stats->stanullfrac = (double) null_cnt / (double) samplerows;
257                 stats->stawidth = total_width / (double) nonnull_cnt;
258
259                 /* Assume it's a unique column (see notes above) */
260                 stats->stadistinct = -1.0;
261
262                 /*
263                  * Determine the top-N lexemes by simply copying pointers from the
264                  * hashtable into an array and applying qsort()
265                  */
266                 track_len = hash_get_num_entries(lexemes_tab);
267
268                 sort_table = (TrackItem **) palloc(sizeof(TrackItem *) * track_len);
269
270                 hash_seq_init(&scan_status, lexemes_tab);
271                 i = 0;
272                 while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL)
273                 {
274                         sort_table[i++] = item;
275                 }
276                 Assert(i == track_len);
277
278                 qsort(sort_table, track_len, sizeof(TrackItem *),
279                           trackitem_compare_frequencies_desc);
280
281                 /* Suppress any single-occurrence items */
282                 while (track_len > 0)
283                 {
284                         if (sort_table[track_len-1]->frequency > 1)
285                                 break;
286                         track_len--;
287                 }
288
289                 /* Determine the number of most common lexemes to be stored */
290                 if (num_mcelem > track_len)
291                         num_mcelem = track_len;
292
293                 /* Generate MCELEM slot entry */
294                 if (num_mcelem > 0)
295                 {
296                         MemoryContext   old_context;
297                         Datum                   *mcelem_values;
298                         float4                  *mcelem_freqs;
299
300                         /* Grab the minimal and maximal frequencies that will get stored */
301                         minfreq = sort_table[num_mcelem - 1]->frequency;
302                         maxfreq = sort_table[0]->frequency;
303
304                         /*
305                          * We want to store statistics sorted on the lexeme value using
306                          * first length, then byte-for-byte comparison. The reason for
307                          * doing length comparison first is that we don't care about the
308                          * ordering so long as it's consistent, and comparing lengths first
309                          * gives us a chance to avoid a strncmp() call.
310                          *
311                          * This is different from what we do with scalar statistics -- they
312                          * get sorted on frequencies. The rationale is that we usually
313                          * search through most common elements looking for a specific
314                          * value, so we can grab its frequency.  When values are presorted
315                          * we can employ binary search for that.  See ts_selfuncs.c for a
316                          * real usage scenario.
317                          */
318                         qsort(sort_table, num_mcelem, sizeof(TrackItem *),
319                                   trackitem_compare_lexemes);
320
321                         /* Must copy the target values into anl_context */
322                         old_context = MemoryContextSwitchTo(stats->anl_context);
323
324                         /*
325                          * We sorted statistics on the lexeme value, but we want to be
326                          * able to find out the minimal and maximal frequency without
327                          * going through all the values.  We keep those two extra
328                          * frequencies in two extra cells in mcelem_freqs.
329                          */
330                         mcelem_values = (Datum *) palloc(num_mcelem * sizeof(Datum));
331                         mcelem_freqs = (float4 *) palloc((num_mcelem + 2) * sizeof(float4));
332
333                         for (i = 0; i < num_mcelem; i++)
334                         {
335                                 TrackItem *item = sort_table[i];
336
337                                 mcelem_values[i] =
338                                         PointerGetDatum(cstring_to_text_with_len(item->key.lexeme,
339                                                                                                                          item->key.length));
340                                 mcelem_freqs[i] = (double) item->frequency / (double) nonnull_cnt;
341                         }
342                         mcelem_freqs[i++] = (double) minfreq / (double) nonnull_cnt;
343                         mcelem_freqs[i] = (double) maxfreq / (double) nonnull_cnt;
344                         MemoryContextSwitchTo(old_context);
345
346                         stats->stakind[0] = STATISTIC_KIND_MCELEM;
347                         stats->staop[0] = TextEqualOperator;
348                         stats->stanumbers[0] = mcelem_freqs;
349                         /* See above comment about two extra frequency fields */
350                         stats->numnumbers[0] = num_mcelem + 2;
351                         stats->stavalues[0] = mcelem_values;
352                         stats->numvalues[0] = num_mcelem;
353                         /* We are storing text values */
354                         stats->statypid[0] = TEXTOID;
355                         stats->statyplen[0] = -1; /* typlen, -1 for varlena */
356                         stats->statypbyval[0] = false;
357                         stats->statypalign[0] = 'i';
358                 }
359         }
360         else
361         {
362                 /* We found only nulls; assume the column is entirely null */
363                 stats->stats_valid = true;
364                 stats->stanullfrac = 1.0;
365                 stats->stawidth = 0;            /* "unknown" */
366                 stats->stadistinct = 0.0;       /* "unknown" */
367         }
368
369         /*
370          * We don't need to bother cleaning up any of our temporary palloc's.
371          * The hashtable should also go away, as it used a child memory context.
372          */
373 }
374
375 /*
376  *      A function to prune the D structure from the Lossy Counting algorithm.
377  *      Consult compute_tsvector_stats() for wider explanation.
378  */
379 static void
380 prune_lexemes_hashtable(HTAB *lexemes_tab, int b_current)
381 {
382         HASH_SEQ_STATUS scan_status;
383         TrackItem               *item;
384
385         hash_seq_init(&scan_status, lexemes_tab);
386         while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL)
387         {
388                 if (item->frequency + item->delta <= b_current)
389                 {
390                         if (hash_search(lexemes_tab, (const void *) &item->key,
391                                                         HASH_REMOVE, NULL) == NULL)
392                                 elog(ERROR, "hash table corrupted");
393                 }
394         }
395 }
396
397 /*
398  * Hash functions for lexemes. They are strings, but not NULL terminated,
399  * so we need a special hash function.
400  */
401 static uint32
402 lexeme_hash(const void *key, Size keysize)
403 {
404         const LexemeHashKey *l = (const LexemeHashKey *) key;
405
406         return DatumGetUInt32(hash_any((const unsigned char *) l->lexeme,
407                                                                    l->length));
408 }
409
410 /*
411  *      Matching function for lexemes, to be used in hashtable lookups.
412  */
413 static int
414 lexeme_match(const void *key1, const void *key2, Size keysize)
415 {
416         /* The keysize parameter is superfluous, the keys store their lengths */
417         return lexeme_compare(key1, key2);
418 }
419
420 /*
421  *      Comparison function for lexemes.
422  */
423 static int
424 lexeme_compare(const void *key1, const void *key2)
425 {
426         const LexemeHashKey     *d1 = (const LexemeHashKey *) key1;
427         const LexemeHashKey     *d2 = (const LexemeHashKey *) key2;
428
429         /* First, compare by length */
430         if (d1->length > d2->length)
431                 return 1;
432         else if (d1->length < d2->length)
433                 return -1;
434         /* Lengths are equal, do a byte-by-byte comparison */
435         return strncmp(d1->lexeme, d2->lexeme, d1->length);
436 }
437
438 /*
439  *      qsort() comparator for sorting TrackItems on frequencies (descending sort)
440  */
441 static int
442 trackitem_compare_frequencies_desc(const void *e1, const void *e2)
443 {
444         const TrackItem * const *t1 = (const TrackItem * const *) e1;
445         const TrackItem * const *t2 = (const TrackItem * const *) e2;
446
447         return (*t2)->frequency - (*t1)->frequency;
448 }
449
450 /*
451  *      qsort() comparator for sorting TrackItems on lexemes
452  */
453 static int
454 trackitem_compare_lexemes(const void *e1, const void *e2)
455 {
456         const TrackItem * const *t1 = (const TrackItem * const *) e1;
457         const TrackItem * const *t2 = (const TrackItem * const *) e2;
458
459         return lexeme_compare(&(*t1)->key, &(*t2)->key);
460 }