]> granicus.if.org Git - postgresql/blobdiff - src/backend/tsearch/ts_typanalyze.c
Update copyright for 2016
[postgresql] / src / backend / tsearch / ts_typanalyze.c
index 199432097bc969b53e327357a841d316253b26ea..0f851ead0607fcecb1fd5516593d65f78b1665e4 100644 (file)
@@ -3,11 +3,11 @@
  * ts_typanalyze.c
  *       functions for gathering statistics from tsvector columns
  *
- * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/tsearch/ts_typanalyze.c,v 1.3 2008/11/27 21:17:39 heikki Exp $
+ *       src/backend/tsearch/ts_typanalyze.c
  *
  *-------------------------------------------------------------------------
  */
@@ -18,7 +18,6 @@
 #include "commands/vacuum.h"
 #include "tsearch/ts_type.h"
 #include "utils/builtins.h"
-#include "utils/hsearch.h"
 
 
 /* A hash key for lexemes */
@@ -31,21 +30,21 @@ typedef struct
 /* A hash table entry for the Lossy Counting algorithm */
 typedef struct
 {
-       LexemeHashKey   key;            /* This is 'e' from the LC algorithm. */
-       int                             frequency;      /* This is 'f'. */
-       int                             delta;          /* And this is 'delta'. */
+       LexemeHashKey key;                      /* This is 'e' from the LC algorithm. */
+       int                     frequency;              /* This is 'f'. */
+       int                     delta;                  /* And this is 'delta'. */
 } TrackItem;
 
 static void compute_tsvector_stats(VacAttrStats *stats,
-                                                                  AnalyzeAttrFetchFunc fetchfunc,
-                                                                  int samplerows,
-                                                                  double totalrows);
+                                          AnalyzeAttrFetchFunc fetchfunc,
+                                          int samplerows,
+                                          double totalrows);
 static void prune_lexemes_hashtable(HTAB *lexemes_tab, int b_current);
 static uint32 lexeme_hash(const void *key, Size keysize);
-static int lexeme_match(const void *key1, const void *key2, Size keysize);
-static int lexeme_compare(const void *key1, const void *key2);
-static int trackitem_compare_frequencies_desc(const void *e1, const void *e2);
-static int trackitem_compare_lexemes(const void *e1, const void *e2);
+static int     lexeme_match(const void *key1, const void *key2, Size keysize);
+static int     lexeme_compare(const void *key1, const void *key2);
+static int     trackitem_compare_frequencies_desc(const void *e1, const void *e2);
+static int     trackitem_compare_lexemes(const void *e1, const void *e2);
 
 
 /*
@@ -63,7 +62,7 @@ ts_typanalyze(PG_FUNCTION_ARGS)
                attr->attstattarget = default_statistics_target;
 
        stats->compute_stats = compute_tsvector_stats;
-       /* see comment about the choice of minrows from analyze.c */
+       /* see comment about the choice of minrows in commands/analyze.c */
        stats->minrows = 300 * attr->attstattarget;
 
        PG_RETURN_BOOL(true);
@@ -92,21 +91,49 @@ ts_typanalyze(PG_FUNCTION_ARGS)
  *     http://www.vldb.org/conf/2002/S10P03.pdf
  *
  *     The Lossy Counting (aka LC) algorithm goes like this:
- *     Let D be a set of triples (e, f, d), where e is an element value, f is
- *     that element's frequency (occurrence count) and d is the maximum error in
- *     f.  We start with D empty and process the elements in batches of size
- *     w. (The batch size is also known as "bucket size".) Let the current batch
- *     number be b_current, starting with 1. For each element e we either
- *     increment its f count, if it's already in D, or insert a new triple into D
- *     with values (e, 1, b_current - 1). After processing each batch we prune D,
- *     by removing from it all elements with f + d <= b_current. Finally, we
- *     gather elements with largest f.  The LC paper proves error bounds on f
- *     dependent on the batch size w, and shows that the required table size
- *     is no more than a few times w.
+ *     Let s be the threshold frequency for an item (the minimum frequency we
+ *     are interested in) and epsilon the error margin for the frequency. Let D
+ *     be a set of triples (e, f, delta), where e is an element value, f is that
+ *     element's frequency (actually, its current occurrence count) and delta is
+ *     the maximum error in f. We start with D empty and process the elements in
+ *     batches of size w. (The batch size is also known as "bucket size" and is
+ *     equal to 1/epsilon.) Let the current batch number be b_current, starting
+ *     with 1. For each element e we either increment its f count, if it's
+ *     already in D, or insert a new triple into D with values (e, 1, b_current
+ *     - 1). After processing each batch we prune D, by removing from it all
+ *     elements with f + delta <= b_current.  After the algorithm finishes we
+ *     suppress all elements from D that do not satisfy f >= (s - epsilon) * N,
+ *     where N is the total number of elements in the input.  We emit the
+ *     remaining elements with estimated frequency f/N.  The LC paper proves
+ *     that this algorithm finds all elements with true frequency at least s,
+ *     and that no frequency is overestimated or is underestimated by more than
+ *     epsilon.  Furthermore, given reasonable assumptions about the input
+ *     distribution, the required table size is no more than about 7 times w.
+ *
+ *     We set s to be the estimated frequency of the K'th word in a natural
+ *     language's frequency table, where K is the target number of entries in
+ *     the MCELEM array plus an arbitrary constant, meant to reflect the fact
+ *     that the most common words in any language would usually be stopwords
+ *     so we will not actually see them in the input.  We assume that the
+ *     distribution of word frequencies (including the stopwords) follows Zipf's
+ *     law with an exponent of 1.
  *
- *     We use a hashtable for the D structure and a bucket width of
- *     statistic_target * 100, where 100 is an arbitrarily chosen constant, meant
- *     to approximate the number of lexemes in a single tsvector.
+ *     Assuming Zipfian distribution, the frequency of the K'th word is equal
+ *     to 1/(K * H(W)) where H(n) is 1/2 + 1/3 + ... + 1/n and W is the number of
+ *     words in the language.  Putting W as one million, we get roughly 0.07/K.
+ *     Assuming top 10 words are stopwords gives s = 0.07/(K + 10).  We set
+ *     epsilon = s/10, which gives bucket width w = (K + 10)/0.007 and
+ *     maximum expected hashtable size of about 1000 * (K + 10).
+ *
+ *     Note: in the above discussion, s, epsilon, and f/N are in terms of a
+ *     lexeme's frequency as a fraction of all lexemes seen in the input.
+ *     However, what we actually want to store in the finished pg_statistic
+ *     entry is each lexeme's frequency as a fraction of all rows that it occurs
+ *     in.  Assuming that the input tsvectors are correctly constructed, no
+ *     lexeme occurs more than once per tsvector, so the final count f is a
+ *     correct estimate of the number of input tsvectors it occurs in, and we
+ *     need only change the divisor from N to nonnull_cnt to get the number we
+ *     want.
  */
 static void
 compute_tsvector_stats(VacAttrStats *stats,
@@ -114,35 +141,42 @@ compute_tsvector_stats(VacAttrStats *stats,
                                           int samplerows,
                                           double totalrows)
 {
-       int                             num_mcelem;
-       int                             null_cnt = 0;
-       double                  total_width = 0;
+       int                     num_mcelem;
+       int                     null_cnt = 0;
+       double          total_width = 0;
+
        /* This is D from the LC algorithm. */
-       HTAB                    *lexemes_tab;
-       HASHCTL                 hash_ctl;
-       HASH_SEQ_STATUS scan_status;
+       HTAB       *lexemes_tab;
+       HASHCTL         hash_ctl;
+       HASH_SEQ_STATUS scan_status;
+
        /* This is the current bucket number from the LC algorithm */
-       int                             b_current;
+       int                     b_current;
+
        /* This is 'w' from the LC algorithm */
-       int                             bucket_width;
-       int vector_no,
-               lexeme_no;
-       LexemeHashKey   hash_key;
-       TrackItem               *item;
+       int                     bucket_width;
+       int                     vector_no,
+                               lexeme_no;
+       LexemeHashKey hash_key;
+       TrackItem  *item;
 
-       /* We want statistic_target * 100 lexemes in the MCELEM array */
-       num_mcelem = stats->attr->attstattarget * 100;
+       /*
+        * We want statistics_target * 10 lexemes in the MCELEM array.  This
+        * multiplier is pretty arbitrary, but is meant to reflect the fact that
+        * the number of individual lexeme values tracked in pg_statistic ought to
+        * be more than the number of values for a simple scalar column.
+        */
+       num_mcelem = stats->attr->attstattarget * 10;
 
        /*
-        * We set bucket width equal to the target number of result lexemes.
-        * This is probably about right but perhaps might need to be scaled
-        * up or down a bit?
+        * We set bucket width equal to (num_mcelem + 10) / 0.007 as per the
+        * comment above.
         */
-       bucket_width = num_mcelem;
+       bucket_width = (num_mcelem + 10) * 1000 / 7;
 
        /*
         * Create the hashtable. It will be in local memory, so we don't need to
-        * worry about initial size too much. Also we don't need to pay any
+        * worry about overflowing the initial size. Also we don't need to pay any
         * attention to locking and memory management.
         */
        MemSet(&hash_ctl, 0, sizeof(hash_ctl));
@@ -152,22 +186,22 @@ compute_tsvector_stats(VacAttrStats *stats,
        hash_ctl.match = lexeme_match;
        hash_ctl.hcxt = CurrentMemoryContext;
        lexemes_tab = hash_create("Analyzed lexemes table",
-                                                         bucket_width * 4,
+                                                         num_mcelem,
                                                          &hash_ctl,
-                                                         HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);
+                                       HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);
 
        /* Initialize counters. */
        b_current = 1;
-       lexeme_no = 1;
+       lexeme_no = 0;
 
        /* Loop over the tsvectors. */
        for (vector_no = 0; vector_no < samplerows; vector_no++)
        {
                Datum           value;
                bool            isnull;
-               TSVector        vector;
-               WordEntry       *curentryptr;
-               char            *lexemesptr;
+               TSVector        vector;
+               WordEntry  *curentryptr;
+               char       *lexemesptr;
                int                     j;
 
                vacuum_delay_point();
@@ -206,7 +240,7 @@ compute_tsvector_stats(VacAttrStats *stats,
                curentryptr = ARRPTR(vector);
                for (j = 0; j < vector->size; j++)
                {
-                       bool                    found;
+                       bool            found;
 
                        /* Construct a hash key */
                        hash_key.lexeme = lexemesptr + curentryptr->pos;
@@ -229,6 +263,9 @@ compute_tsvector_stats(VacAttrStats *stats,
                                item->delta = b_current - 1;
                        }
 
+                       /* lexeme_no is the number of elements processed (ie N) */
+                       lexeme_no++;
+
                        /* We prune the D structure after processing each bucket */
                        if (lexeme_no % bucket_width == 0)
                        {
@@ -237,7 +274,6 @@ compute_tsvector_stats(VacAttrStats *stats,
                        }
 
                        /* Advance to the next WordEntry in the tsvector */
-                       lexeme_no++;
                        curentryptr++;
                }
        }
@@ -247,9 +283,11 @@ compute_tsvector_stats(VacAttrStats *stats,
        {
                int                     nonnull_cnt = samplerows - null_cnt;
                int                     i;
-               TrackItem       **sort_table;
+               TrackItem **sort_table;
                int                     track_len;
-               int                     minfreq, maxfreq;
+               int                     cutoff_freq;
+               int                     minfreq,
+                                       maxfreq;
 
                stats->stats_valid = true;
                /* Do the simple null-frac and average width stats */
@@ -260,60 +298,73 @@ compute_tsvector_stats(VacAttrStats *stats,
                stats->stadistinct = -1.0;
 
                /*
-                * Determine the top-N lexemes by simply copying pointers from the
-                * hashtable into an array and applying qsort()
+                * Construct an array of the interesting hashtable items, that is,
+                * those meeting the cutoff frequency (s - epsilon)*N.  Also identify
+                * the minimum and maximum frequencies among these items.
+                *
+                * Since epsilon = s/10 and bucket_width = 1/epsilon, the cutoff
+                * frequency is 9*N / bucket_width.
                 */
-               track_len = hash_get_num_entries(lexemes_tab);
+               cutoff_freq = 9 * lexeme_no / bucket_width;
 
-               sort_table = (TrackItem **) palloc(sizeof(TrackItem *) * track_len);
+               i = hash_get_num_entries(lexemes_tab);  /* surely enough space */
+               sort_table = (TrackItem **) palloc(sizeof(TrackItem *) * i);
 
                hash_seq_init(&scan_status, lexemes_tab);
-               i = 0;
+               track_len = 0;
+               minfreq = lexeme_no;
+               maxfreq = 0;
                while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL)
                {
-                       sort_table[i++] = item;
+                       if (item->frequency > cutoff_freq)
+                       {
+                               sort_table[track_len++] = item;
+                               minfreq = Min(minfreq, item->frequency);
+                               maxfreq = Max(maxfreq, item->frequency);
+                       }
                }
-               Assert(i == track_len);
+               Assert(track_len <= i);
 
-               qsort(sort_table, track_len, sizeof(TrackItem *),
-                         trackitem_compare_frequencies_desc);
+               /* emit some statistics for debug purposes */
+               elog(DEBUG3, "tsvector_stats: target # mces = %d, bucket width = %d, "
+                        "# lexemes = %d, hashtable size = %d, usable entries = %d",
+                        num_mcelem, bucket_width, lexeme_no, i, track_len);
 
-               /* Suppress any single-occurrence items */
-               while (track_len > 0)
+               /*
+                * If we obtained more lexemes than we really want, get rid of those
+                * with least frequencies.  The easiest way is to qsort the array into
+                * descending frequency order and truncate the array.
+                */
+               if (num_mcelem < track_len)
                {
-                       if (sort_table[track_len-1]->frequency > 1)
-                               break;
-                       track_len--;
+                       qsort(sort_table, track_len, sizeof(TrackItem *),
+                                 trackitem_compare_frequencies_desc);
+                       /* reset minfreq to the smallest frequency we're keeping */
+                       minfreq = sort_table[num_mcelem - 1]->frequency;
                }
-
-               /* Determine the number of most common lexemes to be stored */
-               if (num_mcelem > track_len)
+               else
                        num_mcelem = track_len;
 
                /* Generate MCELEM slot entry */
                if (num_mcelem > 0)
                {
-                       MemoryContext   old_context;
-                       Datum                   *mcelem_values;
-                       float4                  *mcelem_freqs;
-
-                       /* Grab the minimal and maximal frequencies that will get stored */
-                       minfreq = sort_table[num_mcelem - 1]->frequency;
-                       maxfreq = sort_table[0]->frequency;
+                       MemoryContext old_context;
+                       Datum      *mcelem_values;
+                       float4     *mcelem_freqs;
 
                        /*
                         * We want to store statistics sorted on the lexeme value using
                         * first length, then byte-for-byte comparison. The reason for
                         * doing length comparison first is that we don't care about the
-                        * ordering so long as it's consistent, and comparing lengths first
-                        * gives us a chance to avoid a strncmp() call.
+                        * ordering so long as it's consistent, and comparing lengths
+                        * first gives us a chance to avoid a strncmp() call.
                         *
-                        * This is different from what we do with scalar statistics -- they
-                        * get sorted on frequencies. The rationale is that we usually
-                        * search through most common elements looking for a specific
-                        * value, so we can grab its frequency.  When values are presorted
-                        * we can employ binary search for that.  See ts_selfuncs.c for a
-                        * real usage scenario.
+                        * This is different from what we do with scalar statistics --
+                        * they get sorted on frequencies. The rationale is that we
+                        * usually search through most common elements looking for a
+                        * specific value, so we can grab its frequency.  When values are
+                        * presorted we can employ binary search for that.  See
+                        * ts_selfuncs.c for a real usage scenario.
                         */
                        qsort(sort_table, num_mcelem, sizeof(TrackItem *),
                                  trackitem_compare_lexemes);
@@ -326,17 +377,26 @@ compute_tsvector_stats(VacAttrStats *stats,
                         * able to find out the minimal and maximal frequency without
                         * going through all the values.  We keep those two extra
                         * frequencies in two extra cells in mcelem_freqs.
+                        *
+                        * (Note: the MCELEM statistics slot definition allows for a third
+                        * extra number containing the frequency of nulls, but we don't
+                        * create that for a tsvector column, since null elements aren't
+                        * possible.)
                         */
                        mcelem_values = (Datum *) palloc(num_mcelem * sizeof(Datum));
                        mcelem_freqs = (float4 *) palloc((num_mcelem + 2) * sizeof(float4));
 
+                       /*
+                        * See comments above about use of nonnull_cnt as the divisor for
+                        * the final frequency estimates.
+                        */
                        for (i = 0; i < num_mcelem; i++)
                        {
-                               TrackItem *item = sort_table[i];
+                               TrackItem  *item = sort_table[i];
 
                                mcelem_values[i] =
                                        PointerGetDatum(cstring_to_text_with_len(item->key.lexeme,
-                                                                                                                        item->key.length));
+                                                                                                                 item->key.length));
                                mcelem_freqs[i] = (double) item->frequency / (double) nonnull_cnt;
                        }
                        mcelem_freqs[i++] = (double) minfreq / (double) nonnull_cnt;
@@ -352,7 +412,7 @@ compute_tsvector_stats(VacAttrStats *stats,
                        stats->numvalues[0] = num_mcelem;
                        /* We are storing text values */
                        stats->statypid[0] = TEXTOID;
-                       stats->statyplen[0] = -1; /* typlen, -1 for varlena */
+                       stats->statyplen[0] = -1;       /* typlen, -1 for varlena */
                        stats->statypbyval[0] = false;
                        stats->statypalign[0] = 'i';
                }
@@ -362,13 +422,13 @@ compute_tsvector_stats(VacAttrStats *stats,
                /* We found only nulls; assume the column is entirely null */
                stats->stats_valid = true;
                stats->stanullfrac = 1.0;
-               stats->stawidth = 0;            /* "unknown" */
-               stats->stadistinct = 0.0;       /* "unknown" */
+               stats->stawidth = 0;    /* "unknown" */
+               stats->stadistinct = 0.0;               /* "unknown" */
        }
 
        /*
-        * We don't need to bother cleaning up any of our temporary palloc's.
-        * The hashtable should also go away, as it used a child memory context.
+        * We don't need to bother cleaning up any of our temporary palloc's. The
+        * hashtable should also go away, as it used a child memory context.
         */
 }
 
@@ -379,8 +439,8 @@ compute_tsvector_stats(VacAttrStats *stats,
 static void
 prune_lexemes_hashtable(HTAB *lexemes_tab, int b_current)
 {
-       HASH_SEQ_STATUS scan_status;
-       TrackItem               *item;
+       HASH_SEQ_STATUS scan_status;
+       TrackItem  *item;
 
        hash_seq_init(&scan_status, lexemes_tab);
        while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL)
@@ -423,8 +483,8 @@ lexeme_match(const void *key1, const void *key2, Size keysize)
 static int
 lexeme_compare(const void *key1, const void *key2)
 {
-       const LexemeHashKey     *d1 = (const LexemeHashKey *) key1;
-       const LexemeHashKey     *d2 = (const LexemeHashKey *) key2;
+       const LexemeHashKey *d1 = (const LexemeHashKey *) key1;
+       const LexemeHashKey *d2 = (const LexemeHashKey *) key2;
 
        /* First, compare by length */
        if (d1->length > d2->length)
@@ -441,8 +501,8 @@ lexeme_compare(const void *key1, const void *key2)
 static int
 trackitem_compare_frequencies_desc(const void *e1, const void *e2)
 {
-       const TrackItem * const *t1 = (const TrackItem * const *) e1;
-       const TrackItem * const *t2 = (const TrackItem * const *) e2;
+       const TrackItem *const * t1 = (const TrackItem *const *) e1;
+       const TrackItem *const * t2 = (const TrackItem *const *) e2;
 
        return (*t2)->frequency - (*t1)->frequency;
 }
@@ -453,8 +513,8 @@ trackitem_compare_frequencies_desc(const void *e1, const void *e2)
 static int
 trackitem_compare_lexemes(const void *e1, const void *e2)
 {
-       const TrackItem * const *t1 = (const TrackItem * const *) e1;
-       const TrackItem * const *t2 = (const TrackItem * const *) e2;
+       const TrackItem *const * t1 = (const TrackItem *const *) e1;
+       const TrackItem *const * t2 = (const TrackItem *const *) e2;
 
        return lexeme_compare(&(*t1)->key, &(*t2)->key);
 }