pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #12137: ORDER BY with expresion changes the output if added
5+ messages / 4 participants
[nested] [flat]
* BUG #12137: ORDER BY with expresion changes the output if added
@ 2014-12-04 06:43 dima@koulikoff.ru
2014-12-04 16:01 ` Re: BUG #12137: ORDER BY with expresion changes the output if added David G Johnston <david.g.johnston@gmail.com>
2014-12-04 16:22 ` Re: BUG #12137: ORDER BY with expresion changes the output if added Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 5+ messages in thread
From: dima@koulikoff.ru @ 2014-12-04 06:43 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 12137
Logged by: Dmitri Koulikov
Email address: dima@koulikoff.ru
PostgreSQL version: 9.3.5
Operating system: Linux 3.10.17 #1 SMP x86_64 Intel@ 2.20GHz GNU/Lin
Description:
CREATE TABLE geonames
(
geoname_id integer,
name character varying(200),
asciiname character varying(200),
latitude numeric(19,16),
longitude numeric(19,16),
feature character varying(11),
country_code character varying(2),
admin1 character varying(20),
admin2 character varying(80),
admin3 character varying(20),
admin4 character varying(20),
population bigint NOT NULL DEFAULT 0,
elevation integer,
dem integer,
timezone character varying(40),
modified_on date,
created_at timestamp without time zone,
updated_at timestamp without time zone
)
WITH (
OIDS=FALSE
);
CREATE TABLE geoname_names
(
name_id integer,
geoname_id integer,
language character varying(7),
name character varying(200),
preffered boolean NOT NULL DEFAULT false,
short boolean NOT NULL DEFAULT false,
colloquial boolean NOT NULL DEFAULT false,
historic boolean NOT NULL DEFAULT false,
created_at timestamp without time zone,
updated_at timestamp without time zone
)
WITH (
OIDS=FALSE
);
The request
SELECT geonames.geoname_id, geonames.asciiname, geonames.feature,
geonames.country_code, geonames.admin1, geonames.admin2,
CASE WHEN geoname_names.name IS NOT NULL
THEN geoname_names.name ELSE geonames.name END AS name
FROM "geonames" LEFT OUTER JOIN geoname_names
ON geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.name_id IN
(SELECT name_id FROM geoname_names
WHERE geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.language = 'ru'
ORDER BY geoname_names.short DESC, geoname_names.preffered DESC,
geoname_names.colloquial, geoname_names.historic
LIMIT 1
) WHERE country_code = 'RU' and admin1 = '17'
returns Russian names in :name field from the geoname_names table
But if I add a line
ORDER BY CASE WHEN geoname_names.name IS NOT NULL THEN geoname_names.name
ELSE geonames.name END
at the end of the request all the values for name are taken from the
geonames table.
Wrapping into CTE or subquery does not help. The only solution to get sorted
names from geoname_names I found is
ORDER BY geoname_names.name NULLS LAST, geonames.name
but it is not what is needed since for German language, for example, the
sorting order will be wrong.
Is it a bug or feature? How to get the desired result?
Thank you in advance.
With best regards,
Dmitri
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #12137: ORDER BY with expresion changes the output if added
2014-12-04 06:43 BUG #12137: ORDER BY with expresion changes the output if added dima@koulikoff.ru
@ 2014-12-04 16:01 ` David G Johnston <david.g.johnston@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: David G Johnston @ 2014-12-04 16:01 UTC (permalink / raw)
To: pgsql-bugs
dima-2 wrote
> FROM "geonames" LEFT OUTER JOIN geoname_names
> ON geoname_names.geoname_id = geonames.geoname_id
> AND geoname_names.name_id IN
> (SELECT name_id FROM geoname_names
> WHERE geoname_names.geoname_id = geonames.geoname_id
> AND geoname_names.language = 'ru'
> ORDER BY geoname_names.short DESC, geoname_names.preffered DESC,
> geoname_names.colloquial, geoname_names.historic
> LIMIT 1
1. This could use aliases since you have repeating table names
2. I would write the above portion as:
geonames LEFT JOIN (
SELDCT DISTINCT ON name_id, geoname_id FROM ... WHERE ... ORDER BY ...
) gnn USING (geoname_id)
3. I would supply a query of the form:
WITH geonames AS ( values (...),(...) )
, geoname_names AS ( values (...) )
<rest of the query here>
And then show what you want the table output of the query to be.
David J.
--
View this message in context: http://postgresql.nabble.com/BUG-12137-ORDER-BY-with-expresion-changes-the-output-if-added-tp5829220...
Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #12137: ORDER BY with expresion changes the output if added
2014-12-04 06:43 BUG #12137: ORDER BY with expresion changes the output if added dima@koulikoff.ru
@ 2014-12-04 16:22 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-12-09 11:08 ` Re: BUG #12137: ORDER BY with expresion changes the output if added Dmitri Koulikov <koulikoff@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Tom Lane @ 2014-12-04 16:22 UTC (permalink / raw)
To: dima@koulikoff.ru; +Cc: pgsql-bugs
dima@koulikoff.ru writes:
> The request ...
> returns Russian names in :name field from the geoname_names table
> But if I add a line
> ORDER BY CASE WHEN geoname_names.name IS NOT NULL THEN geoname_names.name
> ELSE geonames.name END
> at the end of the request all the values for name are taken from the
> geonames table.
I can't reproduce any such misbehavior with the given information
and some made-up data. Can you provide a self-contained test case?
Or at least EXPLAIN (ANALYZE, VERBOSE) output for both queries?
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #12137: ORDER BY with expresion changes the output if added
2014-12-04 06:43 BUG #12137: ORDER BY with expresion changes the output if added dima@koulikoff.ru
2014-12-04 16:22 ` Re: BUG #12137: ORDER BY with expresion changes the output if added Tom Lane <tgl@sss.pgh.pa.us>
@ 2014-12-09 11:08 ` Dmitri Koulikov <koulikoff@gmail.com>
2014-12-09 18:00 ` Re: BUG #12137: ORDER BY with expresion changes the output if added Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Dmitri Koulikov @ 2014-12-09 11:08 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
On Thursday, December 04, 2014 11:22:21 Tom Lane wrote:
> dima@koulikoff.ru writes:
> > The request ...
> > returns Russian names in :name field from the geoname_names table
> > But if I add a line
> > ORDER BY CASE WHEN geoname_names.name IS NOT NULL THEN geoname_names.name
> > ELSE geonames.name END
> > at the end of the request all the values for name are taken from the
> > geonames table.
>
> I can't reproduce any such misbehavior with the given information
> and some made-up data. Can you provide a self-contained test case?
> Or at least EXPLAIN (ANALYZE, VERBOSE) output for both queries?
>
> regards, tom lane
Hello. I've prepared a sample. It turned out that the problem lies in other
place.
In the attachment there is output from explain and a sample
The problem is with ORDER clause I get only one record while without it there
are two.
As I understand this clause should not affect the size of the output.
Tank you in advance. And sorry for the delayed response.
--
With best regards,
Dmitri Koulikoff
mailto:koulikoff@gmail.com
skype: dima.koulikoff
phone: +7-495-5052185
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
pgsql_explain (text/plain, 8.4K) download
devel=> EXPLAIN (ANALYZE, VERBOSE) SELECT geonames.geoname_id, geonames.asciiname, geonames.feature,
geonames.country_code, geonames.admin1, geonames.admin2,
CASE WHEN geoname_names.name IS NOT NULL
THEN geoname_names.name ELSE geonames.name END AS name
FROM "geonames" LEFT OUTER JOIN geoname_names
ON geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.name_id IN
(SELECT name_id FROM geoname_names
WHERE geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.language = 'ru'
ORDER BY geoname_names.short DESC, geoname_names.preffered DESC,
geoname_names.colloquial, geoname_names.historic
LIMIT 1
) WHERE country_code = 'RU' and admin1 = '17'
;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------
Nested Loop Left Join (cost=0.42..24679.25 rows=86 width=62) (actual time=80.404..102.200 rows=191 loops=1)
Output: geonames.geoname_id, geonames.asciiname, geonames.feature, geonames.country_code, geonames.admin1, geonames.admin2, CASE WHEN (geoname_names.name IS NOT NULL) THEN geoname_names.name ELSE geonames.name END
-> Seq Scan on public.geonames (cost=0.00..23228.67 rows=86 width=44) (actual time=80.341..100.420 rows=191 loops=1)
Output: geonames.geoname_id, geonames.name, geonames.asciiname, geonames.latitude, geonames.longitude, geonames.feature, geonames.country_code, geonames.admin1, geonames.admin2, geonames.admin3, geonames.admin4, geonames.population, geonames.elevation, geonames.dem, geonames.timezone, geonames.modified_on, geonames.created_at, geonames.updated_at
Filter: (((geonames.country_code)::text = 'RU'::text) AND ((geonames.admin1)::text = '17'::text))
Rows Removed by Filter: 673005
-> Index Scan using index_geoname_names_on_geoname_id_language_and_preferences on public.geoname_names (cost=0.42..16.86 rows=1 width=26) (actual time=0.009..0.009 rows=1 loops=191)
Output: geoname_names.name_id, geoname_names.geoname_id, geoname_names.language, geoname_names.name, geoname_names.preffered, geoname_names.short, geoname_names.colloquial, geoname_names.historic, geoname_names.created_at, geoname_names.updated_at
Index Cond: (geoname_names.geoname_id = geonames.geoname_id)
Filter: (SubPlan 1)
Rows Removed by Filter: 1
SubPlan 1
-> Limit (cost=8.46..8.46 rows=1 width=8) (actual time=0.004..0.004 rows=1 loops=305)
Output: geoname_names_1.name_id, geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
-> Sort (cost=8.46..8.46 rows=1 width=8) (actual time=0.004..0.004 rows=1 loops=305)
Output: geoname_names_1.name_id, geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
Sort Key: geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
Sort Method: quicksort Memory: 25kB
-> Index Scan using index_geoname_names_on_geoname_id_language_and_preferences on public.geoname_names geoname_names_1 (cost=0.42..8.45 rows=1 width=8) (actual time=0.002..0.002 rows=1 loops=305)
Output: geoname_names_1.name_id, geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
Index Cond: ((geoname_names_1.geoname_id = geonames.geoname_id) AND ((geoname_names_1.language)::text = 'ru'::text))
Total runtime: 102.260 ms
(22 rows)
devel=> EXPLAIN (ANALYZE, VERBOSE) SELECT geonames.geoname_id, geonames.asciiname, geonames.feature,
geonames.country_code, geonames.admin1, geonames.admin2,
CASE WHEN geoname_names.name IS NOT NULL
THEN geoname_names.name ELSE geonames.name END AS name
FROM "geonames" LEFT OUTER JOIN geoname_names
ON geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.name_id IN
(SELECT name_id FROM geoname_names
WHERE geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.language = 'ru'
ORDER BY geoname_names.short DESC, geoname_names.preffered DESC,
geoname_names.colloquial, geoname_names.historic
LIMIT 1
) WHERE country_code = 'RU' and admin1 = '17'
ORDER BY CASE WHEN geoname_names.name IS NOT NULL THEN geoname_names.name
cs_devel-> ELSE geonames.name END;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------
Sort (cost=24682.02..24682.23 rows=86 width=62) (actual time=98.947..98.955 rows=191 loops=1)
Output: geonames.geoname_id, geonames.asciiname, geonames.feature, geonames.country_code, geonames.admin1, geonames.admin2, (CASE WHEN (geoname_names.name IS NOT NULL) THEN geoname_names.name ELSE geonames.name END)
Sort Key: (CASE WHEN (geoname_names.name IS NOT NULL) THEN geoname_names.name ELSE geonames.name END)
Sort Method: quicksort Memory: 45kB
-> Nested Loop Left Join (cost=0.42..24679.25 rows=86 width=62) (actual time=76.686..98.607 rows=191 loops=1)
Output: geonames.geoname_id, geonames.asciiname, geonames.feature, geonames.country_code, geonames.admin1, geonames.admin2, CASE WHEN (geoname_names.name IS NOT NULL
) THEN geoname_names.name ELSE geonames.name END
-> Seq Scan on public.geonames (cost=0.00..23228.67 rows=86 width=44) (actual time=76.621..96.473 rows=191 loops=1)
Output: geonames.geoname_id, geonames.name, geonames.asciiname, geonames.latitude, geonames.longitude, geonames.feature, geonames.country_code, geonames.admin1, geonames.admin2, geonames.admin3, geonames.admin4, geonames.population, geonames.elevation, geonames.dem, geonames.timezone, geonames.modified_on, geonames.created_at, geonames.updated_at
Filter: (((geonames.country_code)::text = 'RU'::text) AND ((geonames.admin1)::text = '17'::text))
Rows Removed by Filter: 673005
-> Index Scan using index_geoname_names_on_geoname_id_language_and_preferences on public.geoname_names (cost=0.42..16.86 rows=1 width=26) (actual time=0.010..0.011
rows=1 loops=191)
Output: geoname_names.name_id, geoname_names.geoname_id, geoname_names.language, geoname_names.name, geoname_names.preffered, geoname_names.short, geoname_names.colloquial, geoname_names.historic, geoname_names.created_at, geoname_names.updated_at
Index Cond: (geoname_names.geoname_id = geonames.geoname_id)
Filter: (SubPlan 1)
Rows Removed by Filter: 1
SubPlan 1
-> Limit (cost=8.46..8.46 rows=1 width=8) (actual time=0.005..0.005 rows=1 loops=305)
Output: geoname_names_1.name_id, geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
-> Sort (cost=8.46..8.46 rows=1 width=8) (actual time=0.005..0.005 rows=1 loops=305)
Output: geoname_names_1.name_id, geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
Sort Key: geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
Sort Method: quicksort Memory: 25kB
-> Index Scan using index_geoname_names_on_geoname_id_language_and_preferences on public.geoname_names geoname_names_1 (cost=0.42..8.45 rows=1 width=8) (actual time=0.002..0.002 rows=1 loops=305)
Output: geoname_names_1.name_id, geoname_names_1.short, geoname_names_1.preffered, geoname_names_1.colloquial, geoname_names_1.historic
Index Cond: ((geoname_names_1.geoname_id = geonames.geoname_id) AND ((geoname_names_1.language)::text = 'ru'::text))
Total runtime: 99.000 ms
(26 rows)
pgsql_sample (text/plain, 2.9K) download
INSERT INTO "geonames" ("admin1", "admin2", "admin3", "asciiname", "country_code", "created_at", "dem", "elevation", "feature", "geoname_id", "latitude", "longitude", "modified_on", "name", "population", "timezone", "updated_at") VALUES ('admin1_07', 'admin2_05', 'admin3_03', 'Geoname_ascii21', 'CM', '2014-12-09 09:46:31.158343', 556, 170, 'AADM3', 21, -87.5215329463218, -45.1375812771716, '2014-12-03', 'Geoname021', 39451, 'Time/Zone21', '2014-12-09 09:46:31.158343') RETURNING "geoname_id"
INSERT INTO "geonames" ("admin1", "admin2", "admin3", "admin4", "asciiname", "country_code", "created_at", "dem", "elevation", "feature", "geoname_id", "latitude", "longitude", "modified_on", "name", "population", "timezone", "updated_at") VALUES ('admin1_07', 'admin2_05', 'admin3_03', 'admin4_01', 'Geoname_ascii23', 'CM', '2014-12-09 09:46:31.163858', 380, 74, 'AADM4', 23, 8.34005686792595, 17.6904713901371, '2014-09-13', 'Geoname023', 67372, 'Time/Zone23', '2014-12-09 09:46:31.163858') RETURNING "geoname_id"
INSERT INTO "geonames" ("admin1", "admin2", "admin3", "admin4", "asciiname", "country_code", "created_at", "dem", "elevation", "feature", "geoname_id", "latitude", "longitude", "modified_on", "name", "population", "timezone", "updated_at") VALUES ('admin1_07', 'admin2_05', 'admin3_03', 'admin4_02', 'Geoname_ascii24', 'CM', '2014-12-09 09:46:31.166584', 202, 104, 'AADM4', 24, 83.1516503438357, 95.2018051676951, '2014-11-06', 'Geoname024', 96826, 'Time/Zone24', '2014-12-09 09:46:31.166584') RETURNING "geoname_id"
INSERT INTO "geoname_names" ("created_at", "geoname_id", "language", "name", "name_id", "updated_at") VALUES ('2014-12-09 09:46:31.168885', 23, 'en', 'GeonameName #4', 4, '2014-12-09 09:46:31.168885') RETURNING "name_id"
SELECT geonames.geoname_id, geonames.asciiname, geonames.latitude, geonames.longitude, geonames.feature, geonames.country_code, geonames.admin1, geonames.admin2, geonames.admin3, geonames.admin4, geonames.population, geonames.elevation, geonames.dem, geonames.timezone, geonames.modified_on, geonames.created_at, geonames.updated_at,
CASE WHEN geoname_names.name IS NOT NULL
THEN geoname_names.name ELSE geonames.name END AS name
FROM "geonames" LEFT OUTER JOIN geoname_names
ON geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.name_id IN
(SELECT name_id FROM geoname_names
WHERE geoname_names.geoname_id = geonames.geoname_id
AND geoname_names.language = 'en'
ORDER BY geoname_names.short DESC, geoname_names.preffered DESC,
geoname_names.colloquial ASC, geoname_names.historic ASC
LIMIT 1
) WHERE "geonames"."country_code" = 'CM' AND "geonames"."feature" = 'AADM4' AND "geonames"."admin1" = 'admin1_07' AND "geonames"."admin2" = 'admin2_05' AND "geonames"."admin3" = 'admin3_03'
ORDER BY CASE WHEN geoname_names.name IS NOT NULL THEN geoname_names.name ELSE geonames.name END LIMIT 1
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #12137: ORDER BY with expresion changes the output if added
2014-12-04 06:43 BUG #12137: ORDER BY with expresion changes the output if added dima@koulikoff.ru
2014-12-04 16:22 ` Re: BUG #12137: ORDER BY with expresion changes the output if added Tom Lane <tgl@sss.pgh.pa.us>
2014-12-09 11:08 ` Re: BUG #12137: ORDER BY with expresion changes the output if added Dmitri Koulikov <koulikoff@gmail.com>
@ 2014-12-09 18:00 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2014-12-09 18:00 UTC (permalink / raw)
To: Dmitri Koulikov <koulikoff@gmail.com>; +Cc: pgsql-bugs
Dmitri Koulikov <koulikoff@gmail.com> writes:
> Hello. I've prepared a sample. It turned out that the problem lies in other
> place.
> In the attachment there is output from explain and a sample
This isn't a self-contained test case :-(. I guessed at some index
definitions but couldn't reproduce the plan exactly, and in any case
it's clear from your EXPLAIN output there are more than just these
three rows in your tables.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2014-12-09 18:00 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-12-04 06:43 BUG #12137: ORDER BY with expresion changes the output if added dima@koulikoff.ru
2014-12-04 16:01 ` David G Johnston <david.g.johnston@gmail.com>
2014-12-04 16:22 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-12-09 11:08 ` Dmitri Koulikov <koulikoff@gmail.com>
2014-12-09 18:00 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox