mirror of
https://github.com/postgres/postgres.git
synced 2025-08-21 10:42:50 +03:00
Add pg_sequences view
Like pg_tables, pg_views, and others, this view contains information about sequences in a way that is independent of the system catalog layout but more comprehensive than the information schema. To help implement the view, add a new internal function pg_sequence_last_value() to return the last value of a sequence. This is kept separate from pg_sequence_parameters() to separate querying run-time state from catalog-like information. Reviewed-by: Andreas Karlsson <andreas@proxel.se>
This commit is contained in:
@@ -1534,8 +1534,8 @@ pg_sequence_parameters(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
TupleDesc tupdesc;
|
||||
Datum values[5];
|
||||
bool isnull[5];
|
||||
Datum values[6];
|
||||
bool isnull[6];
|
||||
SeqTable elm;
|
||||
Relation seqrel;
|
||||
Buffer buf;
|
||||
@@ -1551,7 +1551,7 @@ pg_sequence_parameters(PG_FUNCTION_ARGS)
|
||||
errmsg("permission denied for sequence %s",
|
||||
RelationGetRelationName(seqrel))));
|
||||
|
||||
tupdesc = CreateTemplateTupleDesc(5, false);
|
||||
tupdesc = CreateTemplateTupleDesc(6, false);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "start_value",
|
||||
INT8OID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "minimum_value",
|
||||
@@ -1562,6 +1562,8 @@ pg_sequence_parameters(PG_FUNCTION_ARGS)
|
||||
INT8OID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "cycle_option",
|
||||
BOOLOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 6, "cache_size",
|
||||
INT8OID, -1, 0);
|
||||
|
||||
BlessTupleDesc(tupdesc);
|
||||
|
||||
@@ -1574,6 +1576,7 @@ pg_sequence_parameters(PG_FUNCTION_ARGS)
|
||||
values[2] = Int64GetDatum(seq->max_value);
|
||||
values[3] = Int64GetDatum(seq->increment_by);
|
||||
values[4] = BoolGetDatum(seq->is_cycled);
|
||||
values[5] = Int64GetDatum(seq->cache_value);
|
||||
|
||||
UnlockReleaseBuffer(buf);
|
||||
relation_close(seqrel, NoLock);
|
||||
@@ -1581,6 +1584,46 @@ pg_sequence_parameters(PG_FUNCTION_ARGS)
|
||||
return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the last value from the sequence
|
||||
*
|
||||
* Note: This has a completely different meaning than lastval().
|
||||
*/
|
||||
Datum
|
||||
pg_sequence_last_value(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
SeqTable elm;
|
||||
Relation seqrel;
|
||||
Buffer buf;
|
||||
HeapTupleData seqtuple;
|
||||
Form_pg_sequence seq;
|
||||
bool is_called;
|
||||
int64 result;
|
||||
|
||||
/* open and AccessShareLock sequence */
|
||||
init_sequence(relid, &elm, &seqrel);
|
||||
|
||||
if (pg_class_aclcheck(relid, GetUserId(), ACL_SELECT | ACL_USAGE) != ACLCHECK_OK)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
errmsg("permission denied for sequence %s",
|
||||
RelationGetRelationName(seqrel))));
|
||||
|
||||
seq = read_seq_tuple(elm, seqrel, &buf, &seqtuple);
|
||||
|
||||
is_called = seq->is_called;
|
||||
result = seq->last_value;
|
||||
|
||||
UnlockReleaseBuffer(buf);
|
||||
relation_close(seqrel, NoLock);
|
||||
|
||||
if (is_called)
|
||||
PG_RETURN_INT64(result);
|
||||
else
|
||||
PG_RETURN_NULL();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
seq_redo(XLogReaderState *record)
|
||||
|
Reference in New Issue
Block a user