timescaledb icon indicating copy to clipboard operation
timescaledb copied to clipboard

[Bug]: Creating a foreign key referencing a tiered hypertable causes assertion failure

Open zilder opened this issue 6 months ago • 0 comments

What type of bug is this?

Crash

What subsystems and features are affected?

Command processing

What happened?

Getting an assertion failure when creating an FK referencing a tiered hypertable, see the reproduction steps.

TimescaleDB version affected

2.16.0

PostgreSQL version used

15.4

What operating system did you use?

Ubuntu 22.04.2

What installation method did you use?

Source

What platform did you run on?

Not applicable

Relevant log output and stack trace

#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=139514495584064) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=139514495584064) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=139514495584064, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ee33f642476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ee33f6287f3 in __GI_abort () at ./stdlib/abort.c:79
#5  0x00005e66a32079ff in ExceptionalCondition (conditionName=conditionName@entry=0x7ee33f8de6d6 "false", errorType=errorType@entry=0x7ee33f8c832d "FailedAssertion",
    fileName=fileName@entry=0x7ee33f8ce470 "/home/zilder/projects/timescaledb/src/foreign_key.c", lineNumber=lineNumber@entry=164) at assert.c:69
#6  0x00007ee33f87c00f in clone_constraint_on_chunk (parentUpdTrigger=1214020, parentDelTrigger=1214017, confdelsetcols=0x7ffe343bca60, numfkdelsetcols=0, conffeqop=0x7ffe343bcbf0, conppeqop=0x7ffe343bcb70,
    conpfeqop=0x7ffe343bcaf0, confkey=0x7ffe343bca20, conkey=0x7ffe343bc9e0, numfks=1, fk=0x5e66a56c9d60, parentRel=0x7ee335e7b718, chunk=0x5e66a56ca178) at /home/zilder/projects/timescaledb/src/foreign_key.c:164
#7  propagate_fk (ht_rel=0x7ee335e7b718, fk_tuple=<optimized out>, chunks=0x5e66a56ca290) at /home/zilder/projects/timescaledb/src/foreign_key.c:83
#8  0x00007ee33f87c2f2 in ts_fk_propagate (conrelid=conrelid@entry=1214011, ht=0x5e66a5686be8) at /home/zilder/projects/timescaledb/src/foreign_key.c:133
#9  0x00007ee33f88b8cd in process_altertable_end_table (cmd=<optimized out>, parsetree=<optimized out>) at /home/zilder/projects/timescaledb/src/process_utility.c:3979
#10 process_altertable_end (cmd=<optimized out>, parsetree=<optimized out>) at /home/zilder/projects/timescaledb/src/process_utility.c:3995
#11 process_ddl_command_end (cmd=<optimized out>) at /home/zilder/projects/timescaledb/src/process_utility.c:4375
#12 process_ddl_event_command_end (trigdata=<optimized out>) at /home/zilder/projects/timescaledb/src/process_utility.c:4607
#13 ts_timescaledb_process_ddl_event (fcinfo=0x7ffe343bcf70) at /home/zilder/projects/timescaledb/src/process_utility.c:4644
#14 0x00005e66a2eb6f3a in EventTriggerInvoke (fn_oid_list=0x5e66a56dc6a0, trigdata=0x7ffe343bcfe0) at event_trigger.c:920
#15 0x00005e66a2eb7a22 in EventTriggerDDLCommandEnd (parsetree=parsetree@entry=0x5e66a55b3f48) at event_trigger.c:727
#16 0x00005e66a30d3b34 in ProcessUtilitySlow (pstate=0x5e66a5521c28, pstmt=0x5e66a55b4528,
    queryString=0x5e66a55b2db0 "CREATE TABLE notes (metrics_id integer REFERENCES metrics(id) ON DELETE CASCADE,\n", ' ' <repeats 20 times>, "msg text);", context=PROCESS_UTILITY_TOPLEVEL, params=0x0,
    queryEnv=0x0, qc=0x7ffe343bdec0, dest=<optimized out>) at utility.c:1923
#17 0x00005e66a30d2fb9 in standard_ProcessUtility (pstmt=0x5e66a55b4528,
    queryString=0x5e66a55b2db0 "CREATE TABLE notes (metrics_id integer REFERENCES metrics(id) ON DELETE CASCADE,\n", ' ' <repeats 20 times>, "msg text);", readOnlyTree=<optimized out>,
    context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x5e66a55b45c0, qc=0x7ffe343bdec0) at utility.c:1074
#18 0x00007ee33f885d22 in prev_ProcessUtility (args=args@entry=0x7ffe343bd3b0) at /home/zilder/projects/timescaledb/src/process_utility.c:101
#19 0x00007ee33f886666 in timescaledb_ddl_command_start (pstmt=0x5e66a55b4528, query_string=<optimized out>, readonly_tree=<optimized out>, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=<optimized out>,
    dest=0x5e66a55b45c0, completion_tag=0x7ffe343bdec0) at /home/zilder/projects/timescaledb/src/process_utility.c:4589
#20 0x00007ee332ec0e2d in timescaledb_osm::hooks::osm_process_utility_hook::osm_process_utility_hook_inner (pstmt=0x5e66a55b4528, query_string=0x5e66a55b2db0, read_only_tree=false, context=0, params=0x0,
    query_env=0x0, dest=0x5e66a55b45c0, qc=0x7ffe343bdec0) at src/hooks.rs:59
#21 0x00007ee332fb4290 in timescaledb_osm::hooks::osm_process_utility_hook::{closure#0} () at src/hooks.rs:10
#22 0x00007ee332fa8ec0 in std::panicking::try::do_call<timescaledb_osm::hooks::osm_process_utility_hook::{closure_env#0}, ()> (data=0x7ffe343bd670)
    at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:552
#23 0x00007ee332fab51b in __rust_try.llvm.10777979070602599861 () from /home/zilder/pg15/lib/postgresql/timescaledb_osm-1.9.0-dev.so
#24 0x00007ee332fa5a72 in std::panicking::try<(), timescaledb_osm::hooks::osm_process_utility_hook::{closure_env#0}> (f=...) at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:516
#25 0x00007ee333fb1416 in std::panic::catch_unwind<timescaledb_osm::hooks::osm_process_utility_hook::{closure_env#0}, ()> (f=<error reading variable: Cannot access memory at address 0x669ee>)
    at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panic.rs:142
#26 0x00007ee332fbc853 in pgrx_pg_sys::submodules::panic::run_guarded<timescaledb_osm::hooks::osm_process_utility_hook::{closure_env#0}, ()> (f=...)
    at /home/zilder/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pgrx-pg-sys-0.11.3/src/submodules/panic.rs:408
#27 0x00007ee332fc0434 in pgrx_pg_sys::submodules::panic::pgrx_extern_c_guard<timescaledb_osm::hooks::osm_process_utility_hook::{closure_env#0}, ()> (
    f=<error reading variable: Cannot access memory at address 0x669ee>) at /home/zilder/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pgrx-pg-sys-0.11.3/src/submodules/panic.rs:385
#28 0x00007ee332ec09e5 in timescaledb_osm::hooks::osm_process_utility_hook (pstmt=0x5e66a55b4528, query_string=0x5e66a55b2db0, read_only_tree=false, context=0, params=0x0, query_env=0x0, dest=0x5e66a55b45c0,
    qc=0x7ffe343bdec0) at src/hooks.rs:12
#29 0x00005e66a30d13e6 in PortalRunUtility (portal=portal@entry=0x5e66a562a600, pstmt=0x5e66a55b4528, isTopLevel=isTopLevel@entry=true, setHoldSnapshot=setHoldSnapshot@entry=false,
    dest=dest@entry=0x5e66a55b45c0, qc=qc@entry=0x7ffe343bdec0) at pquery.c:1158
#30 0x00005e66a30d151a in PortalRunMulti (portal=portal@entry=0x5e66a562a600, isTopLevel=isTopLevel@entry=true, setHoldSnapshot=setHoldSnapshot@entry=false, dest=dest@entry=0x5e66a55b45c0,
    altdest=altdest@entry=0x5e66a55b45c0, qc=qc@entry=0x7ffe343bdec0) at pquery.c:1315
#31 0x00005e66a30d1c3c in PortalRun (portal=portal@entry=0x5e66a562a600, count=count@entry=9223372036854775807, isTopLevel=isTopLevel@entry=true, run_once=run_once@entry=true, dest=dest@entry=0x5e66a55b45c0,
    altdest=altdest@entry=0x5e66a55b45c0, qc=0x7ffe343bdec0) at pquery.c:791
#32 0x00005e66a30cd76b in exec_simple_query (query_string=0x5e66a55b2db0 "CREATE TABLE notes (metrics_id integer REFERENCES metrics(id) ON DELETE CASCADE,\n", ' ' <repeats 20 times>, "msg text);")
    at postgres.c:1250
#33 0x00005e66a30cf3b5 in PostgresMain (dbname=<optimized out>, username=<optimized out>) at postgres.c:4598
#34 0x00005e66a3035d9a in BackendRun (port=0x5e66a55dab30, port=0x5e66a55dab30) at postmaster.c:4511
#35 BackendStartup (port=0x5e66a55dab30) at postmaster.c:4239
#36 ServerLoop () at postmaster.c:1806
#37 0x00005e66a3036e6e in PostmasterMain (argc=argc@entry=3, argv=argv@entry=0x5e66a551a760) at postmaster.c:1478
#38 0x00005e66a2d446d4 in main (argc=3, argv=0x5e66a551a760) at main.c:202

How can we reproduce the bug?

CREATE TABLE metrics (
    id int8 PRIMARY KEY,
    ts timestamptz,
    device_id integer,
    val float
);
SELECT create_hypertable('metrics', 'id', chunk_time_interval=>100);
INSERT INTO metrics VALUES (101, '2024-08-07', 1, 123);
COPY metrics FROM '/home/zilder/projects/timescaledb-osm/test/data/metrics_ts_1.csv' delimiter ',' header;
SELECT tier_chunk('_timescaledb_internal._hyper_2_37_chunk');
CALL _osm_internal.move_chunk_to_osm (0, 37, 'metrics_ts_1.parquet', 1, 1, _osm_internal.fetch_metadata('metrics_ts_1.parquet'));

-- fails here
CREATE TABLE notes (
    metrics_id integer REFERENCES metrics(id) ON DELETE CASCADE,
    msg text
);

-- or
CREATE TABLE notes (
    metrics_id integer,
    msg text
);
ALTER TABLE notes ADD FOREIGN KEY (metrics_id) REFERENCES metrics (id) ON DELETE CASCADE;

zilder avatar Aug 07 '24 14:08 zilder