-
Notifications
You must be signed in to change notification settings - Fork 18
/
pgcsvstat.c
1268 lines (1066 loc) · 34.1 KB
/
pgcsvstat.c
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* pgcsvstat, a PostgreSQL app to gather statistical informations
* from a PostgreSQL database.
*
* This software is released under the PostgreSQL Licence.
*
* Guillaume Lelarge, [email protected], 2011-2024.
*
* pgstats/pgcsvstat.c
*/
/*
* System headers
*/
#include <sys/stat.h>
/*
* PostgreSQL headers
*/
#include "postgres_fe.h"
#include "common/logging.h"
#include "fe_utils/cancel.h"
#include "fe_utils/connect_utils.h"
extern char *optarg;
/*
* Defines
*/
#define PGCSVSTAT_VERSION "1.4.0"
/* these are the opts structures for command line params */
struct options
{
bool quiet;
bool nodb;
char *directory;
char *dbname;
char *hostname;
char *port;
char *username;
int major;
int minor;
};
/* global variables */
struct options *opts;
PGconn *conn;
/* function prototypes */
static void help(const char *progname);
void get_opts(int, char **);
void *myalloc(size_t size);
char *mystrdup(const char *str);
int sql_exec(const char *sql, const char *filename, bool quiet);
void sql_exec_dump_pgstatactivity(void);
void sql_exec_dump_pgstatarchiver(void);
void sql_exec_dump_pgstatbgwriter(void);
void sql_exec_dump_pgstatcheckpointer(void);
void sql_exec_dump_pgstatdatabase(void);
void sql_exec_dump_pgstatdatabaseconflicts(void);
void sql_exec_dump_pgstatreplication(void);
void sql_exec_dump_pgstatreplicationslots(void);
void sql_exec_dump_pgstatslru(void);
void sql_exec_dump_pgstatsubscription(void);
void sql_exec_dump_pgstatwal(void);
void sql_exec_dump_pgstatwalreceiver(void);
void sql_exec_dump_pgstatalltables(void);
void sql_exec_dump_pgstatallindexes(void);
void sql_exec_dump_pgstatioalltables(void);
void sql_exec_dump_pgstatioallindexes(void);
void sql_exec_dump_pgstatioallsequences(void);
void sql_exec_dump_pgstatuserfunctions(void);
void sql_exec_dump_pgclass_size(void);
void sql_exec_dump_pgstatstatements(void);
void sql_exec_dump_xlog_stat(void);
void sql_exec_dump_pgstatprogressanalyze(void);
void sql_exec_dump_pgstatprogressbasebackup(void);
void sql_exec_dump_pgstatprogresscluster(void);
void sql_exec_dump_pgstatprogresscopy(void);
void sql_exec_dump_pgstatprogresscreateindex(void);
void sql_exec_dump_pgstatprogressvacuum(void);
void fetch_version(void);
bool check_superuser(void);
bool backend_minimum_version(int major, int minor);
bool backend_has_pgstatstatements(void);
/* function to parse command line options and check for some usage errors. */
void
get_opts(int argc, char **argv)
{
int c;
const char *progname;
progname = get_progname(argv[0]);
/* set the defaults */
opts->quiet = false;
opts->nodb = false;
opts->directory = NULL;
opts->dbname = NULL;
opts->hostname = NULL;
opts->port = NULL;
opts->username = NULL;
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
help(progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
puts("pgcsvstats " PGCSVSTAT_VERSION " (compiled with PostgreSQL " PG_VERSION ")");
exit(0);
}
}
/* get opts */
while ((c = getopt(argc, argv, "h:p:U:d:D:q")) != -1)
{
switch (c)
{
/* specify the database */
case 'd':
opts->dbname = mystrdup(optarg);
break;
/* specify the directory */
case 'D':
opts->directory = mystrdup(optarg);
break;
/* don't show headers */
case 'q':
opts->quiet = true;
break;
/* host to connect to */
case 'h':
opts->hostname = mystrdup(optarg);
break;
/* port to connect to on remote host */
case 'p':
opts->port = mystrdup(optarg);
break;
/* username */
case 'U':
opts->username = mystrdup(optarg);
break;
default:
pg_log_error("Try \"%s --help\" for more information.\n", progname);
exit(EXIT_FAILURE);
}
}
}
static void
help(const char *progname)
{
printf("%s gathers statistics from a PostgreSQL database.\n\n"
"Usage:\n"
" %s [OPTIONS]...\n"
"\nGeneral options:\n"
" -d DBNAME database to connect to\n"
" -D DIRECTORY directory for stats files (defaults to current)\n"
" -q quiet\n"
" --help show this help, then exit\n"
" --version output version information, then exit\n"
"\nConnection options:\n"
" -h HOSTNAME database server host or socket directory\n"
" -p PORT database server port number\n"
" -U USER connect as specified database user\n"
"\nIt creates CSV files for each report.\n\n"
"Report bugs to <[email protected]>.\n",
progname, progname);
}
void *
myalloc(size_t size)
{
void *ptr = malloc(size);
if (!ptr)
{
pg_log_error("out of memory (myalloc)");
exit(EXIT_FAILURE);
}
return ptr;
}
char *
mystrdup(const char *str)
{
char *result = strdup(str);
if (!result)
{
pg_log_error("out of memory (mystrdup)");
exit(EXIT_FAILURE);
}
return result;
}
/*
* Actual code to extrac statistics from the database
* and to store the output data in CSV files.
*/
int
sql_exec(const char *query, const char* filename, bool quiet)
{
PGresult *res;
FILE *fdcsv;
struct stat st;
int nfields;
int nrows;
int i, j;
int size;
/* open the csv file */
fdcsv = fopen(filename, "a");
if (!fdcsv)
{
pg_log_error("Cannot open file %s, errno %d\n", filename, errno);
PQfinish(conn);
exit(EXIT_FAILURE);
}
/* get size of file */
stat(filename, &st);
size = st.st_size;
/* make the call */
res = PQexec(conn, query);
/* check and deal with errors */
if (!res || PQresultStatus(res) > 2)
{
pg_log_error("query failed: %s\n", PQerrorMessage(conn));
pg_log_info("query was: %s\n", query);
PQclear(res);
PQfinish(conn);
exit(-1);
}
/* get the number of fields */
nrows = PQntuples(res);
nfields = PQnfields(res);
/* print a header */
if (!quiet && size == 0)
{
for (j = 0; j < nfields; j++)
{
fprintf(fdcsv, "%s", PQfname(res, j));
if (j < nfields - 1)
fprintf(fdcsv, ";");
}
fprintf(fdcsv, "\n");
}
/* for each row, dump the information */
for (i = 0; i < nrows; i++)
{
for (j = 0; j < nfields; j++)
{
fprintf(fdcsv, "%s", PQgetvalue(res, i, j));
if (j < nfields - 1)
fprintf(fdcsv, ";");
}
fprintf(fdcsv, "\n");
}
/* cleanup */
PQclear(res);
/* close the csv file */
fclose(fdcsv);
return 0;
}
/*
* Dump all activities.
*/
void
sql_exec_dump_pgstatactivity()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), datid, datname, %s, %s"
"usesysid, usename, %s%s%s%s%s"
"date_trunc('seconds', query_start) AS query_start, "
"%s%s%s%s%s%s%s state "
"FROM pg_stat_activity "
"ORDER BY %s",
backend_minimum_version(9, 2) ? "pid" : "procpid",
backend_minimum_version(13, 0) ? "leader_pid, " : "",
backend_minimum_version(9, 0) ? "application_name, " : "",
backend_minimum_version(8, 1) ? "client_addr, " : "",
backend_minimum_version(9, 1) ? "client_hostname, " : "",
backend_minimum_version(8, 1) ? "client_port, date_trunc('seconds', backend_start) AS backend_start, " : "",
backend_minimum_version(8, 3) ? "date_trunc('seconds', xact_start) AS xact_start, " : "",
backend_minimum_version(9, 2) ? "state_change, " : "",
backend_minimum_version(9, 6) ? "wait_event_type, wait_event, " : backend_minimum_version(8, 2) ? "waiting, " : "",
backend_minimum_version(9, 4) ? "backend_xid, " : "",
backend_minimum_version(9, 4) ? "backend_xmin, " : "",
backend_minimum_version(14, 0) ? "query_id, " : "",
backend_minimum_version(9, 2) ? "query, " : "current_query,",
backend_minimum_version(10, 0) ? "backend_type, " : "",
backend_minimum_version(9, 2) ? "pid" : "procpid"); // the last one is for the ORDER BY
snprintf(filename, sizeof(filename),
"%s/pg_stat_activity.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all bgwriter stats.
*/
void
sql_exec_dump_pgstatbgwriter()
{
char query[1024];
char filename[1024];
if (backend_minimum_version(17, 0))
{
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), buffers_clean, "
"maxwritten_clean, buffers_alloc, "
"date_trunc('seconds', stats_reset) AS stats_reset "
"FROM pg_stat_bgwriter ");
}
else
{
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), checkpoints_timed, "
"checkpoints_req, %sbuffers_checkpoint, buffers_clean, "
"maxwritten_clean, buffers_backend, %sbuffers_alloc%s "
"FROM pg_stat_bgwriter ",
backend_minimum_version(9, 2) ? "checkpoint_write_time, checkpoint_sync_time, " : "",
backend_minimum_version(9, 1) ? "buffers_backend_fsync, " : "",
backend_minimum_version(9, 1) ? ", date_trunc('seconds', stats_reset) AS stats_reset " : "");
}
snprintf(filename, sizeof(filename),
"%s/pg_stat_bgwriter.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all checkpointer stats.
*/
void
sql_exec_dump_pgstatcheckpointer()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), num_timed, num_requested, "
"restartpoints_timed, restartpoints_req, restartpoints_done, "
"write_time, sync_time, buffers_written, "
"date_trunc('seconds', stats_reset) AS stats_reset "
"FROM pg_stat_checkpointer ");
snprintf(filename, sizeof(filename),
"%s/pg_stat_checkpointer.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all archiver stats.
*/
void
sql_exec_dump_pgstatarchiver()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), archived_count, "
"last_archived_wal, date_trunc('seconds', last_archived_time) AS last_archived_time, "
"failed_count, "
"last_failed_wal, date_trunc('seconds', last_failed_time) AS last_failed_time, "
"date_trunc('seconds', stats_reset) AS stats_reset "
"FROM pg_stat_archiver ");
snprintf(filename, sizeof(filename),
"%s/pg_stat_archiver.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all databases stats.
*/
void
sql_exec_dump_pgstatdatabase()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), datid, datname, "
"numbackends, xact_commit, xact_rollback, blks_read, blks_hit"
"%s%s%s%s%s "
"FROM pg_stat_database "
"ORDER BY datname",
backend_minimum_version(8, 3) ? ", tup_returned, tup_fetched, tup_inserted, tup_updated, tup_deleted" : "",
backend_minimum_version(9, 1) ? ", conflicts, date_trunc('seconds', stats_reset) AS stats_reset" : "",
backend_minimum_version(9, 2) ? ", temp_files, temp_bytes, deadlocks, blk_read_time, blk_write_time" : "",
backend_minimum_version(12, 0) ? ", checksum_failures, checksum_last_failure" : "",
backend_minimum_version(14, 0) ? ", session_time, active_time, idle_in_transaction_time, sessions, sessions_abandoned, sessions_fatal, sessions_killed" : "");
snprintf(filename, sizeof(filename),
"%s/pg_stat_database.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all databases conflicts stats.
*/
void
sql_exec_dump_pgstatdatabaseconflicts()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), * "
"FROM pg_stat_database_conflicts "
"ORDER BY datname");
snprintf(filename, sizeof(filename),
"%s/pg_stat_database_conflicts.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all replication stats.
*/
void
sql_exec_dump_pgstatreplication()
{
char query[1024];
char filename[1024];
/* get the oid and database name from the system pg_database table */
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), %s, usesysid, usename, "
"application_name, client_addr, client_hostname, client_port, "
"date_trunc('seconds', backend_start) AS backend_start, %sstate, "
"%s AS master_location, %s%s"
"sync_priority, "
"sync_state%s "
"FROM pg_stat_replication "
"ORDER BY application_name",
backend_minimum_version(9, 2) ? "pid" : "procpid",
backend_minimum_version(9, 4) ? "backend_xmin, " : "",
backend_minimum_version(10, 0) ? "pg_current_wal_lsn()" : "pg_current_xlog_location()",
backend_minimum_version(10, 0) ? "sent_lsn, write_lsn, flush_lsn, replay_lsn, " :
"sent_location, write_location, flush_location, replay_location, ",
backend_minimum_version(10, 0) ? "write_lag, flush_lag, replay_lag, " : "",
backend_minimum_version(12, 0) ? ", reply_time" : "");
snprintf(filename, sizeof(filename),
"%s/pg_stat_replication.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all replication slots stats.
*/
void
sql_exec_dump_pgstatreplicationslots()
{
char query[1024];
char filename[1024];
/* get the oid and database name from the system pg_database table */
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), slot_name, "
"spill_txns, spill_count, spill_bytes, "
"stream_txns, stream_count, stream_bytes, "
"total_txns, total_bytes, "
"date_trunc('seconds', stats_reset) AS stats_reset "
"FROM pg_stat_replication_slots "
"ORDER BY slot_name");
snprintf(filename, sizeof(filename),
"%s/pg_stat_replication_slots.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all SLRU stats.
*/
void
sql_exec_dump_pgstatslru()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), name, "
"blks_zeroed, blks_hit, blks_read, blks_written, blks_exists, "
"flushes, truncates, "
"date_trunc('seconds', stats_reset) AS stats_reset "
"FROM pg_stat_slru "
"ORDER BY name");
snprintf(filename, sizeof(filename),
"%s/pg_stat_slru.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all subscriptions stats.
*/
void
sql_exec_dump_pgstatsubscription()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), subid, subname%s, "
"pid%s, relid, relname, received_lsn, "
"date_trunc('seconds', last_msg_send_time) AS last_msg_send_time, "
"date_trunc('seconds', last_msg_receipt_time) AS last_msg_receipt_time, "
"latest_end_lsn, date_trunc('seconds', latest_end_time) AS latest_end_time "
"FROM pg_stat_subscription s "
"LEFT JOIN pg_class c ON c.oid=s.relid "
"ORDER BY subid",
backend_minimum_version(17, 0) ? ", worker_type" : "",
backend_minimum_version(16, 0) ? ", leader_pid" : "");
snprintf(filename, sizeof(filename),
"%s/pg_stat_subscription.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all WAL stats.
*/
void
sql_exec_dump_pgstatwal()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), "
"wal_records, wal_fpi, wal_bytes, wal_buffers_full, wal_write, "
"wal_sync, wal_write_time, wal_sync_time, "
"date_trunc('seconds', stats_reset) AS stats_reset "
"FROM pg_stat_wal");
snprintf(filename, sizeof(filename),
"%s/pg_stat_wal.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all wal receiver stats.
*/
void
sql_exec_dump_pgstatwalreceiver()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT pid, status, receive_start_lsn, receive_start_tli, "
"written_lsn, flushed_lsn, received_tli, "
"date_trunc('seconds', last_msg_send_time) last_msg_send_time, "
"date_trunc('seconds', last_msg_receipt_time) last_msg_receipt_time, "
"latest_end_lsn, date_trunc('seconds', latest_end_time) latest_end_time, "
"slot_name, sender_host, sender_port, conninfo "
"FROM pg_stat_wal_receiver "
"ORDER BY pid");
snprintf(filename, sizeof(filename),
"%s/pg_stat_wal_receiver.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all tables stats.
*/
void
sql_exec_dump_pgstatalltables()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), relid, schemaname, relname, "
"seq_scan%s, seq_tup_read, idx_scan%s, idx_tup_fetch, "
"n_tup_ins, n_tup_upd, n_tup_del"
"%s%s%s%s%s%s%s "
"FROM pg_stat_all_tables "
"WHERE schemaname <> 'information_schema' "
"ORDER BY schemaname, relname",
backend_minimum_version(16, 0) ? ", date_trunc('seconds', last_seq_scan) AS last_seq_scan" : "",
backend_minimum_version(16, 0) ? ", date_trunc('seconds', last_idx_scan) AS last_idx_scan" : "",
backend_minimum_version(8, 3) ? ", n_tup_hot_upd" : "",
backend_minimum_version(16, 0) ? ", n_tup_newpage_upd" : "",
backend_minimum_version(8, 3) ? ", n_live_tup, n_dead_tup" : "",
backend_minimum_version(9, 4) ? ", n_mod_since_analyze" : "",
backend_minimum_version(13, 0) ? ", n_ins_since_vacuum" : "",
backend_minimum_version(8, 2) ? ", date_trunc('seconds', last_vacuum) AS last_vacuum, date_trunc('seconds', last_autovacuum) AS last_autovacuum, date_trunc('seconds',last_analyze) AS last_analyze, date_trunc('seconds',last_autoanalyze) AS last_autoanalyze" : "",
backend_minimum_version(9, 1) ? ", vacuum_count, autovacuum_count, analyze_count, autoanalyze_count" : "");
snprintf(filename, sizeof(filename),
"%s/pg_stat_all_tables.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all indexes stats.
*/
void
sql_exec_dump_pgstatallindexes()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), "
"relid, indexrelid, schemaname, relname, indexrelname, "
"idx_scan%s, idx_tup_read, idx_tup_fetch "
"FROM pg_stat_all_indexes "
"WHERE schemaname <> 'information_schema' "
"ORDER BY schemaname, relname",
backend_minimum_version(16, 0) ? ", date_trunc('seconds', last_idx_scan) AS last_idx_scan" : ""
);
snprintf(filename, sizeof(filename),
"%s/pg_stat_all_indexes.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all tables IO stats.
*/
void
sql_exec_dump_pgstatioalltables()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), * "
"FROM pg_statio_all_tables "
"WHERE schemaname <> 'information_schema' "
"ORDER BY schemaname, relname");
snprintf(filename, sizeof(filename),
"%s/pg_statio_all_tables.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all indexes IO stats.
*/
void
sql_exec_dump_pgstatioallindexes()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), * "
"FROM pg_statio_all_indexes "
"WHERE schemaname <> 'information_schema' "
"ORDER BY schemaname, relname");
snprintf(filename, sizeof(filename),
"%s/pg_statio_all_indexes.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all sequences IO stats.
*/
void
sql_exec_dump_pgstatioallsequences()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), * "
"FROM pg_statio_all_sequences "
"WHERE schemaname <> 'information_schema' "
"ORDER BY schemaname, relname");
snprintf(filename, sizeof(filename),
"%s/pg_statio_all_sequences.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all functions stats.
*/
void
sql_exec_dump_pgstatuserfunctions()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), * "
"FROM pg_stat_user_functions "
"WHERE schemaname <> 'information_schema' "
"ORDER BY schemaname, funcname");
snprintf(filename, sizeof(filename),
"%s/pg_stat_user_functions.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all size class stats.
*/
void
sql_exec_dump_pgclass_size()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), n.nspname, c.relname, c.relkind, "
"c.reltuples, c.relpages%s%s "
"FROM pg_class c "
"JOIN pg_namespace n ON n.oid=c.relnamespace "
"WHERE n.nspname <> 'information_schema' "
"ORDER BY n.nspname, c.relname",
backend_minimum_version(9, 2) ? ", c.relallvisible" : "",
backend_minimum_version(8, 1) ? ", pg_relation_size(c.oid)" : "");
snprintf(filename, sizeof(filename),
"%s/pg_class_size.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all statements stats.
* to be fixed wrt v14
*/
void
sql_exec_dump_pgstatstatements()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), r.rolname, d.datname, "
"%sregexp_replace(query, E'\n', ' ', 'g') as query, %scalls, %s, rows, "
"shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, "
"local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, "
"temp_blks_read, temp_blks_written%s%s%s%s%s%s "
"FROM pg_stat_statements q "
"LEFT JOIN pg_database d ON q.dbid=d.oid "
"LEFT JOIN pg_roles r ON q.userid=r.oid "
"ORDER BY r.rolname, d.datname",
backend_minimum_version(14, 0) ? "toplevel, queryid, " : "",
backend_minimum_version(13, 0) ? "plans, total_plan_time, min_plan_time, max_plan_time, mean_plan_time, stddev_plan_time, " : "",
backend_minimum_version(13, 0) ? "total_exec_time, min_exec_time, max_exec_time, mean_exec_time, stddev_exec_time" : "total_time",
backend_minimum_version(17, 0) ? ", shared_blk_read_time, shared_blk_write_time, local_blk_read_time, local_blk_write_time" : ", blk_read_time, blk_write_time",
backend_minimum_version(15, 0) ? ", temp_blk_read_time, temp_blk_write_time" : "",
backend_minimum_version(13, 0) ? ", wal_records, wal_fpi, wal_bytes" : "",
backend_minimum_version(15, 0) ? ", jit_functions, jit_generation_time, jit_inlining_count, jit_inlining_time, jit_optimization_count, jit_optimization_time, jit_emission_count, jit_emission_time" : "",
backend_minimum_version(17, 0) ? ", date_trunc('seconds', stats_since) AS stats_since " : "",
backend_minimum_version(17, 0) ? ", date_trunc('seconds', minmax_stats_since) AS minmax_stats_since " : "");
snprintf(filename, sizeof(filename),
"%s/pg_stat_statements.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump all xlog stats.
*/
void
sql_exec_dump_xlog_stat()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
backend_minimum_version(10, 0)
?
"SELECT date_trunc('seconds', now()), pg_walfile_name(pg_current_wal_lsn())=pg_ls_dir AS current, pg_ls_dir AS filename, "
"(SELECT modification FROM pg_stat_file('pg_wal/'||pg_ls_dir)) AS modification_timestamp "
"FROM pg_ls_dir('pg_wal') "
"WHERE pg_ls_dir ~ E'^[0-9A-F]{24}' "
"ORDER BY pg_ls_dir"
:
"SELECT date_trunc('seconds', now()), pg_xlogfile_name(pg_current_xlog_location())=pg_ls_dir AS current, pg_ls_dir AS filename, "
"(SELECT modification FROM pg_stat_file('pg_xlog/'||pg_ls_dir)) AS modification_timestamp "
"FROM pg_ls_dir('pg_xlog') "
"WHERE pg_ls_dir ~ E'^[0-9A-F]{24}' "
"ORDER BY pg_ls_dir");
snprintf(filename, sizeof(filename),
"%s/pg_xlog_stat.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump ANALYZE progress
*/
void
sql_exec_dump_pgstatprogressanalyze()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), pid, datid, datname, "
"relid, relid::regclass relname, phase, sample_blks_total, "
"sample_blks_scanned, ext_stats_total, ext_stats_computed, "
"child_tables_total, child_tables_done, current_child_table_relid, "
"current_child_table_relid::regclass current_child_table_relname "
"FROM pg_stat_progress_analyze "
"ORDER BY pid");
snprintf(filename, sizeof(filename),
"%s/pg_stat_progress_analyze.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump BASE BACKUP progress
*/
void
sql_exec_dump_pgstatprogressbasebackup()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), pid, phase, "
"backup_total, backup_streamed, "
"tablespaces_total, tablespaces_streamed "
"FROM pg_stat_progress_basebackup "
"ORDER BY pid");
snprintf(filename, sizeof(filename),
"%s/pg_stat_progress_basebackup.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump CLUSTER progress
*/
void
sql_exec_dump_pgstatprogresscluster()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), pid, datid, datname, "
"relid, relid::regclass relname, command, phase, "
"cluster_index_relid, cluster_index_relid::regclass cluster_index_relname, "
"heap_tuples_scanned, heap_tuples_written, heap_blks_total, "
"heap_blks_scanned, index_rebuild_count "
"FROM pg_stat_progress_cluster "
"ORDER BY pid");
snprintf(filename, sizeof(filename),
"%s/pg_stat_progress_cluster.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump COPY progress
*/
void
sql_exec_dump_pgstatprogresscopy()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), pid, datid, datname, "
"relid, relid::regclass relname, command, type, "
"bytes_processed, bytes_total, "
"tuples_processed, tuples_excluded%s "
"FROM pg_stat_progress_copy "
"ORDER BY pid",
backend_minimum_version(17, 0) ? ", tuples_excluded" : "");
snprintf(filename, sizeof(filename),
"%s/pg_stat_progress_copy.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump CREATE INDEX progress
*/
void
sql_exec_dump_pgstatprogresscreateindex()
{
char query[1024];
char filename[1024];
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), pid, datid, datname, "
"relid, relid::regclass relname, index_relid, index_relid::regclass index_relname, "
"command, phase, lockers_total, lockers_done, current_locker_pid, "
"blocks_total, blocks_done, tuples_total, tuples_done, "
"partitions_total, partitions_done "
"FROM pg_stat_progress_create_index "
"ORDER BY pid");
snprintf(filename, sizeof(filename),
"%s/pg_stat_progress_create_index.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Dump VACUUM progress
*/
void
sql_exec_dump_pgstatprogressvacuum()
{
char query[1024];
char filename[1024];
/* get the oid and database name from the system pg_database table */
snprintf(query, sizeof(query),
"SELECT date_trunc('seconds', now()), pid, datid, datname, "
"relid, relid::regclass relname, phase, "
"heap_blks_total, heap_blks_scanned, heap_blks_vacuumed, "
"index_vacuum_count, %s, %s "
"FROM pg_stat_progress_vacuum "
"ORDER BY pid",
backend_minimum_version(17, 0) ? "max_dead_tuple_bytes" : "max_dead_tuples",
backend_minimum_version(17, 0) ? "dead_tuple_bytes" : "num_dead_tuples");
snprintf(filename, sizeof(filename),
"%s/pg_stat_progress_vacuum.csv", opts->directory);
sql_exec(query, filename, opts->quiet);
}
/*
* Fetch PostgreSQL major and minor numbers
*/
void
fetch_version()
{
char query[1024];
PGresult *res;