"Transaction local" statistics are incorrect at speed

Поиск
Список
Период
Сортировка
От Tom Lane
Тема "Transaction local" statistics are incorrect at speed
Дата
Msg-id 26700.1396898553@sss.pgh.pa.us
обсуждение исходный текст
Ответы Re: "Transaction local" statistics are incorrect at speed  (Stephen Frost <sfrost@snowman.net>)
Список pgsql-hackers
My Salesforce colleague Teja Mupparti found an interesting bug.
Consider the following example:

drop table if exists test;
create table test(i int);

insert into test values(1);

select pg_sleep(1);

begin;
insert into test values(2);
insert into test values(3);
select pg_stat_get_xact_tuples_inserted('test'::regclass);
commit;

select pg_sleep(1);

begin;
insert into test values(4);
insert into test values(5);
select pg_stat_get_xact_tuples_inserted('test'::regclass);
commit;

If you do this by hand, or with the above script verbatim, the
pg_stat_get_xact_tuples_inserted() calls both report "2", which
is what you'd expect: the counts are supposed to reflect rows
inserted in the current transaction.

However, if you take out the pg_sleep calls, you get entirely
different results, and soon realize that the counts are including
the previous transactions!

The reason for this is that pgstat_report_stat() includes a delay
check, such that it doesn't ship off statistics counts to the collector
unless at least 500 ms have elapsed since the last report.  Without
the sleeps, the later transactions execute while the previous
transactions' counts are still being held locally, *and those counts
get included into the reported totals*.

This seems like a pretty clear bug to me; does anyone want to argue
that it isn't?

In the case of pg_stat_get_xact_tuples_inserted and a couple of other
routines, it would be entirely trivial to fix: just ignore
tabentry->t_counts.t_tuples_inserted (which is the count held over from
previous transactions) and only total the trans->tuples_inserted counters.
However, for a number of other counters such as blocks_fetched, we don't
store transaction-local counts separately, and would have to start doing
so if we wanted to make these functions work as documented.

Thoughts?  I have other things to do right now than fix this myself.
        regards, tom lane



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Stephen Frost
Дата:
Сообщение: Re: B-Tree support function number 3 (strxfrm() optimization)
Следующее
От: Andres Freund
Дата:
Сообщение: Why is it not sane to pass ExecStoreTuple(shouldFree=true) for tuples point into buffers