v2-0004-Remove-FormData_pg_sequence_data-from-init_params.patch
text/x-diff
Filename: v2-0004-Remove-FormData_pg_sequence_data-from-init_params.patch
Type: text/x-diff
Part: 3
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v2-0004
Subject: Remove FormData_pg_sequence_data from init_params()/sequence.c
| File | + | − |
|---|---|---|
| src/backend/commands/sequence.c | 49 | 32 |
From f9e8dc91c2defc81d6b69f512b7994d3c9d0c4a4 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 1 Dec 2023 12:00:45 +0900
Subject: [PATCH v2 04/10] Remove FormData_pg_sequence_data from
init_params()/sequence.c
init_params() sets up "last_value" and "is_called" for a sequence, based
on the sequence properties in pg_sequences. This simplifies the logic
around log_cnt, which is reset to 0 when the metadata of a sequence is
expected to start from afresh when its properties are updated.
---
src/backend/commands/sequence.c | 81 ++++++++++++++++++++-------------
1 file changed, 49 insertions(+), 32 deletions(-)
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index ef71efdb82..10c96a266f 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -106,7 +106,9 @@ static Form_pg_sequence_data read_seq_tuple(Relation rel,
static void init_params(ParseState *pstate, List *options, bool for_identity,
bool isInit,
Form_pg_sequence seqform,
- Form_pg_sequence_data seqdataform,
+ int64 *last_value,
+ bool *reset_state,
+ bool *is_called,
bool *need_seq_rewrite,
List **owned_by);
static void do_setval(Oid relid, int64 next, bool iscalled);
@@ -121,7 +123,9 @@ ObjectAddress
DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
{
FormData_pg_sequence seqform;
- FormData_pg_sequence_data seqdataform;
+ int64 last_value;
+ bool reset_state;
+ bool is_called;
bool need_seq_rewrite;
List *owned_by;
CreateStmt *stmt = makeNode(CreateStmt);
@@ -164,7 +168,7 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
/* Check and set all option values */
init_params(pstate, seq->options, seq->for_identity, true,
- &seqform, &seqdataform,
+ &seqform, &last_value, &reset_state, &is_called,
&need_seq_rewrite, &owned_by);
/*
@@ -179,7 +183,7 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
{
case SEQ_COL_LASTVAL:
coldef = makeColumnDef("last_value", INT8OID, -1, InvalidOid);
- value[i - 1] = Int64GetDatumFast(seqdataform.last_value);
+ value[i - 1] = Int64GetDatumFast(last_value);
break;
case SEQ_COL_LOG:
coldef = makeColumnDef("log_cnt", INT8OID, -1, InvalidOid);
@@ -448,6 +452,9 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
ObjectAddress address;
Relation rel;
HeapTuple seqtuple;
+ bool reset_state = false;
+ bool is_called;
+ int64 last_value;
HeapTuple newdatatuple;
/* Open and lock sequence, and check for ownership along the way. */
@@ -481,12 +488,14 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
/* copy the existing sequence data tuple, so it can be modified locally */
newdatatuple = heap_copytuple(&datatuple);
newdataform = (Form_pg_sequence_data) GETSTRUCT(newdatatuple);
+ last_value = newdataform->last_value;
+ is_called = newdataform->is_called;
UnlockReleaseBuffer(buf);
/* Check and set new values */
init_params(pstate, stmt->options, stmt->for_identity, false,
- seqform, newdataform,
+ seqform, &last_value, &reset_state, &is_called,
&need_seq_rewrite, &owned_by);
/* If needed, rewrite the sequence relation itself */
@@ -513,6 +522,10 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
/*
* Insert the modified tuple into the new storage file.
*/
+ newdataform->last_value = last_value;
+ newdataform->is_called = is_called;
+ if (reset_state)
+ newdataform->log_cnt = 0;
fill_seq_with_data(seqrel, newdatatuple);
}
@@ -1229,17 +1242,19 @@ read_seq_tuple(Relation rel, Buffer *buf, HeapTuple seqdatatuple)
/*
* init_params: process the options list of CREATE or ALTER SEQUENCE, and
* store the values into appropriate fields of seqform, for changes that go
- * into the pg_sequence catalog, and fields of seqdataform for changes to the
- * sequence relation itself. Set *need_seq_rewrite to true if we changed any
- * parameters that require rewriting the sequence's relation (interesting for
- * ALTER SEQUENCE). Also set *owned_by to any OWNED BY option, or to NIL if
- * there is none.
+ * into the pg_sequence catalog, and fields for changes to the sequence
+ * relation itself (is_called, last_value or any state it may hold). Set
+ * *need_seq_rewrite to true if we changed any parameters that require
+ * rewriting the sequence's relation (interesting for ALTER SEQUENCE). Also
+ * set *owned_by to any OWNED BY option, or to NIL if there is none. Set
+ * *reset_state if the internal state of the sequence needs to change on a
+ * follow-up nextval().
*
* If isInit is true, fill any unspecified options with default values;
* otherwise, do not change existing options that aren't explicitly overridden.
*
* Note: we force a sequence rewrite whenever we change parameters that affect
- * generation of future sequence values, even if the seqdataform per se is not
+ * generation of future sequence values, even if the metadata per se is not
* changed. This allows ALTER SEQUENCE to behave transactionally. Currently,
* the only option that doesn't cause that is OWNED BY. It's *necessary* for
* ALTER SEQUENCE OWNED BY to not rewrite the sequence, because that would
@@ -1250,7 +1265,9 @@ static void
init_params(ParseState *pstate, List *options, bool for_identity,
bool isInit,
Form_pg_sequence seqform,
- Form_pg_sequence_data seqdataform,
+ int64 *last_value,
+ bool *reset_state,
+ bool *is_called,
bool *need_seq_rewrite,
List **owned_by)
{
@@ -1353,11 +1370,11 @@ init_params(ParseState *pstate, List *options, bool for_identity,
}
/*
- * We must reset log_cnt when isInit or when changing any parameters that
- * would affect future nextval allocations.
+ * We must reset the state when isInit or when changing any parameters
+ * that would affect future nextval allocations.
*/
if (isInit)
- seqdataform->log_cnt = 0;
+ *reset_state = true;
/* AS type */
if (as_type != NULL)
@@ -1406,7 +1423,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("INCREMENT must not be zero")));
- seqdataform->log_cnt = 0;
+ *reset_state = true;
}
else if (isInit)
{
@@ -1418,7 +1435,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
{
seqform->seqcycle = boolVal(is_cycled->arg);
Assert(BoolIsValid(seqform->seqcycle));
- seqdataform->log_cnt = 0;
+ *reset_state = true;
}
else if (isInit)
{
@@ -1429,7 +1446,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
if (max_value != NULL && max_value->arg)
{
seqform->seqmax = defGetInt64(max_value);
- seqdataform->log_cnt = 0;
+ *reset_state = true;
}
else if (isInit || max_value != NULL || reset_max_value)
{
@@ -1445,7 +1462,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
}
else
seqform->seqmax = -1; /* descending seq */
- seqdataform->log_cnt = 0;
+ *reset_state = true;
}
/* Validate maximum value. No need to check INT8 as seqmax is an int64 */
@@ -1461,7 +1478,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
if (min_value != NULL && min_value->arg)
{
seqform->seqmin = defGetInt64(min_value);
- seqdataform->log_cnt = 0;
+ *reset_state = true;
}
else if (isInit || min_value != NULL || reset_min_value)
{
@@ -1477,7 +1494,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
}
else
seqform->seqmin = 1; /* ascending seq */
- seqdataform->log_cnt = 0;
+ *reset_state = true;
}
/* Validate minimum value. No need to check INT8 as seqmin is an int64 */
@@ -1528,30 +1545,30 @@ init_params(ParseState *pstate, List *options, bool for_identity,
if (restart_value != NULL)
{
if (restart_value->arg != NULL)
- seqdataform->last_value = defGetInt64(restart_value);
+ *last_value = defGetInt64(restart_value);
else
- seqdataform->last_value = seqform->seqstart;
- seqdataform->is_called = false;
- seqdataform->log_cnt = 0;
+ *last_value = seqform->seqstart;
+ *is_called = false;
+ *reset_state = true;
}
else if (isInit)
{
- seqdataform->last_value = seqform->seqstart;
- seqdataform->is_called = false;
+ *last_value = seqform->seqstart;
+ *is_called = false;
}
/* crosscheck RESTART (or current value, if changing MIN/MAX) */
- if (seqdataform->last_value < seqform->seqmin)
+ if (*last_value < seqform->seqmin)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("RESTART value (%lld) cannot be less than MINVALUE (%lld)",
- (long long) seqdataform->last_value,
+ (long long) *last_value,
(long long) seqform->seqmin)));
- if (seqdataform->last_value > seqform->seqmax)
+ if (*last_value > seqform->seqmax)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("RESTART value (%lld) cannot be greater than MAXVALUE (%lld)",
- (long long) seqdataform->last_value,
+ (long long) *last_value,
(long long) seqform->seqmax)));
/* CACHE */
@@ -1563,7 +1580,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("CACHE (%lld) must be greater than zero",
(long long) seqform->seqcache)));
- seqdataform->log_cnt = 0;
+ *reset_state = true;
}
else if (isInit)
{
--
2.43.0