Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hBfx2-0006rW-Gp for pgsql-general@arkaria.postgresql.org; Wed, 03 Apr 2019 13:29:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hBfx0-0007dJ-Ee for pgsql-general@arkaria.postgresql.org; Wed, 03 Apr 2019 13:29:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hBfwz-0007Wc-Lf for pgsql-general@lists.postgresql.org; Wed, 03 Apr 2019 13:29:10 +0000 Received: from mail.wordtothewise.com ([104.225.223.158]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hBfwv-0000zC-KH for pgsql-general@lists.postgresql.org; Wed, 03 Apr 2019 13:29:09 +0000 Received: from [192.168.0.87] (unknown [37.228.250.230]) by mail.wordtothewise.com (Postfix) with ESMTPSA id E17D39F146 for ; Wed, 3 Apr 2019 06:29:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wordtothewise.com; s=aardvark; t=1554298143; bh=Ey6pbugoDP0YDLTB6pUIeQgpEbqMhPR8BzYfEnG5ukk=; h=From:Subject:Date:References:To:In-Reply-To:From; b=JwYNwj+wTVral/WwS2Nhni1r8vHibnVkNh2S7LPdpcFAImkXtLwFPz0bs6CzK39qd K1QdsmaDfmSxjok2ObZJkPQneRf2gJaN4U3jiK8NVlxTnjus5MnvNI0Gse9yjVVQV+ i06F6RDPTkwE39d5bNOoY6Jm8XiLDayIQuJgB9a4= From: Steve Atkins Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.2 \(3445.102.3\)) Subject: Re: SQL queries not matching on certain fields Date: Wed, 3 Apr 2019 14:29:00 +0100 References: <084039d1-bddb-2fc6-1ad3-4be7562fd9bb@nutomic.com> To: PG mailing List In-Reply-To: <084039d1-bddb-2fc6-1ad3-4be7562fd9bb@nutomic.com> Message-Id: X-Mailer: Apple Mail (2.3445.102.3) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Apr 3, 2019, at 2:06 PM, Felix Ableitner wrote: >=20 > Hello, >=20 > I'm having a very strange problem with the Postgres database for my = website. Some SQL queries are not matching on certain fields. I am = running these commands via the psql command. >=20 > Here is a query that works as expected: >=20 > # SELECT id, "preferredUsername" FROM actor WHERE = "preferredUsername"=3D'emma'; > id | preferredUsername > -------+------------------- > 48952 | emma > 58672 | emma > (2 rows) >=20 > The following query should work as well, because the username exists. = But in fact, it consistently returns nothing: >=20 > # SELECT id, "preferredUsername" FROM actor WHERE = "preferredUsername"=3D'mailab'; > id | preferredUsername > ----+------------------- >=20 > (0 rows) >=20 > There are some workarounds which fix the WHERE statement, all of the = following work as expected: >=20 > SELECT id, "preferredUsername" FROM actor WHERE = trim("preferredUsername")=3D'mailab'; >=20 >=20 >=20 > SELECT id, "preferredUsername" FROM actor WHERE "preferredUsername" = ILIKE 'mailab';=20 >=20 > SELECT id, "preferredUsername" FROM actor WHERE = md5("preferredUsername")=3Dmd5('mailab');=20 >=20 >=20 > Now you might think that there is something wrong with the encoding, = or the field contains some extra whitespace. But SHOW SERVER_ENCODING = and SHOW SERVER_ENCODING show UTF8. Length and m5 sum are also exactly = as expected. And I checked the individual bytes with get_byte(), all of = them are in the range 97-122. >=20 > About the setup, this is Postgres 10 running in Docker, on an Ubuntu = VPS (see below for all versions etc). I had this problem before on the = same setup, so I did an export to text file with pg_dump, and imported = into a completely new database with psql. That fixed the problem for a = few days, but it came back soon after. >=20 > The problem only seems to affect one or two specific columns, and only = a few specific rows in those columns. Most other rows work normally. = Affected columns also randomly start working again after a few days, and = other columns get affected. I havent noticed any kind of pattern. >=20 > You can find the table definition here: = https://gitlab.com/snippets/1840320 You can use "explain" to see what plan is being used for the query, but = I'm betting that it's using an index on preferredUsername. Your = workarounds won't use that index, they'll scan the table. If that index is corrupted it could cause the symptoms you're seeing. = You can use "reindex" to rebuild the index from scratch and see if it = fixes it but corrupted indexes aren't normal, and the issue seems to be = recurring. On physical hardware I'd be wondering about filesystem = corruption and taking a good look at my system logs. On someone else's = VPS you don't have the same visibility, but I'd still check the system = logs for issues. Cheers, Steve >=20 > Version info: >=20 > Postgres Docker Image: postgres:10.7-alpine=20 > Docker version: 18.09.2 > OS: Ubuntu 18.04.2 >=20 > Please tell me if you have any idea how to fix or debug this. I = already asked multiple people, and no one has a clue what is going on. >=20 > Best, > Felix Ableitner >=20