Thread

  1. BUG #18932: PostgreSQL fails to handle EXCEPT operation when the left branch is an Empty Table

    PG Bug reporting form <noreply@postgresql.org> — 2025-05-15T13:06:35Z

    The following bug has been logged on the website:
    
    Bug reference:      18932
    Logged by:          jinhui lai
    Email address:      jinhuilai@email.ncu.edu.cn
    PostgreSQL version: 17.5
    Operating system:   ubuntu 22.04
    Description:        
    
    Hi, PostgreSQL Developers,
    Please consider such query:
    SELECT * FROM empty_table EXCEPT SELECT * FROM not_empty_table;
    Obviously, the above query always returns an empty set. I think it should
    return an empty set quickly. However, it waste much time. PostgreSQL fails
    to optimize queries containing EXCEPT operations when the left branch is an
    Empty Table. This leads to unnecessary memory consumption and query
    cancellation, even though the result should be deterministically empty.
    I think this is a common case in actual production scenarios. It's important
    to clarify that users might not intentionally perform EXCEPT operations on
    empty tables. Rather, they may be unaware that a table is empty . For
    example, when data has been deleted by another user or process. If
    PostgreSQL can address this performance bug, it would significantly improve
    query efficiency and save users valuable time in such cases.
    Thank you for your valuable time, looking forward to your reply!
    Best regard,
    Jinhui Lai
    You can reproduce the bug as follow steps:
    psql -U postgres -c "CREATE DATABASE testdb;"
    pgbench -U postgres -i -s 1000 testdb
    psql -U postgres
    \timing on
    \c testdb;
    testdb=# CREATE TABLE empty_table AS SELECT * FROM pgbench_accounts LIMIT 0;
    SELECT 0
    Time: 3.111 ms
    testdb=# SELECT * FROM empty_table EXCEPT SELECT * FROM pgbench_accounts;
     aid | bid | abalance | filler
    -----+-----+----------+--------
    (0 rows)
    Time: 16077.804 ms (00:16.078) -- expect: less time
    testdb=# explain SELECT * FROM empty_table EXCEPT SELECT * FROM
    pgbench_accounts;
                                               QUERY PLAN
    -------------------------------------------------------------------------------------------------
     HashSetOp Except  (cost=0.00..5139362.35 rows=200 width=356)
       ->  Append  (cost=0.00..4139360.25 rows=100000210 width=356)
             ->  Subquery Scan on "*SELECT* 1"  (cost=0.00..14.20 rows=210
    width=356)
                   ->  Seq Scan on empty_table  (cost=0.00..12.10 rows=210
    width=352)
             ->  Subquery Scan on "*SELECT* 2"  (cost=0.00..3639345.00
    rows=100000000 width=101)
                   ->  Seq Scan on pgbench_accounts  (cost=0.00..2639345.00
    rows=100000000 width=97)
     JIT:
       Functions: 6
       Options: Inlining true, Optimization true, Expressions true, Deforming
    true
    (9 rows)
    Time: 1.674 ms
    
    
  2. Re: BUG #18932: PostgreSQL fails to handle EXCEPT operation when the left branch is an Empty Table

    David G. Johnston <david.g.johnston@gmail.com> — 2025-05-15T14:14:13Z

    On Thu, May 15, 2025, 07:25 PG Bug reporting form <noreply@postgresql.org>
    wrote:
    
    > The following bug has been logged on the website:
    >
    > Bug reference:      18932
    > Logged by:          jinhui lai
    > Email address:      jinhuilai@email.ncu.edu.cn
    > PostgreSQL version: 17.5
    > Operating system:   ubuntu 22.04
    > Description:
    >
    > Hi, PostgreSQL Developers,
    > Please consider such query:
    > SELECT * FROM empty_table EXCEPT SELECT * FROM not_empty_table;
    > Obviously, the above query always returns an empty set. I think it should
    > return an empty set quickly.
    >
    
    The failure to do so is not a bug.  See other similar complaints in the
    past month or so.
    
    Patches in this area could be accepted but the corner cases being described
    don't seem to be highly motivating.
    
    David J.