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 1lSLHp-00015x-SZ for pgsql-general@arkaria.postgresql.org; Fri, 02 Apr 2021 15:00:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lSLHo-0008TB-Qu for pgsql-general@arkaria.postgresql.org; Fri, 02 Apr 2021 15:00:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSLHo-0008T3-JT for pgsql-general@lists.postgresql.org; Fri, 02 Apr 2021 15:00:36 +0000 Received: from smtp12.smtpout.orange.fr ([80.12.242.134] helo=smtp.smtpout.orange.fr) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.92) (envelope-from ) id 1lSLHl-0005nF-M0 for pgsql-general@lists.postgresql.org; Fri, 02 Apr 2021 15:00:35 +0000 Received: from X201.home ([193.252.24.13]) by mwinf5d47 with ME id nr0V2400R0GxKGH03r0VyE; Fri, 02 Apr 2021 17:00:30 +0200 X-ME-Helo: X201.home X-ME-Date: Fri, 02 Apr 2021 17:00:30 +0200 X-ME-IP: 193.252.24.13 Date: Fri, 2 Apr 2021 17:00:29 +0200 From: Vincent Veyron To: pgsql-general@lists.postgresql.org Subject: Re: How to implement expiration in PostgreSQL? Message-Id: <20210402170029.798700ddc425019ed1d01ebf@wanadoo.fr> In-Reply-To: <3AE654F0-815D-461B-B7D8-A137FC1C6493@gmail.com> References: <3AE654F0-815D-461B-B7D8-A137FC1C6493@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 1 Apr 2021 09:51:38 +0800 Glen Huang wrote: Hi, If you are in a position to modify your design, I believe your problem comes from this part : > WHERE paid OR join_time > now() - ‘1h’::interval which suggests that there is a 'paid' column being updated. I learned that the proper way to structure a database to maintain membership and save a lot of grief is the following : create table seated_member( name text not null, ..., join_time date not null default now, validity integer not null default 1 ); where validity is a number of whatever unit is appropriate; typically 'year' for a club membership (newspapers use 'issue number' instead of join_time to account for strikes, when no paper is issued). In your case, 'hour' I suppose. All you need to do when the member pays is to update the 'validity' field with the proper amount of units. This makes for very simple and efficient queries to retrieve the data, and you only need to write : WHERE now() < join_time + 'validity hours'::interval to retrieve valid accounts. Accounts expire automatically, deleting them can wait; it also makes it easier to send reminders before the expiration date -- Bien à vous, Vincent Veyron https://marica.fr Logiciel de gestion des contentieux juridiques, des contrats et des sinistres d'assurance