aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/sequence/sequence.c
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2024-02-26 16:04:59 +0900
committerMichael Paquier <michael@paquier.xyz>2024-02-26 16:04:59 +0900
commit449e798c77ed9a03f8bb04e5d324d4e3cfbbae8e (patch)
tree91f643e47147d44a95fc707908549de2d539eb5d /src/backend/access/sequence/sequence.c
parent025f0a6f9113efe89bb65b9efe0cb96a5d7c7ad1 (diff)
downloadpostgresql-449e798c77ed9a03f8bb04e5d324d4e3cfbbae8e.tar.gz
postgresql-449e798c77ed9a03f8bb04e5d324d4e3cfbbae8e.zip
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
Diffstat (limited to 'src/backend/access/sequence/sequence.c')
-rw-r--r--src/backend/access/sequence/sequence.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/backend/access/sequence/sequence.c b/src/backend/access/sequence/sequence.c
new file mode 100644
index 00000000000..9e30e6e08fe
--- /dev/null
+++ b/src/backend/access/sequence/sequence.c
@@ -0,0 +1,78 @@
+/*-------------------------------------------------------------------------
+ *
+ * sequence.c
+ * Generic routines for sequence-related code.
+ *
+ * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/backend/access/sequence/sequence.c
+ *
+ *
+ * NOTES
+ * This file contains sequence_ routines that implement access to sequences
+ * (in contrast to other relation types like indexes).
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "access/relation.h"
+#include "access/sequence.h"
+#include "storage/lmgr.h"
+
+static inline void validate_relation_kind(Relation r);
+
+/* ----------------
+ * sequence_open - open a sequence relation by relation OID
+ *
+ * This is essentially relation_open plus check that the relation
+ * is a sequence.
+ * ----------------
+ */
+Relation
+sequence_open(Oid relationId, LOCKMODE lockmode)
+{
+ Relation r;
+
+ r = relation_open(relationId, lockmode);
+
+ validate_relation_kind(r);
+
+ return r;
+}
+
+/* ----------------
+ * sequence_close - close a sequence
+ *
+ * If lockmode is not "NoLock", we then release the specified lock.
+ *
+ * Note that it is often sensible to hold a lock beyond relation_close;
+ * in that case, the lock is released automatically at xact end.
+ * ----------------
+ */
+void
+sequence_close(Relation relation, LOCKMODE lockmode)
+{
+ relation_close(relation, lockmode);
+}
+
+/* ----------------
+ * validate_relation_kind - check the relation's kind
+ *
+ * Make sure relkind is from an index
+ * ----------------
+ */
+static inline void
+validate_relation_kind(Relation r)
+{
+ if (r->rd_rel->relkind != RELKIND_SEQUENCE)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("cannot open relation \"%s\"",
+ RelationGetRelationName(r)),
+ errdetail_relkind_not_supported(r->rd_rel->relkind)));
+}