Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for trigger context before accessing data #6820

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .unreleased/fix_6820
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes: #6820 Fix a crash when the ts_hypertable_insert_blocker was called directly
Thanks: @brasic for reporting a crash when the ts_hypertable_insert_blocker was called directly
9 changes: 6 additions & 3 deletions src/hypertable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1318,12 +1318,15 @@ TS_FUNCTION_INFO_V1(ts_hypertable_insert_blocker);
Datum
ts_hypertable_insert_blocker(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
const char *relname = get_rel_name(trigdata->tg_relation->rd_id);

if (!CALLED_AS_TRIGGER(fcinfo))
elog(ERROR, "insert_blocker: not called by trigger manager");

TriggerData *trigdata = (TriggerData *) fcinfo->context;
Ensure(trigdata != NULL, "trigdata has to be set");
Ensure(trigdata->tg_relation != NULL, "tg_relation has to be set");

const char *relname = get_rel_name(trigdata->tg_relation->rd_id);

if (ts_guc_restoring)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
Expand Down
3 changes: 3 additions & 0 deletions test/expected/triggers.out
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,7 @@ CREATE TRIGGER t3 AFTER UPDATE ON transition_test REFERENCING NEW TABLE AS new_t
ERROR: trigger with transition tables not supported on hypertables
CREATE TRIGGER t4 AFTER DELETE ON transition_test REFERENCING OLD TABLE AS old_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
ERROR: trigger with transition tables not supported on hypertables
-- Test insert blocker trigger does not crash when called directly
SELECT _timescaledb_functions.insert_blocker();
ERROR: insert_blocker: not called by trigger manager
\set ON_ERROR_STOP 1
4 changes: 3 additions & 1 deletion test/sql/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ CREATE TRIGGER t4 AFTER DELETE ON transition_test REFERENCING OLD TABLE AS old_t
CREATE TRIGGER t2 AFTER INSERT ON transition_test REFERENCING NEW TABLE AS new_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER t3 AFTER UPDATE ON transition_test REFERENCING NEW TABLE AS new_trans OLD TABLE AS old_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER t4 AFTER DELETE ON transition_test REFERENCING OLD TABLE AS old_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
\set ON_ERROR_STOP 1

-- Test insert blocker trigger does not crash when called directly
SELECT _timescaledb_functions.insert_blocker();
jnidzwetzki marked this conversation as resolved.
Show resolved Hide resolved

\set ON_ERROR_STOP 1
Loading