Skip to content

Commit 1ce2bd4

Browse files
committed
spock.sub_alter_options: validate input, skip no-op restarts, add regression tests
Harden the sub_alter_options() implementation in three ways: * No-op optimisation: track a `changed` flag through the JSONB parsing loop. alter_subscription() — which sends SIGTERM to the apply worker at commit — is only called when at least one option was actually modified. An empty object '{}' now returns false without disturbing the apply worker. * Return value: the function now returns false when nothing changed and true when at least one subscription field was updated, giving callers a reliable signal about whether a worker restart is coming. * Input validation: forward_origins elements are checked against the only supported sentinel value "all". Any other string raises INVALID_PARAMETER_VALUE with a hint, replacing the previous TODO comment. Add tests/regress/sql/alter_options.sql covering: baseline state snapshot, no-op '{}', each option individually (forward_origins, apply_delay, skip_schema), a multi-option call, baseline restore, and all expected error paths (non-object JSON, unknown key, wrong value types, invalid interval, non-existent subscription, invalid forward_origins value).
1 parent f020a1f commit 1ce2bd4

4 files changed

Lines changed: 642 additions & 6 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ REGRESS = preseed infofuncs init_fail init preseed_check basic conflict_secondar
5454
excluded_schema conflict_stat \
5555
toasted replication_set exception_row_capture matview bidirectional primary_key \
5656
interfaces foreign_key copy sequence triggers parallel functions row_filter \
57-
row_filter_sampling att_list column_filter apply_delay \
57+
row_filter_sampling att_list column_filter apply_delay alter_options \
5858
extended node_origin_cascade multiple_upstreams tuple_origin autoddl \
5959
sync_event sync_table generated_columns spill_transaction read_only drop
6060

src/spock_functions.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ spock_alter_subscription_options(PG_FUNCTION_ARGS)
947947
JsonbIterator *it;
948948
JsonbIteratorToken r;
949949
JsonbValue v;
950+
bool changed = false;
950951

951952
/* XXX: Only used for locking purposes. */
952953
(void) get_local_node(true, false);
@@ -984,20 +985,33 @@ spock_alter_subscription_options(PG_FUNCTION_ARGS)
984985

985986
while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_END_ARRAY)
986987
{
988+
char *elem;
989+
987990
if (r != WJB_ELEM || v.type != jbvString)
988991
ereport(ERROR,
989992
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
990993
errmsg("option \"%s\" must contain only strings",
991994
key)));
992-
/* TODO: further commits should perform a precheck of the input */
993-
result = lappend(result,
994-
pnstrdup(v.val.string.val, v.val.string.len));
995+
996+
elem = pnstrdup(v.val.string.val, v.val.string.len);
997+
998+
/* forward_origins only accepts the sentinel value "all" */
999+
if (strcmp(key, "forward_origins") == 0 &&
1000+
strcmp(elem, "all") != 0)
1001+
ereport(ERROR,
1002+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1003+
errmsg("invalid value \"%s\" for option \"forward_origins\"",
1004+
elem),
1005+
errhint("The only supported value is \"all\".")));
1006+
1007+
result = lappend(result, elem);
9951008
}
9961009

9971010
if (strcmp(key, "forward_origins") == 0)
9981011
sub->forward_origins = result;
9991012
else
10001013
sub->skip_schema = result;
1014+
changed = true;
10011015
}
10021016
else if (strcmp(key, "apply_delay") == 0)
10031017
{
@@ -1015,6 +1029,7 @@ spock_alter_subscription_options(PG_FUNCTION_ARGS)
10151029
CStringGetDatum(delay_str),
10161030
ObjectIdGetDatum(InvalidOid),
10171031
Int32GetDatum(-1)));
1032+
changed = true;
10181033
}
10191034
else
10201035
{
@@ -1026,9 +1041,10 @@ spock_alter_subscription_options(PG_FUNCTION_ARGS)
10261041
}
10271042
}
10281043

1029-
alter_subscription(sub);
1044+
if (changed)
1045+
alter_subscription(sub);
10301046

1031-
PG_RETURN_BOOL(true);
1047+
PG_RETURN_BOOL(changed);
10321048
}
10331049

10341050
/*

0 commit comments

Comments
 (0)