-
Notifications
You must be signed in to change notification settings - Fork 17
/
pgstat.c
4760 lines (4311 loc) · 152 KB
/
pgstat.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
/*
* pgstat, a PostgreSQL app to gather statistical informations
* from a PostgreSQL database, and act like a vmstat tool.
*
* This software is released under the PostgreSQL Licence.
*
* Guillaume Lelarge, guillaume@lelarge.info, 2014-2024.
*
* pgstats/pgstat.c
*/
/*
* System headers
*/
#include <sys/ioctl.h>
/*
* PostgreSQL headers
*/
#include "postgres_fe.h"
#include "common/logging.h"
#include "fe_utils/connect_utils.h"
#include "libpq/pqsignal.h"
/*
* Defines
*/
#define PGSTAT_VERSION "1.4.0"
#define PGSTAT_DEFAULT_LINES 20
#define PGSTAT_DEFAULT_STRING_SIZE 1024
#define PGSTAT_OLDEST_STAT_RESET "0001-01-01"
#define half_rounded(x) (((x) + ((x) < 0 ? -1 : 1)) / 2)
/*
* Structs and enums
*/
/* units enum */
typedef enum
{
NO_UNIT = 0,
ALL_UNIT,
SIZE_UNIT
} unit_t;
/* stats enum */
typedef enum
{
NONE = 0,
ARCHIVER,
BGWRITER,
BUFFERCACHE,
CHECKPOINTER,
CONNECTION,
DATABASE,
TABLE,
TABLEIO,
INDEX,
FUNCTION,
STATEMENT,
SLRU,
XLOG,
DEADLIVE,
TEMPFILE,
REPSLOTS,
WAITEVENT,
WAL,
PROGRESS_ANALYZE,
PROGRESS_BASEBACKUP,
PROGRESS_CLUSTER,
PROGRESS_COPY,
PROGRESS_CREATEINDEX,
PROGRESS_VACUUM,
PBPOOLS,
PBSTATS
} stat_t;
/* these are the options structure for command line parameters */
struct options
{
/* misc */
bool verbose;
bool dontredisplayheader;
stat_t stat;
char *substat;
char *filter;
bool human_readable;
/* connection parameters */
char *dbname;
char *hostname;
char *port;
char *username;
/* version number */
int major;
int minor;
/* extension namespace (pg_stat_statements or pg_buffercache) */
char *namespace;
/* frequency */
int interval;
int count;
};
/* structs for pretty printing */
struct size_pretty_unit
{
const char *name;
long limit;
bool round;
long unitbits;
};
struct nosize_pretty_unit
{
const char *name;
long limit;
bool round;
long divider;
};
/* pg_stat_archiver struct */
struct pgstatarchiver
{
long archived_count;
/*
we don't put these columns here because it makes no sense to get a diff between the new and the old values
? last_archived_wal;
? last_archived_time;
*/
long failed_count;
/*
we don't put these columns here because it makes no sense to get a diff between the new and the old values
? last_failed_wal;
? last_failed_time;
*/
char *stats_reset;
};
/* pg_stat_bgwriter struct */
struct pgstatbgwriter
{
long buffers_clean;
long maxwritten_clean;
long buffers_alloc;
char *stats_reset;
};
/* pg_stat_checkpointer struct */
struct pgstatcheckpointer
{
long checkpoints_timed; /* real name is num_timed */
long checkpoints_requested; /* real name is num_requested */
long restartpoints_timed;
long restartpoints_requested; /* real name is restartpoints_req */
long restartpoints_done;
long write_time;
long sync_time;
long buffers_written;
char *stats_reset;
};
/* pg_stat_database struct */
struct pgstatdatabase
{
/*
we don't put numbackends here because it makes no sense to get a diff between the new and the old values
long numbackends;
*/
long xact_commit;
long xact_rollback;
long blks_read;
long blks_hit;
long tup_returned;
long tup_fetched;
long tup_inserted;
long tup_updated;
long tup_deleted;
long conflicts;
long temp_files;
long temp_bytes;
long deadlocks;
long checksum_failures;
/* checksum_last_failure */
float blk_read_time;
float blk_write_time;
float session_time;
float active_time;
float idle_in_transaction_time;
long sessions;
long sessions_abandoned;
long sessions_fatal;
long sessions_killed;
char *stats_reset;
};
/* pg_stat_all_tables struct */
struct pgstattable
{
long seq_scan;
/*
we don't put the timestamps here because it makes no sense to get a diff between the new and the old values
? last_seq_scan;
*/
long seq_tup_read;
long idx_scan;
/*
we don't put the timestamps here because it makes no sense to get a diff between the new and the old values
? last_idx_scan;
*/
long idx_tup_fetch;
long n_tup_ins;
long n_tup_upd;
long n_tup_del;
long n_tup_hot_upd;
long n_tup_newpage_upd;
long n_live_tup;
long n_dead_tup;
long n_mod_since_analyze;
long n_ins_since_vacuum;
/*
we don't put the timestamps here because it makes no sense to get a diff between the new and the old values
? last_vacuum;
? last_autovacuum;
? last_analyze;
? last_autoanalyze;
*/
long vacuum_count;
long autovacuum_count;
long analyze_count;
long autoanalyze_count;
};
/* pg_statio_all_tables struct */
struct pgstattableio
{
long heap_blks_read;
long heap_blks_hit;
long idx_blks_read;
long idx_blks_hit;
long toast_blks_read;
long toast_blks_hit;
long tidx_blks_read;
long tidx_blks_hit;
};
/* pg_stat_all_indexes struct */
struct pgstatindex
{
long idx_scan;
/*
we don't put the timestamps here because it makes no sense to get a diff between the new and the old values
? last_idx_scan;
*/
long idx_tup_read;
long idx_tup_fetch;
};
/* pg_stat_user_functions struct */
struct pgstatfunction
{
long calls;
float total_time;
float self_time;
};
/* pg_stat_statements struct */
struct pgstatstatement
{
/*
long userid;
long dbid;
long queryid;
text query;
*/
long plans;
float total_plan_time;
/*
float min_plan_time;
float max_plan_time;
float mean_plan_time;
float stddev_plan_time;
*/
long calls;
float total_exec_time;
/*
float min_exec_time;
float max_exec_time;
float mean_exec_time;
float stddev_exec_time;
*/
long rows;
long shared_blks_hit;
long shared_blks_read;
long shared_blks_dirtied;
long shared_blks_written;
long local_blks_hit;
long local_blks_read;
long local_blks_dirtied;
long local_blks_written;
long temp_blks_read;
long temp_blks_written;
float shared_blk_read_time; /* 9.2 - 16, blk_read_time */
float shared_blk_write_time; /* 9.2 - 16, blk_write_time */
float local_blk_read_time; /* 9.2 - 16, blk_read_time */
float local_blk_write_time; /* 9.2 - 16, blk_write_time */
float temp_blk_read_time; /* 9.2 - 15, blk_read_time */
float temp_blk_write_time; /* 9.2 - 15, blk_write_time */
long wal_records;
long wal_fpi;
long wal_bytes;
long jit_functions;
float jit_generation_time;
long jit_inlining_count;
float jit_inlining_time;
long jit_optimization_count;
float jit_optimization_time;
long jit_emission_count;
float jit_emission_time;
long jit_deform_count;
float jit_deform_time;
char *stats_since;
char *minmax_stats_since;
};
/* pg_stat_slru struct */
struct pgstatslru
{
long blks_zeroed;
long blks_hit;
long blks_read;
long blks_written;
long blks_exists;
long flushes;
long truncates;
char *stats_reset;
};
/* pg_stat_wal struct */
struct pgstatwal
{
long wal_records;
long wal_fpi;
long wal_bytes;
long wal_buffers_full;
long wal_write;
long wal_sync;
float wal_write_time;
float wal_sync_time;
char *stats_reset;
};
/* deadlivestats struct */
struct deadlivestats
{
long live;
long dead;
};
/* repslots struct */
/* TODO : there is a lot of other informations, might want to check them */
struct repslots
{
char *currentlocation;
char *restartlsn;
long restartlsndiff;
};
/* xlogstats struct */
struct xlogstats
{
char *location;
long locationdiff;
};
/* pgBouncer stats struct */
struct pgbouncerstats
{
long total_request;
long total_received;
long total_sent;
long total_query_time;
/* not used yet
float avg_req;
float avg_recv;
float avg_sent;
float avg_query;
*/
};
/*
* Global variables
*/
PGconn *conn;
struct options *opts;
extern char *optarg;
struct pgstatarchiver *previous_pgstatarchiver;
struct pgstatbgwriter *previous_pgstatbgwriter;
struct pgstatcheckpointer *previous_pgstatcheckpointer;
struct pgstatdatabase *previous_pgstatdatabase;
struct pgstattable *previous_pgstattable;
struct pgstattableio *previous_pgstattableio;
struct pgstatindex *previous_pgstatindex;
struct pgstatfunction *previous_pgstatfunction;
struct pgstatstatement *previous_pgstatstatement;
struct pgstatslru *previous_pgstatslru;
struct pgstatwal *previous_pgstatwal;
struct xlogstats *previous_xlogstats;
struct deadlivestats *previous_deadlivestats;
struct repslots *previous_repslots;
struct pgbouncerstats *previous_pgbouncerstats;
int hdrcnt = 0;
volatile sig_atomic_t wresized;
static int winlines = PGSTAT_DEFAULT_LINES;
static const struct size_pretty_unit size_pretty_units[] = {
{" b", 10 * 1024, false, 0},
{"kB", 20 * 1024 - 1, true, 10},
{"MB", 20 * 1024 - 1, true, 20},
{"GB", 20 * 1024 - 1, true, 30},
{"TB", 20 * 1024 - 1, true, 40},
{"PB", 20 * 1024 - 1, true, 50},
{NULL, 0, false, 0}
};
static const struct nosize_pretty_unit nosize_pretty_units[] = {
{" ", 10 * 1000, false, 1000},
{"k", 20 * 1000 - 1, true, 1000},
{"M", 20 * 1000 - 1, true, 1000},
{"G", 20 * 1000 - 1, true, 1000},
{"T", 20 * 1000 - 1, true, 1000},
{"P", 20 * 1000 - 1, true, 1000},
{NULL, 0, false, 0}
};
/*
* Function prototypes
*/
static void help(const char *progname);
void get_opts(int, char **);
#ifndef FE_MEMUTILS_H
void *pg_malloc(size_t size);
char *pg_strdup(const char *in);
#endif
char *pg_size_pretty(long long size);
char *pg_nosize_pretty(long long size);
void format(char *r, long long value, long length, unit_t SIZE_UNIT);
void format_time(char *r, float value, long length);
void print_pgstatarchiver(void);
void print_pgstatbgwriter(void);
void print_pgstatcheckpointer(void);
void print_pgstatconnection(void);
void print_pgstatdatabase(void);
void print_pgstattable(void);
void print_pgstattableio(void);
void print_pgstatindex(void);
void print_pgstatfunction(void);
void print_pgstatstatement(void);
void print_pgstatslru(void);
void print_pgstatwal(void);
void print_pgstatprogressanalyze(void);
void print_pgstatprogressbasebackup(void);
void print_pgstatprogresscluster(void);
void print_pgstatprogresscopy(void);
void print_pgstatprogresscreateindex(void);
void print_pgstatprogressvacuum(void);
void print_pgstatwaitevent(void);
void print_buffercache(void);
void print_deadlivestats(void);
void print_repslotsstats(void);
void print_tempfilestats(void);
void print_xlogstats(void);
void print_pgbouncerpools(void);
void print_pgbouncerstats(void);
void fetch_version(void);
char *fetch_setting(char *name);
void fetch_pgbuffercache_namespace(void);
void fetch_pgstatstatements_namespace(void);
bool backend_minimum_version(int major, int minor);
void print_header(void);
void print_line(void);
void allocate_struct(void);
static void needhdr(int dummy);
static void needresize(int);
void doresize(void);
static void quit_properly(SIGNAL_ARGS);
/*
* Print help message
*/
static void
help(const char *progname)
{
printf("%s gathers statistics from a PostgreSQL database.\n\n"
"Usage:\n"
" %s [OPTIONS] [delay [count]]\n"
"\nGeneral options:\n"
" -f FILTER include only this object\n"
" (only works for database, table, tableio,\n"
" index, function, statement statistics,\n"
" replication slots, and slru)\n"
" -H display human-readable values\n"
" -n do not redisplay header\n"
" -s STAT stats to collect\n"
" -S SUBSTAT part of stats to display\n"
" (only works for database and statement)\n"
" -v verbose\n"
" -?|--help show this help, then exit\n"
" -V|--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"
" -d DBNAME database to connect to\n"
"\nThe default stat is pg_stat_bgwriter, but you can change it with\n"
"the -s command line option, and one of its value (STAT):\n"
" * archiver for pg_stat_archiver (only for 9.4+)\n"
" * bgwriter for pg_stat_bgwriter\n"
" * buffercache for pg_buffercache (needs the extension)\n"
" * checkpointer for pg_stat_bgwriter (<17) or\n"
" for pg_stat_checkpointer (17+)\n"
" * connection (only for 9.2+)\n"
" * database for pg_stat_database\n"
" * table for pg_stat_all_tables\n"
" * tableio for pg_statio_all_tables\n"
" * index for pg_stat_all_indexes\n"
" * function for pg_stat_user_function\n"
" * statement for pg_stat_statements (needs the extension)\n"
" * slru for pg_stat_slru (only for 13+)\n"
" * xlog for xlog writes (only for 9.2+)\n"
" * deadlive for dead/live tuples stats\n"
" * repslots for replication slots\n"
" * tempfile for temporary file usage\n"
" * waitevent for wait events usage\n"
" * wal for pg_stat_wal (only for 14+)\n"
" * progress_analyze for analyze progress monitoring (only for\n"
" 13+)\n"
" * progress_basebackup for base backup progress monitoring (only\n"
" for 13+)\n"
" * progress_cluster for cluster progress monitoring (only for\n"
" 12+)\n"
" * progress_copy for copy progress monitoring (only for\n"
" 14+)\n"
" * progress_createindex for create index progress monitoring (only\n"
" for 12+)\n"
" * progress_vacuum for vacuum progress monitoring (only for\n"
" 9.6+)\n"
" * pbpools for pgBouncer pools statistics\n"
" * pbstats for pgBouncer statistics\n\n"
"Report bugs to <guillaume@lelarge.info>.\n",
progname, progname);
}
/*
* 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->verbose = false;
opts->dontredisplayheader = false;
opts->stat = NONE;
opts->substat = NULL;
opts->filter = NULL;
opts->human_readable = false;
opts->dbname = NULL;
opts->hostname = NULL;
opts->port = NULL;
opts->username = NULL;
opts->namespace = NULL;
opts->interval = 1;
opts->count = -1;
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("pgstats " PGSTAT_VERSION " (compiled with PostgreSQL " PG_VERSION ")");
exit(0);
}
}
/* get opts */
while ((c = getopt(argc, argv, "h:Hp:U:d:f:ns:S:v")) != -1)
{
switch (c)
{
/* specify the database */
case 'd':
opts->dbname = pg_strdup(optarg);
break;
/* specify the filter */
case 'f':
opts->filter = pg_strdup(optarg);
break;
/* do not redisplay the header */
case 'n':
opts->dontredisplayheader = true;
break;
/* don't show headers */
case 'v':
opts->verbose = true;
break;
/* specify the stat */
case 's':
if (opts->stat != NONE)
{
pg_log_error("You can only use once the -s command line switch.\n");
exit(EXIT_FAILURE);
}
if (!strcmp(optarg, "archiver"))
{
opts->stat = ARCHIVER;
}
else if (!strcmp(optarg, "bgwriter"))
{
opts->stat = BGWRITER;
}
else if (!strcmp(optarg, "buffercache"))
{
opts->stat = BUFFERCACHE;
}
else if (!strcmp(optarg, "checkpointer"))
{
opts->stat = CHECKPOINTER;
}
else if (!strcmp(optarg, "connection"))
{
opts->stat = CONNECTION;
}
else if (!strcmp(optarg, "database"))
{
opts->stat = DATABASE;
}
else if (!strcmp(optarg, "table"))
{
opts->stat = TABLE;
}
else if (!strcmp(optarg, "tableio"))
{
opts->stat = TABLEIO;
}
else if (!strcmp(optarg, "index"))
{
opts->stat = INDEX;
}
else if (!strcmp(optarg, "function"))
{
opts->stat = FUNCTION;
}
else if (!strcmp(optarg, "statement"))
{
opts->stat = STATEMENT;
}
else if (!strcmp(optarg, "slru"))
{
opts->stat = SLRU;
}
else if (!strcmp(optarg, "wal"))
{
opts->stat = WAL;
}
else if (!strcmp(optarg, "xlog"))
{
opts->stat = XLOG;
}
else if (!strcmp(optarg, "deadlive"))
{
opts->stat = DEADLIVE;
}
else if (!strcmp(optarg, "repslots"))
{
opts->stat = REPSLOTS;
}
else if (!strcmp(optarg, "tempfile"))
{
opts->stat = TEMPFILE;
}
else if (!strcmp(optarg, "waitevent"))
{
opts->stat = WAITEVENT;
}
else if (!strcmp(optarg, "progress_analyze"))
{
opts->stat = PROGRESS_ANALYZE;
}
else if (!strcmp(optarg, "progress_basebackup"))
{
opts->stat = PROGRESS_BASEBACKUP;
}
else if (!strcmp(optarg, "progress_cluster"))
{
opts->stat = PROGRESS_CLUSTER;
}
else if (!strcmp(optarg, "progress_copy"))
{
opts->stat = PROGRESS_COPY;
}
else if (!strcmp(optarg, "progress_createindex"))
{
opts->stat = PROGRESS_CREATEINDEX;
}
else if (!strcmp(optarg, "progress_vacuum"))
{
opts->stat = PROGRESS_VACUUM;
}
else if (!strcmp(optarg, "pbpools"))
{
opts->stat = PBPOOLS;
}
else if (!strcmp(optarg, "pbstats"))
{
opts->stat = PBSTATS;
}
else
{
pg_log_error("Unknown service \"%s\".\n", optarg);
pg_log_info("Try \"%s --help\" for more information.\n", progname);
exit(EXIT_FAILURE);
}
break;
/* specify the substat */
case 'S':
opts->substat = pg_strdup(optarg);
break;
/* host to connect to */
case 'h':
opts->hostname = pg_strdup(optarg);
break;
/* display human-readable values */
case 'H':
opts->human_readable = true;
break;
/* port to connect to on remote host */
case 'p':
opts->port = pg_strdup(optarg);
break;
/* username */
case 'U':
opts->username = pg_strdup(optarg);
break;
default:
pg_log_error("Try \"%s --help\" for more information.\n", progname);
exit(EXIT_FAILURE);
}
}
if (optind < argc)
{
opts->interval = atoi(argv[optind]);
if (opts->interval == 0)
{
pg_log_error("Invalid delay.\n");
pg_log_info("Try \"%s --help\" for more information.\n", progname);
exit(EXIT_FAILURE);
}
optind++;
}
if (optind < argc)
{
opts->count = atoi(argv[optind]);
if (opts -> count == 0)
{
pg_log_error("Invalid count.\n");
pg_log_info("Try \"%s --help\" for more information.\n", progname);
exit(EXIT_FAILURE);
}
}
if (opts->stat == PBPOOLS || opts->stat == PBSTATS)
{
/*
* Set (or override) database name.
* It should always be pgbouncer
*/
opts->dbname = pg_strdup("pgbouncer");
}
if (opts->dbname == NULL)
{
/*
* We want to use dbname for possible error reports later,
* and in case someone has set and is using PGDATABASE
* in its environment preserve that name for later usage
*/
if (!getenv("PGDATABASE"))
opts->dbname = "postgres";
else
opts->dbname = getenv("PGDATABASE");
}
}
#ifndef FE_MEMUTILS_H
/*
* "Safe" wrapper around malloc().
*/
void *
pg_malloc(size_t size)
{
void *tmp;
/* Avoid unportable behavior of malloc(0) */
if (size == 0)
size = 1;
tmp = malloc(size);
if (!tmp)
{
pg_log_error("out of memory (pg_malloc)\n");
exit(EXIT_FAILURE);
}
return tmp;
}
/*
* "Safe" wrapper around strdup().
*/
char *
pg_strdup(const char *in)
{
char *tmp;
if (!in)
{
pg_log_error("cannot duplicate null pointer (internal error)\n");
exit(EXIT_FAILURE);
}
tmp = strdup(in);
if (!tmp)
{
pg_log_error("out of memory (pg_strdup)\n");
exit(EXIT_FAILURE);
}
return tmp;
}
#endif
/*
* Display metrics with a size unit
*/
char *pg_size_pretty(long long size)
{
char *buf;
const struct size_pretty_unit *SIZE_UNIT;
buf = malloc( sizeof(char) * (64+1));
for (SIZE_UNIT = size_pretty_units; SIZE_UNIT->name != NULL; SIZE_UNIT++)
{
long bits;
long long abs_size = size < 0 ? 0 - size : size;
if (SIZE_UNIT[1].name == NULL || abs_size < SIZE_UNIT->limit)
{
if (SIZE_UNIT->round)
size = half_rounded(size);
snprintf(buf, sizeof(buf), "%lld %s", size, SIZE_UNIT->name);
break;
}
bits = (SIZE_UNIT[1].unitbits - SIZE_UNIT->unitbits - (SIZE_UNIT[1].round == true)
+ (SIZE_UNIT->round == true));
size /= 1 << bits;
}
return(buf);
}
/*
* Display metrics with a unit
*/
char *pg_nosize_pretty(long long size)
{
char *buf;
const struct nosize_pretty_unit *SIZE_UNIT;
buf = malloc( sizeof(char) * (64+1));
for (SIZE_UNIT = nosize_pretty_units; SIZE_UNIT->name != NULL; SIZE_UNIT++)
{
if (SIZE_UNIT[1].name == NULL || size < SIZE_UNIT->limit)
{
snprintf(buf, sizeof(buf), "%lld %s", size, SIZE_UNIT->name);
break;
}
size = size / SIZE_UNIT->divider;
}
return(buf);
}
/*
* Format a long long value as a string
*/
void format(char *r, long long value, long length, unit_t unit)
{
char v[64] = "";
// check if pretty print
if (unit == NO_UNIT)
{
sprintf(v, "%lld", value);
}
else
{
long long abs_value = value < 0 ? 0 - value : value;
sprintf(v, "%s%s",
value < 0 ? "-":"",
unit == SIZE_UNIT ? pg_size_pretty(abs_value) : pg_nosize_pretty(abs_value)
);
}
// check for overflow
if (length < strlen(v))
{
// Overflow!
sprintf(v, "!OF!");
}
// initialize with empty string
strcpy(r, "");
// add spaces
for(long i=0; i<length-strlen(v); i++)
strcat(r, " ");
// add value
strcat(r, v);
}
/*
* Format a duration value as a string
*/
void format_time(char *r, float value, long length)
{
long value_int;
char v[64] = "";
// format the value
value_int = value*100;
sprintf(v, "%ld.%d", value_int/100, abs(value_int)%100);
// check for overflow
if (length < strlen(v))
{
// Overflow!
sprintf(v, "!OF!");
}
// allocate the string
strcpy(r, "");
// add spaces
for(long i=0; i<length-strlen(v); i++)
strcat(r, " ");
// add value
strcat(r, v);
}
/*
* Dump all archiver stats.
*/
void
print_pgstatarchiver()
{
char sql[PGSTAT_DEFAULT_STRING_SIZE];
PGresult *res;
int nrows;