Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PostgreSQL 17. #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions agent/bin/collector_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ SELECT \
s.local_blks_written, \
s.temp_blks_read, \
s.temp_blks_written, \
s.blk_read_time, \
s.blk_write_time, \
s.shared_blk_read_time, \
s.shared_blk_write_time, \
s.temp_blk_read_time, \
s.temp_blk_write_time \
FROM \
Expand Down Expand Up @@ -199,8 +199,6 @@ WHERE \
SELECT \
buffers_clean, \
maxwritten_clean, \
buffers_backend, \
buffers_backend_fsync, \
buffers_alloc \
FROM \
pg_stat_bgwriter"
Expand Down
32 changes: 8 additions & 24 deletions agent/bin/pg_statsrepo.sql
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ CREATE TABLE statsrepo.statement
local_blks_written bigint,
temp_blks_read bigint,
temp_blks_written bigint,
blk_read_time double precision,
blk_write_time double precision,
shared_blk_read_time double precision,
shared_blk_write_time double precision,
temp_blk_read_time double precision,
temp_blk_write_time double precision,
FOREIGN KEY (snapid) REFERENCES statsrepo.snapshot (snapid) ON DELETE CASCADE,
Expand Down Expand Up @@ -560,8 +560,6 @@ CREATE TABLE statsrepo.bgwriter
snapid bigint,
buffers_clean bigint,
maxwritten_clean bigint,
buffers_backend bigint,
buffers_backend_fsync bigint,
buffers_alloc bigint,
FOREIGN KEY (snapid) REFERENCES statsrepo.snapshot (snapid) ON DELETE CASCADE
);
Expand Down Expand Up @@ -3106,8 +3104,8 @@ CREATE FUNCTION statsrepo.get_query_activity_statements(
OUT calls bigint,
OUT total_exec_time numeric,
OUT time_per_call numeric,
OUT blk_read_time numeric,
OUT blk_write_time numeric,
OUT shared_blk_read_time numeric,
OUT shared_blk_write_time numeric,
OUT tmp_blk_read_time numeric,
OUT tmp_blk_write_time numeric,
OUT dbid oid,
Expand All @@ -3128,8 +3126,8 @@ $$
t1.total_exec_time::numeric(30, 3),
CASE t1.calls
WHEN 0 THEN 0 ELSE (t1.total_exec_time / t1.calls)::numeric(30, 3) END,
t1.blk_read_time::numeric(30, 3),
t1.blk_write_time::numeric(30, 3),
t1.shared_blk_read_time::numeric(30, 3),
t1.shared_blk_write_time::numeric(30, 3),
t1.tmp_blk_read_time::numeric(30, 3),
t1.tmp_blk_write_time::numeric(30, 3),
t1.dbid,
Expand All @@ -3149,8 +3147,8 @@ $$
statsrepo.sub(st2.total_plan_time, st1.total_plan_time) AS total_plan_time,
statsrepo.sub(st2.calls, st1.calls) AS calls,
statsrepo.sub(st2.total_exec_time, st1.total_exec_time) AS total_exec_time,
statsrepo.sub(st2.blk_read_time, st1.blk_read_time) AS blk_read_time,
statsrepo.sub(st2.blk_write_time, st1.blk_write_time) AS blk_write_time,
statsrepo.sub(st2.shared_blk_read_time, st1.shared_blk_read_time) AS shared_blk_read_time,
statsrepo.sub(st2.shared_blk_write_time, st1.shared_blk_write_time) AS shared_blk_write_time,
statsrepo.sub(st2.temp_blk_read_time, st1.temp_blk_read_time) AS tmp_blk_read_time,
statsrepo.sub(st2.temp_blk_write_time, st1.temp_blk_write_time) AS tmp_blk_write_time
FROM
Expand Down Expand Up @@ -3508,17 +3506,13 @@ CREATE FUNCTION statsrepo.get_bgwriter_tendency(
IN snapid_end bigint,
OUT "timestamp" text,
OUT bgwriter_write_tps numeric,
OUT backend_write_tps numeric,
OUT backend_fsync_tps numeric,
OUT bgwriter_stopscan_tps numeric,
OUT buffer_alloc_tps numeric
) RETURNS SETOF record AS
$$
SELECT
t.timestamp,
statsrepo.tps(t.bgwriter_write, t.duration),
statsrepo.tps(t.backend_write, t.duration),
statsrepo.tps(t.backend_fsync, t.duration),
statsrepo.tps(t.bgwriter_stopscan, t.duration),
statsrepo.tps(t.buffer_alloc, t.duration)
FROM
Expand All @@ -3527,8 +3521,6 @@ $$
s.snapid,
pg_catalog.to_char(s.time, 'YYYY-MM-DD HH24:MI') AS timestamp,
b.buffers_clean - pg_catalog.lag(b.buffers_clean) OVER w AS bgwriter_write,
b.buffers_backend - pg_catalog.lag(b.buffers_backend) OVER w AS backend_write,
b.buffers_backend_fsync - pg_catalog.lag(b.buffers_backend_fsync) OVER w AS backend_fsync,
b.maxwritten_clean - pg_catalog.lag(b.maxwritten_clean) OVER w AS bgwriter_stopscan,
b.buffers_alloc - pg_catalog.lag(b.buffers_alloc) OVER w AS buffer_alloc,
s.time - pg_catalog.lag(s.time) OVER w AS duration
Expand All @@ -3554,21 +3546,13 @@ CREATE OR REPLACE FUNCTION statsrepo.get_bgwriter_stats(
IN snapid_end bigint,
OUT bgwriter_write_avg numeric,
OUT bgwriter_write_max numeric,
OUT backend_write_avg numeric,
OUT backend_write_max numeric,
OUT backend_fsync_avg numeric,
OUT backend_fsync_max numeric,
OUT bgwriter_stopscan_avg numeric,
OUT buffer_alloc_avg numeric
) RETURNS SETOF record AS
$$
SELECT
pg_catalog.round(pg_catalog.avg(bgwriter_write_tps), 3),
pg_catalog.round(pg_catalog.max(bgwriter_write_tps), 3),
pg_catalog.round(pg_catalog.avg(backend_write_tps), 3),
pg_catalog.round(pg_catalog.max(backend_write_tps), 3),
pg_catalog.round(pg_catalog.avg(backend_fsync_tps), 3),
pg_catalog.round(pg_catalog.max(backend_fsync_tps), 3),
pg_catalog.round(pg_catalog.avg(bgwriter_stopscan_tps), 3),
pg_catalog.round(pg_catalog.avg(buffer_alloc_tps), 3)
FROM
Expand Down
2 changes: 1 addition & 1 deletion agent/bin/writer_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ INSERT INTO statsrepo.plan \
INSERT INTO statsrepo.lock VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)"

#define SQL_INSERT_BGWRITER "\
INSERT INTO statsrepo.bgwriter VALUES ($1, $2, $3, $4, $5, $6)"
INSERT INTO statsrepo.bgwriter VALUES ($1, $2, $3, $4)"

#define SQL_INSERT_REPLICATION "\
INSERT INTO statsrepo.replication VALUES \
Expand Down
33 changes: 15 additions & 18 deletions agent/lib/last_xact_activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ shmem_startup(void)
static void
backend_shutdown_hook(int code, Datum arg)
{
statEntry *entry = get_stat_entry(MyBackendId);
statEntry *entry = get_stat_entry(MyProcNumber);
if (entry)
entry->pid = 0;
}
Expand Down Expand Up @@ -753,7 +753,7 @@ ru_set_queryid(uint64 queryid)
Assert(!IsParallelWorker());

LWLockAcquire(ru_ss->queryids_lock, LW_EXCLUSIVE);
ru_ss->queryids[MyBackendId] = queryid;
ru_ss->queryids[MyProcNumber] = queryid;
LWLockRelease(ru_ss->queryids_lock);
}

Expand Down Expand Up @@ -1035,7 +1035,7 @@ myExecutorStart(QueryDesc *queryDesc, int eflags)
else
standard_ExecutorStart(queryDesc, eflags);

entry = get_stat_entry(MyBackendId);
entry = get_stat_entry(MyProcNumber);

entry->change_count++;

Expand All @@ -1044,7 +1044,7 @@ myExecutorStart(QueryDesc *queryDesc, int eflags)
*/
if (!entry->inxact)
{
init_entry(MyBackendId, GetSessionUserId());
init_entry(MyProcNumber, GetSessionUserId());
/*
* Remember to free activity snapshot on ExecutorEnd when we're out of
* transaction here.
Expand Down Expand Up @@ -1134,7 +1134,7 @@ myExecutorEnd(QueryDesc * queryDesc)
if (IsParallelWorker())
{
LWLockAcquire(ru_ss->queryids_lock, LW_SHARED);
queryId = ru_ss->queryids[ParallelLeaderBackendId];
queryId = ru_ss->queryids[ParallelLeaderProcNumber];
LWLockRelease(ru_ss->queryids_lock);
}
else
Expand Down Expand Up @@ -1194,7 +1194,7 @@ exit_transaction_if_needed()
{
if (immediate_exit_xact)
{
statEntry *entry = get_stat_entry(MyBackendId);
statEntry *entry = get_stat_entry(MyProcNumber);

entry->inxact = false;
immediate_exit_xact = false;
Expand All @@ -1207,14 +1207,14 @@ myProcessUtility0(Node *parsetree, const char *queryString)
statEntry *entry;
TransactionStmt *stmt;

entry = get_stat_entry(MyBackendId);
entry = get_stat_entry(MyProcNumber);

/*
* Initialize stat entry if I find that the PID of this backend has changed
* unexpectedly.
*/
if (MyProc->pid != 0 && entry->pid != MyProc->pid)
init_entry(MyBackendId, GetSessionUserId());
init_entry(MyProcNumber, GetSessionUserId());

switch (nodeTag(parsetree))
{
Expand All @@ -1227,7 +1227,7 @@ myProcessUtility0(Node *parsetree, const char *queryString)
{
case TRANS_STMT_BEGIN:
entry->change_count++;
init_entry(MyBackendId, GetSessionUserId());
init_entry(MyProcNumber, GetSessionUserId());
entry->inxact = true;
break;
case TRANS_STMT_COMMIT:
Expand Down Expand Up @@ -1265,7 +1265,7 @@ myProcessUtility0(Node *parsetree, const char *queryString)
if (!entry->inxact)
{
immediate_exit_xact = true;
init_entry(MyBackendId, GetSessionUserId());
init_entry(MyProcNumber, GetSessionUserId());
entry->inxact = true;
}

Expand Down Expand Up @@ -1491,9 +1491,6 @@ statsinfo_rusage_internal(FunctionCallInfo fcinfo)
}

LWLockRelease(ru_ss->lock);

/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
}

#define RUSAGE_STATS_INFO_COLS 2
Expand Down Expand Up @@ -1829,14 +1826,14 @@ buffer_size(int nbackends)
static char*
get_query_entry(int beid)
{
if (beid < 1 || beid > stat_buffer->max_id) return NULL;
return query_buffer + buffer_size_per_backend * (beid - 1);
if (beid < 0 || beid >= stat_buffer->max_id) return NULL;
return query_buffer + buffer_size_per_backend * beid;
}

static statEntry *
get_stat_entry(int beid) {
if (beid < 1 || beid > stat_buffer->max_id) return NULL;
return &stat_buffer->entries[beid - 1];
if (beid < 0 || beid >= stat_buffer->max_id) return NULL;
return &stat_buffer->entries[beid];
}

static void
Expand Down Expand Up @@ -1883,7 +1880,7 @@ attatch_shmem(void)
MemSet(stat_buffer, 0, bufsize);
query_buffer = (char*)(&stat_buffer->entries[max_backends]);
stat_buffer->max_id = max_backends;
for (beid = 1 ; beid <= max_backends ; beid++)
for (beid = 0 ; beid < max_backends ; beid++)
init_entry(beid, 0);
}
}
Expand Down
32 changes: 14 additions & 18 deletions agent/lib/libstatsinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ sample_activity(void)
int waiting = 0;
int running = 0;
int i;
int numbackends = 0;

if (!long_xacts)
{
Expand All @@ -648,9 +649,11 @@ sample_activity(void)
}

now = GetCurrentTimestamp();
numbackends = pgstat_fetch_stat_numbackends();

for (i = pgstat_fetch_stat_numbackends(); i > 0; i--)
for (i = 1; i <= numbackends; i++)
{
LocalPgBackendStatus *local_be;
PgBackendStatus *be;
long secs;
int usecs;
Expand All @@ -660,7 +663,8 @@ sample_activity(void)
LongXactEntry *entry;
int procpid;

be = pgstat_get_beentry_by_backend_id(i);
local_be = pgstat_get_local_beentry_by_index(i);
be = &local_be->backendStatus;
if (!be)
continue;

Expand Down Expand Up @@ -1060,9 +1064,6 @@ statsinfo_long_xact(PG_FUNCTION_ARGS)
}
}

/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);

return (Datum) 0;
}

Expand Down Expand Up @@ -1156,9 +1157,6 @@ statsinfo_wait_sampling_profile(PG_FUNCTION_ARGS)
}
}

/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);

return (Datum) 0;
}

Expand Down Expand Up @@ -2282,8 +2280,7 @@ statsinfo_devicestats(PG_FUNCTION_ARGS)
entry->overflow_dit = 0;
}

/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
/* clean up */
SPI_finish();

return (Datum) 0;
Expand Down Expand Up @@ -2594,9 +2591,6 @@ statsinfo_profile(PG_FUNCTION_ARGS)

fclose(fp);

/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);

PG_RETURN_VOID();
}

Expand Down Expand Up @@ -3370,9 +3364,6 @@ statsinfo_tablespaces(PG_FUNCTION_ARGS)
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}

/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);

return (Datum) 0;
}

Expand Down Expand Up @@ -4604,15 +4595,20 @@ static void
probe_waits(void)
{
int i;
int numbackends = 0;

numbackends = pgstat_fetch_stat_numbackends();

for (i = pgstat_fetch_stat_numbackends(); i > 0; i--)
for (i = 1; i <= numbackends; i++)
{
LocalPgBackendStatus *local_be;
PgBackendStatus *be;
wait_samplingEntry item;
PGPROC *proc;
int procpid;

be = pgstat_get_beentry_by_backend_id(i);
local_be = pgstat_get_local_beentry_by_index(i);
be = &local_be->backendStatus;
if (!be)
continue;

Expand Down
4 changes: 0 additions & 4 deletions reporter/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,6 @@ report_instance_activity(PGconn *conn, ReportScope *scope, FILE *out)
res = pgut_execute(conn, SQL_SELECT_BGWRITER_STATS, lengthof(params), params);
fprintf(out, "Written Buffers By BGWriter (Average) : %s buffers/s\n", PQgetvalue(res, 0, 0));
fprintf(out, "Written Buffers By BGWriter (Maximum) : %s buffers/s\n", PQgetvalue(res, 0, 1));
fprintf(out, "Written Buffers By Backend (Average) : %s buffers/s\n", PQgetvalue(res, 0, 2));
fprintf(out, "Written Buffers By Backend (Maximum) : %s buffers/s\n", PQgetvalue(res, 0, 3));
fprintf(out, "Backend Executed fsync (Average) : %s sync/s\n", PQgetvalue(res, 0, 4));
fprintf(out, "Backend Executed fsync (Maximum) : %s sync/s\n\n", PQgetvalue(res, 0, 5));
PQclear(res);

fprintf(out, "/** Transaction Increase Tendency **/\n");
Expand Down
Loading