/* Copyright (c) 2007, Mark Wong */
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "display.h"
#include "pg.h"
#include "pg_top.h"
#define QUERY_PROCESSES \
"SELECT pid, query\n" \
"FROM pg_stat_activity;"
#define QUERY_PROCESSES_9_1 \
"SELECT procpid, current_query\n" \
"FROM pg_stat_activity;"
#define CURRENT_QUERY \
"SELECT query\n" \
"FROM pg_stat_activity\n" \
"WHERE pid = %d;"
#define CURRENT_QUERY_9_1 \
"SELECT current_query\n" \
"FROM pg_stat_activity\n" \
"WHERE procpid = %d;"
#define GET_LOCKS \
"SELECT datname, relname, mode, granted\n" \
"FROM pg_stat_activity, pg_locks\n" \
"LEFT OUTER JOIN pg_class\n" \
"ON relation = pg_class.oid\n"\
"WHERE pg_stat_activity.pid = %d\n" \
" AND pg_stat_activity.pid = pg_locks.pid;"
#define GET_LOCKS_9_1 \
"SELECT datname, relname, mode, granted\n" \
"FROM pg_stat_activity, pg_locks\n" \
"LEFT OUTER JOIN pg_class\n" \
"ON relation = pg_class.oid\n"\
"WHERE procpid = %d\n" \
" AND procpid = pid;"
char *index_ordernames[] = {
"idx_scan", "idx_tup_fetch", "idx_tup_read", NULL
};
char *table_ordernames[] = {
"seq_scan", "seq_tup_read", "idx_scan", "idx_tup_fetch", "n_tup_ins",
"n_tup_upd", "n_tup_del", NULL
};
int (*table_compares[]) () =
{
compare_seq_scan,
compare_seq_tup_read,
compare_idx_scan_t,
compare_idx_tup_fetch_t,
compare_n_tup_ins,
compare_n_tup_upd,
compare_n_tup_del,
NULL
};
int (*index_compares[]) () =
{
compare_idx_scan,
compare_idx_tup_fetch,
compare_idx_tup_read,
NULL
};
struct index_node
{
long long indexrelid;
/* Index to the index name in the PGresult object. */
int name_index;
/* The change in the previous values and current values. */
long long diff_idx_scan;
long long diff_idx_tup_read;
long long diff_idx_tup_fetch;
/* The previous values. */
long long old_idx_scan;
long long old_idx_tup_read;
long long old_idx_tup_fetch;
/* The value totals. */
long long total_idx_scan;
long long total_idx_tup_read;
long long total_idx_tup_fetch;
struct index_node *next;
};
struct table_node
{
long long relid;
/* Index to the relation name in the PGresult object. */
int name_index;
/* The change in the previous values and current values. */
long long diff_idx_scan;
long long diff_idx_tup_fetch;
long long diff_n_tup_del;
long long diff_n_tup_ins;
long long diff_n_tup_upd;
long long diff_seq_scan;
long long diff_seq_tup_read;
/* The previous values. */
long long old_idx_scan;
long long old_idx_tup_fetch;
long long old_n_tup_del;
long long old_n_tup_ins;
long long old_n_tup_upd;
long long old_seq_scan;
long long old_seq_tup_read;
/* The value totals. */
long long total_idx_scan;
long long total_idx_tup_fetch;
long long total_n_tup_del;
long long total_n_tup_ins;
long long total_n_tup_upd;
long long total_seq_scan;
long long total_seq_tup_read;
struct table_node *next;
};
struct index_node *get_index_stats(struct index_node *, long long);
struct index_node *insert_index_stats(struct index_node *, struct index_node *);
struct index_node *new_index_node(long long);
void update_index_stats(struct index_node *, long long, long long, long long);
struct index_node *upsert_index_stats(struct index_node *, long long,
long long, long long, long long);
struct table_node *get_table_stats(struct table_node *, long long);
struct table_node *insert_table_stats(struct table_node *, struct table_node *);
struct table_node *new_table_node(long long);
void update_table_stats(struct table_node *, long long, long long, long long,
long long, long long, long long, long long);
struct table_node *upsert_table_stats(struct table_node *, long long,
long long, long long, long long, long long, long long, long long,
long long);
float pg_version(PGconn *);
int
compare_idx_scan(const void *vp1, const void *vp2)
{
struct index_node **pp1 = (struct index_node **) <