timescale / timescaledb

An open-source time-series SQL database optimized for fast ingest and complex queries. Packaged as a PostgreSQL extension.
https://www.timescale.com/
Other
17.51k stars 879 forks source link

[Bug]: Server crashes when an untyped value passed as a parameter for add_policies() #5920

Open alexanderlaw opened 1 year ago

alexanderlaw commented 1 year ago

What type of bug is this?

Crash

What subsystems and features are affected?

Continuous aggregate

What happened?

While playing with the add_policies() function to compose a simple reproducer for https://github.com/timescale/timescaledb/issues/5907, I discovered that this function can trigger a server crash (by reaching pg_unreachable()) if an "unknown" value is passed to the function in parameters drop_after, compress_after, refresh_start_offset, and refresh_end_offset.

TimescaleDB version affected

2.12.0-dev

PostgreSQL version used

15.3

What operating system did you use?

Ubuntu 22.04 x86_64

What installation method did you use?

Source

What platform did you run on?

On prem/Self-hosted

Relevant log output and stack trace

Core was generated by `postgres: law regression [local] SELECT                                       '.
Program terminated with signal SIGABRT, Aborted.

warning: Section `.reg-xstate/3871845' in core file too small.
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140179747433856) at ./nptl/pthread_kill.c:44
44      ./nptl/pthread_kill.c: No such file or directory.
(gdb) bt
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140179747433856) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=140179747433856) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=140179747433856, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007f7e236f1476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007f7e236d77f3 in __GI_abort () at ./stdlib/abort.c:79
#5  0x00007f7e1a305356 in interval_to_int64 (interval=94682946019816, type=705)
    at .../timescaledb/tsl/src/bgw_policy/continuous_aggregate_api.c:400
#6  0x00007f7e1a30ccb9 in validate_and_create_policies (all_policies=..., if_exists=false)
    at .../timescaledb/tsl/src/bgw_policy/policies_v2.c:124
#7  0x00007f7e1a30d40e in policies_add (fcinfo=0x561d17953e28)
    at .../timescaledb/tsl/src/bgw_policy/policies_v2.c:297
#8  0x00007f7e1a435b71 in ts_policies_add (fcinfo=0x561d17953e28)
    at .../timescaledb/src/cross_module_fn.c:58
#9  0x0000561d15ee2380 in ExecInterpExpr (state=0x561d17953d40, econtext=0x561d17953a40, 
    isnull=0x7fffd962b37f) at execExprInterp.c:727
#10 0x0000561d15ee4714 in ExecInterpExprStillValid (state=0x561d17953d40, econtext=0x561d17953a40, 
    isNull=0x7fffd962b37f) at execExprInterp.c:1826
#11 0x0000561d15f42715 in ExecEvalExprSwitchContext (state=0x561d17953d40, econtext=0x561d17953a40, 
    isNull=0x7fffd962b37f) at ../../../src/include/executor/executor.h:344
#12 0x0000561d15f4278d in ExecProject (projInfo=0x561d17953d38)
    at ../../../src/include/executor/executor.h:378
#13 0x0000561d15f429d7 in ExecResult (pstate=0x561d17953928) at nodeResult.c:136
#14 0x0000561d15efb4f6 in ExecProcNodeFirst (node=0x561d17953928) at execProcnode.c:464
#15 0x0000561d15eee5f2 in ExecProcNode (node=0x561d17953928) at ../../../src/include/executor/executor.h:262
#16 0x0000561d15ef14f9 in ExecutePlan (estate=0x561d179536f0, planstate=0x561d17953928, 
    use_parallel_mode=false, operation=CMD_SELECT, sendTuples=true, numberTuples=0, 
    direction=ForwardScanDirection, dest=0x561d1795c660, execute_once=true) at execMain.c:1636
#17 0x0000561d15eeed29 in standard_ExecutorRun (queryDesc=0x561d1768d5b0, direction=ForwardScanDirection, 
    count=0, execute_once=true) at execMain.c:363
#18 0x0000561d15eeeb14 in ExecutorRun (queryDesc=0x561d1768d5b0, direction=ForwardScanDirection, count=0, 
    execute_once=true) at execMain.c:307
#19 0x0000561d1616c23c in PortalRunSelect (portal=0x561d176445f0, forward=true, count=0, 
    dest=0x561d1795c660) at pquery.c:924
#20 0x0000561d1616be5a in PortalRun (portal=0x561d176445f0, count=9223372036854775807, isTopLevel=true, 
    run_once=true, dest=0x561d1795c660, altdest=0x561d1795c660, qc=0x7fffd962b780) at pquery.c:768
#21 0x0000561d16164cd4 in exec_simple_query (
--Type <RET> for more, q to quit, c to continue without paging--
    query_string=0x561d175d15b0 "SELECT timescaledb_experimental.add_policies('cagg', drop_after => '20 days');") at postgres.c:1250
#22 0x0000561d16169c05 in PostgresMain (dbname=0x561d17600468 "regression", username=0x561d17600448 "law")
    at postgres.c:4598
#23 0x0000561d1608ef8d in BackendRun (port=0x561d175f7b80) at postmaster.c:4511
#24 0x0000561d1608e814 in BackendStartup (port=0x561d175f7b80) at postmaster.c:4239
#25 0x0000561d1608a6e7 in ServerLoop () at postmaster.c:1806
#26 0x0000561d16089e44 in PostmasterMain (argc=3, argv=0x561d17539a80) at postmaster.c:1478
#27 0x0000561d15f7dad5 in main (argc=3, argv=0x561d17539a80) at main.c:202

How can we reproduce the bug?

CREATE EXTENSION timescaledb;

CREATE TABLE t(d date NOT NULL, i int);
SELECT create_hypertable('t', 'd');

CREATE MATERIALIZED VIEW cagg(d, sumi) WITH (timescaledb.continuous)
AS SELECT time_bucket('1 day', d), sum(i) FROM t
   GROUP BY time_bucket('1 day', d);

SELECT timescaledb_experimental.add_policies('cagg', drop_after => '20 days');
konskov commented 1 year ago

Thank you for reporting this @alexanderlaw, I was able to reproduce it with the steps you shared.