diff --git a/expected/pg_rational_test.out b/expected/pg_rational_test.out index 73ba945..2c51079 100644 --- a/expected/pg_rational_test.out +++ b/expected/pg_rational_test.out @@ -688,3 +688,8 @@ select rational_intermediate(NULL, '15/16'); 1/2 (1 row) +-- test timeout +set statement_timeout = '3s'; +select rational_intermediate('2147483646/2147483647'::rational, '1/1'::rational); +ERROR: canceling statement due to statement timeout +set statement_timeout to default; diff --git a/pg_rational.c b/pg_rational.c index 4057b5a..c4d8f0d 100644 --- a/pg_rational.c +++ b/pg_rational.c @@ -2,6 +2,7 @@ #include "fmgr.h" #include "access/hash.h" #include "libpq/pqformat.h" /* send/recv functions */ +#include "miscadmin.h" #if PG_VERSION_NUM >= 110000 #include "common/int.h" /* portable overflow detection */ @@ -379,6 +380,9 @@ rational_intermediate(PG_FUNCTION_ARGS) while (true) { + /* Allow query cancel (e.g. Ctrl+C) and statement_timeout . */ + CHECK_FOR_INTERRUPTS(); + mediant(&lo, &hi, med); if (cmp(med, &x) < 1) memcpy(&lo, med, sizeof(Rational)); diff --git a/sql/pg_rational_test.sql b/sql/pg_rational_test.sql index c6359db..b45d3d7 100644 --- a/sql/pg_rational_test.sql +++ b/sql/pg_rational_test.sql @@ -269,3 +269,8 @@ select rational_intermediate('1/3', NULL); select rational_intermediate('3/2', NULL); -- though not the other direction select rational_intermediate(NULL, '15/16'); + +-- test timeout +set statement_timeout = '3s'; +select rational_intermediate('2147483646/2147483647'::rational, '1/1'::rational); +set statement_timeout to default; \ No newline at end of file