diff options
Diffstat (limited to 'src/backend/commands/sequence.c')
-rw-r--r-- | src/backend/commands/sequence.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 9f28d40466b..8c1131f0202 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -1773,6 +1773,67 @@ pg_sequence_parameters(PG_FUNCTION_ARGS) return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull)); } + +/* + * Return the sequence tuple. + * + * This is primarily intended for use by pg_dump to gather sequence data + * without needing to individually query each sequence relation. + */ +Datum +pg_sequence_read_tuple(PG_FUNCTION_ARGS) +{ + Oid relid = PG_GETARG_OID(0); + SeqTable elm; + Relation seqrel; + Datum values[SEQ_COL_LASTCOL] = {0}; + bool isnull[SEQ_COL_LASTCOL] = {0}; + TupleDesc resultTupleDesc; + HeapTuple resultHeapTuple; + Datum result; + + resultTupleDesc = CreateTemplateTupleDesc(SEQ_COL_LASTCOL); + TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "last_value", + INT8OID, -1, 0); + TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "log_cnt", + INT8OID, -1, 0); + TupleDescInitEntry(resultTupleDesc, (AttrNumber) 3, "is_called", + BOOLOID, -1, 0); + resultTupleDesc = BlessTupleDesc(resultTupleDesc); + + init_sequence(relid, &elm, &seqrel); + + /* + * Return all NULLs for sequences for which we lack privileges, other + * sessions' temporary sequences, and unlogged sequences on standbys. + */ + if (pg_class_aclcheck(relid, GetUserId(), ACL_SELECT) == ACLCHECK_OK && + !RELATION_IS_OTHER_TEMP(seqrel) && + (RelationIsPermanent(seqrel) || !RecoveryInProgress())) + { + Buffer buf; + HeapTupleData seqtuple; + Form_pg_sequence_data seq; + + seq = read_seq_tuple(seqrel, &buf, &seqtuple); + + values[0] = Int64GetDatum(seq->last_value); + values[1] = Int64GetDatum(seq->log_cnt); + values[2] = BoolGetDatum(seq->is_called); + + UnlockReleaseBuffer(buf); + } + else + memset(isnull, true, sizeof(isnull)); + + sequence_close(seqrel, NoLock); + + resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull); + result = HeapTupleGetDatum(resultHeapTuple); + PG_RETURN_DATUM(result); +} + + /* * Return the last value from the sequence * |