gc.sql (4147B)
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 -- Compare elapsed time instead of adding the duration to the start: 38 -- indefinite durations use INT8_MAX and must never overflow here. 39 EXECUTE format('DELETE FROM %I.merchant_fountain_withdrawals fw' 40 ' USING %I.merchant_token_families tf' 41 ' WHERE tf.token_family_serial = fw.token_family_serial' 42 ' AND tf.duration < $1 - fw.slot_start', s, s) 43 USING in_now; 44 -- Keep the complete response until the last issue key of that 45 -- withdrawal expires. Collecting individual signatures would leave 46 -- an incomplete response for requests spanning multiple key periods. 47 EXECUTE format('DELETE FROM %I.merchant_fountain_withdraw_sigs fws' 48 ' USING (SELECT saved.fountain_serial, saved.h_request' 49 ' FROM %I.merchant_fountain_withdraw_sigs saved' 50 ' GROUP BY saved.fountain_serial, saved.h_request' 51 ' HAVING max(saved.signature_validity_end) < $1) expired' 52 ' WHERE fws.fountain_serial = expired.fountain_serial' 53 ' AND fws.h_request = expired.h_request', s, s) 54 USING in_now; 55 EXCEPTION 56 WHEN undefined_table THEN 57 NULL; 58 WHEN undefined_function THEN 59 NULL; 60 WHEN OTHERS THEN 61 -- Do not abandon garbage collection for all remaining instances 62 -- (and the global tables) just because one instance failed. 63 RAISE WARNING 'Garbage collection failed for %: % (%)', 64 s, SQLERRM, SQLSTATE; 65 END; 66 COMMIT; 67 END LOOP; 68 DELETE FROM merchant.tan_challenges 69 WHERE expiration_date < $1; 70 -- Restore the global search_path so subsequent statements on this 71 -- session don't leak into the last visited instance schema. 72 SET LOCAL search_path TO merchant; 73 END $$; 74 COMMENT ON PROCEDURE merchant_do_gc 75 IS 'Calls per-instance garbage collection subroutines across every instance.' 76 ' For each' 77 ' instance runs merchant_statistic_*_gc and DELETEs expired tan_challenges' 78 ' / merchant_unclaim_signatures.'; 79 -- Note: deliberately no COMMIT here. gen-procedures.sh wraps the entire 80 -- concatenation of sql_global_procedures in a single BEGIN ... COMMIT; 81 -- a COMMIT in this file would close that transaction early, so everything 82 -- appended after this file would be installed outside of it (and the 83 -- generator's own trailing COMMIT would warn 'there is no transaction in 84 -- progress'). The COMMIT inside the procedure body above is a plpgsql 85 -- transaction-control statement and is unrelated.