merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

gc.sql (2546B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2026 Taler Systems SA
      4 --
      5 -- TALER is free software; you can redistribute it and/or modify it under the
      6 -- terms of the GNU General Public License as published by the Free Software
      7 -- Foundation; either version 3, or (at your option) any later version.
      8 --
      9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 -- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 --
     13 -- You should have received a copy of the GNU General Public License along with
     14 -- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 --
     16 
     17 DROP PROCEDURE IF EXISTS merchant_do_gc;
     18 CREATE PROCEDURE merchant_do_gc(in_now INT8)
     19 LANGUAGE plpgsql
     20 AS $$
     21 DECLARE
     22   rec RECORD;
     23   s TEXT;
     24 BEGIN
     25   -- Drop validation-pending instances that never confirmed in time.  The
     26   -- AFTER DELETE trigger on merchant.merchant_instances will DROP the
     27   -- per-instance schema for each removed row.
     28   DELETE FROM merchant.merchant_instances
     29     WHERE validation_needed
     30       AND validation_expiration < in_now;
     31 
     32   -- Per-instance GC: loop over all surviving instances and run the
     33   -- per-instance GC helpers + targeted DELETEs in each schema.
     34   FOR rec IN SELECT merchant_serial FROM merchant.merchant_instances
     35   LOOP
     36     s := 'merchant_instance_' || rec.merchant_serial::TEXT;
     37     BEGIN
     38       EXECUTE format('SET LOCAL search_path TO %I, merchant', s);
     39       CALL merchant_statistic_amount_gc ();
     40       CALL merchant_statistic_bucket_gc ();
     41       CALL merchant_statistic_counter_gc ();
     42       EXECUTE format('DELETE FROM %I.merchant_unclaim_signatures'
     43                      ' WHERE expiration_time < $1', s) USING in_now;
     44     EXCEPTION
     45       WHEN undefined_table THEN
     46         NULL;
     47       WHEN undefined_function THEN
     48         NULL;
     49     END;
     50   END LOOP;
     51   DELETE FROM merchant.tan_challenges
     52     WHERE expiration_date < $1;
     53   -- Restore the global search_path so subsequent statements on this
     54   -- session don't leak into the last visited instance schema.
     55   SET LOCAL search_path TO merchant;
     56 END $$;
     57 COMMENT ON PROCEDURE merchant_do_gc
     58   IS 'Calls per-instance garbage collection subroutines across every instance.'
     59      ' Removes expired pending-validation instances first (whose ON DELETE'
     60      ' trigger drops the entire per-instance schema), then for each surviving'
     61      ' instance runs merchant_statistic_*_gc and DELETEs expired tan_challenges'
     62      ' / merchant_unclaim_signatures.';
     63 
     64 COMMIT;