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 1nKoyX-00018I-4v for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Feb 2022 22:10:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nKoyU-00023z-E9 for pgsql-bugs@arkaria.postgresql.org; Thu, 17 Feb 2022 22:10:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nKoyT-00023q-Oi for pgsql-bugs@lists.postgresql.org; Thu, 17 Feb 2022 22:10:06 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nKoyO-0007U8-15 for pgsql-bugs@lists.postgresql.org; Thu, 17 Feb 2022 22:10:03 +0000 Received: by mail-wr1-x42e.google.com with SMTP id o24so11363857wro.3 for ; Thu, 17 Feb 2022 14:09:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=m3pr2ILqNDvxG2nPfcmfYmj15uqNIQcokRweEJJYn1Y=; b=i0PN2da77L8gafLbkmI8ogSI9W6bjUGHPU3zOrqSa+Z/8tz6O6dpawTXLvnwrd8AaY VHrR7lWt7sm5JfnXv7emgo4XLUyCN+74t27geNkqkvHzcrz9zBDIYNGyi0t8zygJePhv liz58n/IfVMlVFodyFhGgjSTGfppyHIgSN5zP2fHig+Z5OckCic2dj15f+3D2CLrPiSR UjY7aq5eExDrXIfM/JHcmdIH3yBUNV0gtTVTkmXI2UFOS9K8av8SmlWILBOtORc2orJS +3C2KwziwSzRbRWK7RCw4wnOPm24exptzUdrkTx3oSgHQ7mfzCymJeMzJs12grbFDXdP 7EXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=m3pr2ILqNDvxG2nPfcmfYmj15uqNIQcokRweEJJYn1Y=; b=GWTlnpuSfjJ3OKw4ra679TjgO2vSMWz/rCRIF+5Im+tvUqz0bpnCTF9o3OSRqvbcmx 1H9yq2pwuAoHbi27ffi7/3rH1Amn92+TgeftaMhpkqw/d9id0EeCNehnWiBqZwU+oPNR a75bLYcgvCi1L4PUiqbkmTCDh6ETvoEqdluFTFYtRJU8YhNPnkfThfkQx1c3yiTOLIOp CR5O9sKad8dSjM+8vxgbZSb+a0u96yXeU4fbxaTfo8DWuge3bKSaFt9nq9WwdAPAIOE/ qbfxwlqxl9WAPYymukabJCaaMEinM5AnnQ5DqWFi2K3y9t/67euxQb1yOPPSVsTNaaSk wLVQ== X-Gm-Message-State: AOAM531qnlXSCZ3pCRvovpWNMY4xWJ6u0E9psxmjReKM6rplY89k3ymh 9vif35zbrKkDrUC80VRuldOfnPQf+5SYx2tKXtNyraZIEpFqPfFfWeKKPHUe9l53+QFEnM3dAxy oiCFvEplMNT7fRENc/KV2tZjcqdbF2H1m3/56s0zvQ1NoAORVbRja9L/FaxwgqDVb1wxnm0hoSR 4hx5rhA4qDBAYcG/1VXsf2HuTW8WnhcIGFHmiSNBPe8Fhb X-Google-Smtp-Source: ABdhPJz1ZBS8zctIJPG68Mjg+5BIxc2P+6tga4YjmvtbpkKYENZ4js0K82wrzqlb0llllm4alsabiw== X-Received: by 2002:a5d:608d:0:b0:1e4:9b64:8cb4 with SMTP id w13-20020a5d608d000000b001e49b648cb4mr3574545wrt.712.1645135798980; Thu, 17 Feb 2022 14:09:58 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-251-241.net.upcbroadband.cz. [86.49.251.241]) by smtp.gmail.com with ESMTPSA id o10sm31287324wrc.98.2022.02.17.14.09.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Feb 2022 14:09:58 -0800 (PST) Message-ID: Date: Thu, 17 Feb 2022 23:09:56 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Optimizer picking a poor plan for Serializable Transaction Isolation Content-Language: en-US To: Tyler Rockwood , pgsql-bugs@lists.postgresql.org References: From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/17/22 19:55, Tyler Rockwood wrote: > We introduced our first query hint into our code today, and based on > this wiki page > I figured I > report it as a bug. > > Our table looks something like: > > CREATE TABLE IF NOT EXISTS threads ( > account_id VARCHAR(64), > group_id BIGINT, > thread_id VARCHAR(64), > "timestamp" BIGINT NOT NULL, > has_unread BOOLEAN NULL, > CONSTRAINT pk_threads PRIMARY KEY (account_id, group_id, thread_id) > ); > CREATE UNIQUE INDEX threads_by_owner_group_time_id ON threads (account_id, group_id, "timestamp", thread_id); > CREATE UNIQUE INDEX threads_by_owner_group_unread_time_id ON threads ( > account_id, > group_id, > has_unread, > "timestamp", > thread_id > ); > > We have a query that is a lookup - essentially this: > > SELECT * FROM threads WHERE account_id = ? AND group_id = ? AND thread_id = ?; > > Usually everything is fine and this does a query on the primary key > index - but sometimes postgres decides to pick the > |threads_by_owner_group_unread_time_id|index. > > Picking the index is an issue because it ends up scanning essentially > the whole index range for a given |account_id|and |group_id|. Due to the > way upgrading predicate locks works - this almost always ends up locking > the entire table if that group has enough threads. This causes a ton of > contention in our database as this query happens for every incoming > message (we have to book keep the |has_unread|column properly) and the > predicate lock on the whole table interferes with every other active > account's processing (we processing all events for a single account > sequentially). > > I *think*this index is being picked over the primary key because it's > queried frequently and is likely to be cached or something. Anyways at > any other isolation level this would be fine, but at serializability it > ends up hurting throughput because we now retry a ton transactions > unnecessarily. > Unlikely - we don't use usage stats for query planning. You're probably getting different query plans for parameter values with different row count estimates. And if the estimates are wrong (which may happen if the columns are correlated in some way), the estimates may be off, and the optimizer can pick the wrong plan. But it's really hard to give more insight because you didn't show the query plans, and you also didn't mention the Postgres version. See https://wiki.postgresql.org/wiki/Slow_Query_Questions regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company