~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-proposed

« back to all changes in this revision

Viewing changes to src/backend/commands/analyze.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 *
9
9
 *
10
10
 * IDENTIFICATION
11
 
 *        $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.136 2009/05/05 18:02:11 tgl Exp $
 
11
 *        $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.139 2009/06/11 14:48:55 momjian Exp $
12
12
 *
13
13
 *-------------------------------------------------------------------------
14
14
 */
177
177
                {
178
178
                        if (onerel->rd_rel->relisshared)
179
179
                                ereport(WARNING,
180
 
                                                (errmsg("skipping \"%s\" --- only superuser can analyze it",
181
 
                                                                RelationGetRelationName(onerel))));
 
180
                                 (errmsg("skipping \"%s\" --- only superuser can analyze it",
 
181
                                                 RelationGetRelationName(onerel))));
182
182
                        else if (onerel->rd_rel->relnamespace == PG_CATALOG_NAMESPACE)
183
183
                                ereport(WARNING,
184
184
                                                (errmsg("skipping \"%s\" --- only superuser or database owner can analyze it",
234
234
                                        RelationGetRelationName(onerel))));
235
235
 
236
236
        /*
237
 
         * Switch to the table owner's userid, so that any index functions are
238
 
         * run as that user.
 
237
         * Switch to the table owner's userid, so that any index functions are run
 
238
         * as that user.
239
239
         */
240
240
        GetUserIdAndContext(&save_userid, &save_secdefcxt);
241
241
        SetUserIdAndContext(onerel->rd_rel->relowner, true);
363
363
        }
364
364
 
365
365
        /*
366
 
         * Quit if no analyzable columns
 
366
         * Quit if no analyzable columns and no pg_class update needed.
367
367
         */
368
 
        if (attr_cnt <= 0 && !analyzableindex)
369
 
        {
370
 
                /*
371
 
                 * We report that the table is empty; this is just so that the
372
 
                 * autovacuum code doesn't go nuts trying to get stats about a
373
 
                 * zero-column table.
374
 
                 */
375
 
                if (update_reltuples)
376
 
                        pgstat_report_analyze(onerel, 0, 0);
 
368
        if (attr_cnt <= 0 && !analyzableindex && !update_reltuples)
377
369
                goto cleanup;
378
 
        }
379
370
 
380
371
        /*
381
372
         * Determine how many rows we need to sample, using the worst case from
476
467
 
477
468
        /*
478
469
         * Same for indexes. Vacuum always scans all indexes, so if we're part of
479
 
         * VACUUM ANALYZE, don't overwrite the accurate count already inserted by 
 
470
         * VACUUM ANALYZE, don't overwrite the accurate count already inserted by
480
471
         * VACUUM.
481
472
         */
482
473
        if (!vacstmt->vacuum)
507
498
                        ivinfo.index = Irel[ind];
508
499
                        ivinfo.vacuum_full = false;
509
500
                        ivinfo.analyze_only = true;
 
501
                        ivinfo.estimated_count = true;
510
502
                        ivinfo.message_level = elevel;
511
 
                        ivinfo.num_heap_tuples = -1; /* not known for sure */
 
503
                        ivinfo.num_heap_tuples = onerel->rd_rel->reltuples;
512
504
                        ivinfo.strategy = vac_strategy;
513
505
 
514
506
                        stats = index_vacuum_cleanup(&ivinfo, NULL);
727
719
                return NULL;
728
720
 
729
721
        /*
730
 
         * Create the VacAttrStats struct.  Note that we only have a copy of
731
 
         * the fixed fields of the pg_attribute tuple.
 
722
         * Create the VacAttrStats struct.      Note that we only have a copy of the
 
723
         * fixed fields of the pg_attribute tuple.
732
724
         */
733
725
        stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
734
726
        stats->attr = (Form_pg_attribute) palloc(ATTRIBUTE_FIXED_PART_SIZE);
745
737
        stats->tupattnum = attnum;
746
738
 
747
739
        /*
748
 
         * The fields describing the stats->stavalues[n] element types default
749
 
         * to the type of the field being analyzed, but the type-specific
750
 
         * typanalyze function can change them if it wants to store something
751
 
         * else.
 
740
         * The fields describing the stats->stavalues[n] element types default to
 
741
         * the type of the field being analyzed, but the type-specific typanalyze
 
742
         * function can change them if it wants to store something else.
752
743
         */
753
744
        for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
754
745
        {
902
893
                                        double *totalrows, double *totaldeadrows)
903
894
{
904
895
        int                     numrows = 0;    /* # rows now in reservoir */
905
 
        double          samplerows = 0; /* total # rows collected */
 
896
        double          samplerows = 0; /* total # rows collected */
906
897
        double          liverows = 0;   /* # live rows seen */
907
898
        double          deadrows = 0;   /* # dead rows seen */
908
899
        double          rowstoskip = -1;        /* -1 means not set yet */
939
930
                 * the maxoffset value stays good (else concurrent VACUUM might delete
940
931
                 * tuples out from under us).  Hence, pin the page until we are done
941
932
                 * looking at it.  We also choose to hold sharelock on the buffer
942
 
                 * throughout --- we could release and re-acquire sharelock for
943
 
                 * each tuple, but since we aren't doing much work per tuple, the
944
 
                 * extra lock traffic is probably better avoided.
 
933
                 * throughout --- we could release and re-acquire sharelock for each
 
934
                 * tuple, but since we aren't doing much work per tuple, the extra
 
935
                 * lock traffic is probably better avoided.
945
936
                 */
946
937
                targbuffer = ReadBufferExtended(onerel, MAIN_FORKNUM, targblock,
947
938
                                                                                RBM_NORMAL, vac_strategy);
960
951
 
961
952
                        /*
962
953
                         * We ignore unused and redirect line pointers.  DEAD line
963
 
                         * pointers should be counted as dead, because we need vacuum
964
 
                         * to run to get rid of them.  Note that this rule agrees with
965
 
                         * the way that heap_page_prune() counts things.
 
954
                         * pointers should be counted as dead, because we need vacuum to
 
955
                         * run to get rid of them.      Note that this rule agrees with the
 
956
                         * way that heap_page_prune() counts things.
966
957
                         */
967
958
                        if (!ItemIdIsNormal(itemid))
968
959
                        {
992
983
                                        break;
993
984
 
994
985
                                case HEAPTUPLE_INSERT_IN_PROGRESS:
 
986
 
995
987
                                        /*
996
988
                                         * Insert-in-progress rows are not counted.  We assume
997
989
                                         * that when the inserting transaction commits or aborts,
999
991
                                         * count.  This works right only if that transaction ends
1000
992
                                         * after we finish analyzing the table; if things happen
1001
993
                                         * in the other order, its stats update will be
1002
 
                                         * overwritten by ours.  However, the error will be
1003
 
                                         * large only if the other transaction runs long enough
1004
 
                                         * to insert many tuples, so assuming it will finish
1005
 
                                         * after us is the safer option.
 
994
                                         * overwritten by ours.  However, the error will be large
 
995
                                         * only if the other transaction runs long enough to
 
996
                                         * insert many tuples, so assuming it will finish after us
 
997
                                         * is the safer option.
1006
998
                                         *
1007
999
                                         * A special case is that the inserting transaction might
1008
 
                                         * be our own.  In this case we should count and sample
 
1000
                                         * be our own.  In this case we should count and sample
1009
1001
                                         * the row, to accommodate users who load a table and
1010
1002
                                         * analyze it in one transaction.  (pgstat_report_analyze
1011
 
                                         * has to adjust the numbers we send to the stats collector
1012
 
                                         * to make this come out right.)
 
1003
                                         * has to adjust the numbers we send to the stats
 
1004
                                         * collector to make this come out right.)
1013
1005
                                         */
1014
1006
                                        if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(targtuple.t_data)))
1015
1007
                                        {
1019
1011
                                        break;
1020
1012
 
1021
1013
                                case HEAPTUPLE_DELETE_IN_PROGRESS:
 
1014
 
1022
1015
                                        /*
1023
1016
                                         * We count delete-in-progress rows as still live, using
1024
1017
                                         * the same reasoning given above; but we don't bother to
1027
1020
                                         * If the delete was done by our own transaction, however,
1028
1021
                                         * we must count the row as dead to make
1029
1022
                                         * pgstat_report_analyze's stats adjustments come out
1030
 
                                         * right.  (Note: this works out properly when the row
1031
 
                                         * was both inserted and deleted in our xact.)
 
1023
                                         * right.  (Note: this works out properly when the row was
 
1024
                                         * both inserted and deleted in our xact.)
1032
1025
                                         */
1033
1026
                                        if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmax(targtuple.t_data)))
1034
1027
                                                deadrows += 1;
1062
1055
                                        /*
1063
1056
                                         * t in Vitter's paper is the number of records already
1064
1057
                                         * processed.  If we need to compute a new S value, we
1065
 
                                         * must use the not-yet-incremented value of samplerows
1066
 
                                         * as t.
 
1058
                                         * must use the not-yet-incremented value of samplerows as
 
1059
                                         * t.
1067
1060
                                         */
1068
1061
                                        if (rowstoskip < 0)
1069
1062
                                                rowstoskip = get_next_S(samplerows, targrows, &rstate);
1393
1386
                {
1394
1387
                        /* Yes, replace it */
1395
1388
                        stup = heap_modify_tuple(oldtup,
1396
 
                                                                        RelationGetDescr(sd),
1397
 
                                                                        values,
1398
 
                                                                        nulls,
1399
 
                                                                        replaces);
 
1389
                                                                         RelationGetDescr(sd),
 
1390
                                                                         values,
 
1391
                                                                         nulls,
 
1392
                                                                         replaces);
1400
1393
                        ReleaseSysCache(oldtup);
1401
1394
                        simple_heap_update(sd, &stup->t_self, stup);
1402
1395
                }
1891
1884
                        stats->numnumbers[0] = num_mcv;
1892
1885
                        stats->stavalues[0] = mcv_values;
1893
1886
                        stats->numvalues[0] = num_mcv;
 
1887
 
1894
1888
                        /*
1895
 
                         * Accept the defaults for stats->statypid and others.
1896
 
                         * They have been set before we were called (see vacuum.h)
 
1889
                         * Accept the defaults for stats->statypid and others. They have
 
1890
                         * been set before we were called (see vacuum.h)
1897
1891
                         */
1898
1892
                }
1899
1893
        }
2240
2234
                        stats->numnumbers[slot_idx] = num_mcv;
2241
2235
                        stats->stavalues[slot_idx] = mcv_values;
2242
2236
                        stats->numvalues[slot_idx] = num_mcv;
 
2237
 
2243
2238
                        /*
2244
 
                         * Accept the defaults for stats->statypid and others.
2245
 
                         * They have been set before we were called (see vacuum.h)
 
2239
                         * Accept the defaults for stats->statypid and others. They have
 
2240
                         * been set before we were called (see vacuum.h)
2246
2241
                         */
2247
2242
                        slot_idx++;
2248
2243
                }
2320
2315
 
2321
2316
                        /*
2322
2317
                         * The object of this loop is to copy the first and last values[]
2323
 
                         * entries along with evenly-spaced values in between.  So the
 
2318
                         * entries along with evenly-spaced values in between.  So the
2324
2319
                         * i'th value is values[(i * (nvals - 1)) / (num_hist - 1)].  But
2325
2320
                         * computing that subscript directly risks integer overflow when
2326
2321
                         * the stats target is more than a couple thousand.  Instead we
2352
2347
                        stats->staop[slot_idx] = mystats->ltopr;
2353
2348
                        stats->stavalues[slot_idx] = hist_values;
2354
2349
                        stats->numvalues[slot_idx] = num_hist;
 
2350
 
2355
2351
                        /*
2356
 
                         * Accept the defaults for stats->statypid and others.
2357
 
                         * They have been set before we were called (see vacuum.h)
 
2352
                         * Accept the defaults for stats->statypid and others. They have
 
2353
                         * been set before we were called (see vacuum.h)
2358
2354
                         */
2359
2355
                        slot_idx++;
2360
2356
                }