From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Alexander Farber <alexander.farber@gmail.com>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: if not exists (SELECT 1... UNION SELECT 1...)
Date: Wed, 16 Feb 2022 13:24:23 +0100
Message-ID: <09dbff99faf9a57526ea042aa24ef46d7db263ff.camel@cybertec.at> (raw)
In-Reply-To: <CAADeyWgesg5apJqYYHKqpETSPdyZ5unQ9mnL-CgA9srqSf+CEg@mail.gmail.com>
References: <CAADeyWgesg5apJqYYHKqpETSPdyZ5unQ9mnL-CgA9srqSf+CEg@mail.gmail.com>
On Wed, 2022-02-16 at 11:51 +0100, Alexander Farber wrote:
> when I search for a non existent word in the two tables hosted in PostgreSQL 14.1 then I get zero records as expected:
>
> words_en=> SELECT 1 FROM words_nouns WHERE word = 'ABCDE' UNION SELECT 1 FROM words_verbs WHERE word = 'ABCDE';
> ?column?
> ----------
> (0 rows)
>
> But when I try to use the same command in my stored function, then it goes through,
> as if the word would exist (and a new record is inserted into the words_puzzle table):
>
> CREATE OR REPLACE FUNCTION words_guess_puzzle(
> in_mid bigint,
> in_social integer,
> in_sid text,
> in_auth text,
> in_guess text,
> OUT out_text text
> ) RETURNS text AS
> $func$
> [...]
> -- check if the in_guess is a valid word - WHY DOES THIS NEVER TRIGGER?
> IF NOT EXISTS(SELECT 1 FROM words_nouns WHERE word = in_guess UNION
> SELECT 1 FROM words_verbs WHERE word = in_guess) THEN
> out_text := '___WRONG___'
> RETURN;
> END IF;
> [...]
>
> What could be the reason please?
One reason could be index corruption. If one query uses an index and the other doesn't,
that could lead to different results.
The other option is of course a trivial error, like you are using a different search
string or connect to a different database.
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
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: laurenz.albe@cybertec.at, alexander.farber@gmail.com
Subject: Re: if not exists (SELECT 1... UNION SELECT 1...)
In-Reply-To: <09dbff99faf9a57526ea042aa24ef46d7db263ff.camel@cybertec.at>
References: <CAADeyWgesg5apJqYYHKqpETSPdyZ5unQ9mnL-CgA9srqSf+CEg@mail.gmail.com> <09dbff99faf9a57526ea042aa24ef46d7db263ff.camel@cybertec.at>
* 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