Skip to content

Commit cf91b7a

Browse files
authored
Add the reported timeline id to the monitor events table. (#753)
1 parent ca79fba commit cf91b7a

File tree

5 files changed

+157
-11
lines changed

5 files changed

+157
-11
lines changed

src/bin/pg_autoctl/monitor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,7 @@ monitor_print_last_events(Monitor *monitor, char *formation, int group, int coun
22322232
paramCount, paramTypes, paramValues,
22332233
&context, &printLastEvents))
22342234
{
2235-
log_error("Failed to retrieve current state from the monitor");
2235+
log_error("Failed to retrieve last events from the monitor");
22362236
return false;
22372237
}
22382238

src/monitor/notifications.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ InsertEvent(AutoFailoverNode *node, char *description)
134134
replicationStateTypeOid, /* reportedstate */
135135
replicationStateTypeOid, /* goalstate */
136136
TEXTOID, /* pg_stat_replication.sync_state */
137+
INT4OID, /* timeline_id */
137138
LSNOID, /* reportedLSN */
138139
INT4OID, /* candidate_priority */
139140
BOOLOID, /* replication_quorum */
@@ -150,6 +151,7 @@ InsertEvent(AutoFailoverNode *node, char *description)
150151
ObjectIdGetDatum(reportedStateOid), /* reportedstate */
151152
ObjectIdGetDatum(goalStateOid), /* goalstate */
152153
CStringGetTextDatum(SyncStateToString(node->pgsrSyncState)), /* sync_state */
154+
Int32GetDatum(node->reportedTLI), /* reportedTLI */
153155
LSNGetDatum(node->reportedLSN), /* reportedLSN */
154156
Int32GetDatum(node->candidatePriority), /* candidate_priority */
155157
BoolGetDatum(node->replicationQuorum), /* replication_quorum */
@@ -162,9 +164,9 @@ InsertEvent(AutoFailoverNode *node, char *description)
162164
const char *insertQuery =
163165
"INSERT INTO " AUTO_FAILOVER_EVENT_TABLE
164166
"(formationid, nodeid, groupid, nodename, nodehost, nodeport,"
165-
" reportedstate, goalstate, reportedrepstate, reportedlsn,"
167+
" reportedstate, goalstate, reportedrepstate, reportedtli, reportedlsn,"
166168
" candidatepriority, replicationquorum, description) "
167-
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) "
169+
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) "
168170
"RETURNING eventid";
169171

170172
SPI_connect();

src/monitor/pgautofailover--1.5--1.6.sql

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ DROP FUNCTION pgautofailover.get_other_nodes(int);
1818
DROP FUNCTION pgautofailover.get_other_nodes
1919
(integer,pgautofailover.replication_state);
2020

21+
DROP FUNCTION pgautofailover.last_events(int);
22+
DROP FUNCTION pgautofailover.last_events(text,int);
23+
DROP FUNCTION pgautofailover.last_events(text,int,int);
24+
2125
DROP FUNCTION pgautofailover.current_state(text);
2226
DROP FUNCTION pgautofailover.current_state(text,int);
2327

@@ -158,6 +162,51 @@ INSERT INTO pgautofailover.node
158162
candidatepriority, replicationquorum, nodecluster
159163
FROM pgautofailover.node_upgrade_old;
160164

165+
166+
ALTER TABLE pgautofailover.event
167+
RENAME TO event_upgrade_old;
168+
169+
CREATE TABLE pgautofailover.event
170+
(
171+
eventid bigint not null DEFAULT nextval('pgautofailover.event_eventid_seq'::regclass),
172+
eventtime timestamptz not null default now(),
173+
formationid text not null,
174+
nodeid bigint not null,
175+
groupid int not null,
176+
nodename text not null,
177+
nodehost text not null,
178+
nodeport integer not null,
179+
reportedstate pgautofailover.replication_state not null,
180+
goalstate pgautofailover.replication_state not null,
181+
reportedrepstate text,
182+
reportedtli int not null default 1 check (reportedtli > 0),
183+
reportedlsn pg_lsn not null default '0/0',
184+
candidatepriority int,
185+
replicationquorum bool,
186+
description text,
187+
188+
PRIMARY KEY (eventid)
189+
);
190+
191+
ALTER SEQUENCE pgautofailover.event_eventid_seq
192+
OWNED BY pgautofailover.event.eventid;
193+
194+
INSERT INTO pgautofailover.event
195+
(
196+
eventid, eventtime, formationid, nodeid, groupid,
197+
nodename, nodehost, nodeport,
198+
reportedstate, goalstate, reportedrepstate,
199+
reportedtli, reportedlsn, candidatepriority, replicationquorum,
200+
description
201+
)
202+
SELECT eventid, eventtime, formationid, nodeid, groupid,
203+
nodename, nodehost, nodeport,
204+
reportedstate, goalstate, reportedrepstate,
205+
1 as reportedtli, reportedlsn, candidatepriority, replicationquorum,
206+
description
207+
FROM pgautofailover.event_upgrade_old as event;
208+
209+
DROP TABLE pgautofailover.event_upgrade_old;
161210
DROP TABLE pgautofailover.node_upgrade_old;
162211
DROP TYPE pgautofailover.old_replication_state;
163212

@@ -405,6 +454,91 @@ CREATE TRIGGER disable_secondary_check
405454
EXECUTE PROCEDURE pgautofailover.update_secondary_check();
406455

407456

457+
CREATE FUNCTION pgautofailover.last_events
458+
(
459+
count int default 10
460+
)
461+
RETURNS SETOF pgautofailover.event LANGUAGE SQL STRICT
462+
AS $$
463+
with last_events as
464+
(
465+
select eventid, eventtime, formationid,
466+
nodeid, groupid, nodename, nodehost, nodeport,
467+
reportedstate, goalstate,
468+
reportedrepstate, reportedtli, reportedlsn,
469+
candidatepriority, replicationquorum, description
470+
from pgautofailover.event
471+
order by eventid desc
472+
limit count
473+
)
474+
select * from last_events order by eventtime, eventid;
475+
$$;
476+
477+
comment on function pgautofailover.last_events(int)
478+
is 'retrieve last COUNT events';
479+
480+
grant execute on function pgautofailover.last_events(int)
481+
to autoctl_node;
482+
483+
CREATE FUNCTION pgautofailover.last_events
484+
(
485+
formation_id text default 'default',
486+
count int default 10
487+
)
488+
RETURNS SETOF pgautofailover.event LANGUAGE SQL STRICT
489+
AS $$
490+
with last_events as
491+
(
492+
select eventid, eventtime, formationid,
493+
nodeid, groupid, nodename, nodehost, nodeport,
494+
reportedstate, goalstate,
495+
reportedrepstate, reportedtli, reportedlsn,
496+
candidatepriority, replicationquorum, description
497+
from pgautofailover.event
498+
where formationid = formation_id
499+
order by eventid desc
500+
limit count
501+
)
502+
select * from last_events order by eventtime, eventid;
503+
$$;
504+
505+
comment on function pgautofailover.last_events(text,int)
506+
is 'retrieve last COUNT events for given formation';
507+
508+
grant execute on function pgautofailover.last_events(text,int)
509+
to autoctl_node;
510+
511+
CREATE FUNCTION pgautofailover.last_events
512+
(
513+
formation_id text,
514+
group_id int,
515+
count int default 10
516+
)
517+
RETURNS SETOF pgautofailover.event LANGUAGE SQL STRICT
518+
AS $$
519+
with last_events as
520+
(
521+
select eventid, eventtime, formationid,
522+
nodeid, groupid, nodename, nodehost, nodeport,
523+
reportedstate, goalstate,
524+
reportedrepstate, reportedtli, reportedlsn,
525+
candidatepriority, replicationquorum, description
526+
from pgautofailover.event
527+
where formationid = formation_id
528+
and groupid = group_id
529+
order by eventid desc
530+
limit count
531+
)
532+
select * from last_events order by eventtime, eventid;
533+
$$;
534+
535+
comment on function pgautofailover.last_events(text,int,int)
536+
is 'retrieve last COUNT events for given formation and group';
537+
538+
grant execute on function pgautofailover.last_events(text,int,int)
539+
to autoctl_node;
540+
541+
408542
CREATE FUNCTION pgautofailover.current_state
409543
(
410544
IN formation_id text default 'default',

src/monitor/pgautofailover.sql

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ CREATE TABLE pgautofailover.event
176176
reportedstate pgautofailover.replication_state not null,
177177
goalstate pgautofailover.replication_state not null,
178178
reportedrepstate text,
179+
reportedtli int not null default 1 check (reportedtli > 0),
179180
reportedlsn pg_lsn not null default '0/0',
180181
candidatepriority int,
181182
replicationquorum bool,
@@ -510,7 +511,7 @@ with last_events as
510511
select eventid, eventtime, formationid,
511512
nodeid, groupid, nodename, nodehost, nodeport,
512513
reportedstate, goalstate,
513-
reportedrepstate, reportedlsn,
514+
reportedrepstate, reportedtli, reportedlsn,
514515
candidatepriority, replicationquorum, description
515516
from pgautofailover.event
516517
order by eventid desc
@@ -522,6 +523,9 @@ $$;
522523
comment on function pgautofailover.last_events(int)
523524
is 'retrieve last COUNT events';
524525

526+
grant execute on function pgautofailover.last_events(int)
527+
to autoctl_node;
528+
525529
CREATE FUNCTION pgautofailover.last_events
526530
(
527531
formation_id text default 'default',
@@ -534,7 +538,7 @@ with last_events as
534538
select eventid, eventtime, formationid,
535539
nodeid, groupid, nodename, nodehost, nodeport,
536540
reportedstate, goalstate,
537-
reportedrepstate, reportedlsn,
541+
reportedrepstate, reportedtli, reportedlsn,
538542
candidatepriority, replicationquorum, description
539543
from pgautofailover.event
540544
where formationid = formation_id
@@ -547,6 +551,9 @@ $$;
547551
comment on function pgautofailover.last_events(text,int)
548552
is 'retrieve last COUNT events for given formation';
549553

554+
grant execute on function pgautofailover.last_events(text,int)
555+
to autoctl_node;
556+
550557
CREATE FUNCTION pgautofailover.last_events
551558
(
552559
formation_id text,
@@ -560,7 +567,7 @@ with last_events as
560567
select eventid, eventtime, formationid,
561568
nodeid, groupid, nodename, nodehost, nodeport,
562569
reportedstate, goalstate,
563-
reportedrepstate, reportedlsn,
570+
reportedrepstate, reportedtli, reportedlsn,
564571
candidatepriority, replicationquorum, description
565572
from pgautofailover.event
566573
where formationid = formation_id
@@ -574,6 +581,9 @@ $$;
574581
comment on function pgautofailover.last_events(text,int,int)
575582
is 'retrieve last COUNT events for given formation and group';
576583

584+
grant execute on function pgautofailover.last_events(text,int,int)
585+
to autoctl_node;
586+
577587
CREATE FUNCTION pgautofailover.current_state
578588
(
579589
IN formation_id text default 'default',

tests/pgautofailover_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,19 +669,19 @@ def get_events_str(self):
669669
if events:
670670
return "\n".join(
671671
[
672-
"%32s %8s %17s/%-17s %7s %10s %s"
672+
"%32s %8s %17s/%-17s %10s %10s %s"
673673
% (
674674
"eventtime",
675675
"name",
676676
"state",
677677
"goal state",
678678
"repl st",
679-
"lsn",
679+
"tli:lsn",
680680
"event",
681681
)
682682
]
683683
+ [
684-
"%32s %8s %17s/%-17s %7s %10s %s" % result
684+
"%32s %8s %17s/%-17s %3d:%7s %10s %s" % result
685685
for result in events
686686
]
687687
)
@@ -1274,7 +1274,7 @@ def get_events(self):
12741274
last_events_query = (
12751275
"select eventtime, nodename, "
12761276
"reportedstate, goalstate, "
1277-
"reportedrepstate, reportedlsn, description "
1277+
"reportedrepstate, reportedtli, reportedlsn, description "
12781278
"from pgautofailover.last_events('default', count => 20)"
12791279
)
12801280
return self.monitor.get_events()
@@ -1842,7 +1842,7 @@ def get_events(self):
18421842
last_events_query = (
18431843
"select eventtime, nodename, "
18441844
"reportedstate, goalstate, "
1845-
"reportedrepstate, reportedlsn, description "
1845+
"reportedrepstate, reportedtli, reportedlsn, description "
18461846
"from pgautofailover.last_events('default', count => 20)"
18471847
)
18481848

0 commit comments

Comments
 (0)