Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qxrBR-003rYJ-2X for pgsql-performance@arkaria.postgresql.org; Tue, 31 Oct 2023 16:01:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qxrBP-003Atd-LA for pgsql-performance@arkaria.postgresql.org; Tue, 31 Oct 2023 16:01:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qxrBP-003AtP-9A for pgsql-performance@lists.postgresql.org; Tue, 31 Oct 2023 16:01:35 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qxrBM-0040tK-FN for pgsql-performance@postgresql.org; Tue, 31 Oct 2023 16:01:34 +0000 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-53e751aeb3cso9580841a12.2 for ; Tue, 31 Oct 2023 09:01:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stiltsoft.com; s=google; t=1698768091; x=1699372891; darn=postgresql.org; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=NpAYf0RB/8jlYRF1Ez59Wq7hdSMag0M9IhyPn4R2qQw=; b=iS5EAcQtYoWhOGyBoLSXDEsXy+1rcUDJZYPnyvVKEtIAh6SQ5HYA67AObRvT75LXEn SAq9DZZHWkwXV6PfSYxEpAOjKzreWUDoS6Z24cnyvDqH3TPYxx65kZv7DbXvvWUrolc6 LsCs6hhZ8ZO2X3t/ckEz8ndq7N/MMC2Up5ybk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698768091; x=1699372891; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=NpAYf0RB/8jlYRF1Ez59Wq7hdSMag0M9IhyPn4R2qQw=; b=nQbBIgxvnHh4rF9tpwcffdI7NPv+CLDM0Emz3mP5402t5oh6/gShMSB4Gd5SmaFeT1 1WlpZDoDGAwOwHykpIBUnonWXDJpRJ6NBDURehIN+ZfHxcZ0KOSQ8JBu5FNeaiVuYE7x Oo+OXFK4aFIk1PTCP680YOOIA+jrPVDGlH7CwTjncBlROXFSYyxAMqr2sw9xw4rB0MhE JIzMSL4Bicdb7dbYYaaoZNBNQ9DmTH3R7RUUlkYSfB0JroGqSxyN3WJyIufN2w1cZvny UbvW2eriqmiUZN72VzQALpHuT2o1sbRJ7SttpjSjiPPwrH0rqaTNPeUW4sTpseBrAgaA SQMQ== X-Gm-Message-State: AOJu0YxIuxsu4QKCFHeA1hyd0hfKefvW2g3SmlYe35OhoXi7tt9zRGqE vHKHOTSBKQGWwng0ez7YwM9oV2deilGA9LWD9fQrgbE3 X-Google-Smtp-Source: AGHT+IEHscMPbClhzZ56O0frQLSAgL72LS074SkW3whDUz/7Ak5hN2u86qCe8+Lyp0id3s3ELsWF5g== X-Received: by 2002:a17:907:d94:b0:9c3:8242:e665 with SMTP id go20-20020a1709070d9400b009c38242e665mr10257900ejc.8.1698768090917; Tue, 31 Oct 2023 09:01:30 -0700 (PDT) Received: from ?IPV6:2a02:a31d:8540:a280:38a:ec48:fa5a:d99e? ([2a02:a31d:8540:a280:38a:ec48:fa5a:d99e]) by smtp.gmail.com with ESMTPSA id gw17-20020a170906f15100b0099bcd1fa5b0sm1182407ejb.192.2023.10.31.09.01.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 31 Oct 2023 09:01:30 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------ZzHClUl0BPCwmx8RmGyixxV0" Message-ID: <6c888a16-b206-4817-b5ca-9e09b904edde@stiltsoft.com> Date: Tue, 31 Oct 2023 17:01:29 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Postgres 15 SELECT query doesn't use index under RLS To: Tom Lane Cc: pgsql-performance@postgresql.org References: <5c1179bb-240b-4c1c-b4b3-2a24868e44bc@stiltsoft.com> <1570249.1697228785@sss.pgh.pa.us> <3153246.1697661350@sss.pgh.pa.us> <1157086.1698329377@sss.pgh.pa.us> Content-Language: en-US From: Alexander Okulovich In-Reply-To: <1157086.1698329377@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------ZzHClUl0BPCwmx8RmGyixxV0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi Tom, > Can you force it in either direction with "set enable_seqscan = off" > (resp. "set enable_indexscan = off")? If so, how do the estimated > costs compare for the two plan shapes? Here are the results from the prod instance: seqscan off indexscan_off Just noticed that the WHEN clause differs from the initial one (392 ids under RLS). Probably, this is why the execution time isn't so catastrophic. Please let me know if this matters, and I'll rerun this with the initial request. Speaking of the stage vs local Docker Postgres instance, the execution time on stage is so short (0.1 ms with seq scan, 0.195 with index scan) that we probably should not consider them. But I'll execute the requests if it's necessary. > Maybe your prod installation has a bloated index, and that's driving > up the estimated cost enough to steer the planner away from it. We tried to make REINDEX CONCURRENTLY on a prod copy, but the planner still used Seq Scan instead of Index Scan afterward. Kind regards, Alexander --------------ZzHClUl0BPCwmx8RmGyixxV0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Hi Tom,

Can you force it in either direction with "set enable_seqscan = off"
(resp. "set enable_indexscan = off")?  If so, how do the estimated
costs compare for the two plan shapes?
Here are the results from the prod instance:

seqscan off

indexscan_off

Just noticed that the WHEN clause differs from the initial one (392 ids under RLS). Probably, this is why the execution time isn't so catastrophic. Please let me know if this matters, and I'll rerun this with the initial request.

Speaking of the stage vs local Docker Postgres instance, the execution time on stage is so short (0.1 ms with seq scan, 0.195 with index scan) that we probably should not consider them. But I'll execute the requests if it's necessary.

Maybe your prod installation has a bloated index, and that's driving
up the estimated cost enough to steer the planner away from it.
We tried to make REINDEX CONCURRENTLY on a prod copy, but the planner still used Seq Scan instead of Index Scan afterward.

Kind regards,

Alexander

--------------ZzHClUl0BPCwmx8RmGyixxV0--