Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nKJME-0007he-GP for pgsql-general@arkaria.postgresql.org; Wed, 16 Feb 2022 12:24:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nKJMD-0001bq-EJ for pgsql-general@arkaria.postgresql.org; Wed, 16 Feb 2022 12:24:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nKJMD-0001bh-1e for pgsql-general@lists.postgresql.org; Wed, 16 Feb 2022 12:24:29 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nKJMA-0006oG-6a for pgsql-general@postgresql.org; Wed, 16 Feb 2022 12:24:28 +0000 Received: by mail-ed1-x52a.google.com with SMTP id z22so3640392edd.1 for ; Wed, 16 Feb 2022 04:24:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=h9kDSaQEdXhDiP6Y4V3WbUoBg8fUJRK4U5VJvg0g2BY=; b=knZWcdiIsD8hJ0fuzsj4gDQlymdcJBQeKOYwjrA15kLtiBO7HdSLvUYvO/Y2wsb3cD GcCHRbhZXtab6mvBGAJXjPY+DGXvunC2nq1YNO/bsBTeHSATjaCazEqxVto5kP4ho/sd DD6N88aa2MKG/dUH8G7twcRXODZ2reUVE65H0w9/8LotKTL5KYutqDIBPNPBvRgTRo3t oZzpVMSs0nqkDZR58/KA59Sy14QCc8aIADVqFQUItzF1YivimNEiCX2AU1NX+eLZVXjm 6+a7Oy/wQ2FRHqz9FVN6yBaSXwrW0C79gAsfuRy9wHK1oljaeoyLganWMAOvHyRFh4Mf r0ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=h9kDSaQEdXhDiP6Y4V3WbUoBg8fUJRK4U5VJvg0g2BY=; b=rzIbuKPTzZIj+eAM0qxQxXXgIXsGwerLkIZEbvYZGaIo5/ZHwSQt2klXpIzKaDipHt j7pSySG53pdA9nD9UPCEVVorS12HFhZkFFSphj6/jWINgepHr+ig6pETkaD4QiiVeOVb 5uZaGlEkpJcRs+lFquHxqifWM8kwFARSn2382ZojYFG1F0UWhOcfrfgDMa6E+OBjAFya Ne/Dn1BLnucUt4ltezAQn4V+CSUXoMx19VOve0ov6OE45M3gJxGaeXT58TUD+is65sHZ PV7VCHK1tk8PNI7cw7IyvE9dNBVWx7H3in15i2ipnT7wGAhwLofedqyR7sYK/Q9BKP9u 6bnA== X-Gm-Message-State: AOAM533X2cM42sgzgorqMexMwZTJLxJkzslxnrVuOeSX/VlXD5FJ7E6P f3ex23myuA2OQhqL3meK7QqC6Z2APqk4BQ== X-Google-Smtp-Source: ABdhPJwa+iOedU0GLsN+9jgwN92ZyBvoeGje4WJBwIS+q8ejs8DQT0UMQJdz0YXquHMqbBCjkWQWhw== X-Received: by 2002:a05:6402:350e:b0:410:d121:aec1 with SMTP id b14-20020a056402350e00b00410d121aec1mr2743622edd.272.1645014264518; Wed, 16 Feb 2022 04:24:24 -0800 (PST) Received: from localhost.localdomain ([2001:871:5e:9ca6:1576:e5da:f09:6e90]) by smtp.gmail.com with ESMTPSA id i28sm3559558ejo.49.2022.02.16.04.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 04:24:23 -0800 (PST) Message-ID: <09dbff99faf9a57526ea042aa24ef46d7db263ff.camel@cybertec.at> Subject: Re: if not exists (SELECT 1... UNION SELECT 1...) From: Laurenz Albe To: Alexander Farber , pgsql-general Date: Wed, 16 Feb 2022 13:24:23 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-2.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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