mirror of
https://github.com/postgres/postgres.git
synced 2025-09-11 00:12:06 +03:00
Introduce sequence_*() access functions
Similarly to tables and indexes, these functions are able to open relations with a sequence relkind, which is useful to make a distinction with the other relation kinds. Previously, commands/sequence.c used a mix of table_{close,open}() and relation_{close,open}() routines when manipulating sequence relations, so this clarifies the code. A direct effect of this change is to align the error messages produced when attempting DDLs for sequences on relations with an unexpected relkind, like a table or an index with ALTER SEQUENCE, providing an extra error detail about the relkind of the relation used in the DDL query. Author: Michael Paquier Reviewed-by: Tomas Vondra Discussion: https://postgr.es/m/ZWlohtKAs0uVVpZ3@paquier.xyz
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include "access/htup_details.h"
|
||||
#include "access/multixact.h"
|
||||
#include "access/relation.h"
|
||||
#include "access/sequence.h"
|
||||
#include "access/table.h"
|
||||
#include "access/transam.h"
|
||||
#include "access/xact.h"
|
||||
@@ -208,7 +209,7 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
|
||||
seqoid = address.objectId;
|
||||
Assert(seqoid != InvalidOid);
|
||||
|
||||
rel = table_open(seqoid, AccessExclusiveLock);
|
||||
rel = sequence_open(seqoid, AccessExclusiveLock);
|
||||
tupDesc = RelationGetDescr(rel);
|
||||
|
||||
/* now initialize the sequence's data */
|
||||
@@ -219,7 +220,7 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
|
||||
if (owned_by)
|
||||
process_owned_by(rel, owned_by, seq->for_identity);
|
||||
|
||||
table_close(rel, NoLock);
|
||||
sequence_close(rel, NoLock);
|
||||
|
||||
/* fill in pg_sequence */
|
||||
rel = table_open(SequenceRelationId, RowExclusiveLock);
|
||||
@@ -324,7 +325,7 @@ ResetSequence(Oid seq_relid)
|
||||
/* Note that we do not change the currval() state */
|
||||
elm->cached = elm->last;
|
||||
|
||||
relation_close(seq_rel, NoLock);
|
||||
sequence_close(seq_rel, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -531,7 +532,7 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
|
||||
ObjectAddressSet(address, RelationRelationId, relid);
|
||||
|
||||
table_close(rel, RowExclusiveLock);
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
|
||||
return address;
|
||||
}
|
||||
@@ -555,7 +556,7 @@ SequenceChangePersistence(Oid relid, char newrelpersistence)
|
||||
fill_seq_with_data(seqrel, &seqdatatuple);
|
||||
UnlockReleaseBuffer(buf);
|
||||
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -662,7 +663,7 @@ nextval_internal(Oid relid, bool check_permissions)
|
||||
Assert(elm->last_valid);
|
||||
Assert(elm->increment != 0);
|
||||
elm->last += elm->increment;
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
last_used_seq = elm;
|
||||
return elm->last;
|
||||
}
|
||||
@@ -849,7 +850,7 @@ nextval_internal(Oid relid, bool check_permissions)
|
||||
|
||||
UnlockReleaseBuffer(buf);
|
||||
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -880,7 +881,7 @@ currval_oid(PG_FUNCTION_ARGS)
|
||||
|
||||
result = elm->last;
|
||||
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
@@ -915,7 +916,7 @@ lastval(PG_FUNCTION_ARGS)
|
||||
RelationGetRelationName(seqrel))));
|
||||
|
||||
result = last_used_seq->last;
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
|
||||
PG_RETURN_INT64(result);
|
||||
}
|
||||
@@ -1030,7 +1031,7 @@ do_setval(Oid relid, int64 next, bool iscalled)
|
||||
|
||||
UnlockReleaseBuffer(buf);
|
||||
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1095,7 +1096,7 @@ lock_and_open_sequence(SeqTable seq)
|
||||
}
|
||||
|
||||
/* We now know we have the lock, and can safely open the rel */
|
||||
return relation_open(seq->relid, NoLock);
|
||||
return sequence_open(seq->relid, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1152,12 +1153,6 @@ init_sequence(Oid relid, SeqTable *p_elm, Relation *p_rel)
|
||||
*/
|
||||
seqrel = lock_and_open_sequence(elm);
|
||||
|
||||
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is not a sequence",
|
||||
RelationGetRelationName(seqrel))));
|
||||
|
||||
/*
|
||||
* If the sequence has been transactionally replaced since we last saw it,
|
||||
* discard any cached-but-unissued values. We do not touch the currval()
|
||||
@@ -1803,7 +1798,7 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
|
||||
result = seq->last_value;
|
||||
|
||||
UnlockReleaseBuffer(buf);
|
||||
relation_close(seqrel, NoLock);
|
||||
sequence_close(seqrel, NoLock);
|
||||
|
||||
if (is_called)
|
||||
PG_RETURN_INT64(result);
|
||||
|
Reference in New Issue
Block a user