pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: J. Roeleveld <joost@antarean.org>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Select .... where id not in (....) returns 0 incorrectly
Date: Mon, 04 Apr 2022 14:08:27 +0200
Message-ID: <5820573.lOV4Wx5bFT@iris> (raw)

Hi all,

While trying to write some queries to clean up records I no longer need, I 
come up with a very strange situation where numbers literally don't add up as 
expected.

If anyone can help me trace where this strange behaviour is coming from and 
how to resolve it, please let me know.
In case this is already resolved in a recent version, I am currently using 
version 11.14.

Please see the following log from psql:

joost=> create temporary table q ( id integer );
CREATE TABLE
joost=> insert into q ( id ) select snapshotlistid from backupitem;
INSERT 0 765
joost=> insert into q ( id ) select snapshotlistid from queue;
INSERT 0 3183

joost=> select count(1) from q;
 count 
-------
  3948
(1 row)

joost=> select count(1) from snapshotlist where id in (select id from q);
 count 
-------
  1810
(1 row)

joost=> select count(1) from snapshotlist where id not in (select id from q);
 count 
-------
     0
(1 row)

joost=> select count(1) from snapshotlist where id not in (select id from 
snapshotlist where id in (select id from q));
  count  
---------
 2293923
(1 row)


The tables are defined like: (Note, I did remove some fields from the tables 
which have no impact. Most are foreign keys to further tables or varchar data 
fields)

CREATE TABLE snapshotlist (
  id SERIAL PRIMARY KEY,
  active boolean,
  created TIMESTAMP DEFAULT clock_timestamp(),
  modified TIMESTAMP
);

CREATE TABLE queue (
  id SERIAL PRIMARY KEY,
  queuetask VARCHAR(500) NOT NULL,
  snapshotlistid INTEGER REFERENCES snapshotlist(id) ON DELETE RESTRICT,
  uuid uuid NOT NULL,
  UNIQUE(uuid)
);

CREATE TABLE backupitem (
  id SERIAL PRIMARY KEY,
  snapshotlistid INTEGER REFERENCES snapshotlist(id) ON DELETE RESTRICT NOT 
NULL,
  UNIQUE(snapshotlistid)
);









view thread (8+ messages)  latest in thread

Message-ID: <5820573.lOV4Wx5bFT@iris>
Permalink:  ../5820573.lOV4Wx5bFT@iris/
Also on:    postgresql.org/message-id/5820573.lOV4Wx5bFT@iris

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: joost@antarean.org
  Subject: Re: Select .... where id not in (....) returns 0 incorrectly
  In-Reply-To: <5820573.lOV4Wx5bFT@iris>
  References: <5820573.lOV4Wx5bFT@iris>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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