question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Data not being propagated to nested queries on where clause

See original GitHub issue

Describe the bug

The title is quite self-explanatory

column "foo.id" does not exist

🐜 This seems to be an execution error, which means that your request syntax seems okay,
    but the resulting statement cannot be executed → Probably not a pg-mem error.

*️⃣ Reconsituted failed SQL statement: SELECT (count (*) )  FROM foo   WHERE (exists ((SELECT (1)  FROM bar   WHERE (bar .foo_id = foo .id) )) )

To Reproduce

CREATE TABLE foo
(
    id    uuid NOT NULL PRIMARY KEY,
    value text NOT NULL
);

CREATE TABLE bar
(
    id uuid NOT NULL PRIMARY KEY,
    foo_id uuid NOT NULL,

    FOREIGN KEY (foo_id) REFERENCES foo(id) ON DELETE CASCADE
);

INSERT INTO foo (id, value) VALUES ('09f67c99-7c8c-40dc-8f15-13599005c657', 'foo one'), ('792f71e1-2c32-4ebe-b4c8-6cf831ce490e', 'foo two');

INSERT INTO bar (id, foo_id) VALUES ('b3381f77-8324-4778-9bd5-74d90fe14553', '792f71e1-2c32-4ebe-b4c8-6cf831ce490e');

SELECT COUNT(*) FROM foo WHERE EXISTS (
    SELECT 1 FROM bar WHERE bar.foo_id = foo.id
)

On postgres:

postgres=*# CREATE TABLE foo
postgres-*# (
postgres(*#     id    uuid NOT NULL PRIMARY KEY,
postgres(*#     value text NOT NULL
postgres(*# );
CREATE TABLE
postgres=*# 
postgres=*# CREATE TABLE bar
postgres-*# (
postgres(*#     id uuid NOT NULL PRIMARY KEY,
postgres(*#     foo_id uuid NOT NULL,
postgres(*# 
postgres(*#     FOREIGN KEY (foo_id) REFERENCES foo(id) ON DELETE CASCADE
postgres(*# );
CREATE TABLE
postgres=*# 
postgres=*# INSERT INTO foo (id, value) VALUES ('09f67c99-7c8c-40dc-8f15-13599005c657', 'foo one'), ('792f71e1-2c32-4ebe-b4c8-6cf831ce490e', 'foo two');
INSERT 0 2
postgres=*# 
postgres=*# INSERT INTO bar (id, foo_id) VALUES ('b3381f77-8324-4778-9bd5-74d90fe14553', '792f71e1-2c32-4ebe-b4c8-6cf831ce490e');
INSERT 0 1
postgres=*# 
postgres=*# SELECT COUNT(*) FROM foo WHERE EXISTS (
postgres(*#     SELECT 1 FROM bar WHERE bar.foo_id = foo.id
postgres(*# );
 count 
-------
     1
(1 row)

pg-mem version

2.6.3

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Fryunicommented, Nov 3, 2022

There is no way to get automatically get the optimized query that I know of.

I called EXPLAIN (ANALYZE, BUFFERS, VERBOSE, COSTS, TIMING, FORMAT JSON) passed it through a plan visualizer (this one). From that I saw which joins were performed to get the same result and rewrote to query with them making sure that the resulting plan was the same or equivalent.

0reactions
oseibonsucommented, Nov 3, 2022

@Fryuni I am somewhat new to Postgres is there a command to have it output the optimized planned query?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does SQL Server propagate WHERE conditions in complex ...
Show the execution plan and look at the filter applied to the tables. If it shows the where condition being applied, then it...
Read more >
SQL How to Write Nested Query Subquery | Example - YouTube
IMPORTANT LINKS:1) Official Website: http://www.techtud.com/2) Virtual GATE: http://virtualgate.in/login/index.
Read more >
How To Use Nested Queries in SQL - DigitalOcean
Begin this operation with the DELETE FROM statement so it's clear where the data is being deleted from, in this case, the upgrade_guests...
Read more >
Chapter 4. Query Performance Optimization - O'Reilly
The best solution is to add a LIMIT clause to the query. ... Of course, asking for more data than you really need...
Read more >
Query Processing Architecture Guide - SQL Server
Hints can propagate through levels of nested views. For example, suppose a query applies the HOLDLOCK hint on a view v1 . When...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found