Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bufFz-0000zA-9R for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Oct 2016 12:37:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bufFy-0004bl-Rj for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Oct 2016 12:37:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bufFy-0004bf-AD for pgsql-hackers@postgresql.org; Thu, 13 Oct 2016 12:37:06 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bufFv-00043w-2B for pgsql-hackers@postgresql.org; Thu, 13 Oct 2016 12:37:05 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id CC82D21C3AA8 for ; Thu, 13 Oct 2016 15:37:00 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from mail.postgrespro.ru ([127.0.0.1]) by localhost (mail.postgrespro.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id b6N77xGf1YqN for ; Thu, 13 Oct 2016 15:37:00 +0300 (MSK) Received: from [192.168.27.145] (unknown [192.168.27.1]) by mail.postgrespro.ru (Postfix) with ESMTPSA id 8AE0321C3A9E for ; Thu, 13 Oct 2016 15:37:00 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1476362220; bh=4GWGPzYe7C3S66PEBogmC90r4+qgOV/cbkjuHWnTZ4M=; h=From:Subject:Date:To; b=GAd8Wi1E6j0Ejn5SbShL3+RLMG9RrJUz2SiEst8yckWN78pmhX00paQbAKEr+AmSS k41g3d+8yu/MFY7bWpdFmvGBDf0axJrIANh+M4Zpq4TLQGsFv/8NB+nfvy4+hd4idj +wQYh8anqoxCMDYuaBSjtvKyZ/0nYkqOeNpDTnuc= From: Stas Kelvich Content-Type: multipart/mixed; boundary="Apple-Mail=_4601BDFC-41DC-44D6-A252-EC07EEB73EB0" Subject: Stats sender and 2pc minor problem Message-Id: Date: Thu, 13 Oct 2016 15:36:59 +0300 To: Pg Hackers Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\)) X-Mailer: Apple Mail (2.3124) X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --Apple-Mail=_4601BDFC-41DC-44D6-A252-EC07EEB73EB0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hello. Statistics sender logic during usual commit and two-phase commit do not str= ictly matches each other and that leads to delta_live_tuples added to n_live_tup in case of truncate in two phase comm= it. That can be see in following example: CREATE TABLE trunc_stats_test5(id serial); INSERT INTO trunc_stats_test5 DEFAULT VALUES; INSERT INTO trunc_stats_test5 DEFAULT VALUES; INSERT INTO trunc_stats_test5 DEFAULT VALUES; BEGIN; TRUNCATE trunc_stats_test5; PREPARE TRANSACTION 'twophase_stats'; COMMIT PREPARED 'twophase_stats'; After that pg_stat_user_tables will have n_live_tup =3D 3 instead of 0. Fix along with test is attached. --Apple-Mail=_4601BDFC-41DC-44D6-A252-EC07EEB73EB0 Content-Disposition: attachment; filename=2pc-stats.patch Content-Type: application/octet-stream; name="2pc-stats.patch" Content-Transfer-Encoding: 7bit diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index a9efee8..c7dee95 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -2227,6 +2227,13 @@ pgstat_twophase_postcommit(TransactionId xid, uint16 info, pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted; pgstat_info->t_counts.t_truncated = rec->t_truncated; + /* forget live/dead stats seen by backend thus far */ + if (rec->t_truncated) + { + pgstat_info->t_counts.t_delta_live_tuples = 0; + pgstat_info->t_counts.t_delta_dead_tuples = 0; + } + pgstat_info->t_counts.t_delta_live_tuples += rec->tuples_inserted - rec->tuples_deleted; pgstat_info->t_counts.t_delta_dead_tuples += diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.out index a811265..e4dc333 100644 --- a/src/test/regress/expected/stats.out +++ b/src/test/regress/expected/stats.out @@ -84,6 +84,7 @@ CREATE TABLE trunc_stats_test1(id serial); CREATE TABLE trunc_stats_test2(id serial); CREATE TABLE trunc_stats_test3(id serial); CREATE TABLE trunc_stats_test4(id serial); +CREATE TABLE trunc_stats_test5(id serial); -- check that n_live_tup is reset to 0 after truncate INSERT INTO trunc_stats_test DEFAULT VALUES; INSERT INTO trunc_stats_test DEFAULT VALUES; @@ -129,6 +130,14 @@ INSERT INTO trunc_stats_test4 DEFAULT VALUES; TRUNCATE trunc_stats_test4; INSERT INTO trunc_stats_test4 DEFAULT VALUES; ROLLBACK; +-- truncate and two-phase commit +INSERT INTO trunc_stats_test5 DEFAULT VALUES; +INSERT INTO trunc_stats_test5 DEFAULT VALUES; +INSERT INTO trunc_stats_test5 DEFAULT VALUES; +BEGIN; +TRUNCATE trunc_stats_test5; +PREPARE TRANSACTION 'twophase_stats'; +COMMIT PREPARED 'twophase_stats'; -- do a seqscan SELECT count(*) FROM tenk2; count @@ -169,7 +178,8 @@ SELECT relname, n_tup_ins, n_tup_upd, n_tup_del, n_live_tup, n_dead_tup trunc_stats_test2 | 1 | 0 | 0 | 1 | 0 trunc_stats_test3 | 4 | 0 | 0 | 2 | 2 trunc_stats_test4 | 2 | 0 | 0 | 0 | 2 -(5 rows) + trunc_stats_test5 | 3 | 0 | 0 | 0 | 0 +(6 rows) SELECT st.seq_scan >= pr.seq_scan + 1, st.seq_tup_read >= pr.seq_tup_read + cl.reltuples, @@ -198,5 +208,5 @@ FROM prevstats AS pr; t (1 row) -DROP TABLE trunc_stats_test, trunc_stats_test1, trunc_stats_test2, trunc_stats_test3, trunc_stats_test4; +DROP TABLE trunc_stats_test, trunc_stats_test1, trunc_stats_test2, trunc_stats_test3, trunc_stats_test4, trunc_stats_test5; -- End of Stats Test diff --git a/src/test/regress/sql/stats.sql b/src/test/regress/sql/stats.sql index b3e2efa..46f044b 100644 --- a/src/test/regress/sql/stats.sql +++ b/src/test/regress/sql/stats.sql @@ -80,6 +80,7 @@ CREATE TABLE trunc_stats_test1(id serial); CREATE TABLE trunc_stats_test2(id serial); CREATE TABLE trunc_stats_test3(id serial); CREATE TABLE trunc_stats_test4(id serial); +CREATE TABLE trunc_stats_test5(id serial); -- check that n_live_tup is reset to 0 after truncate INSERT INTO trunc_stats_test DEFAULT VALUES; @@ -132,6 +133,15 @@ TRUNCATE trunc_stats_test4; INSERT INTO trunc_stats_test4 DEFAULT VALUES; ROLLBACK; +-- truncate and two-phase commit +INSERT INTO trunc_stats_test5 DEFAULT VALUES; +INSERT INTO trunc_stats_test5 DEFAULT VALUES; +INSERT INTO trunc_stats_test5 DEFAULT VALUES; +BEGIN; +TRUNCATE trunc_stats_test5; +PREPARE TRANSACTION 'twophase_stats'; +COMMIT PREPARED 'twophase_stats'; + -- do a seqscan SELECT count(*) FROM tenk2; -- do an indexscan @@ -164,5 +174,5 @@ SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages, SELECT pr.snap_ts < pg_stat_get_snapshot_timestamp() as snapshot_newer FROM prevstats AS pr; -DROP TABLE trunc_stats_test, trunc_stats_test1, trunc_stats_test2, trunc_stats_test3, trunc_stats_test4; +DROP TABLE trunc_stats_test, trunc_stats_test1, trunc_stats_test2, trunc_stats_test3, trunc_stats_test4, trunc_stats_test5; -- End of Stats Test --Apple-Mail=_4601BDFC-41DC-44D6-A252-EC07EEB73EB0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii -- Stas Kelvich Postgres Professional: http://www.postgrespro.com Russian Postgres Company --Apple-Mail=_4601BDFC-41DC-44D6-A252-EC07EEB73EB0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --Apple-Mail=_4601BDFC-41DC-44D6-A252-EC07EEB73EB0--