pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Very newbie question
Date: Mon, 23 Oct 2023 10:50:47 -0500
Message-ID: <a3f8878b-6ba2-4754-8639-50a3d2ec2b44@gmail.com> (raw)
In-Reply-To: <B3F74121-CECE-42D2-B0AB-5D7E6F3A466A@ya.ru>
References: <B3F74121-CECE-42D2-B0AB-5D7E6F3A466A@ya.ru>

On 10/23/23 10:13, Олег Самойлов wrote:
> Back pardon, but I have a very newbie question. I have a partitioned table, partitioned by primary bigint key, size of partition 10000000. I need to get the number of partition which need to archive, which has all rows are olden then 3 month. Here is query:
>
> SELECT id/10000000 as partition
>     FROM delivery
>     GROUP BY partition
>     HAVING max(created_at) < CURRENT_DATE - '3 month'::interval;
>
> The 'id/10000000 as partition' is a number of the partition, it later will be used inside the partition name.
> The query runs long by sequence scan. Has anyone any ideas how to rewrite query so it will use any index?

Maybe:

SELECT DISTINCT id/10000000 as partition
FROM delivery
WHERE max(created_at) < CURRENT_DATE - '3 month'::interval;

I haven't tried it, though.

-- 
Born in Arizona, moved to Babylonia.

view thread (12+ messages)  latest in thread

Message-ID: <a3f8878b-6ba2-4754-8639-50a3d2ec2b44@gmail.com>
Permalink:  ../a3f8878b-6ba2-4754-8639-50a3d2ec2b44@gmail.com/
Also on:    postgresql.org/message-id/a3f8878b-6ba2-4754-8639-50a3d2ec2b44@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com
  Subject: Re: Very newbie question
  In-Reply-To: <a3f8878b-6ba2-4754-8639-50a3d2ec2b44@gmail.com>
  References: <B3F74121-CECE-42D2-B0AB-5D7E6F3A466A@ya.ru> <a3f8878b-6ba2-4754-8639-50a3d2ec2b44@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox