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
|
/*-------------------------------------------------------------------------
*
* pglogical_manager.c
* pglogical worker for managing apply workers in a database
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglogical_manager.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "miscadmin.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "commands/sequence.h"
#include "nodes/makefuncs.h"
#include "replication/reorderbuffer.h"
#include "utils/fmgroids.h"
#include "utils/json.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "pglogical.h"
#include "pglogical_queue.h"
#include "pglogical_repset.h"
#define CATALOG_SEQUENCE_STATE "sequence_state"
#define SEQUENCE_REPLICATION_MIN_CACHE 1000
#define SEQUENCE_REPLICATION_MAX_CACHE 1000000
typedef struct SeqStateTuple {
Oid seqoid;
int32 cache_size;
int64 last_value;
} SeqStateTuple;
#define Natts_sequence_state 3
#define Anum_sequence_state_seqoid 1
#define Anum_sequence_state_cache_size 2
#define Anum_sequence_state_last_value 3
/* Get last value of individual sequence. */
int64
sequence_get_last_value(Oid seqoid)
{
Relation seqrel;
SysScanDesc scan;
HeapTuple tup;
int64 last_value;
Form_pg_sequence seq;
seqrel = table_open(seqoid, AccessShareLock);
scan = systable_beginscan(seqrel, 0, false, NULL, 0, NULL);
tup = systable_getnext(scan);
Assert(HeapTupleIsValid(tup));
seq = (Form_pg_sequence) GETSTRUCT(tup);
last_value = seq->last_value;
systable_endscan(scan);
table_close(seqrel, AccessShareLock);
return last_value;
}
/*
* Process sequence updates.
*/
bool
synchronize_sequences(void)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
PGLogicalLocalNode *local_node;
bool ret = true;
StartTransactionCommand();
local_node = get_local_node(false, true);
if (!local_node)
{
AbortCurrentTransaction();
return ret;
}
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SEQUENCE_STATE, -1);
rel = table_openrv(rv, RowExclusiveLock);
scan = systable_beginscan(rel, 0, true, NULL, 0, NULL);
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
SeqStateTuple *oldseq = (SeqStateTuple *) GETSTRUCT(tuple);
SeqStateTuple *newseq;
int64 last_value;
HeapTuple newtup;
List *repsets;
List *repset_names;
ListCell *lc;
char *nspname;
char *relname;
StringInfoData json;
CHECK_FOR_INTERRUPTS();
last_value = sequence_get_last_value(oldseq->seqoid);
/* Not enough of the sequence was consumed yet for us to care. */
if (oldseq->last_value >= last_value + SEQUENCE_REPLICATION_MIN_CACHE / 2)
continue;
newtup = heap_copytuple(tuple);
newseq = (SeqStateTuple *) GETSTRUCT(newtup);
/* Consumed more than half of cache of the sequence. */
if (newseq->last_value + newseq->cache_size / 2 < last_value)
ret = false;
/* The sequence is consumed too fast, increase the buffer cache. */
if (newseq->last_value + newseq->cache_size <= last_value)
newseq->cache_size = Min(SEQUENCE_REPLICATION_MAX_CACHE,
newseq->cache_size * 2);
newseq->last_value = last_value + newseq->cache_size;
CatalogTupleUpdate(rel, &tuple->t_self, newtup);
repsets = get_seq_replication_sets(local_node->node->id,
oldseq->seqoid);
repset_names = NIL;
foreach (lc, repsets)
{
PGLogicalRepSet *repset = (PGLogicalRepSet *) lfirst(lc);
repset_names = lappend(repset_names, pstrdup(repset->name));
}
nspname = get_namespace_name(get_rel_namespace(oldseq->seqoid));
relname = get_rel_name(oldseq->seqoid);
initStringInfo(&json);
appendStringInfoString(&json, "{\"schema_name\": ");
escape_json(&json, nspname);
appendStringInfoString(&json, ",\"sequence_name\": ");
escape_json(&json, relname);
appendStringInfo(&json, ",\"last_value\": \""INT64_FORMAT"\"",
newseq->last_value);
appendStringInfo(&json, "}");
queue_message(repset_names, GetUserId(),
QUEUE_COMMAND_TYPE_SEQUENCE, json.data);
}
/* Cleanup */
systable_endscan(scan);
table_close(rel, NoLock);
CommitTransactionCommand();
return ret;
}
/*
* Process sequence updates.
*/
void
synchronize_sequence(Oid seqoid)
{
RangeVar *rv;
Relation rel;
Relation seqrel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
SeqStateTuple *newseq;
int64 last_value;
HeapTuple newtup;
List *repsets;
List *repset_names;
ListCell *lc;
char *nspname;
char *relname;
StringInfoData json;
PGLogicalLocalNode *local_node = get_local_node(true, false);
/* Check if the oid points to actual sequence. */
seqrel = table_open(seqoid, AccessShareLock);
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a sequence",
RelationGetRelationName(seqrel))));
/* Now search for it in our tracking table. */
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SEQUENCE_STATE, -1);
rel = table_openrv(rv, RowExclusiveLock);
ScanKeyInit(&key[0],
Anum_sequence_state_seqoid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(seqoid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("\"%s\" is not a replicated sequence",
RelationGetRelationName(seqrel))));
newtup = heap_copytuple(tuple);
newseq = (SeqStateTuple *) GETSTRUCT(newtup);
last_value = sequence_get_last_value(seqoid);
newseq->last_value = last_value + newseq->cache_size;
CatalogTupleUpdate(rel, &tuple->t_self, newtup);
repsets = get_seq_replication_sets(local_node->node->id, seqoid);
repset_names = NIL;
foreach (lc, repsets)
{
PGLogicalRepSet *repset = (PGLogicalRepSet *) lfirst(lc);
repset_names = lappend(repset_names, pstrdup(repset->name));
}
nspname = get_namespace_name(RelationGetNamespace(seqrel));
relname = RelationGetRelationName(seqrel);
initStringInfo(&json);
appendStringInfoString(&json, "{\"schema_name\": ");
escape_json(&json, nspname);
appendStringInfoString(&json, ",\"sequence_name\": ");
escape_json(&json, relname);
appendStringInfo(&json, ",\"last_value\": \""INT64_FORMAT"\"",
newseq->last_value);
appendStringInfo(&json, "}");
queue_message(repset_names, GetUserId(),
QUEUE_COMMAND_TYPE_SEQUENCE, json.data);
/* Cleanup */
systable_endscan(scan);
table_close(rel, NoLock);
table_close(seqrel, AccessShareLock);
}
/*
* Makes sure there is sequence state record for given sequence.
*/
void
pglogical_create_sequence_state_record(Oid seqoid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SEQUENCE_STATE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Check if the state record already exists. */
ScanKeyInit(&key[0],
Anum_sequence_state_seqoid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(seqoid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
/* And if it doesn't insert it. */
if (!HeapTupleIsValid(tuple))
{
Datum values[Natts_sequence_state];
bool nulls[Natts_sequence_state];
TupleDesc tupDesc = RelationGetDescr(rel);
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
values[Anum_sequence_state_seqoid - 1] = ObjectIdGetDatum(seqoid);
values[Anum_sequence_state_cache_size - 1] =
Int32GetDatum(SEQUENCE_REPLICATION_MIN_CACHE);
values[Anum_sequence_state_last_value - 1] =
Int64GetDatum(sequence_get_last_value(seqoid));
tuple = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tuple);
}
/* Cleanup. */
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
CommandCounterIncrement();
}
/*
* Makes sure there isn't sequence state record for given sequence.
*/
void
pglogical_drop_sequence_state_record(Oid seqoid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_SEQUENCE_STATE, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Check if the state record already exists. */
ScanKeyInit(&key[0],
Anum_sequence_state_seqoid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(seqoid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (HeapTupleIsValid(tuple))
simple_heap_delete(rel, &tuple->t_self);
/* Cleanup. */
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
CommandCounterIncrement();
}
|