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 1lRmUt-0003fs-3V for pgsql-general@arkaria.postgresql.org; Thu, 01 Apr 2021 01:51:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRmUs-00063k-1o for pgsql-general@arkaria.postgresql.org; Thu, 01 Apr 2021 01:51:46 +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 1lRmUr-00063d-QW for pgsql-general@lists.postgresql.org; Thu, 01 Apr 2021 01:51:45 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lRmUq-0000J6-0F for pgsql-general@lists.postgresql.org; Thu, 01 Apr 2021 01:51:45 +0000 Received: by mail-pj1-x1035.google.com with SMTP id a22-20020a17090aa516b02900c1215e9b33so2185874pjq.5 for ; Wed, 31 Mar 2021 18:51:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:content-transfer-encoding:mime-version:subject :message-id:date:to; bh=jGrtmB64+39whc9eNCZldwXVQMZr5fBC2JvVkBMG2Hc=; b=YyYqGM3lgDcclV9gHCo+ePHrBv5VF8sXvFMAF9PGf/q9lPLrByvLNdiPCNO1jIJ/Z+ 572FLSw8UYhygHZiM0bfD/YlXcWVBBdxsVYAT6Ztht1zD1iQd3k2uNckJ8U+N7OqTNsT olVMAief0TQii3GPFeP0DplrAhqDgyPTJukSy6DA2F22J+tpYUeEA5i0iX46SgNndFVn QJbFStW8YS/A3kSXdz0Za2mAm0Pt+UJs8MGORRssG/3/+M5fVZF4sGGwzpJ1NHgF76I3 WXwuNGCV22LaqyMqFahOyT9KoIcaAizkBDDXRh1xKTOGjTzukdquOjOG3I3XTMW9uxhS bdmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:content-transfer-encoding :mime-version:subject:message-id:date:to; bh=jGrtmB64+39whc9eNCZldwXVQMZr5fBC2JvVkBMG2Hc=; b=s+M70bnGJwgDJPfuH2KsOj5KOSJcF2+Ei4EsMcWJsJfny1pmxWwV/f/ofvrHtG9D2Q AdXPwFO2e6txiWNSSZikIS0ts2pYURWbNbcEc7LZW1vguuscGO7S2CG4LnR10yU6Omls 0Lu9mTrBCK3RQTslQUDxH/ds+4plIWxcM8FLNpHezqXTKafpMtvGUabfSgC3Y5p74ibA /VXS8jqcWazq8o18FU0H5EkEBPc21HE52eWhuIWRyAH9oRNJ69uQdrDvs1jC8xYE5UAI v0Djon5TNlnQB2orzLeNfQSEk7PUpe736qmhy2sbiL8FixWnun4gSlGgSNtCW1wtHN40 IUMw== X-Gm-Message-State: AOAM530az3MgvxA9fQzipIHVu2G5lFBAtrAkku9YwiWEfrXbY8amo5IT YBwbZT9a2ISO1I9NoTqsvjwQzlIfXkAkNA== X-Google-Smtp-Source: ABdhPJx1pY6hpiJGHLVEg6WHgY0/EMp4vgPZcASlJjE7jnJJCbz9bKnHZfG6xnqvl/Yl5+HNqQdnCA== X-Received: by 2002:a17:902:fe0d:b029:e5:ffb8:1c28 with SMTP id g13-20020a170902fe0db02900e5ffb81c28mr5828840plj.62.1617241901998; Wed, 31 Mar 2021 18:51:41 -0700 (PDT) Received: from glens-imac.lan (67.230.181.248.16clouds.com. [67.230.181.248]) by smtp.gmail.com with ESMTPSA id k17sm3483951pfa.68.2021.03.31.18.51.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Mar 2021 18:51:41 -0700 (PDT) Sender: Glen Huang From: Glen Huang Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.7\)) Subject: How to implement expiration in PostgreSQL? Message-Id: <3AE654F0-815D-461B-B7D8-A137FC1C6493@gmail.com> Date: Thu, 1 Apr 2021 09:51:38 +0800 To: pgsql-general@lists.postgresql.org X-Mailer: Apple Mail (2.3445.9.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, I guess this question has been asked a million times, but all solutions = I can find online don=E2=80=99t really work well for my case. I=E2=80=99ll= list them here and hope someone can shed some light. My use case is to implement joining clubs that require entrance fee: 1. Each clubs only allows maximum number of members. 2. When a person decides to join a club, the seat is reserved for a = limited amount of time. If that person fails to pay within that period, = the seat will be open again I want to write a query that can quickly list all clubs that still have = open seats and #2 is where I want expiration to happen. The solutions I find so far: 1. Exclude closed clubs in queries and periodically delete expired = members I can=E2=80=99t come up with a query that can accomplish this in an = efficient way.=20 WITH seated_member AS ( SELECT club_id, count(member_id) AS num_seated_member FROM member WHERE paid OR join_time > now() - =E2=80=981h=E2=80=99::interval GROUP BY club_id ), open_member AS ( SELECT=20 club_id, max_num_member - coalesce(num_seated_member, 0) AS = num_open_member FROM club LEFT JOIN seated_member USING(club_id) ) SELECT club_id AS open_club FROM open_member WHERE num_open_member > 0 This requires going through all seated members, which can potentially be = large and takes a long time. I can of course add an num_open_member column to the club table and = index it, but the problem becomes how to automatically update it when a = member expires, which take us back to square one. All following solutions assume I add this column and seek to find a way = to update it automatically. 2. Run a cron job This won=E2=80=99t work because the number is updated only after the = cron job is run, which only happens at intervals. 3. Update the column before running any related queries This requires I execute DELETE and UPDATE queries before all seat = related queries. It=E2=80=99s hard to manage and seems to slow down all = such queries. 4. pg_cron My environment wouldn=E2=80=99t allow me to install 3rd-party = extensions, but even if I could, it seems pg_cron run cron jobs = sequentially. I=E2=80=99m not sure it works well when I need to add a = cron job for each newly joined member. =E2=80=94 I=E2=80=99m not aware any other solutions. But the problem seems banal, = and I believe it has been solved for a long time. Would really = appreciate it if someone could at least point me in the right direction. Regards, Glen=