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 1qUdFf-00DaM4-0l for pgsql-sql@arkaria.postgresql.org; Sat, 12 Aug 2023 01:17:11 +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 1qUdEe-00DJd2-5l for pgsql-sql@arkaria.postgresql.org; Sat, 12 Aug 2023 01:16:08 +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 1qUdEd-00DJcs-I4 for pgsql-sql@lists.postgresql.org; Sat, 12 Aug 2023 01:16:08 +0000 Received: from li1929-156.members.linode.com ([172.104.219.156] helo=ln-1.useunix.net) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qUdEU-001ynE-0g for pgsql-sql@lists.postgresql.org; Sat, 12 Aug 2023 01:16:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=useunix.net ; s=dkim-2020090201; h=Content-Transfer-Encoding:Content-Type:MIME-Version: Date:Message-ID:Subject:From:To:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=EI11G+t4c+9q8uvjE0baFMZGrodwYLZG7tX0gxeGZyI=; b=QQrXl9Gt4wRB6Jj94a3ep0bTma f8OCeHVEliqLWGzmmS6s8l2iiD8vVDsCZBnFP1ZRs9ZGkRlnDnKArGPbId7vwBcUj9K5jRcyuuf3K Ct+hNH1PRN+C+QoE1zqwwTQSTyQE53AjIvulhofuTQVlddC/tN8V69VWTBbz9jHy35PhuiwPnhHms faiLS3QYh+6K0RozR3X6lNQi2yS0dX9v67orfCOmTcm2yzaUS99ph2k+IdHwMHZiHPFbP/xjFmvLD VdvIiYX17b9UjfVqr9cF5298LQ5bvI2NF7rRpiF4V+SKLmpf8Nwm6pwIz/w6bRqzKd43bFaUr+yGu jfdqqfAA==; Received: from pool-100-36-175-69.washdc.fios.verizon.net ([100.36.175.69]:58950 helo=s76.ja10629.home) by ln-1.useunix.net with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qUdEP-0000pd-4H for pgsql-sql@lists.postgresql.org; Sat, 12 Aug 2023 01:15:53 +0000 To: pgsql-sql@lists.postgresql.org From: lists-pgsql@useunix.net Subject: index not being used Message-ID: <26113e89-fc44-e0bd-7c4a-b899d5bc591e@useunix.net> Date: Fri, 11 Aug 2023 21:15:51 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.16 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I'm running an older PostgreSQL 9.1 database. I know it's old... an upgrade is planned. I have a table with the following columns. Column | Type | Modifiers | Storage | Description --------+---------+-----------+----------+------------- sat | text | not null | extended | ts | bigint | not null | plain | apid | integer | not null | plain | bin | integer | not null | plain | value | bigint | not null | plain | A unique index on (sat, ts, apid, bin). There are only a handful of unique sat values but there are about 20 million rows in the table as there are many apid values per unit time. This query is fast and uses the index: select max(ts) from table where sat = 'XX'; While this query results in sequential scans and long execution times: select sat, max(ts) from histograms where sat in ('A1', 'A2', 'S1', 'S2') group by 1; Is there any way to formulate this query to make it faster without adding an additional index? Thank you in advance, Wayne