Обсуждение: BUG #18091: Unexpected Result by enable_material
The following bug has been logged on the website:
Bug reference: 18091
Logged by: Jinsheng Ba
Email address: bajinsheng@u.nus.edu
PostgreSQL version: 15.4
Operating system: Ubuntu
Description:
Hello,
The same query returns different results when enable_material is changed:
CREATE TABLE t2(c1 DECIMAL, c2 serial);
CREATE TABLE t4(c0 TEXT);
INSERT INTO t2(c2) VALUES(1), (2);
INSERT INTO t2(c1) VALUES(0.1);
INSERT INTO t4(c0) VALUES('a'), ('b'), ('c');
SET enable_material=true;
SELECT DISTINCT ON (t2.c1) t2.c2 FROM t2, t4; -- {1}, {1}
SET enable_material=false;
SELECT DISTINCT ON (t2.c1) t2.c2 FROM t2, t4; -- {1}, {2}
Reproduction:
docker run -p 5432:5432 -e POSTGRES_PASSWORD=root -it postgres:latest
psql "postgresql://postgres:root@localhost:5432"
On Thu, 7 Sept 2023 at 09:10, PG Bug reporting form
<noreply@postgresql.org> wrote:
> The same query returns different results when enable_material is changed:
I'm not too sure your query is guaranteed to return the same result
allways, let me see..
-- Zapping t4....
> CREATE TABLE t2(c1 DECIMAL, c2 serial);
> INSERT INTO t2(c2) VALUES(1), (2);
> INSERT INTO t2(c1) VALUES(0.1);
This seems to leave t2 as (null,1),(null,2),(0.1,1)
--
...
> SELECT DISTINCT ON (t2.c1) t2.c2 FROM t2, t4; -- {1}, {1}
> SELECT DISTINCT ON (t2.c1) t2.c2 FROM t2, t4; -- {1}, {2}
And this seems to ask for one row from
(null,1,{a,b,c})+(null,2,{a,b,c}), another from (0.1,1,{a,b,c})
So your result would be (1|2) + 1 ( in any order ), as the first set
can be scanned/selected in any order ( and the results returned in any
order )
Normally postgres would not bother on generating different outputs if
nothing is changed, but I think any response in {1,1},{1,2},{2,1} is
correct. Enable_material is probably just changing some access order
details.
You may be able to get better repeatable results using order by judiciously.
Francisco Olarte.
On Thu, Sep 7, 2023 at 3:10 PM PG Bug reporting form <noreply@postgresql.org> wrote:
Hello,
The same query returns different results when enable_material is changed:
CREATE TABLE t2(c1 DECIMAL, c2 serial);
CREATE TABLE t4(c0 TEXT);
INSERT INTO t2(c2) VALUES(1), (2);
INSERT INTO t2(c1) VALUES(0.1);
INSERT INTO t4(c0) VALUES('a'), ('b'), ('c');
SET enable_material=true;
SELECT DISTINCT ON (t2.c1) t2.c2 FROM t2, t4; -- {1}, {1}
SET enable_material=false;
SELECT DISTINCT ON (t2.c1) t2.c2 FROM t2, t4; -- {1}, {2}
With DISTINCT ON, only the first row of each set is kept, which is
unpredictable. Usually you're supposed to use ORDER BY to get the
desired row.
Thanks
Richard
unpredictable. Usually you're supposed to use ORDER BY to get the
desired row.
Thanks
Richard