Joins between foreign tables - Mailing list pgsql-general

From Jason Dusek
Subject Joins between foreign tables
Date
Msg-id CAO3NbwO4jDnXc=ihQ1dbm=wRwJfAYoXdVGJSAaJoXVYiohfdqQ@mail.gmail.com
Whole thread Raw
List pgsql-general

Dear Postgres,

Consider a query like:

SELECT table_on_server1.email FROM table_on_server1 JOIN table_on_server2   ON (table_on_server1.id = table_on_server2.user_id)WHERE table_on_server2.created_at >= date_trunc('day', now())

One could imagine the plan being something like:

  1. Find all the user_ids that result from SELECT user_id FROM table_on_server2 WHERE table_on_server2.created_at >= date_trunc('day', now())

  2. Pass these IDs to a query executed on server1, pushing them down in a WHERE clause.

However, the query actually doesn’t perform at all like that. Which is to say, if one executes the query in (1) from the console and copy pastes the resulting IDs into an IN clause in a second query against table_on_server1, the query returns quickly (milliseconds); whereas if one runs the naive query at the beginning of this email, it does not return for seconds.

What are things we can do to get good performance for queries like this? We’ve tied moving the search for IDs into a temp table and a CTE; it doesn’t seem to make any difference. (Which suggests that joins between one local and one remote table won’t perform well in general, either.)

Best Regards,

Jason Dusek

pgsql-general by date:

Previous
From: Andres Freund
Date:
Subject: Re: [HACKERS] 9.4.1 -> 9.4.2 problem: could not access status of transaction 1
Next
From: Adrian Klaver
Date:
Subject: Re: Momentary Delay