sequence.c 24.8 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * sequence.c
4
 *	  PostgreSQL sequences support code.
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 8 9 10
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.93 2003/03/20 05:18:14 momjian Exp $
12
 *
13 14
 *-------------------------------------------------------------------------
 */
15
#include "postgres.h"
16

17
#include "access/heapam.h"
18
#include "catalog/namespace.h"
19
#include "catalog/pg_type.h"
20
#include "commands/defrem.h"
21
#include "commands/tablecmds.h"
22
#include "commands/sequence.h"
B
Bruce Momjian 已提交
23
#include "miscadmin.h"
24
#include "utils/acl.h"
B
Bruce Momjian 已提交
25
#include "utils/builtins.h"
26

V
Vadim B. Mikheev 已提交
27
/*
28
 * We don't want to log each fetching of a value from a sequence,
V
Vadim B. Mikheev 已提交
29 30 31
 * so we pre-log a few fetches in advance. In the event of
 * crash we can lose as much as we pre-logged.
 */
B
Bruce Momjian 已提交
32
#define SEQ_LOG_VALS	32
33

34 35 36 37 38
/*
 * The "special area" of a sequence's buffer page looks like this.
 */
#define SEQ_MAGIC	  0x1717

39 40
typedef struct sequence_magic
{
41
	uint32		magic;
42
} sequence_magic;
43

44 45 46 47 48 49
/*
 * We store a SeqTable item for every sequence we have touched in the current
 * session.  This is needed to hold onto nextval/currval state.  (We can't
 * rely on the relcache, since it's only, well, a cache, and may decide to
 * discard entries.)
 *
B
Bruce Momjian 已提交
50
 * XXX We use linear search to find pre-existing SeqTable entries.	This is
51 52 53
 * good when only a small number of sequences are touched in a session, but
 * would suck with many different sequences.  Perhaps use a hashtable someday.
 */
54 55
typedef struct SeqTableData
{
56 57 58 59 60 61 62
	struct SeqTableData *next;	/* link to next SeqTable object */
	Oid			relid;			/* pg_class OID of this sequence */
	TransactionId xid;			/* xact in which we last did a seq op */
	int64		last;			/* value last returned by nextval */
	int64		cached;			/* last value already cached for nextval */
	/* if last != cached, we have not used up all the cached values */
	int64		increment;		/* copy of sequence's increment field */
63
} SeqTableData;
64 65 66

typedef SeqTableData *SeqTable;

67
static SeqTable seqtab = NULL;	/* Head of list of SeqTable items */
68

69 70

static void init_sequence(const char *caller, RangeVar *relation,
B
Bruce Momjian 已提交
71
			  SeqTable *p_elm, Relation *p_rel);
72
static Form_pg_sequence read_info(const char *caller, SeqTable elm,
B
Bruce Momjian 已提交
73
		  Relation rel, Buffer *buf);
74
static void init_params(CreateSeqStmt *seq, Form_pg_sequence new);
75
static void do_setval(RangeVar *sequence, int64 next, bool iscalled);
76 77

/*
B
Bruce Momjian 已提交
78
 * DefineSequence
79
 *				Creates a new sequence relation
80 81
 */
void
82
DefineSequence(CreateSeqStmt *seq)
83
{
84
	FormData_pg_sequence new;
85
	CreateStmt *stmt = makeNode(CreateStmt);
86
	Oid			seqoid;
87 88 89
	Relation	rel;
	Buffer		buf;
	PageHeader	page;
90
	sequence_magic *sm;
91 92 93 94 95
	HeapTuple	tuple;
	TupleDesc	tupDesc;
	Datum		value[SEQ_COL_LASTCOL];
	char		null[SEQ_COL_LASTCOL];
	int			i;
96
	NameData	name;
97 98 99 100 101

	/* Check and set values */
	init_params(seq, &new);

	/*
102
	 * Create relation (and fill *null & *value)
103 104 105
	 */
	stmt->tableElts = NIL;
	for (i = SEQ_COL_FIRSTCOL; i <= SEQ_COL_LASTCOL; i++)
106
	{
107 108 109
		ColumnDef  *coldef;
		TypeName   *typnam;

110 111
		typnam = makeNode(TypeName);
		typnam->setof = FALSE;
112
		typnam->arrayBounds = NIL;
B
Bruce Momjian 已提交
113
		typnam->typmod = -1;
114

115 116
		coldef = makeNode(ColumnDef);
		coldef->typename = typnam;
117 118
		coldef->inhcount = 0;
		coldef->is_local = true;
119
		coldef->is_not_null = true;
120 121
		coldef->raw_default = NULL;
		coldef->cooked_default = NULL;
122 123 124
		coldef->constraints = NIL;
		coldef->support = NULL;

125 126 127 128
		null[i - 1] = ' ';

		switch (i)
		{
129
			case SEQ_COL_NAME:
130
				typnam->typeid = NAMEOID;
131
				coldef->colname = "sequence_name";
132
				namestrcpy(&name, seq->sequence->relname);
133
				value[i - 1] = NameGetDatum(&name);
134 135
				break;
			case SEQ_COL_LASTVAL:
136
				typnam->typeid = INT8OID;
137
				coldef->colname = "last_value";
138
				value[i - 1] = Int64GetDatumFast(new.last_value);
139 140
				break;
			case SEQ_COL_INCBY:
141
				typnam->typeid = INT8OID;
142
				coldef->colname = "increment_by";
143
				value[i - 1] = Int64GetDatumFast(new.increment_by);
144 145
				break;
			case SEQ_COL_MAXVALUE:
146
				typnam->typeid = INT8OID;
147
				coldef->colname = "max_value";
148
				value[i - 1] = Int64GetDatumFast(new.max_value);
149 150
				break;
			case SEQ_COL_MINVALUE:
151
				typnam->typeid = INT8OID;
152
				coldef->colname = "min_value";
153
				value[i - 1] = Int64GetDatumFast(new.min_value);
154 155
				break;
			case SEQ_COL_CACHE:
156
				typnam->typeid = INT8OID;
157
				coldef->colname = "cache_value";
158
				value[i - 1] = Int64GetDatumFast(new.cache_value);
159
				break;
V
Vadim B. Mikheev 已提交
160
			case SEQ_COL_LOG:
161
				typnam->typeid = INT8OID;
V
Vadim B. Mikheev 已提交
162
				coldef->colname = "log_cnt";
163
				value[i - 1] = Int64GetDatum((int64) 1);
V
Vadim B. Mikheev 已提交
164
				break;
165
			case SEQ_COL_CYCLE:
166
				typnam->typeid = BOOLOID;
167
				coldef->colname = "is_cycled";
168
				value[i - 1] = BoolGetDatum(new.is_cycled);
169 170
				break;
			case SEQ_COL_CALLED:
171
				typnam->typeid = BOOLOID;
172
				coldef->colname = "is_called";
173
				value[i - 1] = BoolGetDatum(false);
174
				break;
175 176 177 178
		}
		stmt->tableElts = lappend(stmt->tableElts, coldef);
	}

179 180
	stmt->relation = seq->sequence;
	stmt->inhRelations = NIL;
181
	stmt->constraints = NIL;
182
	stmt->hasoids = false;
183
	stmt->oncommit = ONCOMMIT_NOOP;
184

185
	seqoid = DefineRelation(stmt, RELKIND_SEQUENCE);
186

187
	rel = heap_open(seqoid, AccessExclusiveLock);
188
	tupDesc = RelationGetDescr(rel);
189

190 191
	/* Initialize first page of relation with special magic number */

192 193 194
	buf = ReadBuffer(rel, P_NEW);

	if (!BufferIsValid(buf))
195
		elog(ERROR, "DefineSequence: ReadBuffer failed");
196

197 198
	Assert(BufferGetBlockNumber(buf) == 0);

199 200 201 202 203 204
	page = (PageHeader) BufferGetPage(buf);

	PageInit((Page) page, BufferGetPageSize(buf), sizeof(sequence_magic));
	sm = (sequence_magic *) PageGetSpecialPointer(page);
	sm->magic = SEQ_MAGIC;

205 206 207
	/* hack: ensure heap_insert will insert on the just-created page */
	rel->rd_targblock = 0;

208
	/* Now form & insert sequence tuple */
209
	tuple = heap_formtuple(tupDesc, value, null);
210
	simple_heap_insert(rel, tuple);
211

212 213
	Assert(ItemPointerGetOffsetNumber(&(tuple->t_self)) == FirstOffsetNumber);

214
	/*
215 216 217
	 * Two special hacks here:
	 *
	 * 1. Since VACUUM does not process sequences, we have to force the tuple
B
Bruce Momjian 已提交
218 219
	 * to have xmin = FrozenTransactionId now.	Otherwise it would become
	 * invisible to SELECTs after 2G transactions.	It is okay to do this
220 221 222 223 224 225
	 * because if the current transaction aborts, no other xact will ever
	 * examine the sequence tuple anyway.
	 *
	 * 2. Even though heap_insert emitted a WAL log record, we have to emit
	 * an XLOG_SEQ_LOG record too, since (a) the heap_insert record will
	 * not have the right xmin, and (b) REDO of the heap_insert record
B
Bruce Momjian 已提交
226
	 * would re-init page and sequence magic number would be lost.	This
227
	 * means two log records instead of one :-(
228
	 */
229
	LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
230

231
	START_CRIT_SECTION();
232 233 234

	{
		/*
B
Bruce Momjian 已提交
235 236 237 238 239 240
		 * Note that the "tuple" structure is still just a local tuple
		 * record created by heap_formtuple; its t_data pointer doesn't
		 * point at the disk buffer.  To scribble on the disk buffer we
		 * need to fetch the item pointer.	But do the same to the local
		 * tuple, since that will be the source for the WAL log record,
		 * below.
241 242 243 244 245 246 247
		 */
		ItemId		itemId;
		Item		item;

		itemId = PageGetItemId((Page) page, FirstOffsetNumber);
		item = PageGetItem((Page) page, itemId);

248
		HeapTupleHeaderSetXmin((HeapTupleHeader) item, FrozenTransactionId);
249 250
		((HeapTupleHeader) item)->t_infomask |= HEAP_XMIN_COMMITTED;

251
		HeapTupleHeaderSetXmin(tuple->t_data, FrozenTransactionId);
252 253 254
		tuple->t_data->t_infomask |= HEAP_XMIN_COMMITTED;
	}

255 256
	/* XLOG stuff */
	if (!rel->rd_istemp)
257
	{
258 259 260 261
		xl_seq_rec	xlrec;
		XLogRecPtr	recptr;
		XLogRecData rdata[2];
		Form_pg_sequence newseq = (Form_pg_sequence) GETSTRUCT(tuple);
262 263

		/* We do not log first nextval call, so "advance" sequence here */
264
		/* Note we are scribbling on local tuple, not the disk buffer */
265
		newseq->is_called = true;
266 267 268 269 270 271 272 273 274
		newseq->log_cnt = 0;

		xlrec.node = rel->rd_node;
		rdata[0].buffer = InvalidBuffer;
		rdata[0].data = (char *) &xlrec;
		rdata[0].len = sizeof(xl_seq_rec);
		rdata[0].next = &(rdata[1]);

		rdata[1].buffer = InvalidBuffer;
275
		rdata[1].data = (char *) tuple->t_data;
276 277 278 279 280 281 282 283
		rdata[1].len = tuple->t_len;
		rdata[1].next = NULL;

		recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG | XLOG_NO_TRAN, rdata);

		PageSetLSN(page, recptr);
		PageSetSUI(page, ThisStartUpID);
	}
284

285
	END_CRIT_SECTION();
286

287
	LockBuffer(buf, BUFFER_LOCK_UNLOCK);
288 289
	WriteBuffer(buf);
	heap_close(rel, NoLock);
290 291 292
}


293 294
Datum
nextval(PG_FUNCTION_ARGS)
295
{
296
	text	   *seqin = PG_GETARG_TEXT_P(0);
297
	RangeVar   *sequence;
298
	SeqTable	elm;
299
	Relation	seqrel;
300
	Buffer		buf;
301
	Page		page;
302
	Form_pg_sequence seq;
303
	int64		incby,
304 305
				maxv,
				minv,
V
Vadim B. Mikheev 已提交
306 307 308 309
				cache,
				log,
				fetch,
				last;
310
	int64		result,
311 312
				next,
				rescnt = 0;
V
Vadim B. Mikheev 已提交
313
	bool		logit = false;
314

315
	sequence = makeRangeVarFromNameList(textToQualifiedNameList(seqin,
B
Bruce Momjian 已提交
316
															 "nextval"));
317

V
Vadim B. Mikheev 已提交
318
	/* open and AccessShareLock sequence */
319
	init_sequence("nextval", sequence, &elm, &seqrel);
320

321 322
	if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_UPDATE) != ACLCHECK_OK)
		elog(ERROR, "%s.nextval: you don't have permissions to set sequence %s",
323
			 sequence->relname, sequence->relname);
324 325 326 327

	if (elm->last != elm->cached)		/* some numbers were cached */
	{
		elm->last += elm->increment;
328
		relation_close(seqrel, NoLock);
329
		PG_RETURN_INT64(elm->last);
330
	}
331

332 333
	/* lock page' buffer and read tuple */
	seq = read_info("nextval", elm, seqrel, &buf);
334
	page = BufferGetPage(buf);
335

V
Vadim B. Mikheev 已提交
336
	last = next = result = seq->last_value;
337 338 339
	incby = seq->increment_by;
	maxv = seq->max_value;
	minv = seq->min_value;
V
Vadim B. Mikheev 已提交
340 341
	fetch = cache = seq->cache_value;
	log = seq->log_cnt;
342

343
	if (!seq->is_called)
V
Vadim B. Mikheev 已提交
344
	{
345
		rescnt++;				/* last_value if not called */
V
Vadim B. Mikheev 已提交
346 347 348
		fetch--;
		log--;
	}
349

350
	/*
B
Bruce Momjian 已提交
351
	 * Decide whether we should emit a WAL log record.	If so, force up
352 353 354
	 * the fetch count to grab SEQ_LOG_VALS more values than we actually
	 * need to cache.  (These will then be usable without logging.)
	 *
B
Bruce Momjian 已提交
355 356
	 * If this is the first nextval after a checkpoint, we must force a new
	 * WAL record to be written anyway, else replay starting from the
357
	 * checkpoint would fail to advance the sequence past the logged
B
Bruce Momjian 已提交
358
	 * values.	In this case we may as well fetch extra values.
359
	 */
V
Vadim B. Mikheev 已提交
360 361
	if (log < fetch)
	{
362 363
		/* forced log to satisfy local demand for values */
		fetch = log = fetch + SEQ_LOG_VALS;
V
Vadim B. Mikheev 已提交
364 365
		logit = true;
	}
366 367 368 369 370 371 372 373 374 375 376
	else
	{
		XLogRecPtr	redoptr = GetRedoRecPtr();

		if (XLByteLE(PageGetLSN(page), redoptr))
		{
			/* last update of seq was before checkpoint */
			fetch = log = fetch + SEQ_LOG_VALS;
			logit = true;
		}
	}
V
Vadim B. Mikheev 已提交
377

B
Bruce Momjian 已提交
378
	while (fetch)				/* try to fetch cache [+ log ] numbers */
379
	{
380 381 382 383
		/*
		 * Check MAXVALUE for ascending sequences and MINVALUE for
		 * descending sequences
		 */
384
		if (incby > 0)
385
		{
386
			/* ascending sequence */
387 388 389 390
			if ((maxv >= 0 && next > maxv - incby) ||
				(maxv < 0 && next + incby > maxv))
			{
				if (rescnt > 0)
V
Vadim B. Mikheev 已提交
391
					break;		/* stop fetching */
392
				if (!seq->is_cycled)
393
				{
B
Bruce Momjian 已提交
394 395
					char		buf[100];

396
					snprintf(buf, sizeof(buf), INT64_FORMAT, maxv);
397 398 399
					elog(ERROR, "%s.nextval: reached MAXVALUE (%s)",
						 sequence->relname, buf);
				}
400 401 402 403 404 405 406
				next = minv;
			}
			else
				next += incby;
		}
		else
		{
407
			/* descending sequence */
408 409 410 411
			if ((minv < 0 && next < minv - incby) ||
				(minv >= 0 && next + incby < minv))
			{
				if (rescnt > 0)
V
Vadim B. Mikheev 已提交
412
					break;		/* stop fetching */
413
				if (!seq->is_cycled)
414
				{
B
Bruce Momjian 已提交
415 416
					char		buf[100];

417
					snprintf(buf, sizeof(buf), INT64_FORMAT, minv);
418 419 420
					elog(ERROR, "%s.nextval: reached MINVALUE (%s)",
						 sequence->relname, buf);
				}
421 422 423 424 425
				next = maxv;
			}
			else
				next += incby;
		}
V
Vadim B. Mikheev 已提交
426 427 428 429 430 431
		fetch--;
		if (rescnt < cache)
		{
			log--;
			rescnt++;
			last = next;
B
Bruce Momjian 已提交
432 433
			if (rescnt == 1)	/* if it's first result - */
				result = next;	/* it's what to return */
V
Vadim B. Mikheev 已提交
434
		}
435 436
	}

437 438 439
	log -= fetch;				/* adjust for any unfetched numbers */
	Assert(log >= 0);

440 441
	/* save info in local cache */
	elm->last = result;			/* last returned number */
V
Vadim B. Mikheev 已提交
442 443
	elm->cached = last;			/* last fetched number */

444
	START_CRIT_SECTION();
445 446 447

	/* XLOG stuff */
	if (logit && !seqrel->rd_istemp)
V
Vadim B. Mikheev 已提交
448 449 450
	{
		xl_seq_rec	xlrec;
		XLogRecPtr	recptr;
B
Bruce Momjian 已提交
451
		XLogRecData rdata[2];
V
Vadim B. Mikheev 已提交
452

453
		xlrec.node = seqrel->rd_node;
454
		rdata[0].buffer = InvalidBuffer;
B
Bruce Momjian 已提交
455
		rdata[0].data = (char *) &xlrec;
456 457 458
		rdata[0].len = sizeof(xl_seq_rec);
		rdata[0].next = &(rdata[1]);

459
		/* set values that will be saved in xlog */
460
		seq->last_value = next;
461
		seq->is_called = true;
462
		seq->log_cnt = 0;
463

464
		rdata[1].buffer = InvalidBuffer;
B
Bruce Momjian 已提交
465 466 467
		rdata[1].data = (char *) page + ((PageHeader) page)->pd_upper;
		rdata[1].len = ((PageHeader) page)->pd_special -
			((PageHeader) page)->pd_upper;
468 469
		rdata[1].next = NULL;

B
Bruce Momjian 已提交
470
		recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG | XLOG_NO_TRAN, rdata);
V
Vadim B. Mikheev 已提交
471

472 473
		PageSetLSN(page, recptr);
		PageSetSUI(page, ThisStartUpID);
V
Vadim B. Mikheev 已提交
474
	}
475

476
	/* update on-disk data */
V
Vadim B. Mikheev 已提交
477
	seq->last_value = last;		/* last fetched number */
478
	seq->is_called = true;
V
Vadim B. Mikheev 已提交
479
	seq->log_cnt = log;			/* how much is logged */
480

481
	END_CRIT_SECTION();
482

V
Vadim B. Mikheev 已提交
483 484
	LockBuffer(buf, BUFFER_LOCK_UNLOCK);

B
Bruce Momjian 已提交
485
	WriteBuffer(buf);
486

487 488
	relation_close(seqrel, NoLock);

489
	PG_RETURN_INT64(result);
490 491
}

492 493
Datum
currval(PG_FUNCTION_ARGS)
494
{
495
	text	   *seqin = PG_GETARG_TEXT_P(0);
496
	RangeVar   *sequence;
497
	SeqTable	elm;
498
	Relation	seqrel;
499
	int64		result;
500

501
	sequence = makeRangeVarFromNameList(textToQualifiedNameList(seqin,
B
Bruce Momjian 已提交
502
															 "currval"));
503

V
Vadim B. Mikheev 已提交
504
	/* open and AccessShareLock sequence */
505
	init_sequence("currval", sequence, &elm, &seqrel);
506

507 508
	if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_SELECT) != ACLCHECK_OK)
		elog(ERROR, "%s.currval: you don't have permissions to read sequence %s",
509
			 sequence->relname, sequence->relname);
510

511
	if (elm->increment == 0)	/* nextval/read_info were not called */
512
		elog(ERROR, "%s.currval is not yet defined in this session",
513
			 sequence->relname);
514 515 516

	result = elm->last;

517 518
	relation_close(seqrel, NoLock);

519
	PG_RETURN_INT64(result);
520 521
}

B
Bruce Momjian 已提交
522
/*
523 524 525 526
 * Main internal procedure that handles 2 & 3 arg forms of SETVAL.
 *
 * Note that the 3 arg version (which sets the is_called flag) is
 * only for use in pg_dump, and setting the is_called flag may not
B
Bruce Momjian 已提交
527
 * work if multiple users are attached to the database and referencing
528 529
 * the sequence (unlikely if pg_dump is restoring it).
 *
B
Bruce Momjian 已提交
530
 * It is necessary to have the 3 arg version so that pg_dump can
531 532 533 534
 * restore the state of a sequence exactly during data-only restores -
 * it is the only way to clear the is_called flag in an existing
 * sequence.
 */
B
Bruce Momjian 已提交
535
static void
536
do_setval(RangeVar *sequence, int64 next, bool iscalled)
M
 
Marc G. Fournier 已提交
537 538
{
	SeqTable	elm;
539
	Relation	seqrel;
540
	Buffer		buf;
541
	Form_pg_sequence seq;
M
 
Marc G. Fournier 已提交
542

543
	/* open and AccessShareLock sequence */
544
	init_sequence("setval", sequence, &elm, &seqrel);
545 546

	if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_UPDATE) != ACLCHECK_OK)
M
 
Marc G. Fournier 已提交
547
		elog(ERROR, "%s.setval: you don't have permissions to set sequence %s",
548
			 sequence->relname, sequence->relname);
M
 
Marc G. Fournier 已提交
549

550
	/* lock page' buffer and read tuple */
551
	seq = read_info("setval", elm, seqrel, &buf);
M
 
Marc G. Fournier 已提交
552

553
	if ((next < seq->min_value) || (next > seq->max_value))
554
	{
B
Bruce Momjian 已提交
555 556 557 558
		char		bufv[100],
					bufm[100],
					bufx[100];

559 560 561
		snprintf(bufv, sizeof(bufv), INT64_FORMAT, next);
		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seq->min_value);
		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seq->max_value);
562 563 564
		elog(ERROR, "%s.setval: value %s is out of bounds (%s,%s)",
			 sequence->relname, bufv, bufm, bufx);
	}
M
 
Marc G. Fournier 已提交
565 566 567

	/* save info in local cache */
	elm->last = next;			/* last returned number */
B
Bruce Momjian 已提交
568 569
	elm->cached = next;			/* last cached number (forget cached
								 * values) */
M
 
Marc G. Fournier 已提交
570

571
	START_CRIT_SECTION();
572 573 574

	/* XLOG stuff */
	if (!seqrel->rd_istemp)
V
Vadim B. Mikheev 已提交
575 576 577
	{
		xl_seq_rec	xlrec;
		XLogRecPtr	recptr;
B
Bruce Momjian 已提交
578
		XLogRecData rdata[2];
579
		Page		page = BufferGetPage(buf);
V
Vadim B. Mikheev 已提交
580

581
		xlrec.node = seqrel->rd_node;
582
		rdata[0].buffer = InvalidBuffer;
B
Bruce Momjian 已提交
583
		rdata[0].data = (char *) &xlrec;
584 585 586
		rdata[0].len = sizeof(xl_seq_rec);
		rdata[0].next = &(rdata[1]);

587
		/* set values that will be saved in xlog */
588
		seq->last_value = next;
589
		seq->is_called = true;
590
		seq->log_cnt = 0;
591

592
		rdata[1].buffer = InvalidBuffer;
B
Bruce Momjian 已提交
593 594 595
		rdata[1].data = (char *) page + ((PageHeader) page)->pd_upper;
		rdata[1].len = ((PageHeader) page)->pd_special -
			((PageHeader) page)->pd_upper;
596 597
		rdata[1].next = NULL;

B
Bruce Momjian 已提交
598
		recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG | XLOG_NO_TRAN, rdata);
599 600 601

		PageSetLSN(page, recptr);
		PageSetSUI(page, ThisStartUpID);
V
Vadim B. Mikheev 已提交
602
	}
603

604 605
	/* save info in sequence relation */
	seq->last_value = next;		/* last fetched number */
606
	seq->is_called = iscalled;
607
	seq->log_cnt = (iscalled) ? 0 : 1;
608

609
	END_CRIT_SECTION();
M
 
Marc G. Fournier 已提交
610

V
Vadim B. Mikheev 已提交
611 612
	LockBuffer(buf, BUFFER_LOCK_UNLOCK);

B
Bruce Momjian 已提交
613
	WriteBuffer(buf);
614 615

	relation_close(seqrel, NoLock);
616 617
}

618 619 620 621
/*
 * Implement the 2 arg setval procedure.
 * See do_setval for discussion.
 */
622 623 624 625
Datum
setval(PG_FUNCTION_ARGS)
{
	text	   *seqin = PG_GETARG_TEXT_P(0);
626
	int64		next = PG_GETARG_INT64(1);
627 628 629
	RangeVar   *sequence;

	sequence = makeRangeVarFromNameList(textToQualifiedNameList(seqin,
B
Bruce Momjian 已提交
630
															  "setval"));
631

632
	do_setval(sequence, next, true);
633

634
	PG_RETURN_INT64(next);
635 636
}

637 638 639 640
/*
 * Implement the 3 arg setval procedure.
 * See do_setval for discussion.
 */
641 642 643 644
Datum
setval_and_iscalled(PG_FUNCTION_ARGS)
{
	text	   *seqin = PG_GETARG_TEXT_P(0);
645
	int64		next = PG_GETARG_INT64(1);
646
	bool		iscalled = PG_GETARG_BOOL(2);
647
	RangeVar   *sequence;
648

649
	sequence = makeRangeVarFromNameList(textToQualifiedNameList(seqin,
B
Bruce Momjian 已提交
650
															  "setval"));
651

652
	do_setval(sequence, next, iscalled);
653

654
	PG_RETURN_INT64(next);
M
 
Marc G. Fournier 已提交
655 656
}

657

658 659 660 661 662 663 664
/*
 * Given a relation name, open and lock the sequence.  p_elm and p_rel are
 * output parameters.
 */
static void
init_sequence(const char *caller, RangeVar *relation,
			  SeqTable *p_elm, Relation *p_rel)
665
{
666
	Oid			relid = RangeVarGetRelid(relation, false);
667 668
	TransactionId thisxid = GetCurrentTransactionId();
	SeqTable	elm;
669
	Relation	seqrel;
B
Bruce Momjian 已提交
670

671
	/* Look to see if we already have a seqtable entry for relation */
672
	for (elm = seqtab; elm != NULL; elm = elm->next)
673
	{
674
		if (elm->relid == relid)
675 676 677
			break;
	}

678 679 680 681 682 683 684 685
	/*
	 * Open the sequence relation, acquiring AccessShareLock if we don't
	 * already have a lock in the current xact.
	 */
	if (elm == NULL || elm->xid != thisxid)
		seqrel = relation_open(relid, AccessShareLock);
	else
		seqrel = relation_open(relid, NoLock);
686

687
	if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
688 689
		elog(ERROR, "%s.%s: %s is not a sequence",
			 relation->relname, caller, relation->relname);
690

691
	/*
692
	 * Allocate new seqtable entry if we didn't find one.
693 694
	 *
	 * NOTE: seqtable entries remain in the list for the life of a backend.
B
Bruce Momjian 已提交
695 696 697
	 * If the sequence itself is deleted then the entry becomes wasted
	 * memory, but it's small enough that this should not matter.
	 */
698
	if (elm == NULL)
699
	{
700 701
		/*
		 * Time to make a new seqtable entry.  These entries live as long
702 703 704
		 * as the backend does, so we use plain malloc for them.
		 */
		elm = (SeqTable) malloc(sizeof(SeqTableData));
T
Tom Lane 已提交
705 706
		if (elm == NULL)
			elog(ERROR, "Memory exhausted in init_sequence");
707
		elm->relid = relid;
708 709 710 711
		/* increment is set to 0 until we do read_info (see currval) */
		elm->last = elm->cached = elm->increment = 0;
		elm->next = seqtab;
		seqtab = elm;
712 713
	}

714 715 716 717 718
	/* Flag that we have a lock in the current xact. */
	elm->xid = thisxid;

	*p_elm = elm;
	*p_rel = seqrel;
719 720 721
}


722 723 724 725
/* Given an opened relation, lock the page buffer and find the tuple */
static Form_pg_sequence
read_info(const char *caller, SeqTable elm,
		  Relation rel, Buffer *buf)
726
{
727 728 729 730 731
	PageHeader	page;
	ItemId		lp;
	HeapTupleData tuple;
	sequence_magic *sm;
	Form_pg_sequence seq;
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
	if (rel->rd_nblocks > 1)
		elog(ERROR, "%s.%s: invalid number of blocks in sequence",
			 RelationGetRelationName(rel), caller);

	*buf = ReadBuffer(rel, 0);
	if (!BufferIsValid(*buf))
		elog(ERROR, "%s.%s: ReadBuffer failed",
			 RelationGetRelationName(rel), caller);

	LockBuffer(*buf, BUFFER_LOCK_EXCLUSIVE);

	page = (PageHeader) BufferGetPage(*buf);
	sm = (sequence_magic *) PageGetSpecialPointer(page);

	if (sm->magic != SEQ_MAGIC)
		elog(ERROR, "%s.%s: bad magic (%08X)",
			 RelationGetRelationName(rel), caller, sm->magic);

	lp = PageGetItemId(page, FirstOffsetNumber);
	Assert(ItemIdIsUsed(lp));
	tuple.t_data = (HeapTupleHeader) PageGetItem((Page) page, lp);

	seq = (Form_pg_sequence) GETSTRUCT(&tuple);

	elm->increment = seq->increment_by;

	return seq;
760 761 762
}


763
static void
764
init_params(CreateSeqStmt *seq, Form_pg_sequence new)
765
{
766 767 768 769 770
	DefElem    *last_value = NULL;
	DefElem    *increment_by = NULL;
	DefElem    *max_value = NULL;
	DefElem    *min_value = NULL;
	DefElem    *cache_value = NULL;
771
	bool		is_cycled_set = false;
772
	List	   *option;
773

774
	new->is_cycled = false;
775 776
	foreach(option, seq->options)
	{
777
		DefElem    *defel = (DefElem *) lfirst(option);
778

779
		if (strcmp(defel->defname, "increment") == 0)
780 781 782
		{
			if (increment_by)
				elog(ERROR, "DefineSequence: INCREMENT BY defined twice");
783
			increment_by = defel;
784
		}
785
		else if (strcmp(defel->defname, "start") == 0)
786 787 788
		{
			if (last_value)
				elog(ERROR, "DefineSequence: LAST VALUE defined twice");
789
			last_value = defel;
790
		}
791
		else if (strcmp(defel->defname, "maxvalue") == 0)
792 793 794
		{
			if (max_value)
				elog(ERROR, "DefineSequence: MAX VALUE defined twice");
795
			max_value = defel;
796
		}
797
		else if (strcmp(defel->defname, "minvalue") == 0)
798 799 800
		{
			if (min_value)
				elog(ERROR, "DefineSequence: MIN VALUE defined twice");
801
			min_value = defel;
802
		}
803
		else if (strcmp(defel->defname, "cache") == 0)
804 805 806
		{
			if (cache_value)
				elog(ERROR, "DefineSequence: CACHE defined twice");
807
			cache_value = defel;
808
		}
809
		else if (strcmp(defel->defname, "cycle") == 0)
810 811 812 813
		{
			if (is_cycled_set)
				elog(ERROR, "DefineSequence: CYCLE defined twice");
			is_cycled_set = true;
814
			new->is_cycled = (defel->arg != NULL);
815
		}
816
		else
817
			elog(ERROR, "DefineSequence: option \"%s\" not recognized",
818 819 820 821 822
				 defel->defname);
	}

	if (increment_by == (DefElem *) NULL)		/* INCREMENT BY */
		new->increment_by = 1;
823
	else if ((new->increment_by = defGetInt64(increment_by)) == 0)
824
		elog(ERROR, "DefineSequence: can't INCREMENT by 0");
825

826
	if (max_value == (DefElem *) NULL || !max_value->arg)	/* MAXVALUE */
827
	{
828 829 830
		if (new->increment_by > 0)
			new->max_value = SEQ_MAXVALUE;		/* ascending seq */
		else
831
			new->max_value = -1;	/* descending seq */
832
	}
833
	else
834
		new->max_value = defGetInt64(max_value);
835

836
	if (min_value == (DefElem *) NULL || !min_value->arg)	/* MINVALUE */
837
	{
838 839 840 841
		if (new->increment_by > 0)
			new->min_value = 1; /* ascending seq */
		else
			new->min_value = SEQ_MINVALUE;		/* descending seq */
842
	}
843
	else
844
		new->min_value = defGetInt64(min_value);
845 846

	if (new->min_value >= new->max_value)
847
	{
B
Bruce Momjian 已提交
848 849 850
		char		bufm[100],
					bufx[100];

851 852
		snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->min_value);
		snprintf(bufx, sizeof(bufx), INT64_FORMAT, new->max_value);
853 854 855
		elog(ERROR, "DefineSequence: MINVALUE (%s) must be less than MAXVALUE (%s)",
			 bufm, bufx);
	}
856 857

	if (last_value == (DefElem *) NULL) /* START WITH */
858
	{
859 860 861 862
		if (new->increment_by > 0)
			new->last_value = new->min_value;	/* ascending seq */
		else
			new->last_value = new->max_value;	/* descending seq */
863
	}
864
	else
865
		new->last_value = defGetInt64(last_value);
866 867

	if (new->last_value < new->min_value)
868
	{
B
Bruce Momjian 已提交
869 870 871
		char		bufs[100],
					bufm[100];

872 873
		snprintf(bufs, sizeof(bufs), INT64_FORMAT, new->last_value);
		snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->min_value);
874 875 876
		elog(ERROR, "DefineSequence: START value (%s) can't be less than MINVALUE (%s)",
			 bufs, bufm);
	}
877
	if (new->last_value > new->max_value)
878
	{
B
Bruce Momjian 已提交
879 880 881
		char		bufs[100],
					bufm[100];

882 883
		snprintf(bufs, sizeof(bufs), INT64_FORMAT, new->last_value);
		snprintf(bufm, sizeof(bufm), INT64_FORMAT, new->max_value);
884 885 886
		elog(ERROR, "DefineSequence: START value (%s) can't be greater than MAXVALUE (%s)",
			 bufs, bufm);
	}
887 888 889

	if (cache_value == (DefElem *) NULL)		/* CACHE */
		new->cache_value = 1;
890
	else if ((new->cache_value = defGetInt64(cache_value)) <= 0)
891
	{
B
Bruce Momjian 已提交
892 893
		char		buf[100];

894
		snprintf(buf, sizeof(buf), INT64_FORMAT, new->cache_value);
895 896 897
		elog(ERROR, "DefineSequence: CACHE (%s) can't be <= 0",
			 buf);
	}
898 899 900

}

V
Vadim B. Mikheev 已提交
901

B
Bruce Momjian 已提交
902 903
void
seq_redo(XLogRecPtr lsn, XLogRecord *record)
V
Vadim B. Mikheev 已提交
904
{
B
Bruce Momjian 已提交
905 906 907 908 909 910 911
	uint8		info = record->xl_info & ~XLR_INFO_MASK;
	Relation	reln;
	Buffer		buffer;
	Page		page;
	char	   *item;
	Size		itemsz;
	xl_seq_rec *xlrec = (xl_seq_rec *) XLogRecGetData(record);
912
	sequence_magic *sm;
V
Vadim B. Mikheev 已提交
913

914
	if (info != XLOG_SEQ_LOG)
915
		elog(PANIC, "seq_redo: unknown op code %u", info);
V
Vadim B. Mikheev 已提交
916 917 918 919 920

	reln = XLogOpenRelation(true, RM_SEQ_ID, xlrec->node);
	if (!RelationIsValid(reln))
		return;

921
	buffer = XLogReadBuffer(true, reln, 0);
V
Vadim B. Mikheev 已提交
922
	if (!BufferIsValid(buffer))
923
		elog(PANIC, "seq_redo: can't read block of %u/%u",
B
Bruce Momjian 已提交
924
			 xlrec->node.tblNode, xlrec->node.relNode);
V
Vadim B. Mikheev 已提交
925 926 927

	page = (Page) BufferGetPage(buffer);

928 929
	/* Always reinit the page and reinstall the magic number */
	/* See comments in DefineSequence */
930 931 932
	PageInit((Page) page, BufferGetPageSize(buffer), sizeof(sequence_magic));
	sm = (sequence_magic *) PageGetSpecialPointer(page);
	sm->magic = SEQ_MAGIC;
V
Vadim B. Mikheev 已提交
933

B
Bruce Momjian 已提交
934
	item = (char *) xlrec + sizeof(xl_seq_rec);
935 936
	itemsz = record->xl_len - sizeof(xl_seq_rec);
	itemsz = MAXALIGN(itemsz);
B
Bruce Momjian 已提交
937
	if (PageAddItem(page, (Item) item, itemsz,
938
					FirstOffsetNumber, LP_USED) == InvalidOffsetNumber)
939
		elog(PANIC, "seq_redo: failed to add item to page");
V
Vadim B. Mikheev 已提交
940 941 942 943 944 945

	PageSetLSN(page, lsn);
	PageSetSUI(page, ThisStartUpID);
	UnlockAndWriteBuffer(buffer);
}

B
Bruce Momjian 已提交
946 947
void
seq_undo(XLogRecPtr lsn, XLogRecord *record)
V
Vadim B. Mikheev 已提交
948 949 950
{
}

B
Bruce Momjian 已提交
951 952
void
seq_desc(char *buf, uint8 xl_info, char *rec)
V
Vadim B. Mikheev 已提交
953
{
B
Bruce Momjian 已提交
954 955
	uint8		info = xl_info & ~XLR_INFO_MASK;
	xl_seq_rec *xlrec = (xl_seq_rec *) rec;
V
Vadim B. Mikheev 已提交
956 957 958 959 960 961 962 963 964

	if (info == XLOG_SEQ_LOG)
		strcat(buf, "log: ");
	else
	{
		strcat(buf, "UNKNOWN");
		return;
	}

965
	sprintf(buf + strlen(buf), "node %u/%u",
B
Bruce Momjian 已提交
966
			xlrec->node.tblNode, xlrec->node.relNode);
V
Vadim B. Mikheev 已提交
967
}