BUG #12963: WHERE constraints on (INNER) JOIN columns are not propagated to both tables

From: gcp(at)sjeng(dot)org
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #12963: WHERE constraints on (INNER) JOIN columns are not propagated to both tables
Date: 2015-04-04 23:21:13
Message-ID: 20150404232113.2569.93814@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 12963
Logged by: Gian-Carlo Pascutto
Email address: gcp(at)sjeng(dot)org
PostgreSQL version: 9.4.1
Operating system: Linux
Description:

The relevant parts of the schema are as follows:

CREATE TABLE matches
(
seq integer NOT NULL,
start_time integer NOT NULL,
<blah>
CONSTRAINT matches_pkey PRIMARY KEY (seq)
)

CREATE TABLE matches_players
(
seq integer NOT NULL,
player_num smallint NOT NULL,
account_id integer,
<blah>
CONSTRAINT matches_players_pkey PRIMARY KEY (seq, player_num),
CONSTRAINT matches_players_seq_fkey FOREIGN KEY (seq)
REFERENCES matches (seq) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)

The following is the EXPLAIN ANALYZE output of 2 queries.
https://dl.dropboxusercontent.com/u/32496746/postgresql.txt

Because this is an (INNER) JOIN, a WHERE constraint that is given to one
table, on the joined column, applies to the other table as well.
PostgreSQL's query planner does not understand this and consequently
produces a query plan that is a factor 35 slower than when one restates the
constraint.

That is, when given:

join matches on matches.seq = matches_players.seq
where matches.seq > 1151575404 and matches.seq < 1155066769

there should be no need to add:

and matches_players.seq > 1151575404 and matches_players.seq <
1155066769;

because this is implied by the JOIN ON.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Pavel Stehule 2015-04-05 05:06:20 to high memory expensive xpath_table from xml2 contrib extension
Previous Message Tom Lane 2015-04-04 17:34:55 Re: src/port/getopt_long.c lossy with arguments having no option characters