* Return the last value from the sequence * * Note: This has a completely different meaning than lastval(). */
| 2033 | * Note: This has a completely different meaning than lastval(). |
| 2034 | */ |
| 2035 | Datum |
| 2036 | pg_sequence_last_value(PG_FUNCTION_ARGS) |
| 2037 | { |
| 2038 | Oid relid = PG_GETARG_OID(0); |
| 2039 | SeqTable elm; |
| 2040 | Relation seqrel; |
| 2041 | Buffer buf; |
| 2042 | HeapTupleData seqtuple; |
| 2043 | Form_pg_sequence_data seq; |
| 2044 | bool is_called; |
| 2045 | int64 result; |
| 2046 | |
| 2047 | /* open and lock sequence */ |
| 2048 | init_sequence(relid, &elm, &seqrel); |
| 2049 | |
| 2050 | if (pg_class_aclcheck(relid, GetUserId(), ACL_SELECT | ACL_USAGE) != ACLCHECK_OK) |
| 2051 | ereport(ERROR, |
| 2052 | (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 2053 | errmsg("permission denied for sequence %s", |
| 2054 | RelationGetRelationName(seqrel)))); |
| 2055 | |
| 2056 | seq = read_seq_tuple(seqrel, &buf, &seqtuple); |
| 2057 | |
| 2058 | is_called = seq->is_called; |
| 2059 | result = seq->last_value; |
| 2060 | |
| 2061 | UnlockReleaseBuffer(buf); |
| 2062 | relation_close(seqrel, NoLock); |
| 2063 | |
| 2064 | if (is_called) |
| 2065 | PG_RETURN_INT64(result); |
| 2066 | else |
| 2067 | PG_RETURN_NULL(); |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | void |
nothing calls this directly
no test coverage detected