-
Notifications
You must be signed in to change notification settings - Fork 900
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
[Bug]: error when joining two hypertables with aggregate query with CURSOR #4774
Comments
All of the queries you posted work for me on main-branch. Are you sure you tested latest version?
|
Yes (I'm on a different time zone, but the commit time and hash are the same):
I just tried it again, and still get the same error with repros above. I also rebuilt the Docker image and went again, same problem. I'll create a Docker image that will surely allow you to replicate it. Also, are you sure you ran the exact script I pasted? I noticed that the error doesn't occur of the tables are empty, hence why I included a couple of |
OK, so I have a repro available as a Docker image now!
At this point you'll have a Postgres server running with TSDB 2.9.0-dev (commit DO LANGUAGE plpgsql $tran$
DECLARE _ret text;
BEGIN
CREATE TABLE table_a (
id uuid NOT NULL,
timestamp timestamp with time zone NOT NULL,
message_type integer NOT NULL
);
CREATE TABLE table_b (
id uuid NOT NULL,
timestamp timestamp with time zone NOT NULL,
is_authorized boolean NOT NULL
);
SELECT * INTO _ret FROM create_hypertable('table_a', 'timestamp', create_default_indexes => true, chunk_time_interval => interval '1 week');
RAISE NOTICE 'Table A Hypertable created %', _ret;
SELECT * INTO _ret FROM create_hypertable('table_b', 'timestamp', create_default_indexes => true, chunk_time_interval => interval '1 week');
RAISE NOTICE 'Table B Hypertable created %', _ret;
INSERT INTO table_a (id, timestamp, message_type) VALUES ('d0295c43-752c-42e7-b2c3-f03ad2e9f4f3', current_timestamp, 1);
INSERT INTO table_b (id, timestamp, is_authorized) VALUES ('d0295c43-752c-42e7-b2c3-f03ad2e9f4f3', current_timestamp, false);
END;
$tran$; Finally, run the query, which will fail: SELECT
time_bucket_gapfill('1 hour', a.timestamp) AS bucket,
COALESCE(COUNT(*), 0) AS ct
FROM
table_a AS a
LEFT JOIN
table_b AS b ON
b.id = a.id AND
b.timestamp = a.timestamp
WHERE
a.timestamp > NOW() - INTERVAL '1 week' AND a.timestamp < NOW()
GROUP BY
bucket
ORDER BY
1 ASC |
I still cannot reproduce:
|
How are you running the queries? table_a and table_b only have 1 row each is your data different? |
The only data in these tables is this, simply because the error does not appear when the tables are empty: INSERT INTO table_a (id, timestamp, message_type) VALUES ('d0295c43-752c-42e7-b2c3-f03ad2e9f4f3', current_timestamp, 1);
INSERT INTO table_b (id, timestamp, is_authorized) VALUES ('d0295c43-752c-42e7-b2c3-f03ad2e9f4f3', current_timestamp, false); But, given this is a Docker image, you should really be seeing the same results as me - which got me thinking; the only thing that can differ will be how we query. I was using Azure Data Studio, and I guess you were using psql. So... I tried it wih psql, and it works just fine?! 😕 I checked the Postgres logs, and there is one difference - Azure Data Studio silently opens a cursor prior to executing the query, e.g. DECLARE "e402c1c5-e6ec-436c-a9e8-bebf6df7ac4c" CURSOR WITH HOLD FOR SELECT
time_bucket_gapfill('1 hour', a.timestamp) AS bucket,
COALESCE(COUNT(*), 0) AS ct
FROM
table_a AS a
LEFT JOIN
table_b AS b ON
b.id = a.id AND
b.timestamp = a.timestamp
WHERE
a.timestamp > NOW() - INTERVAL '1 week' AND a.timestamp < NOW()
GROUP BY
bucket
ORDER BY
1 ASC So, it seems #4418 is actually resolved afterall! How do you want to proceed from here? Should I close this issue, or do you folks want to fix the problem with cursors? |
I've adjusted the title, it's still a bug but severity is lower since it only occurs with cursors. |
@svenklemm sorry, me again! I've been doing some more testing, and this bug has reared it's head again - without cursors. To reproduce, create the same 2 hypertables as before: DO LANGUAGE plpgsql $tran$
DECLARE _ret text;
BEGIN
CREATE TABLE table_a (
id uuid NOT NULL,
timestamp timestamp with time zone NOT NULL,
message_type integer NOT NULL
);
CREATE TABLE table_b (
id uuid NOT NULL,
timestamp timestamp with time zone NOT NULL,
is_authorized boolean NOT NULL
);
SELECT * INTO _ret FROM create_hypertable('table_a', 'timestamp', create_default_indexes => true, chunk_time_interval => interval '1 week');
RAISE NOTICE 'Table A Hypertable created %', _ret;
SELECT * INTO _ret FROM create_hypertable('table_b', 'timestamp', create_default_indexes => true, chunk_time_interval => interval '1 week');
RAISE NOTICE 'Table B Hypertable created %', _ret;
INSERT INTO table_a (id, timestamp, message_type) VALUES ('d0295c43-752c-42e7-b2c3-f03ad2e9f4f3', current_timestamp, 1);
INSERT INTO table_b (id, timestamp, is_authorized) VALUES ('d0295c43-752c-42e7-b2c3-f03ad2e9f4f3', current_timestamp, false);
END;
$tran$; Then execute: SELECT
time_bucket_gapfill('1 day', a.timestamp) AS bucket,
COALESCE(COUNT(*), 0) AS ct
FROM
table_a2 AS a
LEFT JOIN
table_b2 AS b ON
b.id = a.id AND
b.timestamp = a.timestamp
WHERE
a.timestamp > NOW() - INTERVAL '1 week' AND a.timestamp < NOW() - INTERVAL '1 day'
GROUP BY
bucket
ORDER BY
1 ASC It will fail with: The problem seems to arise when using Changing the join to an |
Doesnt fail for me:
|
@svenklemm are you running the same build as before, or a later commit? (I tested with commit 1d4b9d6) |
I believe I have also run into this bug, running the latest build of the TimescaleDB Docker Container on a Linux x64 host (tag I have three tables, created as follows: CREATE TABLE sensors (
sensor_id SERIAL PRIMARY KEY,
sensor_name VARCHAR(64) UNIQUE NOT NULL
);
CREATE TABLE sensor_data (
time TIMESTAMPTZ NOT NULL,
sensor_id INTEGER REFERENCES sensors,
value real
);
CREATE TABLE sensor_override (
time TIMESTAMPTZ NOT NULL,
sensor_id INTEGER REFERENCES sensors,
value real
);
SELECT create_hypertable('sensor_data', 'time');
SELECT create_hypertable('sensor_override', 'time'); I need the ability in my application to override a sensor for a period of time (e.g. if the user knows that a faulty sensor has been giving incorrect data). I have data points for my sensors every 5 seconds. I have a query that basically says "give me all data for a given sensor in a given time period where there's no override" (i.e. the resulting graph will be continuous unless an override exists, in which case there will be gaps in the graph). The query for this is as follows: SELECT
time_bucket_gapfill('5s', s.time, '2022-11-20T00:00:00Z', '2022-11-22T11:50:11Z') as stime,
AVG(s.value) as value
FROM
sensor_data s
WHERE NOT EXISTS (
SELECT
NULL
FROM
sensor_override ov
WHERE
ov.time = s.time AND
ov.sensor_id = 123 AND
ov.time BETWEEN '2022-11-20T00:00:00Z' AND '2022-11-22T11:50:11Z'
) AND
s.time BETWEEN '2022-11-20T00:00:00Z' AND '2022-11-22T11:50:11Z' AND
s.sensor_id = 123
GROUP BY stime
ORDER BY 1; This query works. However, if I change the end time to 11:50:10, Postgres throws an error:
Decrease the end time by a second, a minute, an hour, a day? Still errors. Bring forward the start time? Error. Make the query range bigger (i.e. 3 days)? Absolutely fine For some reason, any query that is less than about 215,411 seconds (or 43083 points at 1 point every 5 sec) fails. |
Still works fine for me on PG 14 and TimescaleDB 2.9.1. Maybe it's dependent on the data? I also tried eight different query plans: all chunks excluded for both tables, one of the tables, or none of the tables, in runtime or in planning time. Still no error. We need some way to reproduce this. |
I seem to have the same bug in Timescale Cloud. When I increase the query time range, the error disappears. |
@noudreturn Would you please give the complete set of steps for reproducing. |
Dear Author, This issue has been automatically marked as stale due to lack of activity. With only the issue description that is currently provided, we do not have enough information to take action. If you have or find the answers we would need, please reach out. Otherwise, this issue will be closed in 30 days. Thank you! |
I'm getting this error once upgrading to PostgreSQL 16, with TimescaleDB 2.13: create table dummy(
measured timestamptz not null,
device_id int not null,
value float not null
);
select public.create_hypertable('dummy', 'measured');
insert into dummy
select
now() - random() * interval '3 months',
(random()*1000)::int,
random()
from
generate_series(1,1000);
SELECT
1
FROM
(VALUES (1)) AS _(device_id)
LEFT JOIN (
SELECT
device_id,
last(value, measured) AS last_value
FROM
dummy
GROUP BY
device_id
) AS biggest USING (device_id); PostgreSQL 15, TimescaleDB 2.13.0: ?column?
----------
1
(1 row) PostgreSQL 16, TimescaleDB 2.13.0:
|
Issue has been fixed by #6377 Thanks for reporting this! |
I seem to be able to still reproduce this with the latest CREATE TABLE u (id uuid primary key);
INSERT INTO u (id) values ('ad46125f-7e74-4f28-97c7-f8686cd2d012');
CREATE TABLE p (
duration interval,
start_time timestamptz not null,
user_id uuid references u(id)
);
SELECT create_hypertable('p', 'start_time');
INSERT INTO p (duration, start_time, user_id) values ('02:00:00', '2023-12-14 09:01:03.91968+00', 'ad46125f-7e74-4f28-97c7-f8686cd2d012');
SELECT u.id
FROM users u
LEFT JOIN (
SELECT FLOOR(EXTRACT (EPOCH FROM SUM(ap.duration)))::INT8 AS "total_login_seconds"
FROM p ap
WHERE ap.user_id = 'ad46125f-7e74-4f28-97c7-f8686cd2d012'
GROUP BY ap.user_id
) ap ON TRUE
WHERE u.id = 'ad46125f-7e74-4f28-97c7-f8686cd2d012'
; |
Hm, I just tried it and it works fine. Can you double-check that you pulled the latest Docker image? Thanks, |
Strange, I've never pulled that image before so not sure what happened, but it does indeed seem to work after doing a |
Are there any estimates for when a 2.13.1 release might be cut? This is the last thing currently blocking us from being able to upgrade to PostgreSQL 16. |
Hello @kyrias, We don't have an exact release date yet. But we target the next release at the end of January or early February. |
I think this is a fairly important fix that should be backported and released in a patch release. After all, PostgreSQL 16 support was advertised and if bugs crop up, we shouldn't have to wait until the next minor release. Please consider this. |
Hi @svenstaro, Thanks for bringing this up. I discussed this internally. We will build a patch release (2.13.1) at the beginning of January. |
Hello! A have error with query: select time_bucket('10 minutes', event_at),
COUNT(*) as value,
host
from requests
WHERE event_at > now() - interval '10 minutes'
AND host in ('VM-FAST-WEB')
GROUP BY 1, host
ORDER BY 1; result(error): if exclude version:(docker) Help please) |
on pg 15 no problem |
Hey! I've tried replicating your problem on the specified docker image with pg16.2 and TS 2.14.2 but with no luck. Could you double-confirm that you are in fact running 2.14.2 version of Timescale where you can replicate this issue? This issue has been fixed before for PG16 and I'm unable to trigger it either on 2.14.2 version or the main development branch. For reference, here are the steps I tried to reproduce, based on your report:
As you can see, I'm unable to trigger it this way. If you have a better/more consistent way of reproducing this, let me know. Thanks! |
What type of bug is this?
Unexpected error
What subsystems and features are affected?
Query executor
What happened?
When running aggregate queries that join two hypertables, an error occurs:
ORDER/GROUP BY expression not found in targetlist
TimescaleDB version affected
2.8.0 and 2.9.0-dev
PostgreSQL version used
14.5
What operating system did you use?
Debian Bullseye
What installation method did you use?
Source
What platform did you run on?
On prem/Self-hosted
Relevant log output and stack trace
ORDER/GROUP BY expression not found in targetlist
How can we reproduce the bug?
Before starting, ensure you are using TSDB
2.9.0-dev
(I built it from themain
branch). This bug was somewhat apparent in 2.8.0 as well, but overlapped a lot with #4418.Repro 1
First, create two hypertables:
Now execute an aggregate query that uses both tables:
Whether an
INNER
orLEFT
join, this will result in the error:Repro 2
If we modify things slightly, using our own indices that include both the
timestamp
andid
columns, then the issue only appears withINNER JOIN
- not withLEFT JOIN
.First, create two hypertables:
Now execute an aggregate query that uses both tables:
This will result in the error:
Note that if you include any of the joined table columns in the
WHERE
clause, it fails withLEFT JOIN
too, e.g.:The text was updated successfully, but these errors were encountered: