aboutsummaryrefslogtreecommitdiff
path: root/src/test/regress/expected/stats.out
blob: 9713175930331a3c57205d65f0e71daa7735966a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
--
-- Test cumulative stats system
--
-- Must be run after tenk2 has been created (by create_table),
-- populated (by create_misc) and indexed (by create_index).
--
-- conditio sine qua non
SHOW track_counts;  -- must be on
 track_counts 
--------------
 on
(1 row)

-- ensure that both seqscan and indexscan plans are allowed
SET enable_seqscan TO on;
SET enable_indexscan TO on;
-- for the moment, we don't want index-only scans here
SET enable_indexonlyscan TO off;
-- not enabled by default, but we want to test it...
SET track_functions TO 'all';
-- save counters
BEGIN;
SET LOCAL stats_fetch_consistency = snapshot;
CREATE TABLE prevstats AS
SELECT t.seq_scan, t.seq_tup_read, t.idx_scan, t.idx_tup_fetch,
       (b.heap_blks_read + b.heap_blks_hit) AS heap_blks,
       (b.idx_blks_read + b.idx_blks_hit) AS idx_blks,
       pg_stat_get_snapshot_timestamp() as snap_ts
  FROM pg_catalog.pg_stat_user_tables AS t,
       pg_catalog.pg_statio_user_tables AS b
 WHERE t.relname='tenk2' AND b.relname='tenk2';
COMMIT;
-- test effects of TRUNCATE on n_live_tup/n_dead_tup counters
CREATE TABLE trunc_stats_test(id serial);
CREATE TABLE trunc_stats_test1(id serial, stuff text);
CREATE TABLE trunc_stats_test2(id serial);
CREATE TABLE trunc_stats_test3(id serial, stuff text);
CREATE TABLE trunc_stats_test4(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;
INSERT INTO trunc_stats_test DEFAULT VALUES;
TRUNCATE trunc_stats_test;
-- test involving a truncate in a transaction; 4 ins but only 1 live
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
UPDATE trunc_stats_test1 SET id = id + 10 WHERE id IN (1, 2);
DELETE FROM trunc_stats_test1 WHERE id = 3;
BEGIN;
UPDATE trunc_stats_test1 SET id = id + 100;
TRUNCATE trunc_stats_test1;
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
COMMIT;
-- use a savepoint: 1 insert, 1 live
BEGIN;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
SAVEPOINT p1;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
TRUNCATE trunc_stats_test2;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
RELEASE SAVEPOINT p1;
COMMIT;
-- rollback a savepoint: this should count 4 inserts and have 2
-- live tuples after commit (and 2 dead ones due to aborted subxact)
BEGIN;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
SAVEPOINT p1;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
TRUNCATE trunc_stats_test3;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
ROLLBACK TO SAVEPOINT p1;
COMMIT;
-- rollback a truncate: this should count 2 inserts and produce 2 dead tuples
BEGIN;
INSERT INTO trunc_stats_test4 DEFAULT VALUES;
INSERT INTO trunc_stats_test4 DEFAULT VALUES;
TRUNCATE trunc_stats_test4;
INSERT INTO trunc_stats_test4 DEFAULT VALUES;
ROLLBACK;
-- do a seqscan
SELECT count(*) FROM tenk2;
 count 
-------
 10000
(1 row)

-- do an indexscan
-- make sure it is not a bitmap scan, which might skip fetching heap tuples
SET enable_bitmapscan TO off;
SELECT count(*) FROM tenk2 WHERE unique1 = 1;
 count 
-------
     1
(1 row)

RESET enable_bitmapscan;
-- ensure pending stats are flushed
SELECT pg_stat_force_next_flush();
 pg_stat_force_next_flush 
--------------------------
 
(1 row)

-- check effects
BEGIN;
SET LOCAL stats_fetch_consistency = snapshot;
SELECT relname, n_tup_ins, n_tup_upd, n_tup_del, n_live_tup, n_dead_tup
  FROM pg_stat_user_tables
 WHERE relname like 'trunc_stats_test%' order by relname;
      relname      | n_tup_ins | n_tup_upd | n_tup_del | n_live_tup | n_dead_tup 
-------------------+-----------+-----------+-----------+------------+------------
 trunc_stats_test  |         3 |         0 |         0 |          0 |          0
 trunc_stats_test1 |         4 |         2 |         1 |          1 |          0
 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)

SELECT st.seq_scan >= pr.seq_scan + 1,
       st.seq_tup_read >= pr.seq_tup_read + cl.reltuples,
       st.idx_scan >= pr.idx_scan + 1,
       st.idx_tup_fetch >= pr.idx_tup_fetch + 1
  FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr
 WHERE st.relname='tenk2' AND cl.relname='tenk2';
 ?column? | ?column? | ?column? | ?column? 
----------+----------+----------+----------
 t        | t        | t        | t
(1 row)

SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages,
       st.idx_blks_read + st.idx_blks_hit >= pr.idx_blks + 1
  FROM pg_statio_user_tables AS st, pg_class AS cl, prevstats AS pr
 WHERE st.relname='tenk2' AND cl.relname='tenk2';
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

SELECT pr.snap_ts < pg_stat_get_snapshot_timestamp() as snapshot_newer
FROM prevstats AS pr;
 snapshot_newer 
----------------
 t
(1 row)

COMMIT;
----
-- Basic tests for track_functions
---
CREATE FUNCTION stats_test_func1() RETURNS VOID LANGUAGE plpgsql AS $$BEGIN END;$$;
SELECT 'stats_test_func1()'::regprocedure::oid AS stats_test_func1_oid \gset
CREATE FUNCTION stats_test_func2() RETURNS VOID LANGUAGE plpgsql AS $$BEGIN END;$$;
SELECT 'stats_test_func2()'::regprocedure::oid AS stats_test_func2_oid \gset
-- test that stats are accumulated
BEGIN;
SET LOCAL stats_fetch_consistency = none;
SELECT pg_stat_get_function_calls(:stats_test_func1_oid);
 pg_stat_get_function_calls 
----------------------------
                           
(1 row)

SELECT pg_stat_get_xact_function_calls(:stats_test_func1_oid);
 pg_stat_get_xact_function_calls 
---------------------------------
                                
(1 row)

SELECT stats_test_func1();
 stats_test_func1 
------------------
 
(1 row)

SELECT pg_stat_get_xact_function_calls(:stats_test_func1_oid);
 pg_stat_get_xact_function_calls 
---------------------------------
                               1
(1 row)

SELECT stats_test_func1();
 stats_test_func1 
------------------
 
(1 row)

SELECT pg_stat_get_xact_function_calls(:stats_test_func1_oid);
 pg_stat_get_xact_function_calls 
---------------------------------
                               2
(1 row)

SELECT pg_stat_get_function_calls(:stats_test_func1_oid);
 pg_stat_get_function_calls 
----------------------------
                          0
(1 row)

COMMIT;
-- Verify that function stats are not transactional
-- rolled back savepoint in committing transaction
BEGIN;
SELECT stats_test_func2();
 stats_test_func2 
------------------
 
(1 row)

SAVEPOINT foo;
SELECT stats_test_func2();
 stats_test_func2 
------------------
 
(1 row)

ROLLBACK TO SAVEPOINT foo;
SELECT pg_stat_get_xact_function_calls(:stats_test_func2_oid);
 pg_stat_get_xact_function_calls 
---------------------------------
                               2
(1 row)

SELECT stats_test_func2();
 stats_test_func2 
------------------
 
(1 row)

COMMIT;
-- rolled back transaction
BEGIN;
SELECT stats_test_func2();
 stats_test_func2 
------------------
 
(1 row)

ROLLBACK;
SELECT pg_stat_force_next_flush();
 pg_stat_force_next_flush 
--------------------------
 
(1 row)

-- check collected stats
SELECT funcname, calls FROM pg_stat_user_functions WHERE funcid = :stats_test_func1_oid;
     funcname     | calls 
------------------+-------
 stats_test_func1 |     2
(1 row)

SELECT funcname, calls FROM pg_stat_user_functions WHERE funcid = :stats_test_func2_oid;
     funcname     | calls 
------------------+-------
 stats_test_func2 |     4
(1 row)

-- check that a rolled back drop function stats leaves stats alive
BEGIN;
SELECT funcname, calls FROM pg_stat_user_functions WHERE funcid = :stats_test_func1_oid;
     funcname     | calls 
------------------+-------
 stats_test_func1 |     2
(1 row)

DROP FUNCTION stats_test_func1();
-- shouldn't be visible via view
SELECT funcname, calls FROM pg_stat_user_functions WHERE funcid = :stats_test_func1_oid;
 funcname | calls 
----------+-------
(0 rows)

-- but still via oid access
SELECT pg_stat_get_function_calls(:stats_test_func1_oid);
 pg_stat_get_function_calls 
----------------------------
                          2
(1 row)

ROLLBACK;
SELECT funcname, calls FROM pg_stat_user_functions WHERE funcid = :stats_test_func1_oid;
     funcname     | calls 
------------------+-------
 stats_test_func1 |     2
(1 row)

SELECT pg_stat_get_function_calls(:stats_test_func1_oid);
 pg_stat_get_function_calls 
----------------------------
                          2
(1 row)

-- check that function dropped in main transaction leaves no stats behind
BEGIN;
DROP FUNCTION stats_test_func1();
COMMIT;
SELECT funcname, calls FROM pg_stat_user_functions WHERE funcid = :stats_test_func1_oid;
 funcname | calls 
----------+-------
(0 rows)

SELECT pg_stat_get_function_calls(:stats_test_func1_oid);
 pg_stat_get_function_calls 
----------------------------
                           
(1 row)

-- check that function dropped in a subtransaction leaves no stats behind
BEGIN;
SELECT stats_test_func2();
 stats_test_func2 
------------------
 
(1 row)

SAVEPOINT a;
SELECT stats_test_func2();
 stats_test_func2 
------------------
 
(1 row)

SAVEPOINT b;
DROP FUNCTION stats_test_func2();
COMMIT;
SELECT funcname, calls FROM pg_stat_user_functions WHERE funcid = :stats_test_func2_oid;
 funcname | calls 
----------+-------
(0 rows)

SELECT pg_stat_get_function_calls(:stats_test_func2_oid);
 pg_stat_get_function_calls 
----------------------------
                           
(1 row)

-- Check that stats for relations are dropped. For that we need to access stats
-- by oid after the DROP TABLE. Save oids.
CREATE TABLE drop_stats_test();
INSERT INTO drop_stats_test DEFAULT VALUES;
SELECT 'drop_stats_test'::regclass::oid AS drop_stats_test_oid \gset
CREATE TABLE drop_stats_test_xact();
INSERT INTO drop_stats_test_xact DEFAULT VALUES;
SELECT 'drop_stats_test_xact'::regclass::oid AS drop_stats_test_xact_oid \gset
CREATE TABLE drop_stats_test_subxact();
INSERT INTO drop_stats_test_subxact DEFAULT VALUES;
SELECT 'drop_stats_test_subxact'::regclass::oid AS drop_stats_test_subxact_oid \gset
SELECT pg_stat_force_next_flush();
 pg_stat_force_next_flush 
--------------------------
 
(1 row)

SELECT pg_stat_get_live_tuples(:drop_stats_test_oid);
 pg_stat_get_live_tuples 
-------------------------
                       1
(1 row)

DROP TABLE drop_stats_test;
SELECT pg_stat_get_live_tuples(:drop_stats_test_oid);
 pg_stat_get_live_tuples 
-------------------------
                       0
(1 row)

SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                0
(1 row)

-- check that rollback protects against having stats dropped and that local
-- modifications don't pose a problem
SELECT pg_stat_get_live_tuples(:drop_stats_test_xact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       1
(1 row)

SELECT pg_stat_get_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_tuples_inserted 
-----------------------------
                           1
(1 row)

SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                0
(1 row)

BEGIN;
INSERT INTO drop_stats_test_xact DEFAULT VALUES;
SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                1
(1 row)

DROP TABLE drop_stats_test_xact;
SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                0
(1 row)

ROLLBACK;
SELECT pg_stat_force_next_flush();
 pg_stat_force_next_flush 
--------------------------
 
(1 row)

SELECT pg_stat_get_live_tuples(:drop_stats_test_xact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       1
(1 row)

SELECT pg_stat_get_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_tuples_inserted 
-----------------------------
                           2
(1 row)

-- transactional drop
SELECT pg_stat_get_live_tuples(:drop_stats_test_xact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       1
(1 row)

SELECT pg_stat_get_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_tuples_inserted 
-----------------------------
                           2
(1 row)

BEGIN;
INSERT INTO drop_stats_test_xact DEFAULT VALUES;
SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                1
(1 row)

DROP TABLE drop_stats_test_xact;
SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                0
(1 row)

COMMIT;
SELECT pg_stat_force_next_flush();
 pg_stat_force_next_flush 
--------------------------
 
(1 row)

SELECT pg_stat_get_live_tuples(:drop_stats_test_xact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       0
(1 row)

SELECT pg_stat_get_tuples_inserted(:drop_stats_test_xact_oid);
 pg_stat_get_tuples_inserted 
-----------------------------
                           0
(1 row)

-- savepoint rollback (2 levels)
SELECT pg_stat_get_live_tuples(:drop_stats_test_subxact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       1
(1 row)

BEGIN;
INSERT INTO drop_stats_test_subxact DEFAULT VALUES;
SAVEPOINT sp1;
INSERT INTO drop_stats_test_subxact DEFAULT VALUES;
SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_subxact_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                2
(1 row)

SAVEPOINT sp2;
DROP TABLE drop_stats_test_subxact;
ROLLBACK TO SAVEPOINT sp2;
SELECT pg_stat_get_xact_tuples_inserted(:drop_stats_test_subxact_oid);
 pg_stat_get_xact_tuples_inserted 
----------------------------------
                                2
(1 row)

COMMIT;
SELECT pg_stat_force_next_flush();
 pg_stat_force_next_flush 
--------------------------
 
(1 row)

SELECT pg_stat_get_live_tuples(:drop_stats_test_subxact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       3
(1 row)

-- savepoint rolback (1 level)
SELECT pg_stat_get_live_tuples(:drop_stats_test_subxact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       3
(1 row)

BEGIN;
SAVEPOINT sp1;
DROP TABLE drop_stats_test_subxact;
SAVEPOINT sp2;
ROLLBACK TO SAVEPOINT sp1;
COMMIT;
SELECT pg_stat_get_live_tuples(:drop_stats_test_subxact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       3
(1 row)

-- and now actually drop
SELECT pg_stat_get_live_tuples(:drop_stats_test_subxact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       3
(1 row)

BEGIN;
SAVEPOINT sp1;
DROP TABLE drop_stats_test_subxact;
SAVEPOINT sp2;
RELEASE SAVEPOINT sp1;
COMMIT;
SELECT pg_stat_get_live_tuples(:drop_stats_test_subxact_oid);
 pg_stat_get_live_tuples 
-------------------------
                       0
(1 row)

DROP TABLE trunc_stats_test, trunc_stats_test1, trunc_stats_test2, trunc_stats_test3, trunc_stats_test4;
DROP TABLE prevstats;
----
-- pg_stat_get_snapshot_timestamp behavior
----
BEGIN;
SET LOCAL stats_fetch_consistency = snapshot;
-- no snapshot yet, return NULL
SELECT pg_stat_get_snapshot_timestamp();
 pg_stat_get_snapshot_timestamp 
--------------------------------
 
(1 row)

-- any attempt at accessing stats will build snapshot
SELECT pg_stat_get_function_calls(0);
 pg_stat_get_function_calls 
----------------------------
                           
(1 row)

SELECT pg_stat_get_snapshot_timestamp() >= NOW();
 ?column? 
----------
 t
(1 row)

-- shows NULL again after clearing
SELECT pg_stat_clear_snapshot();
 pg_stat_clear_snapshot 
------------------------
 
(1 row)

SELECT pg_stat_get_snapshot_timestamp();
 pg_stat_get_snapshot_timestamp 
--------------------------------
 
(1 row)

COMMIT;
----
-- pg_stat_have_stats behavior
----
-- fixed-numbered stats exist
SELECT pg_stat_have_stats('bgwriter', 0, 0);
 pg_stat_have_stats 
--------------------
 t
(1 row)

-- unknown stats kinds error out
SELECT pg_stat_have_stats('zaphod', 0, 0);
ERROR:  invalid statistics kind: "zaphod"
-- db stats have objoid 0
SELECT pg_stat_have_stats('database', (SELECT oid FROM pg_database WHERE datname = current_database()), 1);
 pg_stat_have_stats 
--------------------
 f
(1 row)

SELECT pg_stat_have_stats('database', (SELECT oid FROM pg_database WHERE datname = current_database()), 0);
 pg_stat_have_stats 
--------------------
 t
(1 row)

-- ensure that stats accessors handle NULL input correctly
SELECT pg_stat_get_replication_slot(NULL);
 pg_stat_get_replication_slot 
------------------------------
 
(1 row)

SELECT pg_stat_get_subscription_stats(NULL);
 pg_stat_get_subscription_stats 
--------------------------------
 
(1 row)

-- End of Stats Test