gc.sql (2914B)
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 -- Per-instance GC: loop over all instances and run the 26 -- per-instance GC helpers + targeted DELETEs in each schema. 27 FOR rec IN SELECT merchant_serial FROM merchant.merchant_instances 28 LOOP 29 s := 'merchant_instance_' || rec.merchant_serial::TEXT; 30 BEGIN 31 EXECUTE format('SET LOCAL search_path TO %I, merchant', s); 32 EXECUTE format('CALL %I.merchant_statistic_amount_gc()', s); 33 EXECUTE format('CALL %I.merchant_statistic_bucket_gc()', s); 34 EXECUTE format('CALL %I.merchant_statistic_counter_gc()', s); 35 EXECUTE format('DELETE FROM %I.merchant_unclaim_signatures' 36 ' WHERE expiration_time < $1', s) USING in_now; 37 EXCEPTION 38 WHEN undefined_table THEN 39 NULL; 40 WHEN undefined_function THEN 41 NULL; 42 WHEN OTHERS THEN 43 -- Do not abandon garbage collection for all remaining instances 44 -- (and the global tables) just because one instance failed. 45 RAISE WARNING 'Garbage collection failed for %: % (%)', 46 s, SQLERRM, SQLSTATE; 47 END; 48 COMMIT; 49 END LOOP; 50 DELETE FROM merchant.tan_challenges 51 WHERE expiration_date < $1; 52 -- Restore the global search_path so subsequent statements on this 53 -- session don't leak into the last visited instance schema. 54 SET LOCAL search_path TO merchant; 55 END $$; 56 COMMENT ON PROCEDURE merchant_do_gc 57 IS 'Calls per-instance garbage collection subroutines across every instance.' 58 ' For each' 59 ' instance runs merchant_statistic_*_gc and DELETEs expired tan_challenges' 60 ' / merchant_unclaim_signatures.'; 61 -- Note: deliberately no COMMIT here. gen-procedures.sh wraps the entire 62 -- concatenation of sql_global_procedures in a single BEGIN ... COMMIT; 63 -- a COMMIT in this file would close that transaction early, so everything 64 -- appended after this file would be installed outside of it (and the 65 -- generator's own trailing COMMIT would warn 'there is no transaction in 66 -- progress'). The COMMIT inside the procedure body above is a plpgsql 67 -- transaction-control statement and is unrelated.