-- LICENSE AND COPYRIGHT
--
-- Copyright (C) 2010 Hubert depesz Lubaczewski
--
-- This program is distributed under the (Revised) BSD License:
-- L<http://www.opensource.org/licenses/bsd-license.php>
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions
-- are met:
--
-- * Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
--
-- * Redistributions in binary form must reproduce the above copyright
--   notice, this list of conditions and the following disclaimer in the
--   documentation and/or other materials provided with the distribution.
--
-- * Neither the name of Hubert depesz Lubaczewski's Organization
--   nor the names of its contributors may be used to endorse or
--   promote products derived from this software without specific
--   prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--
-- Code origin: https://gitlab.com/depesz/Versioning/blob/master/install.versioning.sql
--
--
-- # NAME
--
-- **Versioning** - simplistic take on tracking and applying changes to databases.
--
-- # DESCRIPTION
--
-- This project strives to provide simple way to manage changes to
-- database.
--
-- Instead of making changes on development server, then finding
-- differences between production and development, deciding which ones
-- should be installed on production, and finding a way to install them -
-- you start with writing diffs themselves!
--
-- # INSTALLATION
--
-- To install versioning simply run install.versioning.sql in your database
-- (all of them: production, stage, test, devel, ...).
--
-- # USAGE
--
-- In your files with patches to database, put whole logic in single
-- transaction, and use \_v.\* functions - usually \_v.register_patch() at
-- least to make sure everything is OK.
--
-- For example. Let's assume you have patch files:
--
-- ## 0001.sql:
--
-- ```
-- create table users (id serial primary key, username text);
-- ```
--
-- ## 0002.sql:
--
-- ```
-- insert into users (username) values ('depesz');
-- ```
-- To change it to use versioning you would change the files, to this
-- state:
--
-- 0000.sql:
--
-- ```
-- BEGIN;
-- select _v.register_patch('000-base', NULL, NULL);
-- create table users (id serial primary key, username text);
-- COMMIT;
-- ```
--
-- ## 0002.sql:
--
-- ```
-- BEGIN;
-- select _v.register_patch('001-users', ARRAY['000-base'], NULL);
-- insert into users (username) values ('depesz');
-- COMMIT;
-- ```
--
-- This will make sure that patch 001-users can only be applied after
-- 000-base.
--
-- # AVAILABLE FUNCTIONS
--
-- ## \_v.register_patch( TEXT )
--
-- Registers named patch, or dies if it is already registered.
--
-- Returns integer which is id of patch in \_v.patches table - only if it
-- succeeded.
--
-- ## \_v.register_patch( TEXT, TEXT[] )
--
-- Same as \_v.register_patch( TEXT ), but checks is all given patches (given as
-- array in second argument) are already registered.
--
-- ## \_v.register_patch( TEXT, TEXT[], TEXT[] )
--
-- Same as \_v.register_patch( TEXT, TEXT[] ), but also checks if there are no conflicts with preexisting patches.
--
-- Third argument is array of names of patches that conflict with current one. So
-- if any of them is installed - register_patch will error out.
--
-- ## \_v.unregister_patch( TEXT )
--
-- Removes information about given patch from the versioning data.
--
-- It doesn't remove objects that were created by this patch - just removes
-- metainformation.
--
-- ## \_v.assert_user_is_superuser()
--
-- Make sure that current patch is being loaded by superuser.
--
-- If it's not - it will raise exception, and break transaction.
--
-- ## \_v.assert_user_is_not_superuser()
--
-- Make sure that current patch is not being loaded by superuser.
--
-- If it is - it will raise exception, and break transaction.
--
-- ## \_v.assert_user_is_one_of(TEXT, TEXT, ... )
--
-- Make sure that current patch is being loaded by one of listed users.
--
-- If ```current_user``` is not listed as one of arguments - function will raise
-- exception and break the transaction.

BEGIN;


-- This file adds versioning support to database it will be loaded to.
-- It requires that PL/pgSQL is already loaded - will raise exception otherwise.
-- All versioning "stuff" (tables, functions) is in "_v" schema.

-- All functions are defined as 'RETURNS SETOF INT4' to be able to make them to RETURN literally nothing (0 rows).
-- >> RETURNS VOID<< IS similar, but it still outputs "empty line" in psql when calling
CREATE SCHEMA IF NOT EXISTS _v;
COMMENT ON SCHEMA _v IS 'Schema for versioning data and functionality.';

CREATE TABLE IF NOT EXISTS _v.patches (
    patch_name  TEXT        PRIMARY KEY,
    applied_tsz TIMESTAMPTZ NOT NULL DEFAULT now(),
    applied_by  TEXT        NOT NULL,
    requires    TEXT[],
    conflicts   TEXT[]
);
COMMENT ON TABLE _v.patches              IS 'Contains information about what patches are currently applied on database.';
COMMENT ON COLUMN _v.patches.patch_name  IS 'Name of patch, has to be unique for every patch.';
COMMENT ON COLUMN _v.patches.applied_tsz IS 'When the patch was applied.';
COMMENT ON COLUMN _v.patches.applied_by  IS 'Who applied this patch (PostgreSQL username)';
COMMENT ON COLUMN _v.patches.requires    IS 'List of patches that are required for given patch.';
COMMENT ON COLUMN _v.patches.conflicts   IS 'List of patches that conflict with given patch.';

CREATE OR REPLACE FUNCTION _v.register_patch( IN in_patch_name TEXT, IN in_requirements TEXT[], in_conflicts TEXT[], OUT versioning INT4 ) RETURNS setof INT4 AS $$
DECLARE
    t_text   TEXT;
    t_text_a TEXT[];
    i INT4;
BEGIN
    -- Thanks to this we know only one patch will be applied at a time
    LOCK TABLE _v.patches IN EXCLUSIVE MODE;

    SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name;
    IF FOUND THEN
        RAISE EXCEPTION 'Patch % is already applied!', in_patch_name;
    END IF;

    t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE patch_name = any( in_conflicts ) );
    IF array_upper( t_text_a, 1 ) IS NOT NULL THEN
        RAISE EXCEPTION 'Versioning patches conflict. Conflicting patche(s) installed: %.', array_to_string( t_text_a, ', ' );
    END IF;

    IF array_upper( in_requirements, 1 ) IS NOT NULL THEN
        t_text_a := '{}';
        FOR i IN array_lower( in_requirements, 1 ) .. array_upper( in_requirements, 1 ) LOOP
            SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_requirements[i];
            IF NOT FOUND THEN
                t_text_a := t_text_a || in_requirements[i];
            END IF;
        END LOOP;
        IF array_upper( t_text_a, 1 ) IS NOT NULL THEN
            RAISE EXCEPTION 'Missing prerequisite(s): %.', array_to_string( t_text_a, ', ' );
        END IF;
    END IF;

    INSERT INTO _v.patches (patch_name, applied_tsz, applied_by, requires, conflicts ) VALUES ( in_patch_name, now(), current_user, coalesce( in_requirements, '{}' ), coalesce( in_conflicts, '{}' ) );
    RETURN;
END;
$$ language plpgsql;
COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[], TEXT[] ) IS 'Function to register patches in database. Raises exception if there are conflicts, prerequisites are not installed or the migration has already been installed.';

CREATE OR REPLACE FUNCTION _v.register_patch( TEXT, TEXT[] ) RETURNS setof INT4 AS $$
    SELECT _v.register_patch( $1, $2, NULL );
$$ language sql;
COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[] ) IS 'Wrapper to allow registration of patches without conflicts.';
CREATE OR REPLACE FUNCTION _v.register_patch( TEXT ) RETURNS setof INT4 AS $$
    SELECT _v.register_patch( $1, NULL, NULL );
$$ language sql;
COMMENT ON FUNCTION _v.register_patch( TEXT ) IS 'Wrapper to allow registration of patches without requirements and conflicts.';

CREATE OR REPLACE FUNCTION _v.unregister_patch( IN in_patch_name TEXT, OUT versioning INT4 ) RETURNS setof INT4 AS $$
DECLARE
    i        INT4;
    t_text_a TEXT[];
BEGIN
    -- Thanks to this we know only one patch will be applied at a time
    LOCK TABLE _v.patches IN EXCLUSIVE MODE;

    t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE in_patch_name = ANY( requires ) );
    IF array_upper( t_text_a, 1 ) IS NOT NULL THEN
        RAISE EXCEPTION 'Cannot uninstall %, as it is required by: %.', in_patch_name, array_to_string( t_text_a, ', ' );
    END IF;

    DELETE FROM _v.patches WHERE patch_name = in_patch_name;
    GET DIAGNOSTICS i = ROW_COUNT;
    IF i < 1 THEN
        RAISE EXCEPTION 'Patch % is not installed, so it can''t be uninstalled!', in_patch_name;
    END IF;

    RETURN;
END;
$$ language plpgsql;
COMMENT ON FUNCTION _v.unregister_patch( TEXT ) IS 'Function to unregister patches in database. Dies if the patch is not registered, or if unregistering it would break dependencies.';

CREATE OR REPLACE FUNCTION _v.assert_patch_is_applied( IN in_patch_name TEXT ) RETURNS TEXT as $$
DECLARE
    t_text TEXT;
BEGIN
    SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = in_patch_name;
    IF NOT FOUND THEN
        RAISE EXCEPTION 'Patch % is not applied!', in_patch_name;
    END IF;
    RETURN format('Patch %s is applied.', in_patch_name);
END;
$$ language plpgsql;
COMMENT ON FUNCTION _v.assert_patch_is_applied( TEXT ) IS 'Function that can be used to make sure that patch has been applied.';

CREATE OR REPLACE FUNCTION _v.assert_user_is_superuser() RETURNS TEXT as $$
DECLARE
    v_super bool;
BEGIN
    SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user;
    IF v_super THEN
        RETURN 'assert_user_is_superuser: OK';
    END IF;
    RAISE EXCEPTION 'Current user is not superuser - cannot continue.';
END;
$$ language plpgsql;
COMMENT ON FUNCTION _v.assert_user_is_superuser() IS 'Function that can be used to make sure that patch is being applied using superuser account.';

CREATE OR REPLACE FUNCTION _v.assert_user_is_not_superuser() RETURNS TEXT as $$
DECLARE
    v_super bool;
BEGIN
    SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user;
    IF v_super THEN
        RAISE EXCEPTION 'Current user is superuser - cannot continue.';
    END IF;
    RETURN 'assert_user_is_not_superuser: OK';
END;
$$ language plpgsql;
COMMENT ON FUNCTION _v.assert_user_is_not_superuser() IS 'Function that can be used to make sure that patch is being applied using normal (not superuser) account.';

CREATE OR REPLACE FUNCTION _v.assert_user_is_one_of(VARIADIC p_acceptable_users TEXT[] ) RETURNS TEXT as $$
DECLARE
BEGIN
    IF current_user = any( p_acceptable_users ) THEN
        RETURN 'assert_user_is_one_of: OK';
    END IF;
    RAISE EXCEPTION 'User is not one of: % - cannot continue.', p_acceptable_users;
END;
$$ language plpgsql;
COMMENT ON FUNCTION _v.assert_user_is_one_of(TEXT[]) IS 'Function that can be used to make sure that patch is being applied by one of defined users.';

COMMIT;
--
-- This file is part of TALER
-- Copyright (C) 2014--2022 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
--

BEGIN;

SELECT _v.register_patch('exchange-0001', NULL, NULL);

CREATE SCHEMA exchange;
COMMENT ON SCHEMA exchange IS 'taler-exchange data';

SET search_path TO exchange;

---------------------------------------------------------------------------
--                   General procedures for DB setup
---------------------------------------------------------------------------

CREATE TABLE exchange_tables
  (table_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY
  ,name TEXT NOT NULL
  ,version TEXT NOT NULL
  ,action TEXT NOT NULL
  ,partitioned BOOL NOT NULL
  ,by_range BOOL NOT NULL
  ,finished BOOL NOT NULL DEFAULT(FALSE));
COMMENT ON TABLE exchange_tables
  IS 'Tables of the exchange and their status';
COMMENT ON COLUMN exchange_tables.name
  IS 'Base name of the table (without partition/shard)';
COMMENT ON COLUMN exchange_tables.version
  IS 'Version of the DB in which the given action happened';
COMMENT ON COLUMN exchange_tables.action
  IS 'Action to take on the table (e.g. create, alter, constrain, or foreign). Create is done for the master table and each partition; constrain is only for partitions or for master if there are no partitions; master only on master (takes no argument); foreign only on master if there are no partitions.';
COMMENT ON COLUMN exchange_tables.partitioned
  IS 'TRUE if the table is partitioned';
COMMENT ON COLUMN exchange_tables.by_range
  IS 'TRUE if the table is partitioned by range';
COMMENT ON COLUMN exchange_tables.finished
  IS 'TRUE if the respective migration has been run';

CREATE INDEX exchange_tables_by_pending
  ON exchange_tables (table_serial_id)
  WHERE NOT finished;
COMMENT ON INDEX exchange_tables_by_pending
  IS 'Used by exchange_do_create_tables';



COMMIT;
BEGIN;
SELECT _v.register_patch('exchange-0002', NULL, NULL);
SET search_path TO exchange;
CREATE DOMAIN gnunet_hashcode
  AS BYTEA
  CHECK(LENGTH(VALUE) = 32);
CREATE TYPE taler_amount
  AS
  (val INT8
  ,frac INT4
  );
COMMENT ON TYPE taler_amount
  IS 'Stores an amount, fraction is in units of 1/100000000 of the base value';
CREATE TYPE exchange_do_array_reserve_insert_return_type
  AS
  (transaction_duplicate BOOLEAN
  ,ruuid INT8
  );
COMMENT ON TYPE exchange_do_array_reserve_insert_return_type
  IS 'Return type for exchange_do_array_reserves_insert() stored procedure';
CREATE TYPE exchange_do_select_deposits_missing_wire_return_type
  AS
  (
    batch_deposit_serial_id INT8,
    total_amount taler_amount,
    wire_target_h_payto BYTEA,
    deadline INT8
  );
COMMENT ON TYPE exchange_do_select_deposits_missing_wire_return_type
  IS 'Return type for exchange_do_select_deposits_missing_wire';
CREATE TYPE exchange_do_select_aggregations_above_serial_return_type
  AS
  (
    batch_deposit_serial_id INT8,
    aggregation_serial_id INT8,
    total_amount taler_amount
  );
COMMENT ON TYPE exchange_do_select_aggregations_above_serial_return_type
  IS 'Return type for exchange_do_select_aggregations_above_serial';
CREATE TABLE denominations
  (denominations_serial BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,denom_pub_hash BYTEA PRIMARY KEY CHECK (LENGTH(denom_pub_hash)=64)
  ,denom_type INT4 NOT NULL DEFAULT (1) 
  ,age_mask INT4 NOT NULL DEFAULT (0)
  ,denom_pub BYTEA NOT NULL
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  ,valid_from INT8 NOT NULL
  ,expire_withdraw INT8 NOT NULL
  ,expire_deposit INT8 NOT NULL
  ,expire_legal INT8 NOT NULL
  ,coin taler_amount NOT NULL
  ,fee_withdraw taler_amount NOT NULL
  ,fee_deposit taler_amount NOT NULL
  ,fee_refresh taler_amount NOT NULL
  ,fee_refund taler_amount NOT NULL
  );
COMMENT ON TABLE denominations
  IS 'Main denominations table. All the valid denominations the exchange knows about.';
COMMENT ON COLUMN denominations.denom_type
  IS 'determines cipher type for blind signatures used with this denomination; 0 is for RSA';
COMMENT ON COLUMN denominations.age_mask
  IS 'bitmask with the age restrictions that are being used for this denomination; 0 if denomination does not support the use of age restrictions';
COMMENT ON COLUMN denominations.denominations_serial
  IS 'needed for exchange-auditor replication logic';
CREATE INDEX denominations_by_expire_legal_index
  ON denominations
  (expire_legal);
CREATE TABLE IF NOT EXISTS denomination_revocations
  (denom_revocations_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,denominations_serial INT8 PRIMARY KEY REFERENCES denominations (denominations_serial) ON DELETE CASCADE
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  );
COMMENT ON TABLE denomination_revocations
  IS 'remembering which denomination keys have been revoked';
CREATE OR REPLACE FUNCTION random_bytea(
  bytea_length INT
)
RETURNS BYTEA
  AS $body$
  SELECT decode(string_agg(lpad(to_hex(width_bucket(random(), 0, 1, 256)-1),2,'0') ,''), 'hex')
    FROM generate_series(1, $1);
  $body$
LANGUAGE 'sql'
VOLATILE;
CREATE FUNCTION create_table_wire_targets(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(wire_target_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',wire_target_h_payto BYTEA PRIMARY KEY CHECK (LENGTH(wire_target_h_payto)=32)'
      ',payto_uri TEXT NOT NULL'
      ',access_token BYTEA CHECK(LENGTH(access_token)=32)'
      ' DEFAULT random_bytea(32)'
      ',target_pub BYTEA CHECK(LENGTH(target_pub)=32) DEFAULT NULL'
      ',h_normalized_payto BYTEA CHECK(LENGTH(h_normalized_payto)=32) DEFAULT NULL'
      ',aml_program_lock_timeout INT8 DEFAULT NULL'
    ') %s ;'
    ,'wire_targets'
    ,'PARTITION BY HASH (wire_target_h_payto)'
    ,partition_suffix
);
  PERFORM comment_partitioned_table(
     'All senders and recipients of money via the exchange'
    ,'wire_targets'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Full payto URI. Can identify a regular bank account, or also be a URI identifying a reserve-account (for P2P payments)'
    ,'payto_uri'
    ,'wire_targets'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Unsalted hash of (full) payto_uri'
    ,'wire_target_h_payto'
    ,'wire_targets'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'high-entropy random value that is used as a bearer token used to authenticate access to the KYC SPA and its state (without requiring a signature)'
    ,'access_token'
    ,'wire_targets'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'Public key of a merchant instance or reserve to authenticate access; NULL if KYC is not allowed for the account (if there was no incoming KYC wire transfer yet); updated, thus NOT available to the auditor'
    ,'target_pub'
    ,'wire_targets'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'hash over the normalized payto URI for this account; used for KYC operations; NULL if not available (due to DB migration not initializing this value)'
    ,'h_normalized_payto'
    ,'wire_targets'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'If non-NULL, an AML program should be running and it holds a lock on this account, thus other AML programs should not be started concurrently. Given the possibility of crashes, the lock automatically expires at the time value given in this column. At that time, the lock can be considered stale.'
    ,'aml_program_lock_timeout'
    ,'wire_targets'
    ,NULL
  );
END $$;
CREATE FUNCTION constrain_table_wire_targets(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wire_targets';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wire_target_serial_id_key'
    ' UNIQUE (wire_target_serial_id)'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wire_target_access_token_unique'
    ' UNIQUE (access_token)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_normalized_h_payto_index '
    'ON ' || table_name || ' '
    '(h_normalized_payto);'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wire_targets'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('wire_targets'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE TABLE kyc_alerts
  (h_payto BYTEA PRIMARY KEY CHECK (LENGTH(h_payto)=32)
  ,trigger_type INT4 NOT NULL
  ,UNIQUE(trigger_type,h_payto)
  );
COMMENT ON TABLE kyc_alerts
  IS 'alerts about completed KYC events reliably notifying other components (even if they are not running)';
COMMENT ON COLUMN kyc_alerts.h_payto
  IS 'hash of the normalized payto://-URI for which the KYC status changed';
COMMENT ON COLUMN kyc_alerts.trigger_type
  IS 'identifies the receiver of the alert, as the same h_payto may require multiple components to be notified';
CREATE TABLE wire_fee
  (wire_fee_serial BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,wire_method TEXT NOT NULL
  ,start_date INT8 NOT NULL
  ,end_date INT8 NOT NULL
  ,wire_fee taler_amount NOT NULL
  ,closing_fee taler_amount NOT NULL
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  ,PRIMARY KEY (wire_method, start_date)
  );
COMMENT ON TABLE wire_fee
  IS 'list of the wire fees of this exchange, by date';
COMMENT ON COLUMN wire_fee.wire_fee_serial
  IS 'needed for exchange-auditor replication logic';
CREATE INDEX wire_fee_by_end_date_index
  ON wire_fee
  (end_date);
CREATE TABLE global_fee
  (global_fee_serial BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,start_date INT8 NOT NULL
  ,end_date INT8 NOT NULL
  ,history_fee taler_amount NOT NULL
  ,account_fee taler_amount NOT NULL
  ,purse_fee taler_amount NOT NULL
  ,purse_timeout INT8 NOT NULL
  ,history_expiration INT8 NOT NULL
  ,purse_account_limit INT4 NOT NULL
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  ,PRIMARY KEY (start_date)
  );
COMMENT ON TABLE global_fee
  IS 'list of the global fees of this exchange, by date';
COMMENT ON COLUMN global_fee.global_fee_serial
  IS 'needed for exchange-auditor replication logic';
CREATE INDEX global_fee_by_end_date_index
  ON global_fee
  (end_date);
CREATE TABLE wire_accounts
  (payto_uri TEXT PRIMARY KEY
  ,master_sig BYTEA CHECK (LENGTH(master_sig)=64)
  ,is_active BOOLEAN NOT NULL
  ,last_change INT8 NOT NULL
  ,conversion_url TEXT DEFAULT (NULL)
  ,debit_restrictions TEXT DEFAULT (NULL)
  ,credit_restrictions TEXT DEFAULT (NULL)
  ,priority INT8 NOT NULL DEFAULT (0)
  ,bank_label TEXT DEFAULT (NULL)
  );
COMMENT ON TABLE wire_accounts
  IS 'Table with current and historic bank accounts of the exchange. Entries never expire as we need to remember the last_change column indefinitely.';
COMMENT ON COLUMN wire_accounts.payto_uri
  IS 'payto URI (RFC 8905) with the bank account of the exchange.';
COMMENT ON COLUMN wire_accounts.master_sig
  IS 'Signature of purpose TALER_SIGNATURE_MASTER_WIRE_DETAILS';
COMMENT ON COLUMN wire_accounts.is_active
  IS 'true if we are currently supporting the use of this account.';
COMMENT ON COLUMN wire_accounts.last_change
  IS 'Latest time when active status changed. Used to detect replays of old messages.';
COMMENT ON COLUMN wire_accounts.conversion_url
  IS 'URL of a currency conversion service if conversion is needed when this account is used; NULL if there is no conversion.';
COMMENT ON COLUMN wire_accounts.debit_restrictions
  IS 'JSON array describing restrictions imposed when debiting this account. Empty for no restrictions, NULL if account was migrated from previous database revision or account is disabled.';
COMMENT ON COLUMN wire_accounts.credit_restrictions
  IS 'JSON array describing restrictions imposed when crediting this account. Empty for no restrictions, NULL if account was migrated from previous database revision or account is disabled.';
COMMENT ON COLUMN wire_accounts.priority
  IS 'priority determines the order in which wallets should display wire accounts';
COMMENT ON COLUMN wire_accounts.bank_label
  IS 'label to show in the selector for this bank account in the wallet UI';
CREATE TABLE auditors
  (auditor_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,auditor_pub BYTEA PRIMARY KEY CHECK (LENGTH(auditor_pub)=32)
  ,auditor_name TEXT NOT NULL
  ,auditor_url TEXT NOT NULL
  ,is_active BOOLEAN NOT NULL
  ,last_change INT8 NOT NULL
  );
COMMENT ON TABLE auditors
  IS 'Table with auditors the exchange uses or has used in the past. Entries never expire as we need to remember the last_change column indefinitely.';
COMMENT ON COLUMN auditors.auditor_pub
  IS 'Public key of the auditor.';
COMMENT ON COLUMN auditors.auditor_url
  IS 'The base URL of the auditor.';
COMMENT ON COLUMN auditors.is_active
  IS 'true if we are currently supporting the use of this auditor.';
COMMENT ON COLUMN auditors.last_change
  IS 'Latest time when active status changed. Used to detect replays of old messages.';
CREATE TABLE auditor_denom_sigs
  (auditor_denom_serial BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,auditor_uuid INT8 NOT NULL REFERENCES auditors (auditor_uuid) ON DELETE CASCADE
  ,denominations_serial INT8 NOT NULL REFERENCES denominations (denominations_serial) ON DELETE CASCADE
  ,auditor_sig BYTEA CHECK (LENGTH(auditor_sig)=64)
  ,PRIMARY KEY (denominations_serial, auditor_uuid)
  );
COMMENT ON TABLE auditor_denom_sigs
  IS 'Table with auditor signatures on exchange denomination keys.';
COMMENT ON COLUMN auditor_denom_sigs.auditor_uuid
  IS 'Identifies the auditor.';
COMMENT ON COLUMN auditor_denom_sigs.denominations_serial
  IS 'Denomination the signature is for.';
COMMENT ON COLUMN auditor_denom_sigs.auditor_sig
  IS 'Signature of the auditor, of purpose TALER_SIGNATURE_AUDITOR_EXCHANGE_KEYS.';
CREATE TABLE exchange_sign_keys
  (esk_serial BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,exchange_pub BYTEA PRIMARY KEY CHECK (LENGTH(exchange_pub)=32)
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  ,valid_from INT8 NOT NULL
  ,expire_sign INT8 NOT NULL
  ,expire_legal INT8 NOT NULL
  );
COMMENT ON TABLE exchange_sign_keys
  IS 'Table with master public key signatures on exchange online signing keys.';
COMMENT ON COLUMN exchange_sign_keys.exchange_pub
  IS 'Public online signing key of the exchange.';
COMMENT ON COLUMN exchange_sign_keys.master_sig
  IS 'Signature affirming the validity of the signing key of purpose TALER_SIGNATURE_MASTER_SIGNING_KEY_VALIDITY.';
COMMENT ON COLUMN exchange_sign_keys.valid_from
  IS 'Time when this online signing key will first be used to sign messages.';
COMMENT ON COLUMN exchange_sign_keys.expire_sign
  IS 'Time when this online signing key will no longer be used to sign.';
COMMENT ON COLUMN exchange_sign_keys.expire_legal
  IS 'Time when this online signing key legally expires.';
CREATE TABLE signkey_revocations
  (signkey_revocations_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,esk_serial INT8 PRIMARY KEY REFERENCES exchange_sign_keys (esk_serial) ON DELETE CASCADE
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  );
COMMENT ON TABLE signkey_revocations
  IS 'Table storing which online signing keys have been revoked';
CREATE TABLE extensions
  (extension_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,name TEXT NOT NULL UNIQUE
  ,manifest BYTEA
  );
COMMENT ON TABLE extensions
  IS 'Configurations of the activated extensions';
COMMENT ON COLUMN extensions.name
  IS 'Name of the extension';
COMMENT ON COLUMN extensions.manifest
  IS 'Manifest of the extension as JSON-blob, maybe NULL.  It contains common meta-information and extension-specific configuration.';
CREATE FUNCTION create_table_policy_fulfillments(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'policy_fulfillments';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(h_fulfillment_proof gnunet_hashcode PRIMARY KEY'
      ',fulfillment_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',fulfillment_timestamp INT8 NOT NULL'
      ',fulfillment_proof TEXT'
      ',policy_hash_codes gnunet_hashcode[] NOT NULL'
      ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (h_fulfillment_proof)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'Proofs of fulfillment of policies that were set in deposits'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'Timestamp of the arrival of a proof of fulfillment'
    ,'fulfillment_timestamp'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'JSON object with a proof of the fulfillment of a policy. Supported details depend on the policy extensions supported by the exchange.'
    ,'fulfillment_proof'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'Hash of the fulfillment_proof'
    ,'h_fulfillment_proof'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'Array of the policy_hash_code''s of all policy_details that are fulfilled by this proof'
    ,'policy_hash_codes'
    ,table_name
    ,partition_suffix
  );
END
$$;
COMMENT ON FUNCTION create_table_policy_fulfillments
  IS 'Creates the policy_fulfillments table';
CREATE FUNCTION constrain_table_policy_fulfillments(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  partition_name TEXT;
BEGIN
  partition_name = concat_ws('_', 'policy_fulfillments', partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || partition_name ||
    ' ADD CONSTRAINT ' || partition_name || '_serial_id '
    ' UNIQUE (h_fulfillment_proof, fulfillment_id)'
  );
END
$$;
INSERT INTO exchange_tables
  (name
  ,version
  ,action
  ,partitioned
  ,by_range)
VALUES
  ('policy_fulfillments', 'exchange-0002', 'create', TRUE ,FALSE),
  ('policy_fulfillments', 'exchange-0002', 'constrain', TRUE ,FALSE);
CREATE FUNCTION create_table_policy_details(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'policy_details';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(policy_details_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',policy_hash_code gnunet_hashcode NOT NULL'
      ',policy_json TEXT NOT NULL'
      ',deadline INT8 NOT NULL'
      ',commitment taler_amount NOT NULL'
      ',accumulated_total taler_amount NOT NULL'
      ',fee taler_amount NOT NULL'
      ',transferable taler_amount NOT NULL'
      ',fulfillment_state SMALLINT NOT NULL CHECK(fulfillment_state between 0 and 5)'
      ',h_fulfillment_proof gnunet_hashcode'
    ') %s;'
    ,table_name
    ,'PARTITION BY HASH (h_fulfillment_proof)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'Policies that were provided with deposits via policy extensions.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'ID (GNUNET_HashCode) that identifies a policy.  Will be calculated by the policy extension based on the content'
    ,'policy_hash_code'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'JSON object with options set that the exchange needs to consider when executing a deposit. Supported details depend on the policy extensions supported by the exchange.'
    ,'policy_json'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'Deadline until the policy must be marked as fulfilled (maybe "forever")'
    ,'deadline'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'The amount that this policy commits to.  Invariant: commitment >= fee'
    ,'commitment'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'The sum of all contributions of all deposit that reference this policy.  Invariant: The fulfilment_state must be Insufficient as long as accumulated_total < commitment'
    ,'accumulated_total'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'The fee for this policy, due when the policy is fulfilled or timed out'
    ,'fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'The amount that on fulfillment or timeout will be transferred to the payto-URI''s of the corresponding deposit''s.  The policy fees must have been already deducted from it.  Invariant: fee+transferable <= accumulated_total.  The remaining amount (accumulated_total - fee - transferable) can be refreshed by the owner of the coins when the state is Timeout or Success.'
    ,'transferable'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'State of the fulfillment:
       - 0 (Failure)
       - 1 (Insufficient)
       - 2 (Ready)
       - 4 (Success)
       - 5 (Timeout)'
    ,'fulfillment_state'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'Reference to the proof of the fulfillment of this policy, if it exists.  Invariant: If not NULL, this entry''s .hash_code MUST be part of the corresponding policy_fulfillments.policy_hash_codes array.'
    ,'h_fulfillment_proof'
    ,table_name
    ,partition_suffix
  );
END
$$;
COMMENT ON FUNCTION create_table_policy_details
  IS 'Creates the policy_details table';
CREATE FUNCTION constrain_table_policy_details(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  partition_name TEXT;
BEGIN
  partition_name = concat_ws('_', 'policy_details', partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || partition_name ||
    ' ADD CONSTRAINT ' || partition_name || '_unique_serial_id '
    ' UNIQUE (policy_details_serial_id)'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || partition_name ||
    ' ADD CONSTRAINT ' || partition_name || '_unique_hash_fulfillment_proof '
    ' UNIQUE (policy_hash_code, h_fulfillment_proof)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || partition_name || '_policy_hash_code'
    ' ON ' || partition_name ||
    ' (policy_hash_code);'
  );
END
$$;
CREATE OR REPLACE FUNCTION foreign_table_policy_details()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'policy_details';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_policy_fulfillments'
    ' FOREIGN KEY (h_fulfillment_proof) '
    ' REFERENCES policy_fulfillments (h_fulfillment_proof) ON DELETE RESTRICT'
  );
END
$$;
INSERT INTO exchange_tables
  (name
  ,version
  ,action
  ,partitioned
  ,by_range)
VALUES
  ('policy_details', 'exchange-0002', 'create', TRUE ,FALSE),
  ('policy_details', 'exchange-0002', 'constrain', TRUE ,FALSE),
  ('policy_details', 'exchange-0002', 'foreign', TRUE ,FALSE);
CREATE TABLE profit_drains
  (profit_drain_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,wtid BYTEA PRIMARY KEY CHECK (LENGTH(wtid)=32)
  ,account_section TEXT NOT NULL
  ,payto_uri TEXT NOT NULL
  ,trigger_date INT8 NOT NULL
  ,amount taler_amount NOT NULL
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  ,executed BOOLEAN NOT NULL DEFAULT FALSE
  );
COMMENT ON TABLE profit_drains
  IS 'transactions to be performed to move profits from the escrow account of the exchange to a regular account';
COMMENT ON COLUMN profit_drains.wtid
  IS 'randomly chosen nonce, unique to prevent double-submission';
COMMENT ON COLUMN profit_drains.account_section
  IS 'specifies the configuration section in the taler-exchange-drain configuration with the wire account to drain';
COMMENT ON COLUMN profit_drains.payto_uri
  IS 'specifies the account to be credited';
COMMENT ON COLUMN profit_drains.trigger_date
  IS 'set by taler-exchange-offline at the time of making the signature; not necessarily the exact date of execution of the wire transfer, just for orientation';
COMMENT ON COLUMN profit_drains.amount
  IS 'amount to be transferred';
COMMENT ON COLUMN profit_drains.master_sig
  IS 'EdDSA signature of type TALER_SIGNATURE_MASTER_DRAIN_PROFIT';
COMMENT ON COLUMN profit_drains.executed
  IS 'set to TRUE by taler-exchange-drain on execution of the transaction, not replicated to auditor';
CREATE FUNCTION create_table_legitimization_measures(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(legitimization_measure_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY'
      ',access_token BYTEA NOT NULL CHECK (LENGTH(access_token)=32)'
      ',start_time INT8 NOT NULL'
      ',jmeasures TEXT NOT NULL'
      ',display_priority INT4 NOT NULL' 
      ',is_finished BOOL NOT NULL DEFAULT(FALSE)'
    ') %s ;'
    ,'legitimization_measures'
    ,'PARTITION BY HASH (access_token)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'List of required legitimizations by account'
    ,'legitimization_measures'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'unique ID for this legitimization process at the exchange'
    ,'legitimization_measure_serial_id'
    ,'legitimization_measures'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'foreign key linking the entry to the kyc_targets table, NOT a primary key (multiple legitimizations are possible per account)'
    ,'access_token'
    ,'legitimization_measures'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Time when the measure was triggered (by decision or rule)'
    ,'start_time'
    ,'legitimization_measures'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'JSON object of type LegitimizationMeasures with KYC/AML measures for the account encoded'
    ,'jmeasures'
    ,'legitimization_measures'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Display priority of the rule that triggered this measure; if in the meantime another rule also triggers, the measure is only replaced if the new rule has a higher display priority; probably not really useful, as right now there is only ever one set of legitimization_measures active at any time, might be removed in the future'
    ,'display_priority'
    ,'legitimization_measures'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Set to TRUE if this set of measures was processed; used to avoid indexing measures that are done'
    ,'is_finished'
    ,'legitimization_measures'
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_legitimization_measures(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_measures';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_serial_id_key'
    ' UNIQUE (legitimization_measure_serial_id)');
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_access_token'
    ' ON ' || table_name ||
    ' (access_token)'
    ' WHERE NOT is_finished' ||
    ';'
  );
END
$$;
CREATE FUNCTION foreign_table_legitimization_measures()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_measures';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_key_access_token'
    ' FOREIGN KEY (access_token)'
    ' REFERENCES wire_targets (access_token)'
    '  ON DELETE CASCADE');
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('legitimization_measures'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('legitimization_measures'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('legitimization_measures'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_legitimization_outcomes(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(outcome_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',h_payto BYTEA NOT NULL CHECK (LENGTH(h_payto)=32)'
      ',decision_time INT8 NOT NULL'
      ',expiration_time INT8 NOT NULL'
      ',jproperties TEXT'
      ',new_measure_name TEXT'
      ',to_investigate BOOL NOT NULL'
      ',is_active BOOL NOT NULL DEFAULT(TRUE)'
      ',jnew_rules TEXT'
    ') %s ;'
    ,'legitimization_outcomes'
    ,'PARTITION BY HASH (h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Outcomes of legitimization processes by account'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'unique ID for this legitimization outcome at the exchange'
    ,'outcome_serial_id'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'hash of the normalized payto://-URI this outcome is about; foreign key linking the entry to the kyc_targets table, NOT a primary key (multiple outcomes are possible per account over time)'
    ,'h_payto'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'when was this outcome decided, rounded timestamp'
    ,'decision_time'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'space-separated list of names of measures to trigger immediately, NULL for none, prefixed with a "+" to indicate AND combination for the measures'
    ,'new_measure_name'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'time when the decision expires and the expiration jnew_rules should be applied'
    ,'expiration_time'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'JSON object of type AccountProperties, such as PEP status, business domain, risk assessment, etc.'
    ,'jproperties'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'AML staff should investigate the activity of this account'
    ,'to_investigate'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'TRUE if this is the current authoritative legitimization outcome'
    ,'is_active'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'JSON object of type LegitimizationRuleSet with rules to apply to the various operation types for this account; all KYC checks should first check if active new rules for a given account exist in this table (and apply specified measures); if not, it should check the default rules to decide if a measure is required; NULL if the default rules apply'
    ,'jnew_rules'
    ,'legitimization_outcomes'
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_legitimization_outcomes(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_outcomes';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_target_token'
    ' ON ' || table_name ||
    ' (h_payto)'
    ' WHERE is_active' ||
    ';'
  );
END
$$;
CREATE FUNCTION foreign_table_legitimization_outcomes()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_outcomes';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_serial_id_key'
    ' UNIQUE (outcome_serial_id)');
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('legitimization_outcomes'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('legitimization_outcomes'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('legitimization_outcomes'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_legitimization_processes(
  IN shard_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(legitimization_process_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',h_payto BYTEA NOT NULL CHECK (LENGTH(h_payto)=32)'
      ',start_time INT8 NOT NULL'
      ',expiration_time INT8 NOT NULL DEFAULT (0)'
      ',provider_name TEXT NOT NULL'
      ',provider_user_id TEXT DEFAULT NULL'
      ',provider_legitimization_id TEXT DEFAULT NULL'
      ',redirect_url TEXT DEFAULT NULL'
      ',finished BOOLEAN DEFAULT (FALSE)'
      ',legitimization_measure_serial_id BIGINT'
      ',measure_index INT4 DEFAULT(0)'
      ',error_code INT4 DEFAULT (0)'
      ',error_message TEXT DEFAULT NULL'
    ') %s ;'
    ,'legitimization_processes'
    ,'PARTITION BY HASH (h_payto)'
    ,shard_suffix
  );
  PERFORM comment_partitioned_table(
    'List of legitimization processes (ongoing and completed) by account and provider'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'unique ID for this legitimization process at the exchange'
    ,'legitimization_process_serial_id'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'hash over the normalized payto URI; foreign key linking the entry to the kyc_targets table, NOT a primary key (multiple legitimizations are possible per wire target)'
    ,'h_payto'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'time when the KYC check was initiated, useful for garbage collection (absolute time, not rounded)'
    ,'start_time'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'URL where the user should go to begin the KYC process'
    ,'redirect_url'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'in the future if the respective KYC check was passed successfully; an absolute time (not rounded)'
    ,'expiration_time'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Configuration file section with details about this provider'
    ,'provider_name'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifier for the user at the provider that was used for the legitimization. NULL if provider is unaware.'
    ,'provider_user_id'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifier for the specific legitimization process at the provider. NULL if legitimization was not started.'
    ,'provider_legitimization_id'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Set to TRUE when the specific legitimization process is finished.'
    ,'finished'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'measure that enabled this setup, NULL if client voluntarily initiated the process'
    ,'legitimization_measure_serial_id'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'index of the measure in legitimization_measures that was selected for this KYC setup; NULL if legitimization_measure_serial_id is NULL; enables determination of the context data provided to the external process'
    ,'measure_index'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'TALER_ErrorCode set if the process failed, otherwise NULL'
    ,'error_code'
    ,'legitimization_processes'
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'human-readable error details set if the process failed, otherwise NULL'
    ,'error_message'
    ,'legitimization_processes'
    ,shard_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_legitimization_processes(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  partition_name TEXT;
BEGIN
  partition_name = concat_ws('_', 'legitimization_processes', partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || partition_name
    || ' '
      'ADD CONSTRAINT ' || partition_name || '_serial_key '
        'UNIQUE (legitimization_process_serial_id)');
  EXECUTE FORMAT (
    'CREATE INDEX IF NOT EXISTS ' || partition_name || '_by_provider_and_legi_index '
        'ON '|| partition_name || ' '
        '(provider_name,provider_legitimization_id)'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || partition_name || '_by_provider_and_legi_index '
    'IS ' || quote_literal('used (rarely) in kyc_provider_account_lookup') || ';'
  );
END
$$;
CREATE FUNCTION foreign_table_legitimization_processes()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_processes';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_key_legitimization_measure'
    ' FOREIGN KEY (legitimization_measure_serial_id)'
    ' REFERENCES legitimization_measures (legitimization_measure_serial_id)');
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_unique_measure_and_index'
    ' UNIQUE (legitimization_measure_serial_id,measure_index)');
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('legitimization_processes'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('legitimization_processes'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('legitimization_processes'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_reserves(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'reserves';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(reserve_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA PRIMARY KEY CHECK(LENGTH(reserve_pub)=32)'
      ',current_balance taler_amount NOT NULL DEFAULT (0, 0)'
      ',purses_active INT8 NOT NULL DEFAULT(0)'
      ',purses_allowed INT8 NOT NULL DEFAULT(0)'
      ',birthday INT4 NOT NULL DEFAULT(0)'
      ',expiration_date INT8 NOT NULL'
      ',gc_date INT8 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Summarizes the balance of a reserve. Updated when new funds are added or withdrawn.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'EdDSA public key of the reserve. Knowledge of the private key implies ownership over the balance.'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Current balance remaining with the reserve.'
    ,'current_balance'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Number of purses that were created by this reserve that are not expired and not fully paid.'
    ,'purses_active'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Number of purses that this reserve is allowed to have active at most.'
    ,'purses_allowed'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Used to trigger closing of reserves that have not been drained after some time'
    ,'expiration_date'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Used to forget all information about a reserve during garbage collection'
    ,'gc_date'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Birthday of the user in days after 1970, or 0 if user is an adult and is not subject to age restrictions'
    ,'birthday'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_reserves(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'reserves';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_unique_uuid'
    ' UNIQUE (reserve_uuid)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_expiration_index '
    'ON ' || table_name || ' '
    '(expiration_date'
    ',current_balance'
    ');'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || table_name || '_by_expiration_index '
    'IS ' || quote_literal('used in get_expired_reserves') || ';'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_reserve_uuid_index '
    'ON ' || table_name || ' '
    '(reserve_uuid);'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_gc_date_index '
    'ON ' || table_name || ' '
    '(gc_date);'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || table_name || '_by_gc_date_index '
    'IS ' || quote_literal('for reserve garbage collection') || ';'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('reserves'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('reserves'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_reserve_history (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'reserve_history';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(reserve_history_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
      ',table_name TEXT NOT NULL'
      ',serial_id INT8 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'Links to tables with entries that affected the transaction history of a reserve.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'For which reserve is this a history entry'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'In which table is the history entry'
    ,'table_name'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Which is the generated serial ID of the entry in the table'
    ,'serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Monotonic counter, used to generate Etags for caching'
    ,'reserve_history_serial_id'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_reserve_history(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'reserve_history';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_reserve_history_serial_id_pkey'
    ' PRIMARY KEY (reserve_history_serial_id) '
    ',ADD CONSTRAINT ' || table_name || '_reserve_entry_key'
    ' UNIQUE (reserve_pub, table_name, serial_id)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_reserve_by_time'
    ' ON ' || table_name || ' '
    '(reserve_pub'
    ',reserve_history_serial_id DESC'
    ');'
  );
END
$$;
CREATE FUNCTION foreign_table_reserve_history()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'reserve_history';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
    ' FOREIGN KEY (reserve_pub) '
    ' REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('reserve_history'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('reserve_history'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('reserve_history'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE)
    ;
CREATE FUNCTION create_table_reserves_in(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_in';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(reserve_in_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA PRIMARY KEY'
      ',wire_reference INT8 NOT NULL'
      ',credit taler_amount NOT NULL'
      ',wire_source_h_payto BYTEA CHECK (LENGTH(wire_source_h_payto)=32)'
      ',exchange_account_section TEXT NOT NULL'
      ',execution_date INT8 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'list of transfers of funds into the reserves, one per incoming wire transfer'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies the debited bank account and KYC status'
    ,'wire_source_h_payto'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key of the reserve. Private key signifies ownership of the remaining balance.'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Amount that was transferred into the reserve'
    ,'credit'
    ,table_name
    ,partition_suffix
  );
END $$;
CREATE FUNCTION constrain_table_reserves_in(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_in';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_reserve_in_serial_id_key'
    ' UNIQUE (reserve_in_serial_id)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_reserve_in_serial_id_index '
    'ON ' || table_name || ' '
    '(reserve_in_serial_id);'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_exch_accnt_reserve_in_serial_id_idx '
    'ON ' || table_name || ' '
    '(exchange_account_section'
    ',reserve_in_serial_id ASC'
    ');'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || table_name || '_by_exch_accnt_reserve_in_serial_id_idx '
    'IS ' || quote_literal ('for pg_select_reserves_in_above_serial_id_by_account') || ';'
  );
END
$$;
CREATE FUNCTION foreign_table_reserves_in()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'reserves_in';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
    ' FOREIGN KEY (reserve_pub) '
    ' REFERENCES reserves(reserve_pub) ON DELETE CASCADE'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wire_target_h_payto_foreign'
    ' FOREIGN KEY (wire_source_h_payto)'
    ' REFERENCES wire_targets (wire_target_h_payto)'
    ' ON DELETE RESTRICT'
  );
END $$;
CREATE FUNCTION master_table_reserves_in()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER reserves_in_on_insert
    AFTER INSERT
     ON reserves_in
     FOR EACH ROW EXECUTE FUNCTION reserves_in_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('reserves_in'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('reserves_in'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('reserves_in'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('reserves_in'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_reserves_close(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_close';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(close_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA NOT NULL'
      ',execution_date INT8 NOT NULL'
      ',wtid BYTEA NOT NULL CHECK (LENGTH(wtid)=32)'
      ',wire_target_h_payto BYTEA CHECK (LENGTH(wire_target_h_payto)=32)'
      ',amount taler_amount NOT NULL'
      ',closing_fee taler_amount NOT NULL'
      ',close_request_row INT8 NOT NULL DEFAULT(0)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'wire transfers executed by the reserve to close reserves'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies the credited bank account (and KYC status). Note that closing does not depend on KYC.'
    ,'wire_target_h_payto'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_reserves_close(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_close';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_close_uuid_pkey'
    ' PRIMARY KEY (close_uuid)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_reserve_pub_index '
    'ON ' || table_name || ' (reserve_pub);'
  );
END $$;
CREATE FUNCTION foreign_table_reserves_close()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_close';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
    ' FOREIGN KEY (reserve_pub)'
    ' REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
  );
END $$;
CREATE OR REPLACE FUNCTION reserves_close_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO reserve_history
    (reserve_pub
    ,table_name
    ,serial_id)
  VALUES
    (NEW.reserve_pub
    ,'reserves_close'
    ,NEW.close_uuid);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION reserves_close_insert_trigger()
  IS 'Automatically generate reserve history entry.';
CREATE FUNCTION master_table_reserves_close()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER reserves_close_on_insert
    AFTER INSERT
     ON reserves_close
     FOR EACH ROW EXECUTE FUNCTION reserves_close_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('reserves_close'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('reserves_close'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('reserves_close'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('reserves_close'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_close_requests(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'close_requests';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(close_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
      ',close_timestamp INT8 NOT NULL'
      ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
      ',close taler_amount NOT NULL'
      ',close_fee taler_amount NOT NULL'
      ',payto_uri TEXT NOT NULL'
      ',done BOOL NOT NULL DEFAULT(FALSE)'
      ',PRIMARY KEY (reserve_pub,close_timestamp)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Explicit requests by a reserve owner to close a reserve immediately'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'When the request was created by the client'
    ,'close_timestamp'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature affirming that the reserve is to be closed'
    ,'reserve_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Balance of the reserve at the time of closing, to be wired to the associated bank account (minus the closing fee)'
    ,'close'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies the credited bank account. Optional.'
    ,'payto_uri'
    ,table_name
    ,partition_suffix
  );
END $$;
CREATE FUNCTION constrain_table_close_requests(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'close_requests';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_close_request_uuid_index '
    'ON ' || table_name || ' '
    '(close_request_serial_id);'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_close_request_done_index '
    'ON ' || table_name || ' '
    '(done);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_close_request_uuid_pkey'
    ' UNIQUE (close_request_serial_id)'
  );
END
$$;
CREATE FUNCTION foreign_table_close_requests()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'close_requests';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
    ' FOREIGN KEY (reserve_pub) '
    ' REFERENCES reserves(reserve_pub) ON DELETE CASCADE'
  );
END
$$;
CREATE OR REPLACE FUNCTION close_requests_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO reserve_history
    (reserve_pub
    ,table_name
    ,serial_id)
  VALUES
    (NEW.reserve_pub
    ,'close_requests'
    ,NEW.close_request_serial_id);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION close_requests_insert_trigger()
  IS 'Automatically generate reserve history entry.';
CREATE FUNCTION master_table_close_requests()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER close_requests_on_insert
    AFTER INSERT
     ON close_requests
     FOR EACH ROW EXECUTE FUNCTION close_requests_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('close_requests'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('close_requests'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('close_requests'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('close_requests'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_reserves_open_deposits(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_open_deposits';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(reserve_open_deposit_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
      ',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
      ',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)'
      ',coin_sig BYTEA NOT NULL CHECK (LENGTH(coin_sig)=64)'
      ',contribution taler_amount NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (coin_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'coin contributions paying for a reserve to remain open'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies the specific reserve being paid for (possibly together with reserve_sig).'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_reserves_open_deposits(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_open_deposits';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name || ' '
      'ADD CONSTRAINT ' || table_name || '_coin_unique '
        'PRIMARY KEY (coin_pub,coin_sig)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_uuid '
    'ON ' || table_name || ' '
    '(reserve_open_deposit_uuid);'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_reserve '
    'ON ' || table_name || ' '
    '(reserve_pub);'
  );
END
$$;
CREATE OR REPLACE FUNCTION reserves_open_deposits_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO exchange.coin_history
    (coin_pub
    ,table_name
    ,serial_id)
 VALUES
     (NEW.coin_pub
    ,'reserves_open_deposits'
    ,NEW.reserve_open_deposit_uuid);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION reserves_open_deposits_insert_trigger()
  IS 'Automatically generate coin history entry.';
CREATE FUNCTION master_table_reserves_open_deposits()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER reserves_open_deposits_on_insert
    AFTER INSERT
     ON reserves_open_deposits
     FOR EACH ROW EXECUTE FUNCTION reserves_open_deposits_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('reserves_open_deposits'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('reserves_open_deposits'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('reserves_open_deposits'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_reserves_open_requests(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_open_requests';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(open_request_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA NOT NULL'
      ',request_timestamp INT8 NOT NULL'
      ',expiration_date INT8 NOT NULL'
      ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
      ',reserve_payment taler_amount NOT NULL'
      ',requested_purse_limit INT4 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table (
     'requests to keep a reserve open'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column (
     'Fee to pay for the request from the reserve balance itself.'
    ,'reserve_payment'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_reserves_open_requests(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_open_requests';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_by_uuid'
    ' PRIMARY KEY (open_request_uuid)'
    ',ADD CONSTRAINT ' || table_name || '_by_time'
    ' UNIQUE (reserve_pub,request_timestamp)'
  );
END
$$;
CREATE FUNCTION foreign_table_reserves_open_requests()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'reserves_open_requests';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub '
    ' FOREIGN KEY (reserve_pub)'
    ' REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
  );
END
$$;
CREATE OR REPLACE FUNCTION reserves_open_requests_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO reserve_history
    (reserve_pub
    ,table_name
    ,serial_id)
  VALUES
    (NEW.reserve_pub
    ,'reserves_open_requests'
    ,NEW.open_request_uuid);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION reserves_open_requests_insert_trigger()
  IS 'Automatically generate reserve history entry.';
CREATE FUNCTION master_table_reserves_open_requests()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER reserves_open_requests_on_insert
    AFTER INSERT
     ON reserves_open_requests
     FOR EACH ROW EXECUTE FUNCTION reserves_open_requests_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('reserves_open_requests'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('reserves_open_requests'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('reserves_open_requests'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('reserves_open_requests'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_known_coins(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'known_coins';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(known_coin_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',denominations_serial INT8 NOT NULL'
      ',coin_pub BYTEA NOT NULL PRIMARY KEY CHECK (LENGTH(coin_pub)=32)'
      ',age_commitment_hash BYTEA CHECK (LENGTH(age_commitment_hash)=32)'
      ',denom_sig BYTEA NOT NULL'
      ',remaining taler_amount NOT NULL DEFAULT(0,0)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (coin_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'information about coins and their signatures, so we do not have to store the signatures more than once if a coin is involved in multiple operations'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Denomination of the coin, determines the value of the original coin and applicable fees for coin-specific operations.'
    ,'denominations_serial'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'EdDSA public key of the coin'
    ,'coin_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Value of the coin that remains to be spent'
    ,'remaining'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Optional hash of the age commitment for age restrictions as per DD 24 (active if denom_type has the respective bit set)'
    ,'age_commitment_hash'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'This is the signature of the exchange that affirms that the coin is a valid coin. The specific signature type depends on denom_type of the denomination.'
    ,'denom_sig'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_known_coins(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'known_coins';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_known_coin_id_key'
    ' UNIQUE (known_coin_id)'
  );
END
$$;
CREATE FUNCTION foreign_table_known_coins()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'known_coins';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_denominations'
    ' FOREIGN KEY (denominations_serial) '
    ' REFERENCES denominations (denominations_serial) ON DELETE CASCADE'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('known_coins'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('known_coins'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('known_coins'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_coin_history (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'coin_history';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(coin_history_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY'
      ',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)'
      ',table_name TEXT NOT NULL'
      ',serial_id INT8 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (coin_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'Links to tables with entries that affected the transaction history of a coin.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'For which coin is this a history entry'
    ,'coin_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'In which table is the history entry'
    ,'table_name'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Which is the generated serial ID of the entry in the table'
    ,'serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Monotonic counter, used to generate Etags for caching'
    ,'coin_history_serial_id'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_coin_history(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'coin_history';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_coin_history_serial_id_pkey'
    ' PRIMARY KEY (coin_history_serial_id) '
    ',ADD CONSTRAINT ' || table_name || '_coin_entry_key'
    ' UNIQUE (coin_pub, table_name, serial_id)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_coin_by_time'
    ' ON ' || table_name || ' '
    '(coin_pub'
    ',coin_history_serial_id DESC'
    ');'
  );
END
$$;
CREATE FUNCTION foreign_table_coin_history()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'coin_history';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    ' FOREIGN KEY (coin_pub) '
    ' REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('coin_history'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('coin_history'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('coin_history'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE)
    ;
CREATE FUNCTION create_table_batch_deposits(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'batch_deposits';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(batch_deposit_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY'
      ',shard INT8 NOT NULL'
      ',merchant_pub BYTEA NOT NULL CHECK (LENGTH(merchant_pub)=32)'
      ',wallet_timestamp INT8 NOT NULL'
      ',exchange_timestamp INT8 NOT NULL'
      ',refund_deadline INT8 NOT NULL'
      ',wire_deadline INT8 NOT NULL'
      ',h_contract_terms BYTEA NOT NULL CHECK (LENGTH(h_contract_terms)=64)'
      ',wallet_data_hash BYTEA CHECK (LENGTH(wallet_data_hash)=64) DEFAULT NULL'
      ',wire_salt BYTEA NOT NULL CHECK (LENGTH(wire_salt)=16)'
      ',wire_target_h_payto BYTEA CHECK (LENGTH(wire_target_h_payto)=32)'
      ',policy_details_serial_id INT8'
      ',policy_blocked BOOLEAN NOT NULL DEFAULT FALSE'
      ',total_amount taler_amount NOT NULL'
      ',merchant_sig BYTEA CHECK(LENGTH(merchant_sig)=64) NOT NULL'
      ',done BOOLEAN NOT NULL DEFAULT FALSE'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (shard)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'Information about the contracts for which we have received (batch) deposits.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Used for load sharding in the materialized indices. Should be set based on merchant_pub. 64-bit value because we need an *unsigned* 32-bit value.'
    ,'shard'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Unsalted hash of the target bank account; also used to lookup the KYC status'
    ,'wire_target_h_payto'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'hash over data provided by the wallet upon payment to select a more specific contract'
    ,'wallet_data_hash'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Salt used when hashing the payto://-URI to get the h_wire that was used by the coin deposit signatures; not used to calculate wire_target_h_payto (as that one is unsalted)'
    ,'wire_salt'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Set to TRUE once we have included this (batch) deposit (and all associated coins) in some aggregate wire transfer to the merchant'
    ,'done'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'True if the aggregation of the (batch) deposit is currently blocked by some policy extension mechanism. Used to filter out deposits that must not be processed by the canonical deposit logic.'
    ,'policy_blocked'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'References policy extensions table, NULL if extensions are not used'
    ,'policy_details_serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'total amount'
    ,'total_amount'
    ,'batch_deposits'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'signature by the merchant over the contract terms, of purpose TALER_SIGNATURE_MERCHANT_CONTRACT'
    ,'merchant_sig'
    ,'batch_deposits'
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_batch_deposits(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'batch_deposits';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_batch_deposit_serial_id_pkey'
    ' PRIMARY KEY (batch_deposit_serial_id) '
    ',ADD CONSTRAINT ' || table_name || '_merchant_pub_h_contract_terms'
    ' UNIQUE (shard, merchant_pub, h_contract_terms)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_ready '
    'ON ' || table_name || ' '
    '(shard ASC'
    ',wire_deadline ASC'
    ') WHERE NOT (done OR policy_blocked);'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_for_matching '
    'ON ' || table_name || ' '
    '(shard ASC'
    ',refund_deadline ASC'
    ',wire_target_h_payto'
    ') WHERE NOT (done OR policy_blocked);'
  );
END
$$;
CREATE OR REPLACE FUNCTION foreign_table_batch_deposits()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'batch_deposits';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_policy_details'
    ' FOREIGN KEY (policy_details_serial_id) '
    ' REFERENCES policy_details (policy_details_serial_id) ON DELETE RESTRICT'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('batch_deposits'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('batch_deposits'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('batch_deposits'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE)
    ;
CREATE FUNCTION create_table_coin_deposits(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'coin_deposits';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(coin_deposit_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY'
      ',batch_deposit_serial_id INT8 NOT NULL'
      ',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)'
      ',coin_sig BYTEA NOT NULL CHECK (LENGTH(coin_sig)=64)'
      ',amount_with_fee taler_amount NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (coin_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'Coins which have been deposited with the respective per-coin signatures.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Link to information about the batch deposit this coin was used for'
    ,'batch_deposit_serial_id'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_coin_deposits(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'coin_deposits';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_coin_deposit_serial_id_pkey'
    ' PRIMARY KEY (coin_deposit_serial_id) '
    ',ADD CONSTRAINT ' || table_name || '_unique_coin_sig'
    ' UNIQUE (coin_pub, coin_sig)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_batch '
    'ON ' || table_name || ' '
    '(batch_deposit_serial_id);'
  );
END
$$;
CREATE FUNCTION foreign_table_coin_deposits()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'coin_deposits';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    ' FOREIGN KEY (coin_pub) '
    ' REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
    ',ADD CONSTRAINT ' || table_name || '_foreign_batch_deposits_id'
    ' FOREIGN KEY (batch_deposit_serial_id) '
    ' REFERENCES batch_deposits (batch_deposit_serial_id) ON DELETE CASCADE'
  );
END
$$;
CREATE OR REPLACE FUNCTION coin_deposits_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO exchange.coin_history
    (coin_pub
    ,table_name
    ,serial_id)
 VALUES
     (NEW.coin_pub
    ,'coin_deposits'
    ,NEW.coin_deposit_serial_id);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION coin_deposits_insert_trigger()
  IS 'Automatically generate coin history entry.';
CREATE FUNCTION master_table_coin_deposits()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER coin_deposits_on_insert
    AFTER INSERT
     ON coin_deposits
     FOR EACH ROW EXECUTE FUNCTION coin_deposits_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('coin_deposits'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('coin_deposits'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('coin_deposits'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('coin_deposits'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE)
    ;
CREATE FUNCTION create_table_refunds(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'refunds';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(refund_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)'
      ',batch_deposit_serial_id INT8 NOT NULL'
      ',merchant_sig BYTEA NOT NULL CHECK(LENGTH(merchant_sig)=64)'
      ',rtransaction_id INT8 NOT NULL'
      ',amount_with_fee taler_amount NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (coin_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Data on coins that were refunded. Technically, refunds always apply against specific deposit operations involving a coin. The combination of coin_pub, merchant_pub, h_contract_terms and rtransaction_id MUST be unique, and we usually select by coin_pub so that one goes first.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies ONLY the merchant_pub, h_contract_terms and coin_pub. Multiple deposits may match a refund, this only identifies one of them.'
    ,'batch_deposit_serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'used by the merchant to make refunds unique in case the same coin for the same deposit gets a subsequent (higher) refund'
    ,'rtransaction_id'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_refunds (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'refunds';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_coin_pub_index '
    'ON ' || table_name || ' '
    '(coin_pub);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_refund_serial_id_key'
    ' UNIQUE (refund_serial_id) '
    ',ADD PRIMARY KEY (batch_deposit_serial_id, coin_pub, rtransaction_id) '
  );
END
$$;
CREATE FUNCTION foreign_table_refunds ()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'refunds';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    ' FOREIGN KEY (coin_pub) '
    ' REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
    ',ADD CONSTRAINT ' || table_name || '_foreign_deposit'
    ' FOREIGN KEY (batch_deposit_serial_id) '
    ' REFERENCES batch_deposits (batch_deposit_serial_id) ON DELETE CASCADE'
  );
END
$$;
CREATE OR REPLACE FUNCTION refunds_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO exchange.coin_history
    (coin_pub
    ,table_name
    ,serial_id)
 VALUES
     (NEW.coin_pub
    ,'refunds'
    ,NEW.refund_serial_id);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION refunds_insert_trigger()
  IS 'Automatically generate coin history entry.';
CREATE FUNCTION master_table_refunds()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER refunds_on_insert
    AFTER INSERT
     ON refunds
     FOR EACH ROW EXECUTE FUNCTION refunds_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('refunds'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('refunds'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('refunds'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('refunds'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_wire_out(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wire_out';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE IF NOT EXISTS %I'
      '(wireout_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',execution_date INT8 NOT NULL'
      ',wtid_raw BYTEA UNIQUE NOT NULL CHECK (LENGTH(wtid_raw)=32)'
      ',wire_target_h_payto BYTEA CHECK (LENGTH(wire_target_h_payto)=32)'
      ',exchange_account_section TEXT NOT NULL'
      ',amount taler_amount NOT NULL'
      ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (wtid_raw)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'wire transfers the exchange has executed'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'identifies the configuration section with the debit account of this payment'
    ,'exchange_account_section'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies the credited bank account and KYC status'
    ,'wire_target_h_payto'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_wire_out(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wire_out';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_wire_target_h_payto_index '
    'ON ' || table_name || ' '
    '(wire_target_h_payto);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wireout_uuid_pkey'
    ' PRIMARY KEY (wireout_uuid)'
  );
END
$$;
CREATE FUNCTION wire_out_delete_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  DELETE FROM exchange.aggregation_tracking
   WHERE wtid_raw = OLD.wtid_raw;
  RETURN OLD;
END $$;
COMMENT ON FUNCTION wire_out_delete_trigger()
  IS 'Replicate reserve_out deletions into aggregation_tracking. This replaces an earlier use of an ON DELETE CASCADE that required a DEFERRABLE constraint and conflicted with nice partitioning.';
CREATE FUNCTION master_table_wire_out()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER wire_out_on_delete
    AFTER DELETE
      ON wire_out
     FOR EACH ROW EXECUTE FUNCTION wire_out_delete_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wire_out'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('wire_out'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('wire_out'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_aggregation_transient(
  IN shard_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aggregation_transient';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
    '(amount taler_amount NOT NULL'
    ',wire_target_h_payto BYTEA CHECK (LENGTH(wire_target_h_payto)=32)'
    ',merchant_pub BYTEA CHECK (LENGTH(merchant_pub)=32)'
    ',exchange_account_section TEXT NOT NULL'
    ',legitimization_requirement_serial_id INT8 NOT NULL DEFAULT(0)'
    ',wtid_raw BYTEA NOT NULL CHECK (LENGTH(wtid_raw)=32)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (wire_target_h_payto)'
    ,shard_suffix
  );
  PERFORM comment_partitioned_table(
    'aggregations currently happening (lacking wire_out, usually because the amount is too low); this table is not replicated'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
       'Sum of all of the aggregated deposits (without deposit fees)'
      ,'amount'
      ,table_name
      ,shard_suffix
  );
  PERFORM comment_partitioned_column(
       'public key of the merchant that authorized the deposits'
      ,'merchant_pub'
      ,table_name
      ,shard_suffix
  );
  PERFORM comment_partitioned_column(
       'unsalted hash of the (full) payto URI of the merchant account that should receive the funds'
      ,'wire_target_h_payto'
      ,table_name
      ,shard_suffix
  );
  PERFORM comment_partitioned_column(
       'identifier of the wire transfer'
      ,'wtid_raw'
      ,table_name
      ,shard_suffix
  );
END
$$;
CREATE FUNCTION foreign_table_aggregation_transient()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aggregation_transient';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_wire_target_h_payto'
    ' FOREIGN KEY (wire_target_h_payto) '
    ' REFERENCES wire_targets (wire_target_h_payto) ON DELETE RESTRICT'
  );
END
$$;
CREATE FUNCTION constrain_table_aggregation_transient(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aggregation_transient';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wire_target_h_payto_and_wtid_unique'
    '   UNIQUE (wire_target_h_payto,wtid_raw)'
  );
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('aggregation_transient'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('aggregation_transient'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('aggregation_transient'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_aggregation_tracking(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aggregation_tracking';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(aggregation_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',batch_deposit_serial_id INT8 PRIMARY KEY'
      ',wtid_raw BYTEA NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (batch_deposit_serial_id)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'mapping from wire transfer identifiers (WTID) to deposits (and back)'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'identifier of the wire transfer'
    ,'wtid_raw'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_aggregation_tracking(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aggregation_tracking';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_wtid_raw_index '
    'ON ' || table_name || ' '
    '(wtid_raw);'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || table_name || '_by_wtid_raw_index '
    'IS ' || quote_literal('for lookup_transactions') || ';'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_aggregation_serial_id_key'
    ' UNIQUE (aggregation_serial_id) '
  );
END
$$;
CREATE FUNCTION foreign_table_aggregation_tracking()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aggregation_tracking';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_deposit'
    ' FOREIGN KEY (batch_deposit_serial_id)'
    ' REFERENCES batch_deposits (batch_deposit_serial_id)'
    ' ON DELETE CASCADE'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('aggregation_tracking'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('aggregation_tracking'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('aggregation_tracking'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_unique_refresh_blinding_seed(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'unique_refresh_blinding_seed';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(unique_refresh_blinding_seed_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',blinding_seed BYTEA PRIMARY KEY'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (blinding_seed)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Table to ensure uniqueness of the blinding_seed for CS signatures across all refresh operations. '
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_unique_refresh_blinding_seed(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'unique_refresh_blinding_seed';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_refresh_id_key'
    ' UNIQUE (unique_refresh_blinding_seed_id);'
  );
END
$$;
INSERT INTO exchange_tables
  (name
  ,version
  ,action
  ,partitioned
  ,by_range)
VALUES
  ('unique_refresh_blinding_seed', 'exchange-0002', 'create', TRUE ,FALSE),
  ('unique_refresh_blinding_seed', 'exchange-0002', 'constrain',TRUE ,FALSE);
CREATE FUNCTION create_table_refresh(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'refresh';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(refresh_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',rc BYTEA PRIMARY KEY CONSTRAINT rc_length CHECK(LENGTH(rc)=64)'
      ',execution_date INT8 NOT NULL'
      ',amount_with_fee taler_amount NOT NULL'
      ',old_coin_pub BYTEA NOT NULL'
      ',old_coin_sig BYTEA NOT NULL CHECK(LENGTH(old_coin_sig)=64)'
      ',refresh_seed BYTEA NOT NULL'
      ',noreveal_index INT4 NOT NULL  CONSTRAINT noreveal_index_positive CHECK(noreveal_index>=0)'
      ',planchets_h BYTEA CONSTRAINT planchets_h_length CHECK(LENGTH(planchets_h)=64)'
      ',selected_h BYTEA CONSTRAINT selected_h_length CHECK(LENGTH(selected_h)=64)'
      ',blinding_seed BYTEA CONSTRAINT blinding_seed_length CHECK(LENGTH(blinding_seed)>=32)'
      ',cs_r_values BYTEA[]'
      ',cs_r_choices INT8'
      ',denom_serials INT8[] NOT NULL CONSTRAINT denom_serials_array_length CHECK(cardinality(denom_serials)=cardinality(denom_sigs))'
      ',denom_sigs BYTEA[] NOT NULL CONSTRAINT denom_sigs_array_length CHECK(cardinality(denom_sigs)=cardinality(denom_serials))'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (rc)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'The data provided by the client for the melting operation of an old coin and he choices made by the exchange '
    ' with respect to the cut-and-choose protocol: nonreveal_index and the corresponding chosen'
    ' blinded coin envelope along with the denomination signatures at the moment of the melting.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The hash over the refresh request, which serves as the primary key'
     ' for the lookup during the reveal phase.'
    ,'rc'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The publice nonce from which all other nonces for all n*kappa coin candidates are derived for which'
     ' the old coin proves ownership via signatures'
    ,'refresh_seed'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The gamma value chosen by the exchange in the cut-and-choose protocol'
    ,'noreveal_index'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The date of execution of the melting operation, according to the exchange'
    ,'execution_date'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Reference to the public key of the old coin which is melted'
    ,'old_coin_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature of the old coin''s private key over the melt request'
    ,'old_coin_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Array of references to the denominations'
    ,'denom_serials'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The master seed for the blinding nonces, needed for blind CS signatures; maybe NULL'
    ,'blinding_seed'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The public pairs of R-values provided by the exchange for the CS denominations; might be NULL'
    ,'cs_r_values'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The bitvector of choices made by the exchange for each of the pairs in cs_r_values; maybe NULL.'
     'The vector is stored in network byte order and the lowest bit corresponds to the 0-th entry in cs_r_values (pair)'
    ,'cs_r_choices'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The hash over all kappa*n blinded planchets that were provided by the client'
    ,'planchets_h'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The hash over the n blinded planchets that were selected by the exchange.'
    ,'selected_h'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Array of signatures, one for each blinded envelope'
    ,'denom_sigs'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_refresh(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'refresh';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_old_coin_pub_index '
    'ON ' || table_name || ' '
    '(old_coin_pub);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_refresh_id_key'
    ' UNIQUE (refresh_id);'
  );
END
$$;
CREATE FUNCTION foreign_table_refresh()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'refresh';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    ' FOREIGN KEY (old_coin_pub) '
    ' REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
  );
END
$$;
CREATE FUNCTION refresh_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO coin_history
    (coin_pub
    ,table_name
    ,serial_id)
  VALUES
    (NEW.old_coin_pub
    ,'refresh'
    ,NEW.refresh_id);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION refresh_insert_trigger()
  IS 'Keep track of a particular refresh in the coin_history table.';
CREATE FUNCTION refresh_delete_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  DELETE FROM unique_refresh_blinding_seed
  WHERE blinding_seed = OLD.blinding_seed;
  RETURN OLD;
END $$;
COMMENT ON FUNCTION refresh_delete_trigger()
  IS 'Delete blinding_seed from unique_refresh_blinding_seed table.';
CREATE FUNCTION master_table_refresh()
  RETURNS void
  LANGUAGE plpgsql
  AS $$
BEGIN
  CREATE TRIGGER refresh_on_insert
  AFTER INSERT
  ON refresh
  FOR EACH ROW EXECUTE FUNCTION refresh_insert_trigger();
  CREATE TRIGGER refresh_on_delete
  AFTER DELETE
  ON refresh
  FOR EACH ROW EXECUTE FUNCTION refresh_delete_trigger();
END $$;
COMMENT ON FUNCTION master_table_refresh()
  IS 'Setup triggers to replicate refresh into coin_history and delete blinding_seed from unique_refresh_blinding_seed.';
INSERT INTO exchange_tables
  (name
  ,version
  ,action
  ,partitioned
  ,by_range)
VALUES
  ('refresh', 'exchange-0002', 'create', TRUE ,FALSE),
  ('refresh', 'exchange-0002', 'constrain',TRUE ,FALSE),
  ('refresh', 'exchange-0002', 'foreign', TRUE ,FALSE),
  ('refresh', 'exchange-0002', 'master', TRUE ,FALSE);
CREATE FUNCTION create_table_unique_withdraw_blinding_seed(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'unique_withdraw_blinding_seed';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(unique_withdraw_blinding_seed_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',blinding_seed BYTEA PRIMARY KEY'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (blinding_seed)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Table to ensure uniqueness of the blinding_seed for CS signatures across all withdraw operations. '
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_unique_withdraw_blinding_seed(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'unique_withdraw_blinding_seed';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_withdraw_id_key'
    ' UNIQUE (unique_withdraw_blinding_seed_id);'
  );
END
$$;
INSERT INTO exchange_tables
  (name
  ,version
  ,action
  ,partitioned
  ,by_range)
VALUES
  ('unique_withdraw_blinding_seed', 'exchange-0002', 'create', TRUE ,FALSE),
  ('unique_withdraw_blinding_seed', 'exchange-0002', 'constrain',TRUE ,FALSE);
CREATE FUNCTION create_table_withdraw(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'withdraw';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(withdraw_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',planchets_h BYTEA CONSTRAINT planchets_h_length CHECK(LENGTH(planchets_h)=64)'
      ',execution_date INT8 NOT NULL'
      ',amount_with_fee taler_amount NOT NULL'
      ',reserve_pub BYTEA NOT NULL CONSTRAINT reserve_pub_length CHECK(LENGTH(reserve_pub)=32)'
      ',reserve_sig BYTEA NOT NULL CONSTRAINT reserve_sig_length CHECK(LENGTH(reserve_sig)=64)'
      ',max_age SMALLINT CONSTRAINT max_age_positive CHECK(max_age>=0)'
      ',noreveal_index SMALLINT CONSTRAINT noreveal_index_positive CHECK(noreveal_index>=0)'
      ',selected_h BYTEA CONSTRAINT selected_h_length CHECK(LENGTH(selected_h)=64)'
      ',blinding_seed BYTEA CONSTRAINT blinding_seed_length CHECK(LENGTH(blinding_seed)>=32)'
      ',cs_r_values BYTEA[]'
      ',cs_r_choices INT8'
      ',denom_serials INT8[] NOT NULL CONSTRAINT denom_serials_array_length CHECK(cardinality(denom_serials)=cardinality(denom_sigs))'
      ',denom_sigs BYTEA[] NOT NULL CONSTRAINT denom_sigs_array_length CHECK(cardinality(denom_sigs)=cardinality(denom_serials))'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Commitments made when withdrawing coins and, in case of required proof of age restriction, the gamma value chosen by the exchange. '
     'It also contains the blindly signed coins, their signatures and denominations.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'If the client explicitly commits to age-restricted coins, the gamma value chosen by the exchange in the cut-and-choose protocol; NULL if we did not use age-withdraw.'
    ,'noreveal_index'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The running hash over all committed blinded planchets. Needed for recoup and (when a proof of age-restriction was required); NULL if we did not use age-withdraw.'
     ' in the subsequent cut-and-choose protocol.'
    ,'planchets_h'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The date of execution of this withdrawal, according to the exchange'
    ,'execution_date'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'If the clients commits to age-restricted coins, the maximum age (in years) that the client explicitly commits to with this request; NULL if we did not use age-withdraw.'
    ,'max_age'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Reference to the public key of the reserve from which the coins are going to be withdrawn'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature of the reserve''s private key over the withdraw request'
    ,'reserve_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Array of references to the denominations'
    ,'denom_serials'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'In case of age restriction, the hash of the chosen (noreveal_index) blinded envelopes; NULL if we did not use age-withdraw.'
    ,'selected_h'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Array of signatures over each blinded envelope.  If age-proof was not required, the signed envelopes are the ones'
     ' hashed into planchet_h.  Otherwise (when age-proof is required) the selected planchets (noreveal_index) were signed,'
     ' hashed into selected_h.'
    ,'denom_sigs'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The master seed for the blinding nonces, needed for blind CS signatures; NULL if we did not use age-withdraw or CS'
    ,'blinding_seed'
    ,table_name
    ,partition_suffix
  );
 PERFORM comment_partitioned_column(
     'The pairs of R-values (calculated by the exchange) for the coins of cipher type Clause-Schnorr, based on the blinding_seed; maybe NULL if we did not use CS.'
    ,'cs_r_values'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'The bitvector of choices made by the exchange for each of the pairs in cs_r_values; NULL if we did not use CS.'
     'The vector is stored in network byte order and the lowest bit corresponds to the 0-th entry in cs_r_values (pair)'
    ,'cs_r_choices'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_withdraw(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'withdraw';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_reserve_pub_index '
    'ON ' || table_name || ' '
    '(reserve_pub);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD PRIMARY KEY (reserve_pub, planchets_h);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_withdraw_id_key'
    ' UNIQUE (withdraw_id);'
  );
END
$$;
CREATE FUNCTION foreign_table_withdraw()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'withdraw';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
    ' FOREIGN KEY (reserve_pub)'
    ' REFERENCES reserves(reserve_pub) ON DELETE CASCADE;'
  );
END
$$;
CREATE FUNCTION master_table_withdraw()
  RETURNS void
  LANGUAGE plpgsql
  AS $$
BEGIN
  CREATE TRIGGER withdraw_on_insert
  AFTER INSERT
  ON withdraw
  FOR EACH ROW EXECUTE FUNCTION withdraw_insert_trigger();
  CREATE TRIGGER withdraw_on_delete
  AFTER DELETE
  ON withdraw
  FOR EACH ROW EXECUTE FUNCTION withdraw_delete_trigger();
END $$;
COMMENT ON FUNCTION master_table_withdraw()
  IS 'Setup triggers to replicate withdraw into reserve_history and delete blinding_seed from unique_withdraw_blinding_seed.';
INSERT INTO exchange_tables
  (name
  ,version
  ,action
  ,partitioned
  ,by_range)
VALUES
  ('withdraw', 'exchange-0002', 'create', TRUE ,FALSE),
  ('withdraw', 'exchange-0002', 'constrain',TRUE ,FALSE),
  ('withdraw', 'exchange-0002', 'foreign', TRUE ,FALSE),
  ('withdraw', 'exchange-0002', 'master', TRUE ,FALSE);
CREATE FUNCTION create_table_recoup(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(recoup_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)'
      ',coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)'
      ',coin_blind BYTEA NOT NULL CHECK(LENGTH(coin_blind)=32)'
      ',amount taler_amount NOT NULL'
      ',recoup_timestamp INT8 NOT NULL'
      ',withdraw_id INT8 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (coin_pub);'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Information about recoups that were executed between a coin and a reserve. In this type of recoup, the amount is credited back to the reserve from which the coin originated.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Coin that is being debited in the recoup. Do not CASCADE ON DROP on the coin_pub, as we may keep the coin alive!'
    ,'coin_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies the h_commitment of the recouped coin and provides the link to the credited reserve.'
    ,'withdraw_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature by the coin affirming the recoup, of type TALER_SIGNATURE_WALLET_COIN_RECOUP'
    ,'coin_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Denomination blinding key used when creating the blinded coin from the planchet. Secret revealed during the recoup to provide the linkage between the coin and the withdraw operation.'
    ,'coin_blind'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_recoup(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_coin_pub_index '
    'ON ' || table_name || ' '
    '(coin_pub);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_recoup_uuid_key'
    ' UNIQUE (recoup_uuid) '
  );
END
$$;
CREATE OR REPLACE FUNCTION foreign_table_recoup()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_withdraw'
    ' FOREIGN KEY (withdraw_id) '
    ' REFERENCES withdraw (withdraw_id) ON DELETE CASCADE'
    ',ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    ' FOREIGN KEY (coin_pub) '
    ' REFERENCES known_coins (coin_pub)'
  );
END
$$;
CREATE FUNCTION create_table_recoup_by_reserve(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup_by_reserve';
BEGIN
  PERFORM create_partitioned_table(
  'CREATE TABLE %I'
    '(withdraw_id INT8 NOT NULL' 
    ',coin_pub BYTEA CHECK (LENGTH(coin_pub)=32)' 
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (withdraw_id)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Information in this table is strictly redundant with that of recoup, but saved by a different primary key for fast lookups by withdraw_id.'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_recoup_by_reserve(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup_by_reserve';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_main_index '
    'ON ' || table_name || ' '
    '(withdraw_id);'
  );
END
$$;
CREATE OR REPLACE FUNCTION recoup_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO recoup_by_reserve
    (withdraw_id
    ,coin_pub)
  VALUES
    (NEW.withdraw_id
    ,NEW.coin_pub);
  INSERT INTO coin_history
    (coin_pub
    ,table_name
    ,serial_id)
  VALUES
    (NEW.coin_pub
    ,'recoup'
    ,NEW.recoup_uuid);
  INSERT INTO reserve_history
    (reserve_pub
    ,table_name
    ,serial_id)
  SELECT
     res.reserve_pub
    ,'recoup'
    ,NEW.recoup_uuid
  FROM withdraw wd
  JOIN reserves res
    USING (reserve_pub)
    WHERE wd.withdraw_id = NEW.withdraw_id;
  RETURN NEW;
END $$;
COMMENT ON FUNCTION recoup_insert_trigger()
  IS 'Replicates recoup inserts into recoup_by_reserve table and updates the coin_history table.';
CREATE OR REPLACE FUNCTION recoup_delete_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  DELETE FROM recoup_by_reserve
   WHERE withdraw_id = OLD.withdraw_id
     AND coin_pub = OLD.coin_pub;
  RETURN OLD;
END $$;
COMMENT ON FUNCTION recoup_delete_trigger()
  IS 'Replicate recoup deletions into recoup_by_reserve table.';
CREATE FUNCTION master_table_recoup()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER recoup_on_insert
    AFTER INSERT
     ON recoup
     FOR EACH ROW EXECUTE FUNCTION recoup_insert_trigger();
  CREATE TRIGGER recoup_on_delete
    AFTER DELETE
      ON recoup
     FOR EACH ROW EXECUTE FUNCTION recoup_delete_trigger();
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('recoup'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('recoup'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('recoup'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('recoup_by_reserve'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('recoup_by_reserve'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('recoup'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_recoup_refresh(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup_refresh';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
    '(recoup_refresh_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY'
    ',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)'
    ',known_coin_id BIGINT NOT NULL'
    ',coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)'
    ',coin_blind BYTEA NOT NULL CHECK(LENGTH(coin_blind)=32)'
    ',amount taler_amount NOT NULL'
    ',recoup_timestamp INT8 NOT NULL'
    ',refresh_id INT8 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (coin_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Table of coins that originated from a refresh operation and that were recouped. Links the (fresh) coin to the melted operation (and thus the old coin). A recoup on a refreshed coin credits the old coin and debits the fresh coin.'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Refreshed coin of a revoked denomination where the residual value is credited to the old coin. Do not CASCADE ON DROP on the coin_pub, as we may keep the coin alive!'
    ,'coin_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Used for garbage collection (in the absence of foreign constraints, in the future)'
    ,'known_coin_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Link to the refresh operation. Also identifies the h_blind_ev of the recouped coin (as h_coin_ev).'
    ,'refresh_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Denomination blinding key used when creating the blinded coin from the planchet. Secret revealed during the recoup to provide the linkage between the coin and the refresh operation.'
    ,'coin_blind'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_recoup_refresh(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup_refresh';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_refresh_id_index'
    ' ON ' || table_name || ' '
    '(refresh_id);'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || table_name || '_by_refresh_id_index '
    'IS ' || quote_literal('used in exchange_do_melt for zombie coins (rare)') || ';'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_coin_pub_index'
    ' ON ' || table_name || ' '
    '(coin_pub);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_recoup_refresh_uuid_key'
    ' UNIQUE (recoup_refresh_uuid) '
  );
END
$$;
CREATE FUNCTION foreign_table_recoup_refresh()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'recoup_refresh';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    ' FOREIGN KEY (coin_pub) '
    ' REFERENCES known_coins (coin_pub)'
    ',ADD CONSTRAINT ' || table_name || '_foreign_known_coin_id'
    ' FOREIGN KEY (known_coin_id) '
    ' REFERENCES known_coins (known_coin_id) ON DELETE CASCADE'
    ',ADD CONSTRAINT ' || table_name || '_foreign_refresh_id'
    ' FOREIGN KEY (refresh_id) '
    ' REFERENCES refresh (refresh_id) ON DELETE CASCADE'
  );
END
$$;
CREATE OR REPLACE FUNCTION recoup_refresh_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO exchange.coin_history
    (coin_pub
    ,table_name
    ,serial_id)
  VALUES
     (NEW.coin_pub
    ,'recoup_refresh::NEW'
    ,NEW.recoup_refresh_uuid);
  INSERT INTO exchange.coin_history
    (coin_pub
    ,table_name
    ,serial_id)
  SELECT
     refresh.old_coin_pub
    ,'recoup_refresh::OLD'
    ,NEW.recoup_refresh_uuid
    FROM refresh
    WHERE refresh.refresh_id = NEW.refresh_id;
  RETURN NEW;
END $$;
COMMENT ON FUNCTION coin_deposits_insert_trigger()
  IS 'Automatically generate coin history entry.';
CREATE FUNCTION master_table_recoup_refresh()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER recoup_refresh_on_insert
    AFTER INSERT
     ON recoup_refresh
     FOR EACH ROW EXECUTE FUNCTION recoup_refresh_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('recoup_refresh'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('recoup_refresh'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('recoup_refresh'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('recoup_refresh'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_prewire(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'prewire';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(prewire_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY'
      ',wire_method TEXT NOT NULL'
      ',finished BOOLEAN NOT NULL DEFAULT FALSE'
      ',failed BOOLEAN NOT NULL DEFAULT FALSE'
      ',buf BYTEA NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (prewire_uuid)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'pre-commit data for wire transfers we are about to execute'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'set to TRUE if the bank responded with a non-transient failure to our transfer request'
    ,'failed'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'set to TRUE once bank confirmed receiving the wire transfer request'
    ,'finished'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'serialized data to send to the bank to execute the wire transfer'
    ,'buf'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_prewire(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'prewire';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_finished_index '
    'ON ' || table_name || ' '
    '(finished)'
    ' WHERE finished;'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || table_name || '_by_finished_index '
    'IS ' || quote_literal('for do_gc') || ';'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_failed_finished_index '
    'ON ' || table_name || ' '
    '(prewire_uuid)'
    ' WHERE finished=FALSE'
    '   AND failed=FALSE;'
  );
  EXECUTE FORMAT (
    'COMMENT ON INDEX ' || table_name || '_by_failed_finished_index '
    'IS ' || quote_literal('for wire_prepare_data_get') || ';'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('prewire'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('prewire'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_cs_nonce_locks(
  partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(cs_nonce_lock_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',nonce BYTEA PRIMARY KEY CHECK (LENGTH(nonce)=32)'
      ',op_hash BYTEA NOT NULL CHECK (LENGTH(op_hash)=64)'
      ',max_denomination_serial INT8 NOT NULL'
    ') %s ;'
    ,'cs_nonce_locks'
    ,'PARTITION BY HASH (nonce)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'ensures a Clause Schnorr client nonce is locked for use with an operation identified by a hash'
    ,'cs_nonce_locks'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'actual nonce submitted by the client'
    ,'nonce'
    ,'cs_nonce_locks'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'hash (RC for refresh, blind coin hash for withdraw) the nonce may be used with'
    ,'op_hash'
    ,'cs_nonce_locks'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Maximum number of a CS denomination serial the nonce could be used with, for GC'
    ,'max_denomination_serial'
    ,'cs_nonce_locks'
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_cs_nonce_locks(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'cs_nonce_locks';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_cs_nonce_lock_serial_id_key'
    ' UNIQUE (cs_nonce_lock_serial_id)'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('cs_nonce_locks'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('cs_nonce_locks'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_purse_requests(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_requests';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(purse_requests_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
      ',merge_pub BYTEA NOT NULL CHECK (LENGTH(merge_pub)=32)'
      ',purse_creation INT8 NOT NULL'
      ',purse_expiration INT8 NOT NULL'
      ',h_contract_terms BYTEA NOT NULL CHECK (LENGTH(h_contract_terms)=64)'
      ',age_limit INT4 NOT NULL'
      ',flags INT4 NOT NULL'
      ',in_reserve_quota BOOLEAN NOT NULL DEFAULT(FALSE)'
      ',was_decided BOOLEAN NOT NULL DEFAULT(FALSE)'
      ',amount_with_fee taler_amount NOT NULL'
      ',purse_fee taler_amount NOT NULL'
      ',balance taler_amount NOT NULL DEFAULT (0,0)'
      ',purse_sig BYTEA NOT NULL CHECK(LENGTH(purse_sig)=64)'
      ',PRIMARY KEY (purse_pub)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Requests establishing purses, associating them with a contract but without a target reserve'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key of the purse'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Local time when the purse was created. Determines applicable purse fees.'
    ,'purse_creation'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'When the purse is set to expire'
    ,'purse_expiration'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Hash of the contract the parties are to agree to'
    ,'h_contract_terms'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'see the enum TALER_WalletAccountMergeFlags'
    ,'flags'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'set to TRUE if this purse currently counts against the number of free purses in the respective reserve'
    ,'in_reserve_quota'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Total amount expected to be in the purse'
    ,'amount_with_fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Purse fee the client agreed to pay from the reserve (accepted by the exchange at the time the purse was created). Zero if in_reserve_quota is TRUE.'
    ,'purse_fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'Total amount actually in the purse (updated)'
    ,'balance'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature of the purse affirming the purse parameters, of type TALER_SIGNATURE_PURSE_REQUEST'
    ,'purse_sig'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_purse_requests(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_requests';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_merge_pub '
    'ON ' || table_name || ' '
    '(merge_pub);'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_purse_expiration '
    'ON ' || table_name || ' '
    '(purse_expiration) ' ||
    'WHERE NOT was_decided;'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_purse_requests_serial_id_key'
    ' UNIQUE (purse_requests_serial_id) '
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('purse_requests'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('purse_requests'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_purse_merges(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_merges';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
    '(purse_merge_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
    ',partner_serial_id INT8'
    ',reserve_pub BYTEA NOT NULL CHECK(length(reserve_pub)=32)'
    ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
    ',merge_sig BYTEA NOT NULL CHECK (LENGTH(merge_sig)=64)'
    ',merge_timestamp INT8 NOT NULL'
    ',PRIMARY KEY (purse_pub)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Merge requests where a purse-owner requested merging the purse into the account'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'identifies the partner exchange, NULL in case the target reserve lives at this exchange'
    ,'partner_serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'public key of the target reserve'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'public key of the purse'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'signature by the purse private key affirming the merge, of type TALER_SIGNATURE_WALLET_PURSE_MERGE'
    ,'merge_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'when was the merge message signed'
    ,'merge_timestamp'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_purse_merges(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_merges';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_purse_merge_request_serial_id_key'
    ' UNIQUE (purse_merge_request_serial_id) '
  );
END
$$;
CREATE FUNCTION foreign_table_purse_merges()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_merges';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_partner_serial_id'
    ' FOREIGN KEY (partner_serial_id) '
    ' REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
    ',ADD CONSTRAINT ' || table_name || '_foreign_purse_pub'
    ' FOREIGN KEY (purse_pub) '
    ' REFERENCES purse_requests (purse_pub) ON DELETE CASCADE'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('purse_merges'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('purse_merges'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('purse_merges'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_account_merges(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'account_merges';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE IF NOT EXISTS %I '
      '(account_merge_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
      ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
      ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
      ',wallet_h_payto BYTEA NOT NULL CHECK (LENGTH(wallet_h_payto)=32)'
      ',PRIMARY KEY (purse_pub)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Merge requests where a purse- and account-owner requested merging the purse into the account'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'public key of the target reserve'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'public key of the purse'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'hash over the normalized (!) payto:// URI that identifies the receiving wallet'
    ,'wallet_h_payto'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'signature by the reserve private key affirming the merge, of type TALER_SIGNATURE_WALLET_ACCOUNT_MERGE'
    ,'reserve_sig'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_account_merges(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'account_merges';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_by_reserve_pub '
    'ON ' || table_name || ' '
    '(reserve_pub);'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_account_merge_request_serial_id_key'
    ' UNIQUE (account_merge_request_serial_id) '
  );
END
$$;
CREATE FUNCTION foreign_table_account_merges()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'account_merges';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
    ' FOREIGN KEY (reserve_pub) '
    ' REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
    ',ADD CONSTRAINT ' || table_name || '_foreign_purse_pub'
    ' FOREIGN KEY (purse_pub) '
    ' REFERENCES purse_requests (purse_pub)'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('account_merges'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('account_merges'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('account_merges'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_purse_decision(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_decision';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(purse_decision_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
      ',action_timestamp INT8 NOT NULL'
      ',refunded BOOL NOT NULL'
      ',PRIMARY KEY (purse_pub)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Purses that were decided upon (refund or merge)'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key of the purse'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_purse_decision(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_decision';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_purse_action_serial_id_key'
    ' UNIQUE (purse_decision_serial_id) '
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_purse_decision_purse_pub'
    ' UNIQUE (purse_pub) '
  );
END
$$;
CREATE FUNCTION master_table_purse_decision()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER purse_decision_on_insert
    AFTER INSERT
     ON purse_decision
     FOR EACH ROW EXECUTE FUNCTION purse_decision_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('purse_decision'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('purse_decision'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('purse_decision'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_contracts(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'contracts';
BEGIN
  PERFORM create_partitioned_table(
     'CREATE TABLE %I '
     '(contract_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
     ',pub_ckey BYTEA NOT NULL CHECK (LENGTH(pub_ckey)=32)'
     ',contract_sig BYTEA NOT NULL CHECK (LENGTH(contract_sig)=64)'
     ',e_contract BYTEA NOT NULL'
     ',purse_expiration INT8 NOT NULL'
     ',PRIMARY KEY (purse_pub)'
     ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'encrypted contracts associated with purses'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'public key of the purse that the contract is associated with'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'signature over the encrypted contract by the purse contract key'
    ,'contract_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public ECDH key used to encrypt the contract, to be used with the purse private key for decryption'
    ,'pub_ckey'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'AES-GCM encrypted contract terms (contains gzip compressed JSON after decryption)'
    ,'e_contract'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_contracts(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'contracts';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_contract_serial_id_key'
    ' UNIQUE (contract_serial_id) '
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('contracts'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('contracts'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_history_requests(
  IN shard_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'history_requests';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(history_request_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',reserve_pub BYTEA NOT NULL CHECK (LENGTH(reserve_pub)=32)'
      ',request_timestamp INT8 NOT NULL'
      ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
      ',history_fee taler_amount NOT NULL'
      ',PRIMARY KEY (reserve_pub,request_timestamp)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (reserve_pub)'
    ,shard_suffix
  );
  PERFORM comment_partitioned_table(
     'Paid history requests issued by a client against a reserve'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'When was the history request made'
    ,'request_timestamp'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature approving payment for the history request'
    ,'reserve_sig'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'History fee approved by the signature'
    ,'history_fee'
    ,table_name
    ,shard_suffix
  );
END $$;
CREATE FUNCTION constrain_table_history_requests(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  partition_name TEXT;
BEGIN
  partition_name = concat_ws('_', 'history_requests', partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || partition_name ||
    ' ADD CONSTRAINT ' || partition_name || '_serial_id'
    ' UNIQUE (history_request_serial_id)'
  );
END
$$;
CREATE FUNCTION foreign_table_history_requests()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'history_requests';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_reserve_pub'
    ' FOREIGN KEY (reserve_pub) '
    ' REFERENCES reserves(reserve_pub) ON DELETE CASCADE'
  );
END $$;
CREATE OR REPLACE FUNCTION history_requests_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO reserve_history
    (reserve_pub
    ,table_name
    ,serial_id)
  VALUES
    (NEW.reserve_pub
    ,'history_requests'
    ,NEW.history_request_serial_id);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION history_requests_insert_trigger()
  IS 'Automatically generate reserve history entry.';
CREATE FUNCTION master_table_history_requests()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER history_requests_on_insert
    AFTER INSERT
     ON history_requests
     FOR EACH ROW EXECUTE FUNCTION history_requests_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('history_requests'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('history_requests'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('history_requests'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('history_requests'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_purse_deposits(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_deposits';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(purse_deposit_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',partner_serial_id INT8'
      ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
      ',coin_pub BYTEA NOT NULL'
      ',amount_with_fee taler_amount NOT NULL'
      ',coin_sig BYTEA NOT NULL CHECK(LENGTH(coin_sig)=64)'
      ',PRIMARY KEY (purse_pub,coin_pub)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Requests depositing coins into a purse'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'identifies the partner exchange, NULL in case the target purse lives at this exchange'
    ,'partner_serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key of the purse'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key of the coin being deposited'
    ,'coin_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Total amount being deposited'
    ,'amount_with_fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature of the coin affirming the deposit into the purse, of type TALER_SIGNATURE_PURSE_DEPOSIT'
    ,'coin_sig'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_purse_deposits(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_deposits';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_purse_deposit_serial_id_key'
    ' UNIQUE (purse_deposit_serial_id) '
  );
END
$$;
CREATE FUNCTION foreign_table_purse_deposits()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_deposits';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_partner'
    ' FOREIGN KEY (partner_serial_id) '
    ' REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
    ',ADD CONSTRAINT ' || table_name || '_foreign_coin_pub'
    ' FOREIGN KEY (coin_pub) '
    ' REFERENCES known_coins (coin_pub) ON DELETE CASCADE'
  );
END
$$;
CREATE OR REPLACE FUNCTION purse_deposits_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO exchange.coin_history
    (coin_pub
    ,table_name
    ,serial_id)
 VALUES
    (NEW.coin_pub
    ,'purse_deposits'
    ,NEW.purse_deposit_serial_id);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION purse_deposits_insert_trigger()
  IS 'Automatically generate coin history entry.';
CREATE FUNCTION master_table_purse_deposits()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  CREATE TRIGGER purse_deposits_on_insert
    AFTER INSERT
     ON purse_deposits
     FOR EACH ROW EXECUTE FUNCTION purse_deposits_insert_trigger();
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('purse_deposits'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('purse_deposits'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('purse_deposits'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('purse_deposits'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_wads_in(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wads_in';
BEGIN
  PERFORM create_partitioned_table(
     'CREATE TABLE %I '
     '(wad_in_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     ',wad_id BYTEA PRIMARY KEY CHECK (LENGTH(wad_id)=24)'
     ',origin_exchange_url TEXT NOT NULL'
     ',amount taler_amount NOT NULL'
     ',arrival_time INT8 NOT NULL'
     ',UNIQUE (wad_id, origin_exchange_url)'
     ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (wad_id)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'Incoming exchange-to-exchange wad wire transfers'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Unique identifier of the wad, part of the wire transfer subject'
    ,'wad_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Base URL of the originating URL, also part of the wire transfer subject'
    ,'origin_exchange_url'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Actual amount that was received by our exchange'
    ,'amount'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Time when the wad was received'
    ,'arrival_time'
    ,table_name
    ,partition_suffix
  );
END $$;
CREATE FUNCTION constrain_table_wads_in(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wads_in';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wad_in_serial_id_key'
    ' UNIQUE (wad_in_serial_id) '
    ',ADD CONSTRAINT ' || table_name || '_wad_is_origin_exchange_url_key'
    ' UNIQUE (wad_id, origin_exchange_url) '
  );
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wads_in'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('wads_in'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_wad_in_entries(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wad_in_entries';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(wad_in_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',wad_in_serial_id INT8'
      ',reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32)'
      ',purse_pub BYTEA PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
      ',h_contract BYTEA NOT NULL CHECK(LENGTH(h_contract)=64)'
      ',purse_expiration INT8 NOT NULL'
      ',merge_timestamp INT8 NOT NULL'
      ',amount_with_fee taler_amount NOT NULL'
      ',wad_fee taler_amount NOT NULL'
      ',deposit_fees taler_amount NOT NULL'
      ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
      ',purse_sig BYTEA NOT NULL CHECK (LENGTH(purse_sig)=64)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'list of purses aggregated in a wad according to the sending exchange'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'wad for which the given purse was included in the aggregation'
    ,'wad_in_serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'target account of the purse (must be at the local exchange)'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'public key of the purse that was merged'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'hash of the contract terms of the purse'
    ,'h_contract'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Time when the purse was set to expire'
    ,'purse_expiration'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Time when the merge was approved'
    ,'merge_timestamp'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Total amount in the purse'
    ,'amount_with_fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Total wad fees paid by the purse'
    ,'wad_fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Total deposit fees paid when depositing coins into the purse'
    ,'deposit_fees'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature by the receiving reserve, of purpose TALER_SIGNATURE_ACCOUNT_MERGE'
    ,'reserve_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature by the purse of purpose TALER_SIGNATURE_PURSE_MERGE'
    ,'purse_sig'
    ,table_name
    ,partition_suffix
  );
END $$;
CREATE FUNCTION constrain_table_wad_in_entries(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wad_in_entries';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wad_in_entry_serial_id_key'
    ' UNIQUE (wad_in_entry_serial_id) '
  );
END $$;
CREATE FUNCTION foreign_table_wad_in_entries()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wad_in_entries';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_wad_in'
    ' FOREIGN KEY(wad_in_serial_id)'
    ' REFERENCES wads_in (wad_in_serial_id) ON DELETE CASCADE'
  );
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wad_in_entries'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('wad_in_entries'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('wad_in_entries'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_wads_out(
  IN shard_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wads_out';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I '
      '(wad_out_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',wad_id BYTEA PRIMARY KEY CHECK (LENGTH(wad_id)=24)'
      ',partner_serial_id INT8 NOT NULL'
      ',amount taler_amount NOT NULL'
      ',execution_time INT8 NOT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (wad_id)'
    ,shard_suffix
  );
  PERFORM comment_partitioned_table(
     'Wire transfers made to another exchange to transfer purse funds'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Unique identifier of the wad, part of the wire transfer subject'
    ,'wad_id'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'target exchange of the wad'
    ,'partner_serial_id'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Amount that was wired'
    ,'amount'
    ,table_name
    ,shard_suffix
  );
  PERFORM comment_partitioned_column(
     'Time when the wire transfer was scheduled'
    ,'execution_time'
    ,table_name
    ,shard_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_wads_out(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wads_out';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wad_out_serial_id_key'
    ' UNIQUE (wad_out_serial_id) '
  );
END
$$;
CREATE FUNCTION foreign_table_wads_out()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wads_out';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_partner'
    ' FOREIGN KEY(partner_serial_id)'
    ' REFERENCES partners(partner_serial_id) ON DELETE CASCADE'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wads_out'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('wads_out'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('wads_out'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_wad_out_entries(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wad_out_entries';
BEGIN
  PERFORM create_partitioned_table(
     'CREATE TABLE %I '
     '(wad_out_entry_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     ',wad_out_serial_id INT8'
     ',reserve_pub BYTEA NOT NULL CHECK(LENGTH(reserve_pub)=32)'
     ',purse_pub BYTEA PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
     ',h_contract BYTEA NOT NULL CHECK(LENGTH(h_contract)=64)'
     ',purse_expiration INT8 NOT NULL'
     ',merge_timestamp INT8 NOT NULL'
     ',amount_with_fee taler_amount NOT NULL'
     ',wad_fee taler_amount NOT NULL'
     ',deposit_fees taler_amount NOT NULL'
     ',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
     ',purse_sig BYTEA NOT NULL CHECK (LENGTH(purse_sig)=64)'
     ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'Purses combined into a wad'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Wad the purse was part of'
    ,'wad_out_serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Target reserve for the purse'
    ,'reserve_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key of the purse'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Hash of the contract associated with the purse'
    ,'h_contract'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Time when the purse expires'
    ,'purse_expiration'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Time when the merge was approved'
    ,'merge_timestamp'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Total amount in the purse'
    ,'amount_with_fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'Wad fee charged to the purse'
    ,'wad_fee'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Total deposit fees charged to the purse'
    ,'deposit_fees'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature by the receiving reserve, of purpose TALER_SIGNATURE_ACCOUNT_MERGE'
    ,'reserve_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature by the purse of purpose TALER_SIGNATURE_PURSE_MERGE'
    ,'purse_sig'
    ,table_name
    ,partition_suffix
  );
END
$$;
CREATE FUNCTION constrain_table_wad_out_entries(
  IN partition_suffix TEXT
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wad_out_entries';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_wad_out_entry_serial_id_key'
    ' UNIQUE (wad_out_entry_serial_id) '
  );
END
$$;
CREATE FUNCTION foreign_table_wad_out_entries()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wad_out_entries';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_wad_out'
    ' FOREIGN KEY(wad_out_serial_id)'
    ' REFERENCES wads_out (wad_out_serial_id) ON DELETE CASCADE'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wad_out_entries'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('wad_out_entries'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('wad_out_entries'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE TABLE work_shards
  (shard_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,last_attempt INT8 NOT NULL
  ,start_row INT8 NOT NULL
  ,end_row INT8 NOT NULL
  ,completed BOOLEAN NOT NULL DEFAULT FALSE
  ,job_name TEXT NOT NULL
  ,PRIMARY KEY (job_name, start_row)
  );
COMMENT ON TABLE work_shards
  IS 'coordinates work between multiple processes working on the same job';
COMMENT ON COLUMN work_shards.shard_serial_id
  IS 'unique serial number identifying the shard';
COMMENT ON COLUMN work_shards.last_attempt
  IS 'last time a worker attempted to work on the shard';
COMMENT ON COLUMN work_shards.completed
  IS 'set to TRUE once the shard is finished by a worker';
COMMENT ON COLUMN work_shards.start_row
  IS 'row at which the shard scope starts, inclusive';
COMMENT ON COLUMN work_shards.end_row
  IS 'row at which the shard scope ends, exclusive';
COMMENT ON COLUMN work_shards.job_name
  IS 'unique name of the job the workers on this shard are performing';
CREATE INDEX work_shards_by_job_name_completed_last_attempt_index
  ON work_shards
  (job_name
  ,completed
  ,last_attempt ASC
  );
CREATE INDEX work_shards_by_end_row_index
  ON work_shards
  (end_row DESC);
CREATE INDEX work_shards_by_rows
  ON work_shards
  (job_name
  ,start_row
  ,end_row);
CREATE UNLOGGED TABLE revolving_work_shards
  (shard_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,last_attempt INT8 NOT NULL
  ,start_row INT4 NOT NULL
  ,end_row INT4 NOT NULL
  ,active BOOLEAN NOT NULL DEFAULT FALSE
  ,job_name TEXT NOT NULL
  ,PRIMARY KEY (job_name, start_row)
  );
COMMENT ON TABLE revolving_work_shards
  IS 'coordinates work between multiple processes working on the same job with partitions that need to be repeatedly processed; unlogged because on system crashes the locks represented by this table will have to be cleared anyway, typically using "taler-exchange-dbinit -s"';
COMMENT ON COLUMN revolving_work_shards.shard_serial_id
  IS 'unique serial number identifying the shard';
COMMENT ON COLUMN revolving_work_shards.last_attempt
  IS 'last time a worker attempted to work on the shard';
COMMENT ON COLUMN revolving_work_shards.active
  IS 'set to TRUE when a worker is active on the shard';
COMMENT ON COLUMN revolving_work_shards.start_row
  IS 'row at which the shard scope starts, inclusive';
COMMENT ON COLUMN revolving_work_shards.end_row
  IS 'row at which the shard scope ends, exclusive';
COMMENT ON COLUMN revolving_work_shards.job_name
  IS 'unique name of the job the workers on this shard are performing';
CREATE INDEX revolving_work_shards_by_job_name_active_last_attempt_index
  ON revolving_work_shards
  (job_name
  ,active
  ,last_attempt
  );
CREATE TABLE partners
  (partner_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,partner_master_pub BYTEA NOT NULL CHECK(LENGTH(partner_master_pub)=32)
  ,start_date INT8 NOT NULL
  ,end_date INT8 NOT NULL
  ,next_wad INT8 NOT NULL DEFAULT (0)
  ,wad_frequency INT8 NOT NULL
  ,wad_fee taler_amount NOT NULL
  ,master_sig BYTEA NOT NULL CHECK (LENGTH(master_sig)=64)
  ,partner_base_url TEXT NOT NULL
  ,PRIMARY KEY (partner_master_pub, start_date)
  );
COMMENT ON TABLE partners
  IS 'exchanges we do wad transfers to';
COMMENT ON COLUMN partners.partner_master_pub
  IS 'offline master public key of the partner';
COMMENT ON COLUMN partners.start_date
  IS 'starting date of the partnership';
COMMENT ON COLUMN partners.end_date
  IS 'end date of the partnership';
COMMENT ON COLUMN partners.next_wad
  IS 'at what time should we do the next wad transfer to this partner (frequently updated); set to forever after the end_date';
COMMENT ON COLUMN partners.wad_frequency
  IS 'how often do we promise to do wad transfers';
COMMENT ON COLUMN partners.wad_fee
  IS 'how high is the fee for a wallet to be added to a wad to this partner';
COMMENT ON COLUMN partners.partner_base_url
  IS 'base URL of the REST API for this partner';
COMMENT ON COLUMN partners.master_sig
  IS 'signature of our master public key affirming the partnership, of purpose TALER_SIGNATURE_MASTER_PARTNER_DETAILS';
CREATE INDEX IF NOT EXISTS partner_by_wad_time
  ON partners (next_wad ASC);
CREATE TABLE partner_accounts
  (payto_uri TEXT PRIMARY KEY
  ,partner_serial_id INT8 REFERENCES partners(partner_serial_id) ON DELETE CASCADE
  ,partner_master_sig BYTEA CHECK (LENGTH(partner_master_sig)=64)
  ,last_seen INT8 NOT NULL
  );
CREATE INDEX IF NOT EXISTS partner_accounts_index_by_partner_and_time
  ON partner_accounts (partner_serial_id,last_seen);
COMMENT ON TABLE partner_accounts
  IS 'Table with bank accounts of the partner exchange. Entries never expire as we need to remember the signature for the auditor.';
COMMENT ON COLUMN partner_accounts.payto_uri
  IS 'payto URI (RFC 8905) with the bank account of the partner exchange.';
COMMENT ON COLUMN partner_accounts.partner_master_sig
  IS 'Signature of purpose TALER_SIGNATURE_MASTER_WIRE_DETAILS by the partner master public key';
COMMENT ON COLUMN partner_accounts.last_seen
  IS 'Last time we saw this account as being active at the partner exchange. Used to select the most recent entry, and to detect when we should check again.';
CREATE OR REPLACE FUNCTION create_table_purse_actions(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_actions';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE IF NOT EXISTS %I'
      '(purse_pub BYTEA NOT NULL PRIMARY KEY CHECK(LENGTH(purse_pub)=32)'
      ',action_date INT8 NOT NULL'
      ',partner_serial_id INT8'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'purses awaiting some action by the router'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'public (contract) key of the purse'
    ,'purse_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'when is the purse ready for action'
    ,'action_date'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'wad target of an outgoing wire transfer, 0 for local, NULL if the purse is unmerged and thus the target is still unknown'
    ,'partner_serial_id'
    ,table_name
    ,partition_suffix
  );
END $$;
CREATE OR REPLACE FUNCTION master_table_purse_actions()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_actions';
BEGIN
  CREATE INDEX IF NOT EXISTS purse_action_by_target
    ON purse_actions
    (partner_serial_id,action_date);
  CREATE TRIGGER purse_requests_on_insert
    AFTER INSERT
    ON purse_requests
    FOR EACH ROW EXECUTE FUNCTION purse_requests_insert_trigger();
  COMMENT ON TRIGGER purse_requests_on_insert
          ON purse_requests
    IS 'Here we install an entry for the purse expiration.';
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('purse_actions'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('purse_actions'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE OR REPLACE FUNCTION create_table_purse_deletion(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_deletion';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE IF NOT EXISTS %I'
      '(purse_deletion_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',purse_sig BYTEA CHECK (LENGTH(purse_sig)=64)'
      ',purse_pub BYTEA NOT NULL CHECK (LENGTH(purse_pub)=32)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (purse_pub)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'signatures affirming explicit purse deletions'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'signature of type WALLET_PURSE_DELETE'
    ,'purse_sig'
    ,table_name
    ,partition_suffix
  );
END $$;
COMMENT ON FUNCTION create_table_purse_deletion
  IS 'Creates the purse_deletion table';
CREATE OR REPLACE FUNCTION constrain_table_purse_deletion(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_deletion';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
      ' ADD CONSTRAINT ' || table_name || '_delete_serial_key '
        'UNIQUE (purse_deletion_serial_id)'
  );
END $$;
CREATE OR REPLACE FUNCTION master_table_purse_requests_was_deleted (
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'purse_requests';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE exchange.' || table_name ||
    ' ADD COLUMN'
    ' was_deleted BOOLEAN NOT NULL DEFAULT(FALSE)'
  );
  COMMENT ON COLUMN purse_requests.was_deleted
    IS 'TRUE if the purse was explicitly deleted (purse must have an entry in the purse_deletion table)';
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('purse_deletion'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('purse_deletion'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('purse_requests_was_deleted'
    ,'exchange-0002'
    ,'master'
    ,TRUE
    ,FALSE);
CREATE OR REPLACE FUNCTION create_table_kyc_attributes(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'kyc_attributes';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE IF NOT EXISTS %I'
      '(kyc_attributes_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',collection_time INT8 NOT NULL'
      ',expiration_time INT8 NOT NULL'
      ',encrypted_attributes BYTEA NOT NULL'
      ',legitimization_serial INT8 NOT NULL'
      ',form_name TEXT DEFAULT(NULL)'
      ',by_aml_officer BOOL NOT NULL DEFAULT(FALSE)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'KYC data about particular payment addresses'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'hash of payto://-URI the attributes are about'
    ,'h_payto'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'time when the attributes were collected by the provider'
    ,'collection_time'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'time when the attributes should no longer be considered validated'
    ,'expiration_time'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     '(encrypted) JSON object (as string) with the attributes'
    ,'encrypted_attributes'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Reference the legitimization process for which these attributes are gathered for.'
    ,'legitimization_serial'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Name of the form (FORM_ID) that is captured in the attributes.'
    ,'form_name'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'TRUE if the attributes were submitted by an AML officer.'
    ,'by_aml_officer'
    ,table_name
    ,partition_suffix
  );
END $$;
COMMENT ON FUNCTION create_table_kyc_attributes
  IS 'Creates the kyc_attributes table';
CREATE OR REPLACE FUNCTION constrain_table_kyc_attributes(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'kyc_attributes';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
      ' ADD CONSTRAINT ' || table_name || '_serial_key '
        'UNIQUE (kyc_attributes_serial_id)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_h_payto_index '
    'ON ' || table_name || ' '
    '(h_payto);'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_expiration_time '
    'ON ' || table_name || ' '
    '(expiration_time ASC);'
  );
END $$;
CREATE OR REPLACE FUNCTION foreign_table_kyc_attributes()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'kyc_attributes';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_legitimization_processes'
    ' FOREIGN KEY (legitimization_serial) '
    ' REFERENCES legitimization_processes (legitimization_process_serial_id)' 
  );
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('kyc_attributes'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('kyc_attributes'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('kyc_attributes'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION create_table_kycauths_in(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'kycauths_in';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(kycauth_in_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',account_pub BYTEA CHECK (LENGTH(account_pub)=32)'
      ',wire_reference INT8 NOT NULL'
      ',credit taler_amount NOT NULL'
      ',wire_source_h_payto BYTEA CHECK (LENGTH(wire_source_h_payto)=32)'
      ',exchange_account_section TEXT NOT NULL'
      ',execution_date INT8 NOT NULL'
      ',PRIMARY KEY(wire_source_h_payto, wire_reference)'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (wire_source_h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'list of transfers to register a key for KYC authentication, one per incoming wire transfer'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Identifies the debited bank account and KYC status by the hash over the full payto URI'
    ,'wire_source_h_payto'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key to be associated with the account.'
    ,'account_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Amount that was transferred into the account'
    ,'credit'
    ,table_name
    ,partition_suffix
  );
END $$;
CREATE FUNCTION constrain_table_kycauths_in(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'kycauths_in';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_kycauth_in_serial_id_key'
    ' UNIQUE (kycauth_in_serial_id)'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('kycauths_in'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('kycauths_in'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE TABLE kyc_events (
  kyc_event_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY
 ,event_timestamp INT8 NOT NULL
 ,event_type TEXT NOT NULL
);
COMMENT ON TABLE kyc_events
  IS 'Records of key events for statistics. Populated via triggers.';
COMMENT ON COLUMN kyc_events.event_type
  IS 'Name of the event, such as account-open or sar-filed';
COMMENT ON COLUMN kyc_events.event_timestamp
  IS 'When did the event occur; timestamp in rounded absolute time';
CREATE INDEX kyc_event_index
  ON kyc_events(event_type,event_timestamp);
CREATE TABLE aml_staff
  (aml_staff_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,decider_pub BYTEA PRIMARY KEY CHECK (LENGTH(decider_pub)=32)
  ,master_sig BYTEA CHECK (LENGTH(master_sig)=64)
  ,decider_name TEXT NOT NULL
  ,is_active BOOLEAN NOT NULL
  ,read_only BOOLEAN NOT NULL
  ,last_change INT8 NOT NULL
  );
COMMENT ON TABLE aml_staff
  IS 'Table with AML staff members the exchange uses or has used in the past. Entries never expire as we need to remember the last_change column indefinitely.';
COMMENT ON COLUMN aml_staff.decider_pub
  IS 'Public key of the AML staff member.';
COMMENT ON COLUMN aml_staff.master_sig
  IS 'The master public key signature on the AML staff member status, of type TALER_SIGNATURE_MASTER_AML_KEY.';
COMMENT ON COLUMN aml_staff.decider_name
  IS 'Name of the staff member.';
COMMENT ON COLUMN aml_staff.is_active
  IS 'true if we are currently supporting the use of this AML staff member.';
COMMENT ON COLUMN aml_staff.is_active
  IS 'true if the member has read-only access.';
COMMENT ON COLUMN aml_staff.last_change
  IS 'Latest time when active status changed. Used to detect replays of old messages.';
CREATE OR REPLACE FUNCTION create_table_aml_history(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aml_history';
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE IF NOT EXISTS %I'
      '(aml_history_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',justification TEXT NOT NULL'
      ',decider_pub BYTEA CHECK (LENGTH(decider_pub)=32)'
      ',decider_sig BYTEA CHECK (LENGTH(decider_sig)=64)'
      ',outcome_serial_id INT8 NOT NULL'
      ',kyc_attributes_hash BYTEA CHECK(LENGTH(kyc_attributes_hash)=64) DEFAULT NULL'
      ',kyc_attributes_serial_id INT8 DEFAULT NULL'
    ') %s ;'
    ,table_name
    ,'PARTITION BY HASH (h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'AML decision history for a particular payment destination'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'hash of the payto://-URI this AML history is about'
    ,'h_payto'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'human-readable justification for the status change'
    ,'justification'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Public key of the staff member who made the AML decision'
    ,'decider_pub'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Signature key of the staff member affirming the AML decision; of type AML_DECISION'
    ,'decider_sig'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Actual outcome for the account (included in what decider_sig signs over)'
    ,'outcome_serial_id'
    ,table_name
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'Hash of the new attributes inserted by the AML officer.'
    ,'kyc_attributes_hash'
    ,'aml_history'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'Attributes inserted by the AML officer.'
    ,'kyc_attributes_serial_id'
    ,'aml_history'
    ,NULL
  );
END $$;
COMMENT ON FUNCTION create_table_aml_history
  IS 'Creates the aml_history table';
CREATE OR REPLACE FUNCTION constrain_table_aml_history(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aml_history';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
      ' ADD CONSTRAINT ' || table_name || '_serial_key '
        'UNIQUE (aml_history_serial_id)'
  );
  EXECUTE FORMAT (
    'CREATE INDEX ' || table_name || '_main_index '
    'ON ' || table_name || ' '
    '(h_payto);'
  );
END $$;
CREATE FUNCTION foreign_table_aml_history()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'aml_history';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_legitimization_outcome'
    ' FOREIGN KEY (outcome_serial_id)'
    ' REFERENCES legitimization_outcomes (outcome_serial_id)'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_key_kyc_attributes'
    ' FOREIGN KEY (kyc_attributes_serial_id)'
    ' REFERENCES kyc_attributes (kyc_attributes_serial_id)');
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('aml_history'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('aml_history'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('aml_history'
    ,'exchange-0002'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE TYPE statistic_range AS
  ENUM('century', 'decade', 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second');
CREATE TYPE statistic_type AS
  ENUM('amount', 'number');
CREATE TABLE exchange_statistic_bucket_meta
  (bmeta_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  ,origin TEXT NOT NULL
  ,slug TEXT NOT NULL
  ,description TEXT NOT NULL
  ,stype statistic_type NOT NULL
  ,ranges statistic_range[] NOT NULL
  ,ages INT4[] NOT NULL
  ,UNIQUE(slug,stype)
  ,CONSTRAINT equal_array_length
    CHECK (array_length(ranges,1) =
           array_length(ages,1))
  );
COMMENT ON TABLE exchange_statistic_bucket_meta
  IS 'meta data about a statistic with events falling into buckets we are tracking';
COMMENT ON COLUMN exchange_statistic_bucket_meta.bmeta_serial_id
  IS 'unique identifier for this type of bucket statistic we are tracking';
COMMENT ON COLUMN exchange_statistic_bucket_meta.origin
  IS 'which customization schema does this statistic originate from (used for easy deletion)';
COMMENT ON COLUMN exchange_statistic_bucket_meta.slug
  IS 'keyword (or name) of the statistic; identifies what the statistic is about; should be a slug suitable for a URI path';
COMMENT ON COLUMN exchange_statistic_bucket_meta.description
  IS 'description of the statistic being tracked';
COMMENT ON COLUMN exchange_statistic_bucket_meta.stype
  IS 'statistic type, what kind of data is being tracked, amount or number';
COMMENT ON COLUMN exchange_statistic_bucket_meta.ranges
  IS 'size of the buckets that are being kept for this statistic';
COMMENT ON COLUMN exchange_statistic_bucket_meta.ages
  IS 'determines how long into the past we keep buckets for the range at the given index around (in generations)';
CREATE INDEX exchange_statistic_bucket_meta_by_origin
  ON exchange_statistic_bucket_meta
  (origin);
CREATE FUNCTION create_table_exchange_statistic_bucket_counter (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table (
    'CREATE TABLE %I'
      '(bmeta_serial_id INT8 NOT NULL'
      ' REFERENCES exchange_statistic_bucket_meta (bmeta_serial_id) ON DELETE CASCADE'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',bucket_start INT8 NOT NULL'
      ',bucket_range statistic_range NOT NULL'
      ',cumulative_number INT8 NOT NULL'
      ',UNIQUE (h_payto,bmeta_serial_id,bucket_start,bucket_range)'
    ') %s;'
    ,'exchange_statistic_bucket_counter'
    ,'PARTITION BY HASH (h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'various numeric statistics (cumulative counters) being tracked by bucket into which they fall'
    ,'exchange_statistic_bucket_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'identifies what the statistic is about'
    ,'bmeta_serial_id'
    ,'exchange_statistic_bucket_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'identifies an account (hash of normalized payto) for which the statistic is kept, NULL for global statistics'
    ,'h_payto'
    ,'exchange_statistic_bucket_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'start date for the bucket in seconds since the epoch'
    ,'bucket_start'
    ,'exchange_statistic_bucket_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'range of the bucket'
    ,'bucket_range'
    ,'exchange_statistic_bucket_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'aggregate (sum) of tracked by the statistic; what exactly is tracked is determined by the keyword'
    ,'cumulative_number'
    ,'exchange_statistic_bucket_counter'
    ,partition_suffix
  );
END $$;
CREATE FUNCTION create_table_exchange_statistic_bucket_amount (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table (
    'CREATE TABLE %I'
      '(bmeta_serial_id INT8 NOT NULL'
       ' REFERENCES exchange_statistic_bucket_meta (bmeta_serial_id) ON DELETE CASCADE'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',bucket_start INT8 NOT NULL'
      ',bucket_range statistic_range NOT NULL'
      ',cumulative_value taler_amount NOT NULL'
      ',CHECK ((cumulative_value).val IS NOT NULL AND (cumulative_value).frac IS NOT NULL)'
      ',UNIQUE (h_payto,bmeta_serial_id,bucket_start,bucket_range)'
    ') %s;'
    ,'exchange_statistic_bucket_amount'
    ,'PARTITION BY HASH(h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table (
     'various amount statistics being tracked'
    ,'exchange_statistic_bucket_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'identifies what the statistic is about'
    ,'bmeta_serial_id'
    ,'exchange_statistic_bucket_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column (
     'identifies an account (hash of normalized payto) for which the statistic is kept, NULL for global statistics'
    ,'h_payto'
    ,'exchange_statistic_bucket_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'start date for the bucket in seconds since the epoch'
    ,'bucket_start'
    ,'exchange_statistic_bucket_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'range of the bucket'
    ,'bucket_range'
    ,'exchange_statistic_bucket_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'amount being tracked'
    ,'cumulative_value'
    ,'exchange_statistic_bucket_amount'
    ,partition_suffix
  );
END $$;
CREATE TABLE exchange_statistic_interval_meta
  (imeta_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  ,origin TEXT NOT NULL
  ,slug TEXT NOT NULL
  ,description TEXT NOT NULL
  ,stype statistic_type NOT NULL
  ,ranges INT8[] NOT NULL CHECK (array_length(ranges,1) > 0)
  ,precisions INT8[] NOT NULL CHECK (array_length(precisions,1) > 0)
  ,UNIQUE(slug,stype)
  ,CONSTRAINT equal_array_length
    CHECK (array_length(ranges,1) =
           array_length(precisions,1))
  );
COMMENT ON TABLE exchange_statistic_interval_meta
  IS 'meta data about an interval statistic we are tracking';
COMMENT ON COLUMN exchange_statistic_interval_meta.imeta_serial_id
  IS 'unique identifier for this type of interval statistic we are tracking';
COMMENT ON COLUMN exchange_statistic_interval_meta.origin
  IS 'which customization schema does this statistic originate from (used for easy deletion)';
COMMENT ON COLUMN exchange_statistic_interval_meta.slug
  IS 'keyword (or name) of the statistic; identifies what the statistic is about; should be a slug suitable for a URI path';
COMMENT ON COLUMN exchange_statistic_interval_meta.description
  IS 'description of the statistic being tracked';
COMMENT ON COLUMN exchange_statistic_interval_meta.stype
  IS 'statistic type, what kind of data is being tracked, amount or number';
COMMENT ON COLUMN exchange_statistic_interval_meta.ranges
  IS 'range of values that is being kept for this statistic, in seconds, must be monotonically increasing';
COMMENT ON COLUMN exchange_statistic_interval_meta.precisions
  IS 'determines how precisely we track which events fall into the range at the same index (allowing us to coalesce events with timestamps in proximity close to the given precision), in seconds, 0 is not allowed';
CREATE INDEX exchange_statistic_interval_meta_by_origin
  ON exchange_statistic_interval_meta
  (origin);
CREATE FUNCTION create_table_exchange_statistic_counter_event (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(nevent_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY'
      ',imeta_serial_id INT8'
      ' REFERENCES exchange_statistic_interval_meta (imeta_serial_id) ON DELETE CASCADE'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',slot INT8 NOT NULL'
      ',delta INT8 NOT NULL'
      ',UNIQUE (h_payto,imeta_serial_id,slot)'
    ') %s ;'
    ,'exchange_statistic_counter_event'
    ,'PARTITION BY HASH(h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'number to decrement an interval statistic by when a certain time value is reached'
    ,'exchange_statistic_counter_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'unique identifier for this number event'
    ,'nevent_serial_id'
    ,'exchange_statistic_counter_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies what the statistic is about; must be of stype number'
    ,'imeta_serial_id'
    ,'exchange_statistic_counter_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies an account (hash of normalized payto) for which the statistic is kept, NULL for global statistics'
    ,'h_payto'
    ,'exchange_statistic_counter_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies the time slot at which the given event(s) happened, rounded down by the respective precisions value'
    ,'slot'
    ,'exchange_statistic_counter_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'total cumulative number that was added at the time identified by slot'
    ,'delta'
    ,'exchange_statistic_counter_event'
    ,partition_suffix
  );
END $$;
CREATE FUNCTION constrain_table_exchange_statistic_counter_event(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'exchange_statistic_counter_event';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_nevent_serial_id_key'
    ' UNIQUE (nevent_serial_id)'
  );
END $$;
CREATE FUNCTION create_table_exchange_statistic_interval_counter (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(imeta_serial_id INT8 NOT NULL'
      ' REFERENCES exchange_statistic_interval_meta (imeta_serial_id) ON DELETE CASCADE'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',range INT8 NOT NULL'
      ',event_delimiter INT8 NOT NULL'
      ',cumulative_number INT8 NOT NULL'
      ',UNIQUE (h_payto,imeta_serial_id,range)'
    ') %s ;'
    ,'exchange_statistic_interval_counter'
    ,'PARTITION BY HASH(h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'various numeric statistics (cumulative counters) being tracked'
    ,'exchange_statistic_interval_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies what the statistic is about'
    ,'imeta_serial_id'
    ,'exchange_statistic_interval_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies an account (hash of normalized payto) for which the statistic is kept, NULL for global statistics'
    ,'h_payto'
    ,'exchange_statistic_interval_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'for which range is this the counter; note that the cumulative_number excludes the values already stored in smaller ranges'
    ,'range'
    ,'exchange_statistic_interval_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'determines the last event currently included in the interval'
    ,'event_delimiter'
    ,'exchange_statistic_interval_counter'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'aggregate (sum) of tracked by the statistic; what exactly is tracked is determined by the keyword'
    ,'cumulative_number'
    ,'exchange_statistic_interval_counter'
    ,partition_suffix
  );
END $$;
CREATE FUNCTION foreign_table_exchange_statistic_interval_counter()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'exchange_statistic_interval_counter';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_event_delimiter_foreign_key'
    ' FOREIGN KEY (event_delimiter) '
    ' REFERENCES exchange_statistic_counter_event (nevent_serial_id) ON DELETE RESTRICT'
  );
END $$;
CREATE FUNCTION create_table_exchange_statistic_amount_event (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(aevent_serial_id INT8 GENERATED BY DEFAULT AS IDENTITY'
      ',imeta_serial_id INT8'
      ' REFERENCES exchange_statistic_interval_meta (imeta_serial_id) ON DELETE CASCADE'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',slot INT8 NOT NULL'
      ',delta taler_amount NOT NULL'
      ',CHECK ((delta).val IS NOT NULL AND (delta).frac IS NOT NULL)'
      ',CONSTRAINT event_key UNIQUE (h_payto,imeta_serial_id,slot)'
    ') %s ;'
    ,'exchange_statistic_amount_event'
    ,'PARTITION BY HASH(h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
    'amount to decrement an interval statistic by when a certain time value is reached'
    ,'exchange_statistic_amount_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'unique identifier for this amount event'
    ,'aevent_serial_id'
    ,'exchange_statistic_amount_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies what the statistic is about; must be of clazz interval and of stype amount'
    ,'imeta_serial_id'
    ,'exchange_statistic_amount_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies an account (hash of normalized payto) for which the statistic is kept, NULL for global statistics'
    ,'h_payto'
    ,'exchange_statistic_amount_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies the time slot at which the given event(s) happened'
    ,'slot'
    ,'exchange_statistic_amount_event'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'total cumulative amount that was added at the time identified by slot'
    ,'delta'
    ,'exchange_statistic_amount_event'
    ,partition_suffix
  );
END $$;
CREATE FUNCTION constrain_table_exchange_statistic_amount_event(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT default 'exchange_statistic_amount_event';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_aevent_serial_id_key'
    ' UNIQUE (aevent_serial_id)'
  );
END $$;
CREATE FUNCTION create_table_exchange_statistic_interval_amount (
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(imeta_serial_id INT8 NOT NULL'
      ' REFERENCES exchange_statistic_interval_meta (imeta_serial_id) ON DELETE CASCADE'
      ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
      ',event_delimiter INT8 NOT NULL'
      ',range INT8 NOT NULL'
      ',cumulative_value taler_amount NOT NULL'
      ',CHECK ((cumulative_value).val IS NOT NULL AND (cumulative_value).frac IS NOT NULL)'
      ',UNIQUE (h_payto,imeta_serial_id,range)'
    ') %s ;'
    ,'exchange_statistic_interval_amount'
    ,'PARTITION BY HASH(h_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'various amount statistics being tracked'
    ,'exchange_statistic_interval_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies what the statistic is about'
    ,'imeta_serial_id'
    ,'exchange_statistic_interval_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'identifies an account (hash of normalized payto) for which the statistic is kept, NULL for global statistics'
    ,'h_payto'
    ,'exchange_statistic_interval_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'for which range is this the counter; note that the cumulative_number excludes the values already stored in smaller ranges'
    ,'range'
    ,'exchange_statistic_interval_amount'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
    'amount affected by the event'
    ,'cumulative_value'
    ,'exchange_statistic_interval_amount'
    ,partition_suffix
  );
END $$;
CREATE FUNCTION foreign_table_exchange_statistic_interval_amount()
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'exchange_statistic_interval_amount';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_event_delimiter_foreign_key'
    ' FOREIGN KEY (event_delimiter) '
    ' REFERENCES exchange_statistic_amount_event (aevent_serial_id) ON DELETE RESTRICT'
  );
END $$;
CREATE TYPE exchange_statistic_interval_number_get_return_value
  AS
  (range INT8
  ,rvalue INT8
  );
COMMENT ON TYPE exchange_statistic_interval_number_get_return_value
  IS 'Return type for exchange_statistic_interval_number_get stored procedure';
CREATE TYPE exchange_statistic_interval_amount_get_return_value
  AS
  (range INT8
  ,rvalue taler_amount
  );
COMMENT ON TYPE exchange_statistic_interval_amount_get_return_value
  IS 'Return type for exchange_statistic_interval_amount_get stored procedure';
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('exchange_statistic_bucket_counter'
    ,'exchange-0009'
    ,'create'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_bucket_amount'
    ,'exchange-0009'
    ,'create'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_counter_event'
    ,'exchange-0009'
    ,'create'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_counter_event'
    ,'exchange-0009'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_interval_counter'
    ,'exchange-0009'
    ,'create'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_interval_counter'
    ,'exchange-0009'
    ,'foreign'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_amount_event'
    ,'exchange-0009'
    ,'create'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_amount_event'
    ,'exchange-0009'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_interval_amount'
    ,'exchange-0009'
    ,'create'
    ,TRUE
    ,FALSE),
    ('exchange_statistic_interval_amount'
    ,'exchange-0009'
    ,'foreign'
    ,TRUE
    ,FALSE);
COMMIT;
BEGIN;
SELECT _v.register_patch('exchange-0003', NULL, NULL);
SET search_path TO exchange;
CREATE FUNCTION create_table_kyc_targets(
  IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
  my_rec RECORD;
  my_payto TEXT;
  my_is_wallet BOOL;
  wtc CURSOR FOR
   SELECT
     access_token
    ,target_pub
    ,h_normalized_payto
    ,aml_program_lock_timeout
    ,payto_uri
     FROM exchange.wire_targets;
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE %I'
      '(kyc_target_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
      ',h_normalized_payto BYTEA PRIMARY KEY CHECK(LENGTH(h_normalized_payto)=32)'
      ',access_token BYTEA CHECK(LENGTH(access_token)=32)'
      ' DEFAULT random_bytea(32)'
      ',target_pub BYTEA CHECK(LENGTH(target_pub)=32) DEFAULT NULL'
      ',aml_program_lock_timeout INT8 DEFAULT NULL'
      ',is_wallet BOOL'
    ') %s ;'
    ,'kyc_targets'
    ,'PARTITION BY HASH (h_normalized_payto)'
    ,partition_suffix
  );
  PERFORM comment_partitioned_table(
     'All identities for KYC purposes based on normalized payto URIs'
    ,'kyc_targets'
    ,partition_suffix
  );
  PERFORM comment_partitioned_column(
     'high-entropy random value that is used as a bearer token used to authenticate access to the KYC SPA and its state (without requiring a signature)'
    ,'access_token'
    ,'kyc_targets'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'Public key of a merchant instance or reserve to authenticate access; NULL if KYC is not allowed for the account (if there was no incoming KYC wire transfer yet); updated, thus NOT available to the auditor'
    ,'target_pub'
    ,'kyc_targets'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'hash over the normalized payto URI for this account; used for KYC operations'
    ,'h_normalized_payto'
    ,'kyc_targets'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'If non-NULL, an AML program should be running and it holds a lock on this account, thus other AML programs should not be started concurrently. Given the possibility of crashes, the lock automatically expires at the time value given in this column. At that time, the lock can be considered stale.'
    ,'aml_program_lock_timeout'
    ,'kyc_targets'
    ,NULL
  );
  PERFORM comment_partitioned_column(
     'True if this KYC account is for a wallet, false if it is for a bank account'
    ,'is_wallet'
    ,'kyc_targets'
    ,NULL
  );
  FOR my_rec IN wtc
  LOOP
    my_payto = my_rec.payto_uri;
    my_is_wallet
      = (LOWER (SUBSTRING (my_payto, 22)) =
         'payto://taler-reserve/') OR
        (LOWER (SUBSTRING (my_payto, 27)) =
         'payto://taler-reserve-http/');
    INSERT INTO kyc_targets
      (h_normalized_payto
      ,access_token
      ,target_pub
      ,aml_program_lock_timeout
      ,is_wallet
      ) VALUES (
       my_rec.h_normalized_payto
      ,my_rec.access_token
      ,my_rec.target_pub
      ,my_rec.aml_program_lock_timeout
      ,my_is_wallet
      )
      ON CONFLICT DO NOTHING;
  END LOOP;
END $$;
CREATE FUNCTION constrain_table_kyc_targets(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'kyc_targets';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_kyc_target_serial_id_key'
    ' UNIQUE (kyc_target_serial_id)'
  );
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_kyc_target_access_token_unique'
    ' UNIQUE (access_token)'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('kyc_targets'
    ,'exchange-0003'
    ,'create'
    ,TRUE
    ,FALSE),
    ('kyc_targets'
    ,'exchange-0003'
    ,'constrain'
    ,TRUE
    ,FALSE);
CREATE FUNCTION foreign_table_legitimization_measures3()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_measures';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' DROP CONSTRAINT ' || table_name || '_foreign_key_access_token');
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_key_access_token'
    ' FOREIGN KEY (access_token)'
    ' REFERENCES kyc_targets (access_token)'
    '  ON DELETE CASCADE');
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('legitimization_measures3'
    ,'exchange-0003'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION foreign_table_legitimization_outcomes3()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_outcomes';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_key_h_payto'
    ' FOREIGN KEY (h_payto)'
    ' REFERENCES kyc_targets (h_normalized_payto)'
    '  ON DELETE CASCADE');
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('legitimization_outcomes3'
    ,'exchange-0003'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION foreign_table_legitimization_processes3()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'legitimization_processes';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_foreign_key_h_payto'
    ' FOREIGN KEY (h_payto)'
    ' REFERENCES kyc_targets (h_normalized_payto)'
    '  ON DELETE CASCADE');
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('legitimization_processes3'
    ,'exchange-0003'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION alter_table_wire_targets3()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  ALTER TABLE wire_targets
    DROP COLUMN access_token,
    DROP COLUMN aml_program_lock_timeout,
    DROP COLUMN target_pub;
END $$;
CREATE FUNCTION foreign_table_wire_targets3()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'wire_targets';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD CONSTRAINT ' || table_name || '_fk_kyc_targets'
    ' FOREIGN KEY (h_normalized_payto) REFERENCES kyc_targets (h_normalized_payto)'
  );
END
$$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wire_targets3'
    ,'exchange-0003'
    ,'alter'
    ,TRUE
    ,FALSE),
    ('wire_targets3'
    ,'exchange-0003'
    ,'foreign'
    ,TRUE
    ,FALSE);
CREATE FUNCTION master_table_reserves_out3()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  DROP TABLE IF EXISTS reserves_out;
END $$;
COMMENT ON FUNCTION master_table_reserves_out3()
  IS 'Deletes the obsolete reserves_out table.';
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('reserves_out3'
    ,'exchange-0003'
    ,'master'
    ,TRUE
    ,FALSE);
COMMIT;
BEGIN;
SELECT _v.register_patch('exchange-0004', NULL, NULL);
SET search_path TO exchange;
CREATE OR REPLACE FUNCTION constrain_table_kyc_attributes4(
  IN partition_suffix TEXT
)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'kyc_attributes';
BEGIN
  table_name = concat_ws('_', table_name, partition_suffix);
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
      ' ADD CONSTRAINT ' || table_name || '_legitimization_serial '
        'UNIQUE (h_payto,legitimization_serial)'
  );
END $$;
INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('kyc_attributes4'
    ,'exchange-0004'
    ,'constrain'
    ,TRUE
    ,FALSE);
COMMIT;
--
-- This file is part of TALER
-- Copyright (C) 2025 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
--

BEGIN;

SELECT _v.register_patch('exchange-0005', NULL, NULL);

SET search_path TO exchange;

-- convert all JSON-valued fields from TEXT to JSONB

CREATE FUNCTION alter_table_wire_accounts5()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  ALTER TABLE wire_accounts
    ALTER COLUMN debit_restrictions
      TYPE JSONB
      USING debit_restrictions::JSONB,
    ALTER COLUMN credit_restrictions
      TYPE JSONB
      USING credit_restrictions::JSONB;
END
$$;


CREATE FUNCTION alter_table_legitimization_outcomes5()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  ALTER TABLE legitimization_outcomes
    ALTER COLUMN jproperties
      TYPE JSONB
      USING jproperties::JSONB,
    ALTER COLUMN jnew_rules
      TYPE JSONB
      USING jnew_rules::JSONB;
END
$$;


CREATE FUNCTION alter_table_legitimization_measures5()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  ALTER TABLE legitimization_measures
    ALTER COLUMN jmeasures
      TYPE JSONB
      USING jmeasures::JSONB;
END
$$;


CREATE FUNCTION alter_table_policy_fulfillments5()
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  ALTER TABLE policy_fulfillments
    ALTER COLUMN fulfillment_proof
      TYPE JSONB
      USING fulfillment_proof::JSONB;
END
$$;


CREATE FUNCTION alter_table_kyc_targets5()
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
  table_name TEXT DEFAULT 'kyc_targets';
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE ' || table_name ||
    ' ADD COLUMN  open_time INT8 DEFAULT(NULL)'
    ',ADD COLUMN close_time INT8 DEFAULT(NULL);'
  );
END
$$;


INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('wire_accounts5'
    ,'exchange-0005'
    ,'alter'
    ,TRUE
    ,FALSE),
    ('legitimization_outcomes5'
    ,'exchange-0005'
    ,'alter'
    ,TRUE
    ,FALSE),
    ('legitimization_measures5'
    ,'exchange-0005'
    ,'alter'
    ,TRUE
    ,FALSE),
    ('policy_fulfillments5'
    ,'exchange-0005'
    ,'alter'
    ,TRUE
    ,FALSE),
    ('kyc_targets5'
    ,'exchange-0005'
    ,'alter'
    ,TRUE
    ,FALSE);


COMMIT;
BEGIN;
SET search_path TO exchange;
CREATE OR REPLACE FUNCTION create_partitioned_table(
   IN table_definition TEXT 
  ,IN table_name TEXT 
  ,IN main_table_partition_str TEXT 
  ,IN partition_suffix TEXT DEFAULT NULL 
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  IF (partition_suffix IS NULL)
  THEN
    main_table_partition_str = '';
  ELSE
    IF (partition_suffix::int > 0)
    THEN
      table_name=table_name || '_' || partition_suffix;
    END IF;
  END IF;
  EXECUTE FORMAT(
    table_definition,
    table_name,
    main_table_partition_str
  );
END $$;
COMMENT ON FUNCTION create_partitioned_table
  IS 'Generic function to create a table that is partitioned or sharded.';
CREATE OR REPLACE FUNCTION comment_partitioned_table(
   IN table_comment TEXT
  ,IN table_name TEXT
  ,IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  IF ( (partition_suffix IS NOT NULL) AND
       (partition_suffix::int > 0) )
  THEN
    table_name=table_name || '_' || partition_suffix;
  END IF;
  EXECUTE FORMAT(
     'COMMENT ON TABLE %s IS %s'
    ,table_name
    ,quote_literal(table_comment)
  );
END $$;
COMMENT ON FUNCTION comment_partitioned_table
  IS 'Generic function to create a comment on table that is partitioned.';
CREATE OR REPLACE FUNCTION comment_partitioned_column(
   IN table_comment TEXT
  ,IN column_name TEXT
  ,IN table_name TEXT
  ,IN partition_suffix TEXT DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  IF ( (partition_suffix IS NOT NULL) AND
       (partition_suffix::int > 0) )
  THEN
    table_name=table_name || '_' || partition_suffix;
  END IF;
  EXECUTE FORMAT(
     'COMMENT ON COLUMN %s.%s IS %s'
    ,table_name
    ,column_name
    ,quote_literal(table_comment)
  );
END $$;
COMMENT ON FUNCTION comment_partitioned_column
  IS 'Generic function to create a comment on column of a table that is partitioned.';
CREATE OR REPLACE FUNCTION exchange_do_create_tables(
  num_partitions INTEGER
)
  RETURNS VOID
  LANGUAGE plpgsql
AS $$
DECLARE
  tc CURSOR FOR
    SELECT table_serial_id
          ,name
          ,action
          ,partitioned
          ,by_range
      FROM exchange.exchange_tables
     WHERE NOT finished
     ORDER BY table_serial_id ASC;
BEGIN
  FOR rec IN tc
  LOOP
    CASE rec.action
    WHEN 'create'
    THEN
      IF (rec.partitioned AND
          (num_partitions IS NOT NULL))
      THEN
        EXECUTE FORMAT(
          'SELECT exchange.create_table_%s (%s)'::text
          ,rec.name
          ,quote_literal('0')
        );
        IF (rec.by_range OR
            (num_partitions = 0))
        THEN
          IF (rec.by_range)
          THEN
            EXECUTE FORMAT(
              'CREATE TABLE exchange.%s_default'
              ' PARTITION OF %s'
              ' DEFAULT'
             ,rec.name
             ,rec.name
            );
          ELSE
            EXECUTE FORMAT(
              'CREATE TABLE exchange.%s_default'
              ' PARTITION OF %s'
              ' FOR VALUES WITH (MODULUS 1, REMAINDER 0)'
             ,rec.name
             ,rec.name
            );
          END IF;
        ELSE
          FOR i IN 1..num_partitions LOOP
            EXECUTE FORMAT(
               'CREATE TABLE exchange.%I'
               ' PARTITION OF %I'
               ' FOR VALUES WITH (MODULUS %s, REMAINDER %s)'
              ,rec.name || '_' || i
              ,rec.name
              ,num_partitions
              ,i-1
            );
          END LOOP;
        END IF;
      ELSE
        EXECUTE FORMAT(
          'SELECT exchange.create_table_%s ()'::text
          ,rec.name
        );
      END IF;
      EXECUTE FORMAT(
        'DROP FUNCTION exchange.create_table_%s'::text
          ,rec.name
        );
    WHEN 'alter'
    THEN
      EXECUTE FORMAT(
        'SELECT exchange.alter_table_%s ()'::text
        ,rec.name
      );
      EXECUTE FORMAT(
        'DROP FUNCTION exchange.alter_table_%s'::text
          ,rec.name
        );
    WHEN 'constrain'
    THEN
      ASSERT rec.partitioned, 'constrain action only applies to partitioned tables';
      IF (num_partitions IS NULL)
      THEN
        EXECUTE FORMAT(
           'SELECT exchange.constrain_table_%s (NULL)'::text
          ,rec.name
        );
      ELSE
        IF ( (num_partitions = 0) OR
             (rec.by_range) )
        THEN
          EXECUTE FORMAT(
             'SELECT exchange.constrain_table_%s (%s)'::text
            ,rec.name
            ,quote_literal('default')
          );
        ELSE
          FOR i IN 1..num_partitions LOOP
            EXECUTE FORMAT(
              'SELECT exchange.constrain_table_%s (%s)'::text
              ,rec.name
              ,quote_literal(i)
            );
          END LOOP;
        END IF;
      END IF;
      EXECUTE FORMAT(
        'DROP FUNCTION exchange.constrain_table_%s'::text
          ,rec.name
        );
    WHEN 'foreign'
    THEN
      IF (num_partitions IS NULL)
      THEN
        EXECUTE FORMAT(
          'SELECT exchange.foreign_table_%s (%s)'::text
          ,rec.name
          ,NULL
        );
      END IF;
      EXECUTE FORMAT(
        'DROP FUNCTION exchange.foreign_table_%s'::text
          ,rec.name
        );
    WHEN 'master'
    THEN
      EXECUTE FORMAT(
        'SELECT exchange.master_table_%s ()'::text
        ,rec.name
      );
      EXECUTE FORMAT(
        'DROP FUNCTION exchange.master_table_%s'::text
          ,rec.name
        );
    ELSE
      ASSERT FALSE, 'unsupported action type: ' || rec.action;
    END CASE; 
    UPDATE exchange.exchange_tables
       SET finished=TRUE
     WHERE table_serial_id=rec.table_serial_id;
  END LOOP; 
END $$;
COMMENT ON FUNCTION exchange_do_create_tables
  IS 'Creates all tables for the given number of partitions that need creating. Does NOT support sharding.';
CREATE OR REPLACE FUNCTION amount_normalize(
    IN amount taler_amount
  ,OUT normalized taler_amount
)
LANGUAGE plpgsql
AS $$
BEGIN
  normalized.val = amount.val + amount.frac / 100000000;
  normalized.frac = amount.frac % 100000000;
END $$;
COMMENT ON FUNCTION amount_normalize
  IS 'Returns the normalized amount by adding to the .val the value of (.frac / 100000000) and removing the modulus 100000000 from .frac.';
CREATE OR REPLACE FUNCTION amount_add(
   IN a taler_amount
  ,IN b taler_amount
  ,OUT sum taler_amount
)
LANGUAGE plpgsql
AS $$
BEGIN
  sum = (a.val + b.val, a.frac + b.frac);
  CALL amount_normalize(sum ,sum);
  IF (sum.val > (1<<52))
  THEN
    RAISE EXCEPTION 'addition overflow';
  END IF;
END $$;
COMMENT ON FUNCTION amount_add
  IS 'Returns the normalized sum of two amounts. It raises an exception when the resulting .val is larger than 2^52';
CREATE OR REPLACE FUNCTION amount_left_minus_right(
  IN l taler_amount
 ,IN r taler_amount
 ,OUT diff taler_amount
 ,OUT ok BOOLEAN
)
LANGUAGE plpgsql
AS $$
BEGIN
IF (l.val > r.val)
THEN
  ok = TRUE;
  IF (l.frac >= r.frac)
  THEN
    diff.val = l.val - r.val;
    diff.frac = l.frac - r.frac;
  ELSE
    diff.val = l.val - r.val - 1;
    diff.frac = l.frac + 100000000 - r.frac;
  END IF;
ELSE
  IF (l.val = r.val) AND (l.frac >= r.frac)
  THEN
    diff.val = 0;
    diff.frac = l.frac - r.frac;
    ok = TRUE;
  ELSE
    diff = (-1, -1);
    ok = FALSE;
  END IF;
END IF;
RETURN;
END $$;
COMMENT ON FUNCTION amount_left_minus_right
  IS 'Subtracts the right amount from the left and returns the difference and TRUE, if the left amount is larger than the right, or an invalid amount and FALSE otherwise.';
DROP FUNCTION IF EXISTS exchange_do_withdraw;
CREATE FUNCTION exchange_do_withdraw(
  IN in_amount_with_fee taler_amount,
  IN in_reserve_pub BYTEA,
  IN in_reserve_sig BYTEA,
  IN in_now INT8,
  IN in_min_reserve_gc INT8,
  IN in_planchets_h BYTEA,
  IN in_maximum_age_committed INT2, 
  IN in_noreveal_index INT2, 
  IN in_selected_h BYTEA, 
  IN in_denom_serials INT8[],
  IN in_denom_sigs BYTEA[],
  IN in_blinding_seed BYTEA, 
  IN in_cs_r_values BYTEA[], 
  IN in_cs_r_choices INT8, 
  OUT out_reserve_found BOOLEAN,
  OUT out_balance_ok BOOLEAN,
  OUT out_reserve_balance taler_amount,
  OUT out_age_ok BOOLEAN,
  OUT out_required_age INT2, 
  OUT out_reserve_birthday INT4,
  OUT out_idempotent BOOLEAN,
  OUT out_noreveal_index INT2, 
  OUT out_nonce_reuse BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
  my_reserve RECORD;
  my_difference RECORD;
  my_balance taler_amount;
  my_not_before DATE;
  my_earliest_date DATE;
BEGIN
SELECT current_balance
      ,birthday
      ,gc_date
  INTO my_reserve
  FROM reserves
 WHERE reserve_pub=in_reserve_pub;
out_reserve_found = FOUND;
IF NOT out_reserve_found
THEN
  out_age_ok = FALSE;
  out_required_age = -1;
  out_idempotent = FALSE;
  out_noreveal_index = -1;
  out_reserve_balance.val = 0;
  out_reserve_balance.frac = 0;
  out_balance_ok = FALSE;
  out_nonce_reuse = FALSE;
  out_reserve_birthday = 0;
  RETURN;
END IF;
out_reserve_balance = my_reserve.current_balance;
out_reserve_birthday = my_reserve.birthday;
SELECT noreveal_index
  INTO out_noreveal_index
  FROM withdraw
 WHERE reserve_pub = in_reserve_pub
   AND planchets_h = in_planchets_h;
out_idempotent = FOUND;
IF out_idempotent
THEN
  out_balance_ok = TRUE;
  out_age_ok = TRUE;
  out_required_age = -1;
  out_nonce_reuse = FALSE;
  RETURN;
END IF;
out_noreveal_index = -1;
IF (my_reserve.birthday <> 0)
THEN
  my_not_before=date '1970-01-01' + my_reserve.birthday;
  my_earliest_date = current_date - make_interval(in_maximum_age_committed);
  IF ( (in_maximum_age_committed IS NULL) OR
       (my_earliest_date < my_not_before) )
  THEN
    out_required_age = extract(year FROM age(current_date, my_not_before));
    out_age_ok = FALSE;
    out_balance_ok = TRUE; 
    out_nonce_reuse = FALSE; 
    RETURN;
  END IF;
END IF;
out_age_ok = TRUE;
out_required_age = 0;
SELECT *
  INTO my_difference
  FROM amount_left_minus_right(out_reserve_balance
                              ,in_amount_with_fee);
out_balance_ok = my_difference.ok;
IF NOT out_balance_ok
THEN
  out_nonce_reuse = FALSE; 
  RETURN;
END IF;
my_balance = my_difference.diff;
in_min_reserve_gc=GREATEST(in_min_reserve_gc,my_reserve.gc_date);
UPDATE reserves SET
  gc_date=in_min_reserve_gc
 ,current_balance=my_balance
WHERE
  reserve_pub=in_reserve_pub;
IF in_blinding_seed IS NOT NULL
THEN
  INSERT INTO unique_withdraw_blinding_seed
    (blinding_seed)
  VALUES
    (in_blinding_seed)
  ON CONFLICT DO NOTHING;
  IF NOT FOUND
  THEN
    out_nonce_reuse = TRUE;
    RETURN;
  END IF;
END IF;
out_nonce_reuse = FALSE;
INSERT INTO withdraw
  (planchets_h
  ,execution_date
  ,max_age
  ,amount_with_fee
  ,reserve_pub
  ,reserve_sig
  ,noreveal_index
  ,denom_serials
  ,selected_h
  ,blinding_seed
  ,cs_r_values
  ,cs_r_choices
  ,denom_sigs)
VALUES
  (in_planchets_h
  ,in_now
  ,in_maximum_age_committed
  ,in_amount_with_fee
  ,in_reserve_pub
  ,in_reserve_sig
  ,in_noreveal_index
  ,in_denom_serials
  ,in_selected_h
  ,in_blinding_seed
  ,in_cs_r_values
  ,in_cs_r_choices
  ,in_denom_sigs)
ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  RAISE EXCEPTION 'Conflict on insert into withdraw despite idempotency check for reserve_pub(%) and planchets_h(%)!',
    in_reserve_pub,
    in_planchets_h;
END IF;
END $$;
COMMENT ON FUNCTION exchange_do_withdraw(
  taler_amount,
  BYTEA,
  BYTEA,
  INT8,
  INT8,
  BYTEA,
  INT2,
  INT2,
  BYTEA,
  INT8[],
  BYTEA[],
  BYTEA,
  BYTEA[],
  INT8)
  IS 'Checks whether the reserve has sufficient balance for an withdraw operation (or the request is repeated and was previously approved) and that age requirements are met. If so updates the database with the result. Includes storing the hashes of all blinded planchets, (separately) the hashes of the chosen planchets and denomination signatures, or signaling idempotency (and previous noreveal_index) or nonce reuse';
DROP FUNCTION IF EXISTS exchange_do_refresh;
CREATE FUNCTION exchange_do_refresh(
  IN in_rc BYTEA,
  IN in_now INT8,
  IN in_refresh_seed BYTEA,
  IN in_planchets_h BYTEA,
  IN in_amount_with_fee taler_amount,
  IN in_blinding_seed BYTEA,
  IN in_cs_r_values BYTEA[],
  IN in_cs_r_choices INT8,
  IN in_selected_h BYTEA,
  IN in_denom_sigs BYTEA[],
  IN in_denom_serials INT8[],
  IN in_old_coin_pub BYTEA,
  IN in_old_coin_sig BYTEA,
  IN in_noreveal_index INT4,
  IN in_zombie_required BOOLEAN,
  OUT out_coin_found BOOLEAN,
  OUT out_balance_ok BOOLEAN,
  OUT out_zombie_bad BOOLEAN,
  OUT out_nonce_reuse BOOLEAN,
  OUT out_idempotent BOOLEAN,
  OUT out_noreveal_index INT4,
  OUT out_coin_balance taler_amount)
LANGUAGE plpgsql
AS $$
DECLARE
  known_coin RECORD;
  difference RECORD;
BEGIN
SELECT known_coin_id
     ,remaining
  INTO known_coin
  FROM known_coins
  WHERE coin_pub = in_old_coin_pub;
IF NOT FOUND
THEN
  out_coin_found = FALSE;
  out_balance_ok = TRUE;
  out_zombie_bad = FALSE;
  out_nonce_reuse = FALSE;
  out_idempotent = FALSE;
  out_noreveal_index = -1 ;
  out_coin_balance.val = 0;
  out_coin_balance.frac = 0;
  RETURN;
END IF;
out_coin_found = TRUE;
out_coin_balance = known_coin.remaining;
SELECT TRUE, noreveal_index
INTO out_idempotent, out_noreveal_index
FROM exchange.refresh
WHERE rc=in_rc;
IF out_idempotent
THEN
  out_balance_ok = TRUE;
  out_zombie_bad = FALSE; 
  out_nonce_reuse = FALSE;
RETURN;
END IF;
out_idempotent = FALSE;
out_noreveal_index = in_noreveal_index;
IF in_blinding_seed IS NOT NULL
THEN
  INSERT INTO unique_refresh_blinding_seed
    (blinding_seed)
  VALUES
    (in_blinding_seed)
  ON CONFLICT DO NOTHING;
  IF NOT FOUND
  THEN
      out_nonce_reuse = TRUE;
      out_balance_ok = TRUE;
      out_zombie_bad = FALSE; 
      RETURN;
  END IF;
END IF;
out_nonce_reuse = FALSE;
INSERT INTO exchange.refresh
  (rc
  ,execution_date
  ,old_coin_pub
  ,old_coin_sig
  ,planchets_h
  ,amount_with_fee
  ,noreveal_index
  ,refresh_seed
  ,blinding_seed
  ,cs_r_values
  ,cs_r_choices
  ,selected_h
  ,denom_sigs
  ,denom_serials
  )
  VALUES
  (in_rc
  ,in_now
  ,in_old_coin_pub
  ,in_old_coin_sig
  ,in_planchets_h
  ,in_amount_with_fee
  ,in_noreveal_index
  ,in_refresh_seed
  ,in_blinding_seed
  ,in_cs_r_values
  ,in_cs_r_choices
  ,in_selected_h
  ,in_denom_sigs
  ,in_denom_serials
  )
  ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  RAISE EXCEPTION 'Conflict in refresh despite idempotency check for rc(%)!', rc;
  RETURN;
END IF;
IF in_zombie_required
THEN
  PERFORM
   FROM recoup_refresh
   WHERE refresh_id IN
      (SELECT refresh_id
       FROM refresh
       WHERE old_coin_pub=in_old_coin_pub);
  IF NOT FOUND
  THEN
    out_zombie_bad=TRUE;
    out_balance_ok=FALSE;
    RETURN;
  END IF;
END IF;
out_zombie_bad=FALSE; 
SELECT *
INTO difference
FROM amount_left_minus_right(out_coin_balance
                            ,in_amount_with_fee);
out_balance_ok = difference.ok;
IF NOT out_balance_ok
THEN
  RETURN;
END IF;
out_coin_balance = difference.diff;
UPDATE known_coins
  SET
    remaining = out_coin_balance
  WHERE
    known_coin_id = known_coin.known_coin_id;
END $$;
DROP FUNCTION IF EXISTS exchange_do_deposit;
CREATE FUNCTION exchange_do_deposit(
  IN in_shard INT8,
  IN in_merchant_pub BYTEA,
  IN in_merchant_sig BYTEA,
  IN in_wallet_timestamp INT8,
  IN in_exchange_timestamp INT8,
  IN in_refund_deadline INT8,
  IN in_wire_deadline INT8,
  IN in_h_contract_terms BYTEA,
  IN in_wallet_data_hash BYTEA, 
  IN in_wire_salt BYTEA,
  IN in_wire_target_h_payto BYTEA,
  IN in_h_normalized_payto BYTEA,
  IN in_policy_details_serial_id INT8, 
  IN in_policy_blocked BOOLEAN,
  IN in_receiver_wire_account TEXT,
  IN ina_coin_pub BYTEA[],
  IN ina_coin_sig BYTEA[],
  IN ina_amount_with_fee taler_amount[],
  IN in_total_amount taler_amount,
  IN in_is_wallet BOOL,
  OUT out_exchange_timestamp INT8,
  OUT out_insufficient_balance_coin_index INT4, 
  OUT out_conflict BOOL
 )
LANGUAGE plpgsql
AS $$
DECLARE
  wtsi INT8; 
  bdsi INT8; 
  i INT4;
  ini_amount_with_fee taler_amount;
  ini_coin_pub BYTEA;
  ini_coin_sig BYTEA;
BEGIN
INSERT INTO kyc_targets
  (h_normalized_payto
  ,is_wallet
  ) VALUES (
   in_h_normalized_payto
  ,in_is_wallet
  )
  ON CONFLICT DO NOTHING;
INSERT INTO wire_targets
  (wire_target_h_payto
  ,h_normalized_payto
  ,payto_uri
  ) VALUES (
   in_wire_target_h_payto
  ,in_h_normalized_payto
  ,in_receiver_wire_account
  )
  ON CONFLICT DO NOTHING 
  RETURNING wire_target_serial_id
       INTO wtsi;
IF NOT FOUND
THEN
  SELECT wire_target_serial_id
    INTO wtsi
    FROM wire_targets
   WHERE wire_target_h_payto=in_wire_target_h_payto;
END IF;
INSERT INTO batch_deposits
  (shard
  ,merchant_pub
  ,merchant_sig
  ,wallet_timestamp
  ,exchange_timestamp
  ,refund_deadline
  ,wire_deadline
  ,h_contract_terms
  ,wallet_data_hash
  ,wire_salt
  ,wire_target_h_payto
  ,policy_details_serial_id
  ,policy_blocked
  ,total_amount
  ) VALUES (
   in_shard
  ,in_merchant_pub
  ,in_merchant_sig
  ,in_wallet_timestamp
  ,in_exchange_timestamp
  ,in_refund_deadline
  ,in_wire_deadline
  ,in_h_contract_terms
  ,in_wallet_data_hash
  ,in_wire_salt
  ,in_wire_target_h_payto
  ,in_policy_details_serial_id
  ,in_policy_blocked
  ,in_total_amount)
  ON CONFLICT DO NOTHING 
  RETURNING
    batch_deposit_serial_id
  INTO
    bdsi;
IF NOT FOUND
THEN
  SELECT
      exchange_timestamp
     ,batch_deposit_serial_id
   INTO
      out_exchange_timestamp
     ,bdsi
   FROM batch_deposits
   WHERE shard=in_shard
     AND merchant_pub=in_merchant_pub
     AND h_contract_terms=in_h_contract_terms
     AND wire_target_h_payto=in_wire_target_h_payto
     AND ( (wallet_data_hash=in_wallet_data_hash) OR
           (wallet_data_hash IS NULL AND in_wallet_data_hash IS NULL) )
     AND wire_salt=in_wire_salt
     AND wallet_timestamp=in_wallet_timestamp
     AND refund_deadline=in_refund_deadline
     AND wire_deadline=in_wire_deadline
     AND ( (policy_details_serial_id=in_policy_details_serial_id) OR
           (policy_details_serial_id IS NULL AND in_policy_details_serial_id IS NULL) );
  IF NOT FOUND
  THEN
    out_conflict=TRUE;
    RETURN;
  END IF;
END IF;
out_conflict=FALSE;
FOR i IN 1..array_length(ina_coin_pub,1)
LOOP
  ini_coin_pub = ina_coin_pub[i];
  ini_coin_sig = ina_coin_sig[i];
  ini_amount_with_fee = ina_amount_with_fee[i];
  INSERT INTO coin_deposits
    (batch_deposit_serial_id
    ,coin_pub
    ,coin_sig
    ,amount_with_fee
    ) VALUES (
     bdsi
    ,ini_coin_pub
    ,ini_coin_sig
    ,ini_amount_with_fee
    )
    ON CONFLICT DO NOTHING;
  IF FOUND
  THEN
    UPDATE known_coins kc
      SET
        remaining.frac=(kc.remaining).frac-ini_amount_with_fee.frac
          + CASE
              WHEN (kc.remaining).frac < ini_amount_with_fee.frac
              THEN 100000000
              ELSE 0
            END,
        remaining.val=(kc.remaining).val-ini_amount_with_fee.val
          - CASE
              WHEN (kc.remaining).frac < ini_amount_with_fee.frac
              THEN 1
              ELSE 0
            END
      WHERE coin_pub=ini_coin_pub
        AND ( ((kc.remaining).val > ini_amount_with_fee.val) OR
              ( ((kc.remaining).frac >= ini_amount_with_fee.frac) AND
                ((kc.remaining).val >= ini_amount_with_fee.val) ) );
    IF NOT FOUND
    THEN
      out_insufficient_balance_coin_index=i-1;
      RETURN;
    END IF;
  END IF;
END LOOP; 
END $$;
DROP FUNCTION IF EXISTS exchange_do_check_deposit_idempotent;
CREATE FUNCTION exchange_do_check_deposit_idempotent(
  IN in_shard INT8,
  IN in_merchant_pub BYTEA,
  IN in_wallet_timestamp INT8,
  IN in_exchange_timestamp INT8,
  IN in_refund_deadline INT8,
  IN in_wire_deadline INT8,
  IN in_h_contract_terms BYTEA,
  IN in_wallet_data_hash BYTEA, 
  IN in_wire_salt BYTEA,
  IN in_wire_target_h_payto BYTEA,
  IN in_policy_details_serial_id INT8, 
  IN in_policy_blocked BOOLEAN,
  IN ina_coin_pub BYTEA[],
  IN ina_coin_sig BYTEA[],
  IN ina_amount_with_fee taler_amount[],
  OUT out_exchange_timestamp INT8,
  OUT out_is_idempotent BOOL
 )
LANGUAGE plpgsql
AS $$
DECLARE
  wtsi INT8; 
  bdsi INT8; 
  i INT4;
  ini_amount_with_fee taler_amount;
  ini_coin_pub BYTEA;
  ini_coin_sig BYTEA;
BEGIN
out_exchange_timestamp = in_exchange_timestamp;
SELECT wire_target_serial_id
  INTO wtsi
  FROM wire_targets
 WHERE wire_target_h_payto=in_wire_target_h_payto;
IF NOT FOUND
THEN
  out_is_idempotent = FALSE;
  RETURN;
END IF;
SELECT
    exchange_timestamp
   ,batch_deposit_serial_id
  INTO
    out_exchange_timestamp
   ,bdsi
  FROM batch_deposits
 WHERE shard=in_shard
   AND merchant_pub=in_merchant_pub
   AND h_contract_terms=in_h_contract_terms
   AND wire_target_h_payto=in_wire_target_h_payto
   AND ( (wallet_data_hash=in_wallet_data_hash) OR
         (wallet_data_hash IS NULL AND in_wallet_data_hash IS NULL) )
   AND wire_salt=in_wire_salt
   AND wallet_timestamp=in_wallet_timestamp
   AND refund_deadline=in_refund_deadline
   AND wire_deadline=in_wire_deadline
   AND ( (policy_details_serial_id=in_policy_details_serial_id) OR
         (policy_details_serial_id IS NULL AND in_policy_details_serial_id IS NULL) );
IF NOT FOUND
THEN
  out_is_idempotent=FALSE;
  RETURN;
END IF;
FOR i IN 1..array_length(ina_coin_pub,1)
LOOP
  ini_coin_pub = ina_coin_pub[i];
  ini_coin_sig = ina_coin_sig[i];
  ini_amount_with_fee = ina_amount_with_fee[i];
  PERFORM FROM coin_deposits
    WHERE batch_deposit_serial_id=bdsi
      AND coin_pub=ini_coin_pub
      AND coin_sig=ini_coin_sig
      AND amount_with_fee=ini_amount_with_fee;
  IF NOT FOUND
  THEN
    out_is_idempotent=FALSE;
    RETURN;
  END IF;
END LOOP; 
out_is_idempotent=TRUE;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_melt(
  IN in_cs_rms BYTEA,
  IN in_amount_with_fee taler_amount,
  IN in_rc BYTEA,
  IN in_old_coin_pub BYTEA,
  IN in_old_coin_sig BYTEA,
  IN in_known_coin_id INT8, 
  IN in_noreveal_index INT4,
  IN in_zombie_required BOOLEAN,
  OUT out_balance_ok BOOLEAN,
  OUT out_zombie_bad BOOLEAN,
  OUT out_noreveal_index INT4)
LANGUAGE plpgsql
AS $$
DECLARE
  denom_max INT8;
BEGIN
INSERT INTO exchange.refresh_commitments
  (rc
  ,old_coin_pub
  ,old_coin_sig
  ,amount_with_fee
  ,noreveal_index
  )
  VALUES
  (in_rc
  ,in_old_coin_pub
  ,in_old_coin_sig
  ,in_amount_with_fee
  ,in_noreveal_index)
  ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  out_noreveal_index=-1;
  SELECT
     noreveal_index
    INTO
     out_noreveal_index
    FROM exchange.refresh_commitments
   WHERE rc=in_rc;
  out_balance_ok=FOUND;
  out_zombie_bad=FALSE; 
  RETURN;
END IF;
IF in_zombie_required
THEN
  PERFORM
    FROM recoup_refresh
   WHERE rrc_serial IN
    (SELECT rrc_serial
       FROM refresh_revealed_coins
      WHERE melt_serial_id IN
      (SELECT melt_serial_id
         FROM refresh_commitments
        WHERE old_coin_pub=in_old_coin_pub));
  IF NOT FOUND
  THEN
    out_zombie_bad=TRUE;
    out_balance_ok=FALSE;
    RETURN;
  END IF;
END IF;
out_zombie_bad=FALSE; 
UPDATE known_coins kc
  SET
    remaining.frac=(kc.remaining).frac-in_amount_with_fee.frac
       + CASE
         WHEN (kc.remaining).frac < in_amount_with_fee.frac
         THEN 100000000
         ELSE 0
         END,
    remaining.val=(kc.remaining).val-in_amount_with_fee.val
       - CASE
         WHEN (kc.remaining).frac < in_amount_with_fee.frac
         THEN 1
         ELSE 0
         END
  WHERE coin_pub=in_old_coin_pub
    AND ( ((kc.remaining).val > in_amount_with_fee.val) OR
          ( ((kc.remaining).frac >= in_amount_with_fee.frac) AND
            ((kc.remaining).val >= in_amount_with_fee.val) ) );
IF NOT FOUND
THEN
  out_noreveal_index=-1;
  out_balance_ok=FALSE;
  RETURN;
END IF;
IF in_cs_rms IS NOT NULL
THEN
  SELECT
      denominations_serial
    INTO
      denom_max
    FROM exchange.denominations
      ORDER BY denominations_serial DESC
      LIMIT 1;
  INSERT INTO exchange.cs_nonce_locks
    (nonce
    ,max_denomination_serial
    ,op_hash)
  VALUES
    (in_cs_rms
    ,denom_max
    ,in_rc)
  ON CONFLICT DO NOTHING;
  IF NOT FOUND
  THEN
    SELECT 1
      FROM exchange.cs_nonce_locks
     WHERE nonce=in_cs_rms
       AND op_hash=in_rc;
    IF NOT FOUND
    THEN
       out_balance_ok=FALSE;
       out_zombie_bad=FALSE;
       out_noreveal_index=42; 
       ASSERT false, 'nonce reuse attempted by client';
    END IF;
  END IF;
END IF;
out_balance_ok=TRUE;
out_noreveal_index=in_noreveal_index;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_select_deposits_missing_wire(
  IN in_min_serial_id INT8)
RETURNS SETOF exchange_do_select_deposits_missing_wire_return_type
LANGUAGE plpgsql
AS $$
DECLARE
  missing CURSOR
  FOR
  SELECT
    batch_deposit_serial_id
   ,wire_target_h_payto
   ,wire_deadline
    FROM batch_deposits
    WHERE batch_deposit_serial_id > in_min_serial_id
    ORDER BY batch_deposit_serial_id ASC;
DECLARE
  my_total_val INT8; 
DECLARE
  my_total_frac INT8; 
DECLARE
  my_total taler_amount; 
DECLARE
  my_batch_record RECORD;
DECLARE
  i RECORD;
BEGIN
OPEN missing;
LOOP
  FETCH NEXT FROM missing INTO i;
  EXIT WHEN NOT FOUND;
  SELECT
    SUM((cdep.amount_with_fee).val) AS total_val
   ,SUM((cdep.amount_with_fee).frac::INT8) AS total_frac
    INTO
      my_batch_record
    FROM coin_deposits cdep
    WHERE cdep.batch_deposit_serial_id = i.batch_deposit_serial_id;
  my_total_val=my_batch_record.total_val;
  my_total_frac=my_batch_record.total_frac;
  my_total.val = my_total_val + my_total_frac / 100000000;
  my_total.frac = my_total_frac % 100000000;
  RETURN NEXT (
       i.batch_deposit_serial_id
      ,my_total
      ,i.wire_target_h_payto
      ,i.wire_deadline);
END LOOP;
CLOSE missing;
RETURN;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_select_justification_missing_wire(
  IN in_wire_target_h_payto BYTEA,
  IN in_current_time INT8,
  OUT out_payto_uri TEXT, 
  OUT out_kyc_pending TEXT, 
  OUT out_aml_status INT4, 
  OUT out_aml_limit taler_amount) 
LANGUAGE plpgsql
AS $$
DECLARE
  my_required_checks TEXT[];
DECLARE
  my_aml_data RECORD;
DECLARE
  satisfied CURSOR FOR
  SELECT satisfied_checks
    FROM kyc_attributes
   WHERE h_payto=in_wire_target_h_payto
     AND expiration_time < in_current_time;
DECLARE
  i RECORD;
BEGIN
  out_payto_uri = NULL;
  SELECT payto_uri
    INTO out_payto_uri
    FROM wire_targets
   WHERE wire_target_h_payto=my_wire_target_h_payto;
  my_required_checks = NULL;
  SELECT string_to_array (required_checks, ' ')
    INTO my_required_checks
    FROM legitimization_requirements
    WHERE h_payto=my_wire_target_h_payto;
  SELECT
      new_threshold
     ,kyc_requirements
     ,new_status
    INTO
      my_aml_data
     FROM aml_history
    WHERE h_payto=in_wire_target_h_payto
    ORDER BY aml_history_serial_id 
      DESC LIMIT 1;
  IF FOUND
  THEN
    out_aml_limit=my_aml_data.new_threshold;
    out_aml_status=my_aml_data.kyc_status;
    my_required_checks
       = array_cat (my_required_checks,
                    my_aml_data.kyc_requirements);
  ELSE
    out_aml_limit=NULL;
    out_aml_status=0; 
  END IF;
  OPEN satisfied;
  LOOP
    FETCH NEXT FROM satisfied INTO i;
    EXIT WHEN NOT FOUND;
    FOR i in 1..array_length(i.satisfied_checks)
    LOOP
      my_required_checks
        = array_remove (my_required_checks,
                        i.satisfied_checks[i]);
    END LOOP;
  END LOOP;
  IF ( (my_required_checks IS NOT NULL) AND
       (0 < array_length(my_satisfied_checks)) )
  THEN
    out_kyc_pending
      = array_to_string (my_required_checks, ' ');
  END IF;
  RETURN;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_refund(
  IN in_amount_with_fee taler_amount,
  IN in_amount taler_amount,
  IN in_deposit_fee taler_amount,
  IN in_h_contract_terms BYTEA,
  IN in_rtransaction_id INT8,
  IN in_deposit_shard INT8,
  IN in_known_coin_id INT8,
  IN in_coin_pub BYTEA,
  IN in_merchant_pub BYTEA,
  IN in_merchant_sig BYTEA,
  OUT out_not_found BOOLEAN,
  OUT out_refund_ok BOOLEAN,
  OUT out_gone BOOLEAN,
  OUT out_conflict BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
  bdsi INT8; 
DECLARE
  tmp_val INT8; 
DECLARE
  tmp_frac INT8; 
DECLARE
  tmp taler_amount; 
DECLARE
  deposit taler_amount; 
BEGIN
SELECT
   bdep.batch_deposit_serial_id
  ,(cdep.amount_with_fee).val
  ,(cdep.amount_with_fee).frac
  ,bdep.done
 INTO
   bdsi
  ,deposit.val
  ,deposit.frac
  ,out_gone
 FROM batch_deposits bdep
 JOIN coin_deposits cdep
   USING (batch_deposit_serial_id)
 WHERE cdep.coin_pub=in_coin_pub
  AND shard=in_deposit_shard
  AND merchant_pub=in_merchant_pub
  AND h_contract_terms=in_h_contract_terms;
IF NOT FOUND
THEN
  out_refund_ok=FALSE;
  out_conflict=FALSE;
  out_not_found=TRUE;
  out_gone=FALSE;
  RETURN;
END IF;
INSERT INTO refunds
  (batch_deposit_serial_id
  ,coin_pub
  ,merchant_sig
  ,rtransaction_id
  ,amount_with_fee
  )
  VALUES
  (bdsi
  ,in_coin_pub
  ,in_merchant_sig
  ,in_rtransaction_id
  ,in_amount_with_fee
  )
  ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
   PERFORM
   FROM exchange.refunds
   WHERE coin_pub=in_coin_pub
     AND batch_deposit_serial_id=bdsi
     AND rtransaction_id=in_rtransaction_id
     AND amount_with_fee=in_amount_with_fee;
  IF NOT FOUND
  THEN
    out_refund_ok=FALSE;
    out_conflict=TRUE;
    out_not_found=FALSE;
    RETURN;
  END IF;
  out_refund_ok=TRUE;
  out_conflict=FALSE;
  out_not_found=FALSE;
  out_gone=FALSE;
  RETURN;
END IF;
IF out_gone
THEN
  out_refund_ok=FALSE;
  out_conflict=FALSE;
  out_not_found=FALSE;
  RETURN;
END IF;
SELECT
   SUM((refs.amount_with_fee).val) 
  ,SUM(CAST((refs.amount_with_fee).frac AS INT8)) 
  INTO
   tmp_val
  ,tmp_frac
  FROM refunds refs
  WHERE coin_pub=in_coin_pub
    AND batch_deposit_serial_id=bdsi;
IF tmp_val IS NULL
THEN
  RAISE NOTICE 'failed to sum up existing refunds';
  out_refund_ok=FALSE;
  out_conflict=FALSE;
  out_not_found=FALSE;
  RETURN;
END IF;
tmp.val = tmp_val + tmp_frac / 100000000;
tmp.frac = tmp_frac % 100000000;
IF (tmp.val < deposit.val)
THEN
  out_refund_ok=TRUE;
ELSE
  IF (tmp.val = deposit.val) AND (tmp.frac <= deposit.frac)
  THEN
    out_refund_ok=TRUE;
  ELSE
    out_refund_ok=FALSE;
  END IF;
END IF;
IF (tmp.val = deposit.val) AND (tmp.frac = deposit.frac)
THEN
  in_amount.frac = in_amount.frac + in_deposit_fee.frac;
  in_amount.val = in_amount.val + in_deposit_fee.val;
  in_amount.val = in_amount.val + in_amount.frac / 100000000;
  in_amount.frac = in_amount.frac % 100000000;
END IF;
UPDATE known_coins kc
  SET
    remaining.frac=(kc.remaining).frac+in_amount.frac
       - CASE
         WHEN (kc.remaining).frac+in_amount.frac >= 100000000
         THEN 100000000
         ELSE 0
         END,
    remaining.val=(kc.remaining).val+in_amount.val
       + CASE
         WHEN (kc.remaining).frac+in_amount.frac >= 100000000
         THEN 1
         ELSE 0
         END
  WHERE coin_pub=in_coin_pub;
out_conflict=FALSE;
out_not_found=FALSE;
END $$;
COMMENT ON FUNCTION exchange_do_refund(taler_amount, taler_amount, taler_amount, BYTEA, INT8, INT8, INT8, BYTEA, BYTEA, BYTEA)
  IS 'Executes a refund operation, checking that the corresponding deposit was sufficient to cover the refunded amount';
DROP FUNCTION IF EXISTS exchange_do_recoup_to_reserve;
CREATE FUNCTION exchange_do_recoup_to_reserve(
  IN in_reserve_pub BYTEA,
  IN in_withdraw_id INT8,
  IN in_coin_blind BYTEA,
  IN in_coin_pub BYTEA,
  IN in_known_coin_id INT8,
  IN in_coin_sig BYTEA,
  IN in_reserve_gc INT8,
  IN in_reserve_expiration INT8,
  IN in_recoup_timestamp INT8,
  OUT out_recoup_ok BOOLEAN,
  OUT out_internal_failure BOOLEAN,
  OUT out_recoup_timestamp INT8)
LANGUAGE plpgsql
AS $$
DECLARE
  tmp taler_amount; 
  balance taler_amount; 
  new_balance taler_amount; 
  reserve RECORD;
  rval RECORD;
BEGIN
out_internal_failure=FALSE;
SELECT
   remaining
 INTO
   rval
FROM exchange.known_coins
  WHERE coin_pub=in_coin_pub;
IF NOT FOUND
THEN
  out_internal_failure=TRUE;
  out_recoup_ok=FALSE;
  RETURN;
END IF;
tmp := rval.remaining;
IF tmp.val + tmp.frac = 0
THEN
  SELECT
    recoup_timestamp
  INTO
    out_recoup_timestamp
    FROM exchange.recoup
    WHERE coin_pub=in_coin_pub;
  out_recoup_ok=FOUND;
  RETURN;
END IF;
UPDATE known_coins
  SET
     remaining.val = 0
    ,remaining.frac = 0
  WHERE coin_pub=in_coin_pub;
SELECT current_balance
  INTO reserve
  FROM reserves
 WHERE reserve_pub=in_reserve_pub;
balance = reserve.current_balance;
new_balance.frac=balance.frac+tmp.frac
   - CASE
     WHEN balance.frac+tmp.frac >= 100000000
     THEN 100000000
     ELSE 0
     END;
new_balance.val=balance.val+tmp.val
   + CASE
     WHEN balance.frac+tmp.frac >= 100000000
     THEN 1
     ELSE 0
     END;
UPDATE reserves
  SET
    current_balance = new_balance,
    gc_date=GREATEST(gc_date, in_reserve_gc),
    expiration_date=GREATEST(expiration_date, in_reserve_expiration)
  WHERE reserve_pub=in_reserve_pub;
IF NOT FOUND
THEN
  RAISE NOTICE 'failed to increase reserve balance from recoup';
  out_recoup_ok=TRUE;
  out_internal_failure=TRUE;
  RETURN;
END IF;
INSERT INTO exchange.recoup
  (coin_pub
  ,coin_sig
  ,coin_blind
  ,amount
  ,recoup_timestamp
  ,withdraw_id
  )
VALUES
  (in_coin_pub
  ,in_coin_sig
  ,in_coin_blind
  ,tmp
  ,in_recoup_timestamp
  ,in_withdraw_id);
out_recoup_ok=TRUE;
out_recoup_timestamp=in_recoup_timestamp;
END $$;
DROP FUNCTION IF EXISTS exchange_do_recoup_to_coin;
CREATE FUNCTION exchange_do_recoup_to_coin(
  IN in_old_coin_pub BYTEA,
  IN in_refresh_id INT8,
  IN in_coin_blind BYTEA,
  IN in_coin_pub BYTEA,
  IN in_known_coin_id INT8,
  IN in_coin_sig BYTEA,
  IN in_recoup_timestamp INT8,
  OUT out_recoup_ok BOOLEAN,
  OUT out_internal_failure BOOLEAN,
  OUT out_recoup_timestamp INT8)
LANGUAGE plpgsql
AS $$
DECLARE
  rval RECORD;
DECLARE
  tmp taler_amount; 
BEGIN
out_internal_failure=FALSE;
SELECT
   remaining
 INTO
   rval
FROM exchange.known_coins
  WHERE coin_pub=in_coin_pub;
IF NOT FOUND
THEN
  out_internal_failure=TRUE;
  out_recoup_ok=FALSE;
  RETURN;
END IF;
tmp := rval.remaining;
IF tmp.val + tmp.frac = 0
THEN
  SELECT
      recoup_timestamp
    INTO
      out_recoup_timestamp
    FROM recoup_refresh
    WHERE coin_pub=in_coin_pub;
  out_recoup_ok=FOUND;
  RETURN;
END IF;
UPDATE known_coins
  SET
     remaining.val = 0
    ,remaining.frac = 0
  WHERE coin_pub=in_coin_pub;
UPDATE known_coins kc
  SET
    remaining.frac=(kc.remaining).frac+tmp.frac
       - CASE
         WHEN (kc.remaining).frac+tmp.frac >= 100000000
         THEN 100000000
         ELSE 0
         END,
    remaining.val=(kc.remaining).val+tmp.val
       + CASE
         WHEN (kc.remaining).frac+tmp.frac >= 100000000
         THEN 1
         ELSE 0
         END
  WHERE coin_pub=in_old_coin_pub;
IF NOT FOUND
THEN
  RAISE NOTICE 'failed to increase old coin balance from recoup';
  out_recoup_ok=TRUE;
  out_internal_failure=TRUE;
  RETURN;
END IF;
INSERT INTO recoup_refresh
  (coin_pub
  ,known_coin_id
  ,coin_sig
  ,coin_blind
  ,amount
  ,recoup_timestamp
  ,refresh_id
  )
VALUES
  (in_coin_pub
  ,in_known_coin_id
  ,in_coin_sig
  ,in_coin_blind
  ,tmp
  ,in_recoup_timestamp
  ,in_refresh_id);
out_recoup_ok=TRUE;
out_recoup_timestamp=in_recoup_timestamp;
END $$;
CREATE OR REPLACE PROCEDURE exchange_do_main_gc(
  IN in_ancient_date INT8,
  IN in_now INT8)
LANGUAGE plpgsql
AS $$
DECLARE
  coin_min INT8; 
  batch_deposit_min INT8; 
  withdraw_min INT8; 
  denom_min INT8; 
BEGIN
DELETE FROM prewire
  WHERE finished=TRUE;
DELETE FROM wire_fee
  WHERE end_date < in_ancient_date;
DELETE FROM refresh
  WHERE execution_date < in_ancient_date;
DELETE FROM kycauths_in
  WHERE execution_date < in_ancient_date;
DELETE FROM reserves_in
  WHERE execution_date < in_ancient_date;
DELETE FROM batch_deposits
  WHERE wire_deadline < in_ancient_date;
DELETE FROM withdraw
  WHERE reserve_pub IN (
    SELECT reserve_pub
      FROM reserves
     WHERE gc_date < in_now
       AND current_balance = (0, 0));
DELETE FROM reserves_close
  WHERE reserve_pub IN (
    SELECT reserve_pub
      FROM reserves
     WHERE gc_date < in_now
       AND current_balance = (0, 0));
SELECT withdraw_id
  INTO withdraw_min
  FROM withdraw
  ORDER BY withdraw_id ASC
  LIMIT 1;
DELETE FROM recoup
  WHERE withdraw_id < withdraw_min;
DELETE FROM reserves
  WHERE gc_date < in_now
    AND current_balance = (0, 0);
DELETE FROM denominations
  WHERE expire_legal < in_now
    AND denominations_serial NOT IN
      (SELECT DISTINCT UNNEST(denom_serials)
         FROM withdraw)
    AND denominations_serial NOT IN
      (SELECT DISTINCT denominations_serial
         FROM known_coins
        WHERE coin_pub IN
          (SELECT DISTINCT coin_pub
             FROM recoup))
    AND denominations_serial NOT IN
      (SELECT DISTINCT denominations_serial
         FROM known_coins
        WHERE coin_pub IN
          (SELECT DISTINCT coin_pub
             FROM recoup_refresh));
DELETE FROM recoup_refresh
  WHERE known_coin_id < coin_min;
SELECT known_coin_id
  INTO coin_min
  FROM known_coins
  ORDER BY known_coin_id ASC
  LIMIT 1;
SELECT batch_deposit_serial_id
  INTO batch_deposit_min
  FROM coin_deposits
  ORDER BY batch_deposit_serial_id ASC
  LIMIT 1;
DELETE FROM refunds
  WHERE batch_deposit_serial_id < batch_deposit_min;
DELETE FROM aggregation_tracking
  WHERE batch_deposit_serial_id < batch_deposit_min;
DELETE FROM coin_deposits
  WHERE batch_deposit_serial_id < batch_deposit_min;
SELECT denominations_serial
  INTO denom_min
  FROM denominations
  ORDER BY denominations_serial ASC
  LIMIT 1;
DELETE FROM cs_nonce_locks
  WHERE max_denomination_serial <= denom_min;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_purse_delete(
  IN in_purse_pub BYTEA,
  IN in_purse_sig BYTEA,
  IN in_now INT8,
  OUT out_decided BOOLEAN,
  OUT out_found BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
  my_deposit record;
DECLARE
  my_in_reserve_quota BOOLEAN;
BEGIN
PERFORM refunded FROM purse_decision
  WHERE purse_pub=in_purse_pub;
IF FOUND
THEN
  out_found=TRUE;
  out_decided=TRUE;
  RETURN;
END IF;
out_decided=FALSE;
SELECT in_reserve_quota
  INTO my_in_reserve_quota
  FROM exchange.purse_requests
 WHERE purse_pub=in_purse_pub;
out_found=FOUND;
IF NOT FOUND
THEN
  RETURN;
END IF;
INSERT INTO exchange.purse_deletion
  (purse_pub
  ,purse_sig)
VALUES
  (in_purse_pub
  ,in_purse_sig)
ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  RETURN;
END IF;
DELETE FROM contracts
  WHERE purse_pub=in_purse_pub;
INSERT INTO purse_decision
  (purse_pub
  ,action_timestamp
  ,refunded)
VALUES
  (in_purse_pub
  ,in_now
  ,TRUE);
IF (my_in_reserve_quota)
THEN
  UPDATE reserves
    SET purses_active=purses_active-1
  WHERE reserve_pub IN
    (SELECT reserve_pub
       FROM exchange.purse_merges
      WHERE purse_pub=in_purse_pub
     LIMIT 1);
END IF;
FOR my_deposit IN
  SELECT coin_pub
        ,amount_with_fee
    FROM exchange.purse_deposits
  WHERE purse_pub = in_purse_pub
LOOP
  UPDATE known_coins kc SET
    remaining.frac=(kc.remaining).frac+(my_deposit.amount_with_fee).frac
     - CASE
       WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000
       THEN 100000000
       ELSE 0
       END,
    remaining.val=(kc.remaining).val+(my_deposit.amount_with_fee).val
     + CASE
       WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000
       THEN 1
       ELSE 0
       END
    WHERE coin_pub = my_deposit.coin_pub;
END LOOP;
END $$;
COMMENT ON FUNCTION exchange_do_purse_delete(BYTEA,BYTEA,INT8)
  IS 'Delete a previously undecided purse and refund the coins (if any).';
CREATE OR REPLACE FUNCTION exchange_do_purse_deposit(
  IN in_partner_id INT8,
  IN in_purse_pub BYTEA,
  IN in_amount_with_fee taler_amount,
  IN in_coin_pub BYTEA,
  IN in_coin_sig BYTEA,
  IN in_amount_without_fee taler_amount,
  IN in_reserve_expiration INT8,
  IN in_now INT8,
  OUT out_balance_ok BOOLEAN,
  OUT out_late BOOLEAN,
  OUT out_conflict BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
  was_merged BOOLEAN;
DECLARE
  psi INT8; 
DECLARE
  my_amount taler_amount; 
DECLARE
  was_paid BOOLEAN;
DECLARE
  my_in_reserve_quota BOOLEAN;
DECLARE
  my_reserve_pub BYTEA;
DECLARE
  rval RECORD;
BEGIN
INSERT INTO purse_deposits
  (partner_serial_id
  ,purse_pub
  ,coin_pub
  ,amount_with_fee
  ,coin_sig)
  VALUES
  (in_partner_id
  ,in_purse_pub
  ,in_coin_pub
  ,in_amount_with_fee
  ,in_coin_sig)
  ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  PERFORM
  FROM purse_deposits
  WHERE purse_pub = in_purse_pub
    AND coin_pub = in_coin_pub
    AND coin_sig = in_coin_sig;
  IF NOT FOUND
  THEN
    out_balance_ok=FALSE;
    out_late=FALSE;
    out_conflict=TRUE;
    RETURN;
  ELSE
    out_late=FALSE;
    out_balance_ok=TRUE;
    out_conflict=FALSE;
    RETURN;
  END IF;
END IF;
PERFORM
  FROM exchange.purse_deletion
  WHERE purse_pub = in_purse_pub;
IF FOUND
THEN
  out_late=TRUE;
  out_balance_ok=FALSE;
  out_conflict=FALSE;
  RETURN;
END IF;
UPDATE known_coins kc
  SET
    remaining.frac=(kc.remaining).frac-in_amount_with_fee.frac
       + CASE
         WHEN (kc.remaining).frac < in_amount_with_fee.frac
         THEN 100000000
         ELSE 0
         END,
    remaining.val=(kc.remaining).val-in_amount_with_fee.val
       - CASE
         WHEN (kc.remaining).frac < in_amount_with_fee.frac
         THEN 1
         ELSE 0
         END
  WHERE coin_pub=in_coin_pub
    AND ( ((kc.remaining).val > in_amount_with_fee.val) OR
          ( ((kc.remaining).frac >= in_amount_with_fee.frac) AND
            ((kc.remaining).val >= in_amount_with_fee.val) ) );
IF NOT FOUND
THEN
  out_balance_ok=FALSE;
  out_late=FALSE;
  out_conflict=FALSE;
  RETURN;
END IF;
UPDATE purse_requests pr
  SET
    balance.frac=(pr.balance).frac+in_amount_without_fee.frac
       - CASE
         WHEN (pr.balance).frac+in_amount_without_fee.frac >= 100000000
         THEN 100000000
         ELSE 0
         END,
    balance.val=(pr.balance).val+in_amount_without_fee.val
       + CASE
         WHEN (pr.balance).frac+in_amount_without_fee.frac >= 100000000
         THEN 1
         ELSE 0
         END
  WHERE purse_pub=in_purse_pub;
out_conflict=FALSE;
out_balance_ok=TRUE;
SELECT COALESCE(partner_serial_id,0)
      ,reserve_pub
  INTO psi
      ,my_reserve_pub
  FROM purse_merges
 WHERE purse_pub=in_purse_pub;
IF NOT FOUND
THEN
  out_late=FALSE;
  RETURN;
END IF;
SELECT
    amount_with_fee
   ,in_reserve_quota
  INTO
    rval
  FROM exchange.purse_requests preq
  WHERE (purse_pub=in_purse_pub)
    AND ( ( ( ((preq.amount_with_fee).val <= (preq.balance).val)
          AND ((preq.amount_with_fee).frac <= (preq.balance).frac) )
         OR ((preq.amount_with_fee).val < (preq.balance).val) ) );
IF NOT FOUND
THEN
  out_late=FALSE;
  RETURN;
END IF;
my_amount := rval.amount_with_fee;
my_in_reserve_quota := rval.in_reserve_quota;
INSERT INTO purse_decision
  (purse_pub
  ,action_timestamp
  ,refunded)
VALUES
  (in_purse_pub
  ,in_now
  ,FALSE)
ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  out_late=TRUE;
  RETURN;
END IF;
out_late=FALSE;
IF (my_in_reserve_quota)
THEN
  UPDATE reserves
    SET purses_active=purses_active-1
  WHERE reserve_pub IN
    (SELECT reserve_pub
       FROM purse_merges
      WHERE purse_pub=my_purse_pub
     LIMIT 1);
END IF;
IF (0 != psi)
THEN
  UPDATE purse_actions
     SET action_date=0 
        ,partner_serial_id=psi
   WHERE purse_pub=in_purse_pub;
ELSE
  INSERT INTO reserves
    (reserve_pub
    ,current_balance
    ,expiration_date
    ,gc_date)
  VALUES
    (my_reserve_pub
    ,my_amount
    ,in_reserve_expiration
    ,in_reserve_expiration)
  ON CONFLICT DO NOTHING;
  IF NOT FOUND
  THEN
    UPDATE reserves
      SET
       current_balance.frac=(current_balance).frac+my_amount.frac
        - CASE
          WHEN (current_balance).frac + my_amount.frac >= 100000000
            THEN 100000000
          ELSE 0
          END
      ,current_balance.val=(current_balance).val+my_amount.val
        + CASE
          WHEN (current_balance).frac + my_amount.frac >= 100000000
            THEN 1
          ELSE 0
          END
      ,expiration_date=GREATEST(expiration_date,in_reserve_expiration)
      ,gc_date=GREATEST(gc_date,in_reserve_expiration)
      WHERE reserve_pub=my_reserve_pub;
  END IF;
END IF;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_purse_merge(
  IN in_purse_pub BYTEA,
  IN in_merge_sig BYTEA,
  IN in_merge_timestamp INT8,
  IN in_reserve_sig BYTEA,
  IN in_partner_url TEXT,
  IN in_reserve_pub BYTEA,
  IN in_wallet_h_payto BYTEA,
  IN in_expiration_date INT8,
  OUT out_no_partner BOOLEAN,
  OUT out_no_balance BOOLEAN,
  OUT out_conflict BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
  my_amount taler_amount;
DECLARE
  my_purse_fee taler_amount;
DECLARE
  my_partner_serial_id INT8;
DECLARE
  my_in_reserve_quota BOOLEAN;
DECLARE
  rval RECORD;
DECLARE
  reserve_bal RECORD;
DECLARE
  balance taler_amount;
BEGIN
INSERT INTO reserves
  (reserve_pub
  ,expiration_date
  ,gc_date)
  VALUES
  (in_reserve_pub
  ,in_expiration_date
  ,in_expiration_date)
  ON CONFLICT DO NOTHING;
IF in_partner_url IS NULL
THEN
  my_partner_serial_id=NULL;
ELSE
  SELECT
    partner_serial_id
  INTO
    my_partner_serial_id
  FROM partners
  WHERE partner_base_url=in_partner_url
    AND start_date <= in_merge_timestamp
    AND end_date > in_merge_timestamp;
  IF NOT FOUND
  THEN
    out_no_partner=TRUE;
    out_conflict=FALSE;
    RETURN;
  END IF;
END IF;
out_no_partner=FALSE;
SELECT amount_with_fee
      ,purse_fee
      ,in_reserve_quota
  INTO rval
  FROM purse_requests pr
  WHERE purse_pub=in_purse_pub
    AND (pr.balance).val >= (pr.amount_with_fee).val
    AND ( (pr.balance).frac >= (pr.amount_with_fee).frac OR
          (pr.balance).val > (pr.amount_with_fee).val );
IF NOT FOUND
THEN
  out_no_balance=TRUE;
  out_conflict=FALSE;
  RETURN;
END IF;
my_amount := rval.amount_with_fee;
my_purse_fee := rval.purse_fee;
my_in_reserve_quota := rval.in_reserve_quota;
out_no_balance=FALSE;
INSERT INTO purse_merges
    (partner_serial_id
    ,reserve_pub
    ,purse_pub
    ,merge_sig
    ,merge_timestamp)
  VALUES
    (my_partner_serial_id
    ,in_reserve_pub
    ,in_purse_pub
    ,in_merge_sig
    ,in_merge_timestamp)
  ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  PERFORM
  FROM purse_merges
  WHERE purse_pub=in_purse_pub
     AND merge_sig=in_merge_sig;
  IF NOT FOUND
  THEN
     out_conflict=TRUE;
     RETURN;
  END IF;
  out_conflict=FALSE;
  RETURN;
END IF;
INSERT INTO purse_decision
  (purse_pub
  ,action_timestamp
  ,refunded)
VALUES
  (in_purse_pub
  ,in_merge_timestamp
  ,FALSE)
ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  out_conflict=TRUE;
  RETURN;
END IF;
out_conflict=FALSE;
IF (my_in_reserve_quota)
THEN
  UPDATE reserves
    SET purses_active=purses_active-1
  WHERE reserve_pub IN
    (SELECT reserve_pub
       FROM purse_merges
      WHERE purse_pub=my_purse_pub
     LIMIT 1);
END IF;
INSERT INTO account_merges
  (reserve_pub
  ,reserve_sig
  ,purse_pub
  ,wallet_h_payto)
  VALUES
  (in_reserve_pub
  ,in_reserve_sig
  ,in_purse_pub
  ,in_wallet_h_payto);
IF (0 != my_partner_serial_id)
THEN
  UPDATE purse_actions
     SET action_date=0 
        ,partner_serial_id=my_partner_serial_id
   WHERE purse_pub=in_purse_pub;
ELSE
  my_amount.val = my_amount.val + my_purse_fee.val;
  my_amount.frac = my_amount.frac + my_purse_fee.frac;
  my_amount.val = my_amount.val + my_amount.frac / 100000000;
  my_amount.frac = my_amount.frac % 100000000;
  SELECT current_balance
    INTO reserve_bal
    FROM reserves
   WHERE reserve_pub=in_reserve_pub;
  balance = reserve_bal.current_balance;
  balance.val=balance.val+my_amount.val
     + CASE
       WHEN balance.frac + my_amount.frac >= 100000000
       THEN 1
       ELSE 0
       END;
  balance.frac=balance.frac+my_amount.frac
     - CASE
       WHEN balance.frac + my_amount.frac >= 100000000
       THEN 100000000
       ELSE 0
       END;
  UPDATE reserves
     SET current_balance=balance
   WHERE reserve_pub=in_reserve_pub;
END IF;
RETURN;
END $$;
COMMENT ON FUNCTION exchange_do_purse_merge(BYTEA, BYTEA, INT8, BYTEA, TEXT, BYTEA, BYTEA, INT8)
  IS 'Checks that the partner exists, the purse has not been merged with a different reserve and that the purse is full. If so, persists the merge data and either merges the purse with the reserve or marks it as ready for the taler-exchange-router. Caller MUST abort the transaction on failures so as to not persist data by accident.';
CREATE OR REPLACE FUNCTION exchange_do_reserve_purse(
  IN in_purse_pub BYTEA,
  IN in_merge_sig BYTEA,
  IN in_merge_timestamp INT8,
  IN in_reserve_expiration INT8,
  IN in_reserve_gc INT8,
  IN in_reserve_sig BYTEA,
  IN in_reserve_quota BOOLEAN,
  IN in_purse_fee taler_amount,
  IN in_reserve_pub BYTEA,
  IN in_wallet_h_payto BYTEA,
  OUT out_no_funds BOOLEAN,
  OUT out_no_reserve BOOLEAN,
  OUT out_conflict BOOLEAN)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO purse_merges
    (partner_serial_id
    ,reserve_pub
    ,purse_pub
    ,merge_sig
    ,merge_timestamp)
  VALUES
    (NULL
    ,in_reserve_pub
    ,in_purse_pub
    ,in_merge_sig
    ,in_merge_timestamp)
  ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  PERFORM
  FROM purse_merges
  WHERE purse_pub=in_purse_pub
     AND merge_sig=in_merge_sig;
  IF NOT FOUND
  THEN
     out_conflict=TRUE;
     out_no_reserve=FALSE;
     out_no_funds=FALSE;
     RETURN;
  END IF;
  out_conflict=FALSE;
  out_no_funds=FALSE;
  out_no_reserve=FALSE;
  RETURN;
END IF;
out_conflict=FALSE;
PERFORM
  FROM exchange.reserves
 WHERE reserve_pub=in_reserve_pub;
out_no_reserve = NOT FOUND;
IF (in_reserve_quota)
THEN
  IF (out_no_reserve)
  THEN
    out_no_funds=TRUE;
    RETURN;
  END IF;
  UPDATE exchange.reserves
     SET purses_active=purses_active+1
   WHERE reserve_pub=in_reserve_pub
     AND purses_active < purses_allowed;
  IF NOT FOUND
  THEN
    out_no_funds=TRUE;
    RETURN;
  END IF;
ELSE
  IF (out_no_reserve)
  THEN
    IF ( (0 != in_purse_fee.val) OR
         (0 != in_purse_fee.frac) )
    THEN
      out_no_funds=TRUE;
      RETURN;
    END IF;
    INSERT INTO exchange.reserves
      (reserve_pub
      ,expiration_date
      ,gc_date)
    VALUES
      (in_reserve_pub
      ,in_reserve_expiration
      ,in_reserve_gc);
  ELSE
    UPDATE exchange.reserves
      SET
        current_balance.frac=(current_balance).frac-in_purse_fee.frac
         + CASE
         WHEN (current_balance).frac < in_purse_fee.frac
         THEN 100000000
         ELSE 0
         END,
       current_balance.val=(current_balance).val-in_purse_fee.val
         - CASE
         WHEN (current_balance).frac < in_purse_fee.frac
         THEN 1
         ELSE 0
         END
      WHERE reserve_pub=in_reserve_pub
        AND ( ((current_balance).val > in_purse_fee.val) OR
              ( ((current_balance).frac >= in_purse_fee.frac) AND
                ((current_balance).val >= in_purse_fee.val) ) );
    IF NOT FOUND
    THEN
      out_no_funds=TRUE;
      RETURN;
    END IF;
  END IF;
END IF;
out_no_funds=FALSE;
INSERT INTO account_merges
  (reserve_pub
  ,reserve_sig
  ,purse_pub
  ,wallet_h_payto)
  VALUES
  (in_reserve_pub
  ,in_reserve_sig
  ,in_purse_pub
  ,in_wallet_h_payto);
END $$;
COMMENT ON FUNCTION exchange_do_reserve_purse(BYTEA, BYTEA, INT8, INT8, INT8, BYTEA, BOOLEAN, taler_amount, BYTEA, BYTEA)
  IS 'Create a purse for a reserve.';
CREATE OR REPLACE FUNCTION exchange_do_expire_purse(
  IN in_start_time INT8,
  IN in_end_time INT8,
  IN in_now INT8,
  OUT out_found BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
  my_purse_pub BYTEA;
DECLARE
  my_deposit record;
DECLARE
  my_in_reserve_quota BOOLEAN;
BEGIN
SELECT purse_pub
      ,in_reserve_quota
  INTO my_purse_pub
      ,my_in_reserve_quota
  FROM purse_requests
 WHERE (purse_expiration >= in_start_time) AND
       (purse_expiration < in_end_time) AND
       NOT was_decided
  ORDER BY purse_expiration ASC
 LIMIT 1;
out_found = FOUND;
IF NOT FOUND
THEN
  RETURN;
END IF;
INSERT INTO purse_decision
  (purse_pub
  ,action_timestamp
  ,refunded)
VALUES
  (my_purse_pub
  ,in_now
  ,TRUE);
NOTIFY X8DJSPNYJMNZDAP7GN6YQ4EZVSQXMF3HRP4VAR347WP9SZYP1C200;
IF (my_in_reserve_quota)
THEN
  UPDATE reserves
    SET purses_active=purses_active-1
  WHERE reserve_pub IN
    (SELECT reserve_pub
       FROM exchange.purse_merges
      WHERE purse_pub=my_purse_pub
     LIMIT 1);
END IF;
FOR my_deposit IN
  SELECT coin_pub
        ,amount_with_fee
    FROM purse_deposits
  WHERE purse_pub = my_purse_pub
LOOP
  UPDATE known_coins kc SET
    remaining.frac=(kc.remaining).frac+(my_deposit.amount_with_fee).frac
     - CASE
       WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000
       THEN 100000000
       ELSE 0
       END,
    remaining.val=(kc.remaining).val+(my_deposit.amount_with_fee).val
     + CASE
       WHEN (kc.remaining).frac+(my_deposit.amount_with_fee).frac >= 100000000
       THEN 1
       ELSE 0
       END
    WHERE coin_pub = my_deposit.coin_pub;
  END LOOP;
END $$;
COMMENT ON FUNCTION exchange_do_expire_purse(INT8,INT8,INT8)
  IS 'Finds an expired purse in the given time range and refunds the coins (if any).';
CREATE OR REPLACE FUNCTION exchange_do_reserve_open_deposit(
  IN in_coin_pub BYTEA,
  IN in_known_coin_id INT8,
  IN in_coin_sig BYTEA,
  IN in_reserve_sig BYTEA,
  IN in_reserve_pub BYTEA,
  IN in_coin_total taler_amount,
  OUT out_insufficient_funds BOOLEAN)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO exchange.reserves_open_deposits
  (reserve_sig
  ,reserve_pub
  ,coin_pub
  ,coin_sig
  ,contribution
  )
  VALUES
  (in_reserve_sig
  ,in_reserve_pub
  ,in_coin_pub
  ,in_coin_sig
  ,in_coin_total
  )
  ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
  out_insufficient_funds=FALSE;
  RETURN;
END IF;
UPDATE exchange.known_coins kc
  SET
    remaining.frac=(kc.remaining).frac-in_coin_total.frac
       + CASE
         WHEN (kc.remaining).frac < in_coin_total.frac
         THEN 100000000
         ELSE 0
         END,
    remaining.val=(kc.remaining).val-in_coin_total.val
       - CASE
         WHEN (kc.remaining).frac < in_coin_total.frac
         THEN 1
         ELSE 0
         END
  WHERE coin_pub=in_coin_pub
    AND ( ((kc.remaining).val > in_coin_total.val) OR
          ( ((kc.remaining).frac >= in_coin_total.frac) AND
            ((kc.remaining).val >= in_coin_total.val) ) );
IF NOT FOUND
THEN
  out_insufficient_funds=TRUE;
  RETURN;
END IF;
out_insufficient_funds=FALSE;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_reserve_open(
  IN in_reserve_pub BYTEA,
  IN in_total_paid taler_amount,
  IN in_reserve_payment taler_amount,
  IN in_min_purse_limit INT4,
  IN in_default_purse_limit INT4,
  IN in_reserve_sig BYTEA,
  IN in_desired_expiration INT8,
  IN in_reserve_gc_delay INT8,
  IN in_now INT8,
  IN in_open_fee taler_amount,
  OUT out_open_cost taler_amount,
  OUT out_final_expiration INT8,
  OUT out_no_reserve BOOLEAN,
  OUT out_no_funds BOOLEAN,
  OUT out_reserve_balance taler_amount)
LANGUAGE plpgsql
AS $$
DECLARE
  my_balance taler_amount;
  my_cost taler_amount;
  my_cost_tmp INT8;
  my_years_tmp INT4;
  my_years INT4;
  my_needs_update BOOL;
  my_expiration_date INT8;
  reserve RECORD;
BEGIN
SELECT current_balance
      ,expiration_date
      ,purses_allowed
  INTO reserve
  FROM reserves
 WHERE reserve_pub=in_reserve_pub;
IF NOT FOUND
THEN
  RAISE NOTICE 'reserve not found';
  out_no_reserve = TRUE;
  out_no_funds = TRUE;
  out_reserve_balance.val = 0;
  out_reserve_balance.frac = 0;
  out_open_cost.val = 0;
  out_open_cost.frac = 0;
  out_final_expiration = 0;
  RETURN;
END IF;
out_no_reserve = FALSE;
out_reserve_balance = reserve.current_balance;
IF (reserve.expiration_date < in_now)
THEN
  my_expiration_date = in_now;
ELSE
  my_expiration_date = reserve.expiration_date;
END IF;
my_cost.val = 0;
my_cost.frac = 0;
my_needs_update = FALSE;
my_years = 0;
IF (my_expiration_date < in_desired_expiration)
THEN
  my_years = (31535999999999 + in_desired_expiration - my_expiration_date) / 31536000000000;
  reserve.purses_allowed = in_default_purse_limit;
  my_expiration_date = my_expiration_date + 31536000000000 * my_years;
END IF;
IF (reserve.purses_allowed < in_min_purse_limit)
THEN
  my_years = (31535999999999 + in_desired_expiration - in_now) / 31536000000000;
  my_expiration_date = in_now + 31536000000000 * my_years;
  my_years_tmp = (in_min_purse_limit + in_default_purse_limit - reserve.purses_allowed - 1) / in_default_purse_limit;
  my_years = my_years + my_years_tmp;
  reserve.purses_allowed = reserve.purses_allowed + (in_default_purse_limit * my_years_tmp);
END IF;
IF (my_years > 0)
THEN
  my_cost.val = my_years * in_open_fee.val;
  my_cost_tmp = my_years * in_open_fee.frac / 100000000;
  IF (CAST (my_cost.val + my_cost_tmp AS INT8) < my_cost.val)
  THEN
    out_open_cost.val=9223372036854775807;
    out_open_cost.frac=2147483647;
    out_final_expiration=my_expiration_date;
    out_no_funds=FALSE;
    RAISE NOTICE 'arithmetic issue computing amount';
  RETURN;
  END IF;
  my_cost.val = CAST (my_cost.val + my_cost_tmp AS INT8);
  my_cost.frac = my_years * in_open_fee.frac % 100000000;
  my_needs_update = TRUE;
END IF;
IF NOT my_needs_update
THEN
  out_final_expiration = reserve.expiration_date;
  out_open_cost.val = 0;
  out_open_cost.frac = 0;
  out_no_funds=FALSE;
  RAISE NOTICE 'no change required';
  RETURN;
END IF;
IF ( (in_total_paid.val < my_cost.val) OR
     ( (in_total_paid.val = my_cost.val) AND
       (in_total_paid.frac < my_cost.frac) ) )
THEN
  out_open_cost.val = my_cost.val;
  out_open_cost.frac = my_cost.frac;
  out_no_funds=FALSE;
  IF (reserve.expiration_date >= in_desired_expiration)
  THEN
    RAISE NOTICE 'forcing low expiration time';
    out_final_expiration = 0;
  ELSE
    out_final_expiration = reserve.expiration_date;
  END IF;
  RAISE NOTICE 'amount paid too low';
  RETURN;
END IF;
IF (out_reserve_balance.val > in_reserve_payment.val)
THEN
  IF (out_reserve_balance.frac >= in_reserve_payment.frac)
  THEN
    my_balance.val=out_reserve_balance.val - in_reserve_payment.val;
    my_balance.frac=out_reserve_balance.frac - in_reserve_payment.frac;
  ELSE
    my_balance.val=out_reserve_balance.val - in_reserve_payment.val - 1;
    my_balance.frac=out_reserve_balance.frac + 100000000 - in_reserve_payment.frac;
  END IF;
ELSE
  IF (out_reserve_balance.val = in_reserve_payment.val) AND (out_reserve_balance.frac >= in_reserve_payment.frac)
  THEN
    my_balance.val=0;
    my_balance.frac=out_reserve_balance.frac - in_reserve_payment.frac;
  ELSE
    out_final_expiration = reserve.expiration_date;
    out_open_cost.val = my_cost.val;
    out_open_cost.frac = my_cost.frac;
    out_no_funds=TRUE;
    RAISE NOTICE 'reserve balance too low';
  RETURN;
  END IF;
END IF;
UPDATE reserves SET
  current_balance=my_balance
 ,gc_date=reserve.expiration_date + in_reserve_gc_delay
 ,expiration_date=my_expiration_date
 ,purses_allowed=reserve.purses_allowed
WHERE
 reserve_pub=in_reserve_pub;
out_final_expiration=my_expiration_date;
out_open_cost = my_cost;
out_no_funds=FALSE;
RETURN;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_insert_or_update_policy_details(
  IN in_policy_hash_code BYTEA,
  IN in_policy_json TEXT,
  IN in_deadline INT8,
  IN in_commitment taler_amount,
  IN in_accumulated_total taler_amount,
  IN in_fee taler_amount,
  IN in_transferable taler_amount,
  IN in_fulfillment_state SMALLINT,
  OUT out_policy_details_serial_id INT8,
  OUT out_accumulated_total taler_amount,
  OUT out_fulfillment_state SMALLINT)
LANGUAGE plpgsql
AS $$
DECLARE
    cur_commitment taler_amount;
DECLARE
    cur_accumulated_total taler_amount;
DECLARE
    rval RECORD;
BEGIN
       INSERT INTO policy_details
               (policy_hash_code,
                policy_json,
                deadline,
                commitment,
                accumulated_total,
                fee,
                transferable,
                fulfillment_state)
       VALUES (in_policy_hash_code,
                in_policy_json,
                in_deadline,
                in_commitment,
                in_accumulated_total,
                in_fee,
                in_transferable,
                in_fulfillment_state)
       ON CONFLICT (policy_hash_code) DO NOTHING
       RETURNING policy_details_serial_id INTO out_policy_details_serial_id;
       IF FOUND THEN
               out_accumulated_total = in_accumulated_total;
               out_fulfillment_state = in_fulfillment_state;
               RETURN;
       END IF;
       SELECT policy_details_serial_id
         ,commitment
         ,accumulated_total
       INTO rval
       FROM policy_details
       WHERE policy_hash_code = in_policy_hash_code;
       out_policy_details_serial_id := rval.policy_details_serial_id;
       cur_commitment := rval.commitment;
       cur_accumulated_total := rval.accumulated_total;
       out_accumulated_total.val = cur_accumulated_total.val + in_accumulated_total.val;
       out_accumulated_total.frac = cur_accumulated_total.frac + in_accumulated_total.frac;
       out_accumulated_total.val = out_accumulated_total.val + out_accumulated_total.frac / 100000000;
       out_accumulated_total.frac = out_accumulated_total.frac % 100000000;
       IF (out_accumulated_total.val > (1 << 52))
       THEN
               RAISE EXCEPTION 'accumulation overflow';
       END IF;
       IF (out_fullfillment_state = 2) 
       THEN
               IF (out_accumulated_total.val >= cur_commitment.val OR
                       (out_accumulated_total.val = cur_commitment.val AND
                               out_accumulated_total.frac >= cur_commitment.frac))
               THEN
                       out_fulfillment_state = 3; 
               END IF;
       END IF;
       UPDATE exchange.policy_details
       SET
               accumulated = out_accumulated_total,
               fulfillment_state = out_fulfillment_state
       WHERE
               policy_details_serial_id = out_policy_details_serial_id;
END $$;
DROP FUNCTION IF EXISTS exchange_do_insert_aml_decision;
CREATE FUNCTION exchange_do_insert_aml_decision(
  IN in_payto_uri TEXT, 
  IN in_h_normalized_payto BYTEA,
  IN in_h_full_payto BYTEA, 
  IN in_decision_time INT8,
  IN in_expiration_time INT8,
  IN in_properties JSONB, 
  IN in_kyc_attributes_enc BYTEA, 
  IN in_kyc_attributes_hash BYTEA, 
  IN in_kyc_attributes_expiration INT8, 
  IN in_new_rules JSONB,
  IN in_to_investigate BOOLEAN,
  IN in_new_measure_name TEXT, 
  IN in_jmeasures JSONB, 
  IN in_justification TEXT, 
  IN in_decider_pub BYTEA, 
  IN in_decider_sig BYTEA, 
  IN in_notify_s TEXT,
  IN ina_events TEXT[],
  IN in_form_name TEXT, 
  OUT out_invalid_officer BOOLEAN,
  OUT out_account_unknown BOOLEAN,
  OUT out_last_date INT8,
  OUT out_legitimization_measure_serial_id INT8,
  OUT out_is_wallet BOOL) 
LANGUAGE plpgsql
AS $$
DECLARE
  my_outcome_serial_id INT8;
  my_legitimization_process_serial_id INT8;
  my_kyc_attributes_serial_id INT8;
  my_rec RECORD;
  my_access_token BYTEA;
  my_i INT4;
  ini_event TEXT;
BEGIN
out_account_unknown=FALSE;
out_legitimization_measure_serial_id=0;
IF in_decider_pub IS NOT NULL
THEN
  IF in_justification IS NULL OR in_decider_sig IS NULL
  THEN
    RAISE EXCEPTION 'Got in_decider_sig without justification or signature.';
  END IF;
  PERFORM
    FROM aml_staff
    WHERE decider_pub=in_decider_pub
      AND is_active
      AND NOT read_only;
  IF NOT FOUND
  THEN
    out_invalid_officer=TRUE;
    out_last_date=0;
    RETURN;
  END IF;
END IF;
out_invalid_officer=FALSE;
SELECT decision_time
  INTO out_last_date
  FROM legitimization_outcomes
 WHERE h_payto=in_h_normalized_payto
   AND is_active
 ORDER BY decision_time DESC, outcome_serial_id DESC;
IF FOUND
THEN
  IF in_decider_pub IS NOT NULL AND out_last_date > in_decision_time
  THEN
    RETURN;
  END IF;
  UPDATE legitimization_outcomes
     SET is_active=FALSE
   WHERE h_payto=in_h_normalized_payto
     AND is_active;
ELSE
  out_last_date = 0;
END IF;
SELECT access_token
      ,is_wallet
  INTO my_rec
  FROM kyc_targets
 WHERE h_normalized_payto=in_h_normalized_payto;
IF NOT FOUND
THEN
  IF in_payto_uri IS NULL
  THEN
    out_account_unknown=TRUE;
    RETURN;
  END IF;
  out_is_wallet
    = (LOWER (SUBSTRING (in_payto_uri, 22)) =
       'payto://taler-reserve/') OR
      (LOWER (SUBSTRING (in_payto_uri, 27)) =
       'payto://taler-reserve-http/');
  INSERT INTO kyc_targets
    (h_normalized_payto
    ,is_wallet
    ) VALUES (
     in_h_normalized_payto
    ,out_is_wallet
    )
    RETURNING access_token
      INTO my_access_token;
  INSERT INTO wire_targets
    (wire_target_h_payto
    ,h_normalized_payto
    ,payto_uri
    ) VALUES (
     in_h_full_payto
    ,in_h_normalized_payto
    ,in_payto_uri
    )
    ON CONFLICT DO NOTHING;
ELSE
  my_access_token = my_rec.access_token;
  out_is_wallet = my_rec.is_wallet;
END IF;
IF in_jmeasures IS NOT NULL
THEN
  SELECT legitimization_measure_serial_id
    INTO out_legitimization_measure_serial_id
    FROM legitimization_measures
    WHERE access_token=my_access_token
      AND jmeasures=in_jmeasures
      AND NOT is_finished;
  IF NOT FOUND
  THEN
    INSERT INTO legitimization_measures
      (access_token
      ,start_time
      ,jmeasures
      ,display_priority
      ) VALUES (
       my_access_token
      ,in_decision_time
      ,in_jmeasures
      ,1)
      RETURNING
        legitimization_measure_serial_id
      INTO
        out_legitimization_measure_serial_id;
  END IF;
END IF;
RAISE NOTICE 'marking legi measures of % as finished except for %', my_access_token, out_legitimization_measure_serial_id;
UPDATE legitimization_measures
  SET is_finished=TRUE
  WHERE access_token=my_access_token
    AND NOT is_finished
    AND legitimization_measure_serial_id != out_legitimization_measure_serial_id;
UPDATE legitimization_outcomes
   SET is_active=FALSE
 WHERE h_payto=in_h_normalized_payto
   AND expiration_time >= in_decision_time;
INSERT INTO legitimization_outcomes
  (h_payto
  ,decision_time
  ,expiration_time
  ,jproperties
  ,new_measure_name
  ,to_investigate
  ,jnew_rules
  ) VALUES (
   in_h_normalized_payto
  ,in_decision_time
  ,in_expiration_time
  ,in_properties
  ,in_new_measure_name
  ,in_to_investigate
  ,in_new_rules
  )
  RETURNING outcome_serial_id
       INTO my_outcome_serial_id;
IF in_kyc_attributes_enc IS NOT NULL
THEN
  IF in_kyc_attributes_hash IS NULL OR in_kyc_attributes_expiration IS NULL
  THEN
    RAISE EXCEPTION 'Got in_kyc_attributes_hash without hash or expiration.';
  END IF;
  IF in_decider_pub IS NULL
  THEN
    RAISE EXCEPTION 'Got in_kyc_attributes_hash without in_decider_pub.';
  END IF;
  INSERT INTO legitimization_processes
    (h_payto
    ,start_time
    ,expiration_time
    ,provider_name
    ,provider_user_id
    ,finished
    ) VALUES (
     in_h_normalized_payto
    ,in_decision_time
    ,in_decision_time
    ,'aml-officer'
    ,in_decider_pub
    ,TRUE
    )
    RETURNING legitimization_process_serial_id
    INTO my_legitimization_process_serial_id;
  INSERT INTO kyc_attributes
    (h_payto
    ,collection_time
    ,expiration_time
    ,form_name
    ,by_aml_officer
    ,encrypted_attributes
    ,legitimization_serial
    ) VALUES (
     in_h_normalized_payto
    ,in_decision_time
    ,in_kyc_attributes_expiration
    ,in_form_name
    ,TRUE
    ,in_kyc_attributes_enc
    ,my_legitimization_process_serial_id
    )
    RETURNING kyc_attributes_serial_id
    INTO my_kyc_attributes_serial_id;
  NOTIFY XSX9Z5XGWWYFKXTAYCES63B62527JKNX9XD0131Z08THVV8YW5BZG;
END IF;
IF in_decider_pub IS NOT NULL
THEN
  INSERT INTO aml_history
    (h_payto
    ,outcome_serial_id
    ,justification
    ,decider_pub
    ,decider_sig
    ,kyc_attributes_hash
    ,kyc_attributes_serial_id
    ) VALUES (
     in_h_normalized_payto
    ,my_outcome_serial_id
    ,in_justification
    ,in_decider_pub
    ,in_decider_sig
    ,in_kyc_attributes_hash
    ,my_kyc_attributes_serial_id
  );
END IF;
FOR i IN 1..COALESCE(array_length(ina_events,1),0)
LOOP
  ini_event = ina_events[i];
  INSERT INTO kyc_events
    (event_timestamp
    ,event_type
    ) VALUES (
     in_decision_time
    ,ini_event);
  IF (ini-event = 'ACCOUNT_OPEN')
  THEN
    UPDATE kyc_targets
       SET open_time=in_decision_time
          ,close_time=NULL
     WHERE h_normalized_payto=in_h_normalized_payto;
  END IF;
  IF (ini-event = 'ACCOUNT_IDLE')
  THEN
    UPDATE kyc_targets
       SET close_time=in_decision_time
     WHERE h_normalized_payto=in_h_normalized_payto;
  END IF;
END LOOP;
INSERT INTO kyc_alerts
  (h_payto
  ,trigger_type
  ) VALUES (
   in_h_normalized_payto
  ,1
  )
 ON CONFLICT DO NOTHING;
EXECUTE FORMAT (
   'NOTIFY %s'
  ,in_notify_s);
END $$;
COMMENT ON FUNCTION exchange_do_insert_aml_decision(TEXT, BYTEA, BYTEA, INT8, INT8, JSONB, BYTEA, BYTEA, INT8, JSONB, BOOLEAN, TEXT, JSONB, TEXT, BYTEA, BYTEA, TEXT, TEXT[], TEXT)
  IS 'Checks whether the AML officer is eligible to make AML decisions and if so inserts the decision into the table';
DROP FUNCTION IF EXISTS exchange_do_insert_successor_measure;
CREATE FUNCTION exchange_do_insert_successor_measure(
  IN in_h_normalized_payto BYTEA,
  IN in_decision_time INT8,
  IN in_expiration_time INT8,
  IN in_new_measure_name TEXT, 
  IN in_jmeasures JSONB, 
  OUT out_last_date INT8,
  OUT out_account_unknown BOOLEAN,
  OUT out_legitimization_measure_serial_id INT8
)
LANGUAGE plpgsql
AS $$
DECLARE
  my_outcome_serial_id INT8;
  my_access_token BYTEA;
  my_is_wallet BOOL;
BEGIN
out_account_unknown=FALSE;
out_legitimization_measure_serial_id=0;
SELECT decision_time
  INTO out_last_date
  FROM legitimization_outcomes
 WHERE h_payto=in_h_normalized_payto
   AND is_active
 ORDER BY decision_time DESC, outcome_serial_id DESC;
IF FOUND
THEN
  IF out_last_date > in_decision_time
  THEN
    RETURN;
  END IF;
  UPDATE legitimization_outcomes
     SET is_active=FALSE
   WHERE h_payto=in_h_normalized_payto
     AND is_active;
ELSE
  out_last_date = 0;
END IF;
SELECT access_token
  INTO my_access_token
  FROM kyc_targets
 WHERE h_normalized_payto=in_h_normalized_payto;
IF NOT FOUND
THEN
  IF in_payto_uri IS NULL
  THEN
    out_account_unknown=TRUE;
    RETURN;
  END IF;
  my_is_wallet
    = (LOWER (SUBSTRING (in_payto_uri, 22)) =
       'payto://taler-reserve/') OR
      (LOWER (SUBSTRING (in_payto_uri, 27)) =
       'payto://taler-reserve-http/');
  INSERT INTO kyc_targets
    (h_normalized_payto
    ,is_wallet)
    VALUES
    (in_h_normalized_payto
    ,my_is_wallet)
    RETURNING access_token
      INTO my_access_token;
  INSERT INTO wire_targets
    (wire_target_h_payto
    ,h_normalized_payto
    ,payto_uri)
    VALUES
    (in_h_full_payto
    ,in_h_normalized_payto
    ,in_payto_uri)
  ON CONFLICT DO NOTHING;
END IF;
SELECT legitimization_measure_serial_id
  INTO out_legitimization_measure_serial_id
  FROM legitimization_measures
  WHERE access_token=my_access_token
    AND jmeasures=in_jmeasures
    AND NOT is_finished;
IF NOT FOUND
THEN
  INSERT INTO legitimization_measures
    (access_token
    ,start_time
    ,jmeasures
    ,display_priority)
    VALUES
    (my_access_token
    ,in_decision_time
    ,in_jmeasures
    ,1)
    RETURNING
      legitimization_measure_serial_id
    INTO
      out_legitimization_measure_serial_id;
END IF;
UPDATE legitimization_measures
  SET is_finished=TRUE
  WHERE access_token=my_access_token
    AND NOT is_finished
    AND legitimization_measure_serial_id != out_legitimization_measure_serial_id;
UPDATE legitimization_outcomes
   SET is_active=FALSE
 WHERE h_payto=in_h_normalized_payto
   AND expiration_time >= in_decision_time;
INSERT INTO legitimization_outcomes
  (h_payto
  ,decision_time
  ,expiration_time
  ,jproperties
  ,new_measure_name
  ,to_investigate
  ,jnew_rules
  )
  VALUES
  (in_h_normalized_payto
  ,in_decision_time
  ,in_expiration_time
  ,'{}'::JSONB
  ,in_new_measure_name
  ,FALSE
  ,NULL
  )
  RETURNING
    outcome_serial_id
  INTO
    my_outcome_serial_id;
END $$;
COMMENT ON FUNCTION exchange_do_insert_successor_measure(BYTEA, INT8, INT8, TEXT, JSONB)
  IS 'Checks whether the AML officer is eligible to make AML decisions and if so inserts the decision into the table';
CREATE OR REPLACE FUNCTION exchange_do_insert_aml_officer(
  IN in_decider_pub BYTEA,
  IN in_master_sig BYTEA,
  IN in_decider_name TEXT,
  IN in_is_active BOOLEAN,
  IN in_read_only BOOLEAN,
  IN in_last_change INT8,
  OUT out_last_change INT8)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO exchange.aml_staff
  (decider_pub
  ,master_sig
  ,decider_name
  ,is_active
  ,read_only
  ,last_change
  ) VALUES
  (in_decider_pub
  ,in_master_sig
  ,in_decider_name
  ,in_is_active
  ,in_read_only
  ,in_last_change)
 ON CONFLICT DO NOTHING;
IF FOUND
THEN
  out_last_change=0;
  RETURN;
END IF;
SELECT last_change
  INTO out_last_change
  FROM exchange.aml_staff
  WHERE decider_pub=in_decider_pub;
ASSERT FOUND, 'cannot have INSERT conflict but no AML staff record';
IF out_last_change >= in_last_change
THEN
 RETURN;
END IF;
UPDATE exchange.aml_staff
  SET master_sig=in_master_sig
     ,decider_name=in_decider_name
     ,is_active=in_is_active
     ,read_only=in_read_only
     ,last_change=in_last_change
  WHERE decider_pub=in_decider_pub;
END $$;
COMMENT ON FUNCTION exchange_do_insert_aml_officer(BYTEA, BYTEA, TEXT, BOOL, BOOL, INT8)
  IS 'Inserts or updates AML staff record, making sure the update is more recent than the previous change';
DROP FUNCTION IF EXISTS exchange_do_array_reserves_insert;
CREATE FUNCTION exchange_do_array_reserves_insert(
  IN in_gc_date INT8,
  IN in_reserve_expiration INT8,
  IN ina_reserve_pub BYTEA[],
  IN ina_wire_ref INT8[],
  IN ina_credit taler_amount[],
  IN ina_exchange_account_name TEXT[],
  IN ina_execution_date INT8[],
  IN ina_wire_source_h_payto BYTEA[],
  IN ina_h_normalized_payto BYTEA[],
  IN ina_payto_uri TEXT[],
  IN ina_notify TEXT[])
RETURNS SETOF exchange_do_array_reserve_insert_return_type
LANGUAGE plpgsql
AS $$
DECLARE
  conflict BOOL;
  dup BOOL;
  uuid INT8;
  i INT4;
  my_is_wallet BOOL;
  ini_reserve_pub BYTEA;
  ini_wire_ref INT8;
  ini_credit taler_amount;
  ini_exchange_account_name TEXT;
  ini_execution_date INT8;
  ini_wire_source_h_payto BYTEA;
  ini_h_normalized_payto BYTEA;
  ini_payto_uri TEXT;
  ini_notify TEXT;
BEGIN
  FOR i IN 1..array_length(ina_reserve_pub,1)
  LOOP
    ini_reserve_pub = ina_reserve_pub[i];
    ini_wire_ref = ina_wire_ref[i];
    ini_credit = ina_credit[i];
    ini_exchange_account_name = ina_exchange_account_name[i];
    ini_execution_date = ina_execution_date[i];
    ini_wire_source_h_payto = ina_wire_source_h_payto[i];
    ini_h_normalized_payto = ina_h_normalized_payto[i];
    ini_payto_uri = ina_payto_uri[i];
    ini_notify = ina_notify[i];
    my_is_wallet
      = (LOWER (SUBSTRING (ini_payto_uri, 22)) =
         'payto://taler-reserve/') OR
        (LOWER (SUBSTRING (ini_payto_uri, 27)) =
         'payto://taler-reserve-http/');
    INSERT INTO kyc_targets
      (h_normalized_payto
      ,is_wallet
      ) VALUES (
       ini_h_normalized_payto
      ,my_is_wallet
      )
      ON CONFLICT DO NOTHING;
    INSERT INTO wire_targets
      (wire_target_h_payto
      ,h_normalized_payto
      ,payto_uri
      ) VALUES (
        ini_wire_source_h_payto
       ,ini_h_normalized_payto
       ,ini_payto_uri
      )
    ON CONFLICT DO NOTHING;
    INSERT INTO reserves
      (reserve_pub
      ,current_balance
      ,expiration_date
      ,gc_date
    ) VALUES (
      ini_reserve_pub
     ,ini_credit
     ,in_reserve_expiration
     ,in_gc_date
    )
    ON CONFLICT DO NOTHING
    RETURNING reserve_uuid
      INTO uuid;
    conflict = NOT FOUND;
    INSERT INTO reserves_in
      (reserve_pub
      ,wire_reference
      ,credit
      ,exchange_account_section
      ,wire_source_h_payto
      ,execution_date
    ) VALUES (
      ini_reserve_pub
     ,ini_wire_ref
     ,ini_credit
     ,ini_exchange_account_name
     ,ini_wire_source_h_payto
     ,ini_execution_date
    )
    ON CONFLICT DO NOTHING;
    IF NOT FOUND
    THEN
      IF conflict
      THEN
        dup = TRUE;
      else
        dup = FALSE;
      END IF;
    ELSE
      IF NOT conflict
      THEN
        EXECUTE FORMAT (
          'NOTIFY %s'
          ,ini_notify);
      END IF;
      dup = FALSE;
    END IF;
    RETURN NEXT (dup,uuid);
  END LOOP;
  RETURN;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_batch_reserves_update(
  IN in_reserve_pub BYTEA,
  IN in_expiration_date INT8,
  IN in_wire_ref INT8,
  IN in_credit taler_amount,
  IN in_exchange_account_name TEXT,
  IN in_wire_source_h_payto BYTEA,
  IN in_notify text,
  OUT out_duplicate BOOLEAN)
LANGUAGE plpgsql
AS $$
BEGIN
  INSERT INTO reserves_in
    (reserve_pub
    ,wire_reference
    ,credit
    ,exchange_account_section
    ,wire_source_h_payto
    ,execution_date)
    VALUES
    (in_reserve_pub
    ,in_wire_ref
    ,in_credit
    ,in_exchange_account_name
    ,in_wire_source_h_payto
    ,in_expiration_date)
    ON CONFLICT DO NOTHING;
  IF FOUND
  THEN
    out_duplicate = FALSE;
    UPDATE reserves rs
      SET
         current_balance.frac = (rs.current_balance).frac+in_credit.frac
           - CASE
             WHEN (rs.current_balance).frac + in_credit.frac >= 100000000
               THEN 100000000
             ELSE 1
             END
        ,current_balance.val = (rs.current_balance).val+in_credit.val
           + CASE
             WHEN (rs.current_balance).frac + in_credit.frac >= 100000000
               THEN 1
             ELSE 0
             END
             ,expiration_date=GREATEST(expiration_date,in_expiration_date)
             ,gc_date=GREATEST(gc_date,in_expiration_date)
            WHERE reserve_pub=in_reserve_pub;
    EXECUTE FORMAT (
      'NOTIFY %s'
      ,in_notify);
  ELSE
    out_duplicate = TRUE;
  END IF;
  RETURN;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_get_link_data(
  IN in_coin_pub BYTEA
)
RETURNS SETOF record
LANGUAGE plpgsql
AS $$
DECLARE
  curs CURSOR
  FOR
  SELECT
   melt_serial_id
  FROM refresh_commitments
  WHERE old_coin_pub=in_coin_pub;
DECLARE
  i RECORD;
BEGIN
OPEN curs;
LOOP
    FETCH NEXT FROM curs INTO i;
    EXIT WHEN NOT FOUND;
    RETURN QUERY
      SELECT
       tp.transfer_pub
      ,denoms.denom_pub
      ,rrc.ev_sig
      ,rrc.ewv
      ,rrc.link_sig
      ,rrc.freshcoin_index
      ,rrc.coin_ev
      FROM refresh_revealed_coins rrc
       JOIN refresh_transfer_keys tp
         ON (tp.melt_serial_id=rrc.melt_serial_id)
       JOIN denominations denoms
         ON (rrc.denominations_serial=denoms.denominations_serial)
       WHERE rrc.melt_serial_id =i.melt_serial_id
       ORDER BY tp.transfer_pub,
       rrc.freshcoin_index ASC
       ;
END LOOP;
CLOSE curs;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_batch4_known_coin(
  IN in_coin_pub1 BYTEA,
  IN in_denom_pub_hash1 BYTEA,
  IN in_h_age_commitment1 BYTEA,
  IN in_denom_sig1 BYTEA,
  IN in_coin_pub2 BYTEA,
  IN in_denom_pub_hash2 BYTEA,
  IN in_h_age_commitment2 BYTEA,
  IN in_denom_sig2 BYTEA,
  IN in_coin_pub3 BYTEA,
  IN in_denom_pub_hash3 BYTEA,
  IN in_h_age_commitment3 BYTEA,
  IN in_denom_sig3 BYTEA,
  IN in_coin_pub4 BYTEA,
  IN in_denom_pub_hash4 BYTEA,
  IN in_h_age_commitment4 BYTEA,
  IN in_denom_sig4 BYTEA,
  OUT existed1 BOOLEAN,
  OUT existed2 BOOLEAN,
  OUT existed3 BOOLEAN,
  OUT existed4 BOOLEAN,
  OUT known_coin_id1 INT8,
  OUT known_coin_id2 INT8,
  OUT known_coin_id3 INT8,
  OUT known_coin_id4 INT8,
  OUT denom_pub_hash1 BYTEA,
  OUT denom_pub_hash2 BYTEA,
  OUT denom_pub_hash3 BYTEA,
  OUT denom_pub_hash4 BYTEA,
  OUT age_commitment_hash1 BYTEA,
  OUT age_commitment_hash2 BYTEA,
  OUT age_commitment_hash3 BYTEA,
  OUT age_commitment_hash4 BYTEA)
LANGUAGE plpgsql
AS $$
BEGIN
WITH dd AS (
SELECT
  denominations_serial,
  coin
  FROM denominations
    WHERE denom_pub_hash
    IN
     (in_denom_pub_hash1,
      in_denom_pub_hash2,
      in_denom_pub_hash3,
      in_denom_pub_hash4)
     ),
     input_rows AS (
     VALUES
      (in_coin_pub1,
      in_denom_pub_hash1,
      in_h_age_commitment1,
      in_denom_sig1),
      (in_coin_pub2,
      in_denom_pub_hash2,
      in_h_age_commitment2,
      in_denom_sig2),
      (in_coin_pub3,
      in_denom_pub_hash3,
      in_h_age_commitment3,
      in_denom_sig3),
      (in_coin_pub4,
      in_denom_pub_hash4,
      in_h_age_commitment4,
      in_denom_sig4)
      ),
      ins AS (
      INSERT INTO known_coins (
      coin_pub,
      denominations_serial,
      age_commitment_hash,
      denom_sig,
      remaining
      )
      SELECT
        ir.coin_pub,
        dd.denominations_serial,
        ir.age_commitment_hash,
        ir.denom_sig,
        dd.coin
        FROM input_rows ir
        JOIN dd
          ON dd.denom_pub_hash = ir.denom_pub_hash
          ON CONFLICT DO NOTHING
          RETURNING known_coin_id
      ),
       exists AS (
         SELECT
         CASE
           WHEN
             ins.known_coin_id IS NOT NULL
             THEN
               FALSE
             ELSE
               TRUE
         END AS existed,
         ins.known_coin_id,
         dd.denom_pub_hash,
         kc.age_commitment_hash
         FROM input_rows ir
         LEFT JOIN ins
           ON ins.coin_pub = ir.coin_pub
         LEFT JOIN known_coins kc
           ON kc.coin_pub = ir.coin_pub
         LEFT JOIN dd
           ON dd.denom_pub_hash = ir.denom_pub_hash
         )
SELECT
 exists.existed AS existed1,
 exists.known_coin_id AS known_coin_id1,
 exists.denom_pub_hash AS denom_pub_hash1,
 exists.age_commitment_hash AS age_commitment_hash1,
 (
   SELECT exists.existed
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 ) AS existed2,
 (
   SELECT exists.known_coin_id
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 ) AS known_coin_id2,
 (
   SELECT exists.denom_pub_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 ) AS denom_pub_hash2,
 (
   SELECT exists.age_commitment_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 )AS age_commitment_hash2,
 (
   SELECT exists.existed
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash3
 ) AS existed3,
 (
   SELECT exists.known_coin_id
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash3
 ) AS known_coin_id3,
 (
   SELECT exists.denom_pub_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash3
 ) AS denom_pub_hash3,
 (
   SELECT exists.age_commitment_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash3
 )AS age_commitment_hash3,
 (
   SELECT exists.existed
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash4
 ) AS existed4,
 (
   SELECT exists.known_coin_id
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash4
 ) AS known_coin_id4,
 (
   SELECT exists.denom_pub_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash4
 ) AS denom_pub_hash4,
 (
   SELECT exists.age_commitment_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash4
 )AS age_commitment_hash4
FROM exists;
RETURN;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_batch2_known_coin(
  IN in_coin_pub1 BYTEA,
  IN in_denom_pub_hash1 BYTEA,
  IN in_h_age_commitment1 BYTEA,
  IN in_denom_sig1 BYTEA,
  IN in_coin_pub2 BYTEA,
  IN in_denom_pub_hash2 BYTEA,
  IN in_h_age_commitment2 BYTEA,
  IN in_denom_sig2 BYTEA,
  OUT existed1 BOOLEAN,
  OUT existed2 BOOLEAN,
  OUT known_coin_id1 INT8,
  OUT known_coin_id2 INT8,
  OUT denom_pub_hash1 BYTEA,
  OUT denom_pub_hash2 BYTEA,
  OUT age_commitment_hash1 BYTEA,
  OUT age_commitment_hash2 BYTEA)
LANGUAGE plpgsql
AS $$
BEGIN
WITH dd AS (
SELECT
  denominations_serial,
  coin
  FROM denominations
    WHERE denom_pub_hash
    IN
     (in_denom_pub_hash1,
      in_denom_pub_hash2)
     ),
     input_rows AS (
     VALUES
      (in_coin_pub1,
      in_denom_pub_hash1,
      in_h_age_commitment1,
      in_denom_sig1),
      (in_coin_pub2,
      in_denom_pub_hash2,
      in_h_age_commitment2,
      in_denom_sig2)
      ),
      ins AS (
      INSERT INTO known_coins (
      coin_pub,
      denominations_serial,
      age_commitment_hash,
      denom_sig,
      remaining
      )
      SELECT
        ir.coin_pub,
        dd.denominations_serial,
        ir.age_commitment_hash,
        ir.denom_sig,
        dd.coin
        FROM input_rows ir
        JOIN dd
          ON dd.denom_pub_hash = ir.denom_pub_hash
          ON CONFLICT DO NOTHING
          RETURNING known_coin_id
      ),
       exists AS (
       SELECT
        CASE
          WHEN ins.known_coin_id IS NOT NULL
          THEN
            FALSE
          ELSE
            TRUE
        END AS existed,
        ins.known_coin_id,
        dd.denom_pub_hash,
        kc.age_commitment_hash
        FROM input_rows ir
        LEFT JOIN ins
          ON ins.coin_pub = ir.coin_pub
        LEFT JOIN known_coins kc
          ON kc.coin_pub = ir.coin_pub
        LEFT JOIN dd
          ON dd.denom_pub_hash = ir.denom_pub_hash
     )
SELECT
 exists.existed AS existed1,
 exists.known_coin_id AS known_coin_id1,
 exists.denom_pub_hash AS denom_pub_hash1,
 exists.age_commitment_hash AS age_commitment_hash1,
 (
   SELECT exists.existed
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 ) AS existed2,
 (
   SELECT exists.known_coin_id
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 ) AS known_coin_id2,
 (
   SELECT exists.denom_pub_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 ) AS denom_pub_hash2,
 (
   SELECT exists.age_commitment_hash
   FROM exists
   WHERE exists.denom_pub_hash = in_denom_pub_hash2
 )AS age_commitment_hash2
FROM exists;
RETURN;
END $$;
CREATE OR REPLACE FUNCTION exchange_do_batch1_known_coin(
  IN in_coin_pub1 BYTEA,
  IN in_denom_pub_hash1 BYTEA,
  IN in_h_age_commitment1 BYTEA,
  IN in_denom_sig1 BYTEA,
  OUT existed1 BOOLEAN,
  OUT known_coin_id1 INT8,
  OUT denom_pub_hash1 BYTEA,
  OUT age_commitment_hash1 BYTEA)
LANGUAGE plpgsql
AS $$
BEGIN
WITH dd AS (
SELECT
  denominations_serial,
  coin
  FROM denominations
    WHERE denom_pub_hash
    IN
     (in_denom_pub_hash1,
      in_denom_pub_hash2)
     ),
     input_rows AS (
     VALUES
      (in_coin_pub1,
      in_denom_pub_hash1,
      in_h_age_commitment1,
      in_denom_sig1)
      ),
      ins AS (
      INSERT INTO known_coins (
      coin_pub,
      denominations_serial,
      age_commitment_hash,
      denom_sig,
      remaining
      )
      SELECT
        ir.coin_pub,
        dd.denominations_serial,
        ir.age_commitment_hash,
        ir.denom_sig,
        dd.coin
        FROM input_rows ir
        JOIN dd
          ON dd.denom_pub_hash = ir.denom_pub_hash
          ON CONFLICT DO NOTHING
          RETURNING known_coin_id
      ),
       exists AS (
       SELECT
        CASE
          WHEN ins.known_coin_id IS NOT NULL
          THEN
            FALSE
          ELSE
            TRUE
        END AS existed,
        ins.known_coin_id,
        dd.denom_pub_hash,
        kc.age_commitment_hash
        FROM input_rows ir
        LEFT JOIN ins
          ON ins.coin_pub = ir.coin_pub
        LEFT JOIN known_coins kc
          ON kc.coin_pub = ir.coin_pub
        LEFT JOIN dd
          ON dd.denom_pub_hash = ir.denom_pub_hash
       )
SELECT
 exists.existed AS existed1,
 exists.known_coin_id AS known_coin_id1,
 exists.denom_pub_hash AS denom_pub_hash1,
 exists.age_commitment_hash AS age_commitment_hash1
FROM exists;
RETURN;
END $$;
DROP PROCEDURE IF EXISTS exchange_do_kycauth_in_insert;
CREATE PROCEDURE exchange_do_kycauth_in_insert(
  IN in_account_pub BYTEA,
  IN in_wire_reference INT8,
  IN in_credit taler_amount,
  IN in_wire_source_h_payto BYTEA,
  IN in_h_normalized_payto BYTEA,
  IN in_payto_uri TEXT,
  IN in_exchange_account_name TEXT,
  IN in_execution_date INT8,
  IN in_notify_s TEXT)
LANGUAGE plpgsql
AS $$
DECLARE
  my_is_wallet BOOL;
BEGIN
  INSERT INTO kycauths_in
    (account_pub
    ,wire_reference
    ,credit
    ,wire_source_h_payto
    ,exchange_account_section
    ,execution_date
    ) VALUES (
     in_account_pub
    ,in_wire_reference
    ,in_credit
    ,in_wire_source_h_payto
    ,in_exchange_account_name
    ,in_execution_date
    )
    ON CONFLICT DO NOTHING;
  IF NOT FOUND
  THEN
    RETURN;
  END IF;
  UPDATE kyc_targets
     SET target_pub=in_account_pub
   WHERE h_normalized_payto=in_h_normalized_payto;
  IF NOT FOUND
  THEN
    my_is_wallet
      = (LOWER (SUBSTRING (in_payto_uri, 22)) =
         'payto://taler-reserve/') OR
        (LOWER (SUBSTRING (in_payto_uri, 27)) =
         'payto://taler-reserve-http/');
    INSERT INTO kyc_targets
      (h_normalized_payto
      ,is_wallet
      ,target_pub
      ) VALUES (
       in_h_normalized_payto
      ,my_is_wallet
      ,in_account_pub);
    INSERT INTO wire_targets
      (wire_target_h_payto
      ,h_normalized_payto
      ,payto_uri
      ) VALUES (
       in_wire_source_h_payto
      ,in_h_normalized_payto
      ,in_payto_uri);
  END IF;
  EXECUTE FORMAT (
     'NOTIFY %s'
    ,in_notify_s);
END $$;
DROP FUNCTION IF EXISTS exchange_do_trigger_kyc_rule_for_account;
CREATE FUNCTION exchange_do_trigger_kyc_rule_for_account(
  IN in_h_normalized_payto BYTEA,
  IN in_account_pub BYTEA, 
  IN in_merchant_pub BYTEA, 
  IN in_payto_uri TEXT, 
  IN in_h_full_payto BYTEA,
  IN in_now INT8,
  IN in_jmeasures JSONB,
  IN in_display_priority INT4,
  IN in_notify_s TEXT,
  OUT out_legitimization_measure_serial_id INT8,
  OUT out_bad_kyc_auth BOOL)
LANGUAGE plpgsql
AS $$
DECLARE
  my_rec RECORD;
  my_is_wallet BOOL;
  my_access_token BYTEA;
  my_account_pub BYTEA;
  my_reserve_pub BYTEA;
BEGIN
SELECT access_token
      ,target_pub
  INTO my_rec
  FROM kyc_targets
 WHERE h_normalized_payto=in_h_normalized_payto;
IF FOUND
THEN
  my_access_token = my_rec.access_token;
  my_account_pub = my_rec.target_pub;
  out_bad_kyc_auth = COALESCE ((my_account_pub != in_merchant_pub), TRUE);
ELSE
  my_is_wallet
      = (LOWER (SUBSTRING (in_payto_uri, 22)) =
         'payto://taler-reserve/') OR
        (LOWER (SUBSTRING (in_payto_uri, 27)) =
         'payto://taler-reserve-http/');
  INSERT INTO kyc_targets
    (h_normalized_payto
    ,is_wallet
    ,target_pub
    ) VALUES (
     in_h_normalized_payto
    ,my_is_wallet
    ,in_account_pub
    )
    RETURNING access_token
         INTO my_access_token;
  INSERT INTO wire_targets
    (payto_uri
    ,wire_target_h_payto
    ,h_normalized_payto
    ) VALUES (
     in_payto_uri
    ,in_h_full_payto
    ,in_h_normalized_payto
    );
  out_bad_kyc_auth=TRUE;
END IF;
IF out_bad_kyc_auth
THEN
  PERFORM FROM reserves_in
    WHERE wire_source_h_payto IN (
      SELECT wire_target_h_payto
        FROM wire_targets
       WHERE h_normalized_payto=in_h_normalized_payto
      )
      AND reserve_pub = in_merchant_pub
   ORDER BY execution_date DESC;
  IF FOUND
  THEN
    out_bad_kyc_auth = FALSE;
  END IF;
END IF;
UPDATE legitimization_measures
   SET display_priority=GREATEST(in_display_priority,display_priority)
 WHERE access_token=my_access_token
   AND jmeasures=in_jmeasures
   AND NOT is_finished
 RETURNING legitimization_measure_serial_id
  INTO out_legitimization_measure_serial_id;
IF NOT FOUND
THEN
  INSERT INTO legitimization_measures
    (access_token
    ,start_time
    ,jmeasures
    ,display_priority
    ) VALUES (
     my_access_token
    ,in_now
    ,in_jmeasures
    ,in_display_priority)
    RETURNING legitimization_measure_serial_id
         INTO out_legitimization_measure_serial_id;
  UPDATE legitimization_measures
    SET is_finished=TRUE
    WHERE access_token=my_access_token
      AND NOT is_finished
      AND legitimization_measure_serial_id != out_legitimization_measure_serial_id;
END IF;
EXECUTE FORMAT (
   'NOTIFY %s'
  ,in_notify_s);
END $$;
DROP FUNCTION IF EXISTS exchange_do_lookup_kyc_requirement_by_row;
CREATE FUNCTION exchange_do_lookup_kyc_requirement_by_row(
  IN in_h_normalized_payto BYTEA,
  IN in_account_pub BYTEA, 
  OUT out_account_pub BYTEA, 
  OUT out_reserve_pub BYTEA, 
  OUT out_access_token BYTEA, 
  OUT out_jrules JSONB, 
  OUT out_is_wallet BOOLEAN, 
  OUT out_not_found BOOLEAN,
  OUT out_rule_gen INT8, 
  OUT out_aml_review BOOLEAN, 
  OUT out_kyc_required BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
  my_wtrec RECORD;
  my_lorec RECORD;
  my_ok BOOL;
BEGIN
SELECT access_token
      ,target_pub
      ,is_wallet
  INTO my_wtrec
  FROM kyc_targets
 WHERE h_normalized_payto=in_h_normalized_payto;
IF NOT FOUND
THEN
  out_not_found = TRUE;
  out_kyc_required = FALSE;
  RETURN;
END IF;
my_ok = (in_account_pub IS NOT NULL) AND
        (my_wtrec.target_pub = in_account_pub);
IF ( (NOT my_ok) AND
     (in_account_pub IS NOT NULL) )
THEN
  PERFORM
     FROM reserves_in
    WHERE reserve_pub=in_account_pub
      AND wire_source_h_payto IN
     (SELECT wire_target_h_payto
        FROM wire_targets
       WHERE h_normalized_payto=in_h_normalized_payto);
  IF FOUND
  THEN
    my_wtrec.target_pub = in_account_pub;
    my_ok = TRUE;
  END IF;
END IF;
IF (NOT my_ok AND
    ( (in_account_pub IS NOT NULL) OR
      (my_wtrec.target_pub IS NULL) ) )
THEN
  out_not_found = TRUE;
  out_kyc_required = FALSE;
  RETURN;
END IF;
out_not_found = FALSE;
out_is_wallet = my_wtrec.is_wallet;
out_account_pub = my_wtrec.target_pub;
out_access_token = my_wtrec.access_token;
PERFORM
  FROM legitimization_measures
 WHERE access_token=out_access_token
   AND NOT is_finished
 LIMIT 1;
out_kyc_required = FOUND;
SELECT jnew_rules
      ,to_investigate
      ,outcome_serial_id
  INTO my_lorec
  FROM legitimization_outcomes
 WHERE h_payto=in_h_normalized_payto
   AND is_active;
IF FOUND
THEN
  out_jrules=my_lorec.jnew_rules;
  out_aml_review=my_lorec.to_investigate;
  out_rule_gen=my_lorec.outcome_serial_id;
END IF;
SELECT reserve_pub
  INTO out_reserve_pub
  FROM reserves_in
 WHERE wire_source_h_payto
   IN (SELECT wt.wire_target_h_payto
         FROM wire_targets wt
        WHERE h_normalized_payto=in_h_normalized_payto)
 ORDER BY execution_date DESC, reserve_in_serial_id DESC
 LIMIT 1;
END $$;
DROP FUNCTION IF EXISTS exchange_do_insert_active_legitimization_measure;
CREATE FUNCTION exchange_do_insert_active_legitimization_measure(
  IN in_access_token BYTEA,
  IN in_start_time INT8,
  IN in_jmeasures JSONB,
  OUT out_legitimization_measure_serial_id INT8)
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE legitimization_measures
   SET is_finished=TRUE
 WHERE access_token=in_access_token
   AND NOT is_finished;
INSERT INTO legitimization_measures
  (access_token
  ,start_time
  ,jmeasures
  ,display_priority)
  VALUES
  (in_access_token
  ,in_start_time
  ,in_jmeasures
  ,1)
  RETURNING
    legitimization_measure_serial_id
  INTO
    out_legitimization_measure_serial_id;
END $$;
COMMENT ON FUNCTION exchange_do_insert_active_legitimization_measure(BYTEA, INT8, JSONB)
  IS 'Inserts legitimization measure for an account and marks all existing such measures as inactive';
CREATE OR REPLACE FUNCTION exchange_do_select_aggregations_above_serial(
  IN in_min_serial_id INT8)
RETURNS SETOF exchange_do_select_aggregations_above_serial_return_type
LANGUAGE plpgsql
AS $$
DECLARE
  aggregation CURSOR
  FOR
  SELECT
    batch_deposit_serial_id
   ,aggregation_serial_id
    FROM aggregation_tracking
    WHERE aggregation_serial_id >= in_min_serial_id
    ORDER BY aggregation_serial_id ASC;
DECLARE
  my_total_val INT8; 
DECLARE
  my_total_frac INT8; 
DECLARE
  my_total taler_amount; 
DECLARE
  my_batch_record RECORD;
DECLARE
  i RECORD;
BEGIN
OPEN aggregation;
LOOP
  FETCH NEXT FROM aggregation INTO i;
  EXIT WHEN NOT FOUND;
  SELECT
    SUM((cdep.amount_with_fee).val) AS total_val
   ,SUM((cdep.amount_with_fee).frac::INT8) AS total_frac
    INTO
      my_batch_record
    FROM coin_deposits cdep
    WHERE cdep.batch_deposit_serial_id = i.batch_deposit_serial_id;
  my_total_val=my_batch_record.total_val;
  my_total_frac=my_batch_record.total_frac;
  my_total.val = my_total_val + my_total_frac / 100000000;
  my_total.frac = my_total_frac % 100000000;
  RETURN NEXT (
       i.batch_deposit_serial_id
      ,i.aggregation_serial_id
      ,my_total
      );
END LOOP;
CLOSE aggregation;
RETURN;
END $$;
DROP FUNCTION IF EXISTS exchange_do_persist_kyc_attributes;
CREATE FUNCTION exchange_do_persist_kyc_attributes(
  IN in_process_row INT8,
  IN in_h_payto BYTEA,
  IN in_birthday INT4,
  IN in_provider_name TEXT,
  IN in_provider_account_id TEXT, 
  IN in_provider_legitimization_id TEXT, 
  IN in_collection_time_ts INT8,
  IN in_expiration_time INT8, 
  IN in_expiration_time_ts INT8, 
  IN in_enc_attributes BYTEA,
  IN in_kyc_completed_notify_s TEXT,
  IN in_form_name TEXT, 
  OUT out_ok BOOLEAN) 
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO kyc_attributes
  (h_payto
  ,collection_time
  ,expiration_time
  ,form_name
  ,by_aml_officer
  ,encrypted_attributes
  ,legitimization_serial
 ) VALUES
  (in_h_payto
  ,in_collection_time_ts
  ,in_expiration_time_ts
  ,in_form_name
  ,FALSE
  ,in_enc_attributes
  ,in_process_row);
NOTIFY XSX9Z5XGWWYFKXTAYCES63B62527JKNX9XD0131Z08THVV8YW5BZG;
UPDATE legitimization_processes
  SET provider_user_id=in_provider_account_id
     ,provider_legitimization_id=in_provider_legitimization_id
     ,expiration_time=GREATEST(expiration_time,in_expiration_time)
     ,finished=TRUE
 WHERE h_payto=in_h_payto
   AND legitimization_process_serial_id=in_process_row
   AND provider_name=in_provider_name;
out_ok=FOUND;
UPDATE reserves
   SET birthday=in_birthday
 WHERE (reserve_pub IN
    (SELECT reserve_pub
       FROM reserves_in
      WHERE wire_source_h_payto IN
        (SELECT wire_source_h_payto
           FROM wire_targets
          WHERE h_normalized_payto=in_h_payto) ) )
  AND ( ((current_balance).frac > 0) OR
        ((current_balance).val > 0 ) )
  AND (expiration_date > in_collection_time_ts);
EXECUTE FORMAT (
 'NOTIFY %s'
 ,in_kyc_completed_notify_s);
INSERT INTO kyc_alerts
 (h_payto
 ,trigger_type)
 VALUES
 (in_h_payto,1)
 ON CONFLICT DO NOTHING;
END $$;
COMMENT ON FUNCTION exchange_do_persist_kyc_attributes(INT8, BYTEA, INT4, TEXT, TEXT, TEXT, INT8, INT8, INT8, BYTEA, TEXT, TEXT)
  IS 'Inserts new KYC attributes and updates the status of the legitimization process';
DROP FUNCTION IF EXISTS exchange_do_insert_aml_program_failure;
CREATE FUNCTION exchange_do_insert_aml_program_failure (
  IN in_legitimization_process_serial_id INT8,
  IN in_h_payto BYTEA,
  IN in_now INT8,
  IN in_error_code INT4,
  IN in_error_message TEXT,
  IN in_kyc_completed_notify_s TEXT,
  OUT out_update BOOLEAN) 
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE legitimization_processes
   SET finished=TRUE
      ,error_code=in_error_code
      ,error_message=in_error_message
 WHERE h_payto=in_h_payto
   AND legitimization_process_serial_id=in_legitimization_process_serial_id;
out_update = FOUND;
IF NOT FOUND
THEN
  INSERT INTO legitimization_processes
    (finished
    ,error_code
    ,error_message
    ,h_payto
    ,start_time
    ,provider_section
    ) VALUES (
     TRUE
    ,in_error_code
    ,in_error_message
    ,in_h_payto
    ,in_now
    ,'skip'
   );
END IF;
EXECUTE FORMAT (
 'NOTIFY %s'
 ,in_kyc_completed_notify_s);
INSERT INTO kyc_alerts
 (h_payto
 ,trigger_type)
 VALUES
 (in_h_payto,1)
 ON CONFLICT DO NOTHING;
END $$;
COMMENT ON FUNCTION exchange_do_insert_aml_program_failure(INT8, BYTEA, INT8, INT4, TEXT, TEXT)
  IS 'Stores information about an AML program run that failed into the legitimization_processes table. Either updates a row of an existing legitimization process, or creates a new entry.';
DROP FUNCTION IF EXISTS exchange_do_set_aml_lock;
CREATE FUNCTION exchange_do_set_aml_lock (
  IN in_h_payto BYTEA,
  IN in_now INT8,
  IN in_expiration INT8,
  OUT out_aml_program_lock_timeout INT8) 
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE kyc_targets
   SET aml_program_lock_timeout=in_expiration
 WHERE h_normalized_payto=in_h_payto
   AND ( (aml_program_lock_timeout IS NULL)
      OR (aml_program_lock_timeout < in_now) );
IF NOT FOUND
THEN
  SELECT aml_program_lock_timeout
    INTO out_aml_program_lock_timeout
    FROM kyc_targets
   WHERE h_normalized_payto=in_h_payto;
ELSE
  out_aml_program_lock_timeout = 0;
END IF;
END $$;
COMMENT ON FUNCTION exchange_do_set_aml_lock(BYTEA, INT8, INT8)
  IS 'Tries to lock an account for running an AML program. Returns the timeout of the existing lock, 0 if there is no existing lock, and NULL if we do not know the account.';
DROP FUNCTION IF EXISTS exchange_do_insert_sanction_list_hit;
CREATE FUNCTION exchange_do_insert_sanction_list_hit(
  IN in_h_normalized_payto BYTEA,
  IN in_decision_time INT8,
  IN in_expiration_time INT8,
  IN in_properties JSONB, 
  IN in_new_rules JSONB, 
  IN in_to_investigate BOOLEAN,
  IN in_notify_s TEXT,
  IN ina_events TEXT[],
  OUT out_outcome_serial_id INT8)
LANGUAGE plpgsql
AS $$
DECLARE
  my_i INT4;
  ini_event TEXT;
BEGIN
UPDATE legitimization_outcomes
   SET is_active=FALSE
 WHERE h_payto=in_h_normalized_payto;
INSERT INTO legitimization_outcomes
  (h_payto
  ,decision_time
  ,expiration_time
  ,jproperties
  ,to_investigate
  ,jnew_rules
  )
  VALUES
  (in_h_normalized_payto
  ,in_decision_time
  ,in_expiration_time
  ,in_properties
  ,in_to_investigate
  ,in_new_rules
  )
  RETURNING
    outcome_serial_id
  INTO
    out_outcome_serial_id;
FOR i IN 1..COALESCE(array_length(ina_events,1),0)
LOOP
  ini_event = ina_events[i];
  INSERT INTO kyc_events
    (event_timestamp
    ,event_type)
    VALUES
    (in_decision_time
    ,ini_event);
END LOOP;
EXECUTE FORMAT (
   'NOTIFY %s'
  ,in_notify_s);
END $$;
COMMENT ON FUNCTION exchange_do_insert_sanction_list_hit(BYTEA, INT8, INT8, JSONB, JSONB, BOOLEAN, TEXT, TEXT[])
  IS 'Insert result from sanction list check into the table';
SET search_path TO exchange;
DROP FUNCTION IF EXISTS interval_to_start;
CREATE OR REPLACE FUNCTION interval_to_start (
  IN in_timestamp TIMESTAMP,
  IN in_range statistic_range,
  OUT out_bucket_start INT8
)
LANGUAGE plpgsql
AS $$
BEGIN
  out_bucket_start = EXTRACT(EPOCH FROM DATE_TRUNC(in_range::text, in_timestamp));
END $$;
COMMENT ON FUNCTION interval_to_start
 IS 'computes the start time of the bucket for an event at the current time given the desired bucket range';
DROP PROCEDURE IF EXISTS exchange_do_bump_number_bucket_stat;
CREATE OR REPLACE PROCEDURE exchange_do_bump_number_bucket_stat(
  in_slug TEXT,
  in_h_payto BYTEA,
  in_timestamp TIMESTAMP,
  in_delta INT8
)
LANGUAGE plpgsql
AS $$
DECLARE
  my_meta INT8;
  my_range statistic_range;
  my_bucket_start INT8;
  my_curs CURSOR (arg_slug TEXT)
   FOR SELECT UNNEST(ranges)
         FROM exchange_statistic_bucket_meta
        WHERE slug=arg_slug;
BEGIN
  SELECT bmeta_serial_id
    INTO my_meta
    FROM exchange_statistic_bucket_meta
   WHERE slug=in_slug
     AND stype='number';
  IF NOT FOUND
  THEN
    RETURN;
  END IF;
  OPEN my_curs (arg_slug:=in_slug);
  LOOP
    FETCH NEXT
      FROM my_curs
      INTO my_range;
    EXIT WHEN NOT FOUND;
    SELECT *
      INTO my_bucket_start
      FROM interval_to_start (in_timestamp, my_range);
    UPDATE exchange_statistic_bucket_counter
       SET cumulative_number = cumulative_number + in_delta
     WHERE bmeta_serial_id=my_meta
       AND h_payto=in_h_payto
       AND bucket_start=my_bucket_start
       AND bucket_range=my_range;
    IF NOT FOUND
    THEN
      INSERT INTO exchange_statistic_bucket_counter
        (bmeta_serial_id
        ,h_payto
        ,bucket_start
        ,bucket_range
        ,cumulative_number
        ) VALUES (
         my_meta
        ,in_h_payto
        ,my_bucket_start
        ,my_range
        ,in_delta);
    END IF;
  END LOOP;
  CLOSE my_curs;
END $$;
DROP PROCEDURE IF EXISTS exchange_do_bump_amount_bucket_stat;
CREATE OR REPLACE PROCEDURE exchange_do_bump_amount_bucket_stat(
  in_slug TEXT,
  in_h_payto BYTEA,
  in_timestamp TIMESTAMP,
  in_delta taler_amount
)
LANGUAGE plpgsql
AS $$
DECLARE
  my_meta INT8;
  my_range statistic_range;
  my_bucket_start INT8;
  my_curs CURSOR (arg_slug TEXT)
   FOR SELECT UNNEST(ranges)
         FROM exchange_statistic_bucket_meta
        WHERE slug=arg_slug;
BEGIN
  SELECT bmeta_serial_id
    INTO my_meta
    FROM exchange_statistic_bucket_meta
   WHERE slug=in_slug
     AND stype='amount';
  IF NOT FOUND
  THEN
    RETURN;
  END IF;
  OPEN my_curs (arg_slug:=in_slug);
  LOOP
    FETCH NEXT
      FROM my_curs
      INTO my_range;
    EXIT WHEN NOT FOUND;
    SELECT *
      INTO my_bucket_start
      FROM interval_to_start (in_timestamp, my_range);
    UPDATE exchange_statistic_bucket_amount
      SET
        cumulative_value.val = (cumulative_value).val + (in_delta).val
        + CASE
            WHEN (in_delta).frac + (cumulative_value).frac >= 100000000
            THEN 1
            ELSE 0
          END,
        cumulative_value.frac = (cumulative_value).frac + (in_delta).frac
        - CASE
            WHEN (in_delta).frac + (cumulative_value).frac >= 100000000
            THEN 100000000
            ELSE 0
          END
     WHERE bmeta_serial_id=my_meta
       AND h_payto=in_h_payto
       AND bucket_start=my_bucket_start
       AND bucket_range=my_range;
    IF NOT FOUND
    THEN
      INSERT INTO exchange_statistic_bucket_amount
        (bmeta_serial_id
        ,h_payto
        ,bucket_start
        ,bucket_range
        ,cumulative_value
        ) VALUES (
         my_meta
        ,in_h_payto
        ,my_bucket_start
        ,my_range
        ,in_delta);
    END IF;
  END LOOP;
  CLOSE my_curs;
END $$;
COMMENT ON PROCEDURE exchange_do_bump_amount_bucket_stat
  IS 'Updates an amount statistic tracked over buckets';
DROP PROCEDURE IF EXISTS exchange_do_bump_number_interval_stat;
CREATE OR REPLACE PROCEDURE exchange_do_bump_number_interval_stat(
  in_slug TEXT,
  in_h_payto BYTEA,
  in_timestamp TIMESTAMP,
  in_delta INT8
)
LANGUAGE plpgsql
AS $$
DECLARE
  my_now INT8;
  my_record RECORD;
  my_meta INT8;
  my_ranges INT8[];
  my_precisions INT8[];
  my_rangex INT8;
  my_precisionx INT8;
  my_start INT8;
  my_event INT8;
BEGIN
  my_now = ROUND(EXTRACT(epoch FROM CURRENT_TIMESTAMP(0)::TIMESTAMP) * 1000000)::INT8 / 1000 / 1000;
  SELECT imeta_serial_id
        ,ranges AS ranges
        ,precisions AS precisions
    INTO my_record
    FROM exchange_statistic_interval_meta
   WHERE slug=in_slug
     AND stype='number';
  IF NOT FOUND
  THEN
    RETURN;
  END IF;
  my_start = ROUND(EXTRACT(epoch FROM in_timestamp) * 1000000)::INT8 / 1000 / 1000; 
  my_precisions = my_record.precisions;
  my_ranges = my_record.ranges;
  my_rangex = NULL;
  FOR my_x IN 1..COALESCE(array_length(my_ranges,1),0)
  LOOP
    IF my_now - my_ranges[my_x] < my_start
    THEN
      my_rangex = my_ranges[my_x];
      my_precisionx = my_precisions[my_x];
      EXIT;
    END IF;
  END LOOP;
  IF my_rangex IS NULL
  THEN
    RETURN;
  END IF;
  my_meta = my_record.imeta_serial_id;
  my_start = my_start - my_start % my_precisionx; 
  INSERT INTO exchange_statistic_counter_event AS msce
    (imeta_serial_id
    ,h_payto
    ,slot
    ,delta)
   VALUES
    (my_meta
    ,in_h_payto
    ,my_start
    ,in_delta)
   ON CONFLICT (imeta_serial_id, h_payto, slot)
   DO UPDATE SET
     delta = msce.delta + in_delta
   RETURNING nevent_serial_id
        INTO my_event;
  UPDATE exchange_statistic_interval_counter
     SET cumulative_number = cumulative_number + in_delta
   WHERE imeta_serial_id = my_meta
     AND h_payto = in_h_payto
     AND range=my_rangex;
  IF NOT FOUND
  THEN
    INSERT INTO exchange_statistic_interval_counter
      (imeta_serial_id
      ,h_payto
      ,range
      ,event_delimiter
      ,cumulative_number
     ) VALUES (
       my_meta
      ,in_h_payto
      ,my_rangex
      ,my_event
      ,in_delta);
  END IF;
END $$;
COMMENT ON PROCEDURE exchange_do_bump_number_interval_stat
  IS 'Updates a numeric statistic tracked over an interval';
DROP PROCEDURE IF EXISTS exchange_do_bump_amount_interval_stat;
CREATE OR REPLACE PROCEDURE exchange_do_bump_amount_interval_stat(
  in_slug TEXT,
  in_h_payto BYTEA,
  in_timestamp TIMESTAMP,
  in_delta taler_amount
)
LANGUAGE plpgsql
AS $$
DECLARE
  my_now INT8;
  my_record RECORD;
  my_meta INT8;
  my_ranges INT8[];
  my_precisions INT8[];
  my_x INT;
  my_rangex INT8;
  my_precisionx INT8;
  my_start INT8;
  my_event INT8;
BEGIN
  my_now = ROUND(EXTRACT(epoch FROM CURRENT_TIMESTAMP(0)::TIMESTAMP) * 1000000)::INT8 / 1000 / 1000;
  SELECT imeta_serial_id
        ,ranges
        ,precisions
    INTO my_record
    FROM exchange_statistic_interval_meta
   WHERE slug=in_slug
     AND stype='amount';
  IF NOT FOUND
  THEN
    RETURN;
  END IF;
  my_start = ROUND(EXTRACT(epoch FROM in_timestamp) * 1000000)::INT8 / 1000 / 1000; 
  my_precisions = my_record.precisions;
  my_ranges = my_record.ranges;
  my_rangex = NULL;
  FOR my_x IN 1..COALESCE(array_length(my_ranges,1),0)
  LOOP
    IF my_now - my_ranges[my_x] < my_start
    THEN
      my_rangex = my_ranges[my_x];
      my_precisionx = my_precisions[my_x];
      EXIT;
    END IF;
  END LOOP;
  IF my_rangex IS NULL
  THEN
    RETURN;
  END IF;
  my_start = my_start - my_start % my_precisionx; 
  my_meta = my_record.imeta_serial_id;
  INSERT INTO exchange_statistic_amount_event AS msae
    (imeta_serial_id
    ,h_payto
    ,slot
    ,delta
    ) VALUES (
     my_meta
    ,in_h_payto
    ,my_start
    ,in_delta
    )
    ON CONFLICT (imeta_serial_id, h_payto, slot)
    DO UPDATE SET
      delta.val = (msae.delta).val + (in_delta).val
        + CASE
          WHEN (in_delta).frac + (msae.delta).frac >= 100000000
          THEN 1
          ELSE 0
        END,
      delta.frac = (msae.delta).frac + (in_delta).frac
        - CASE
          WHEN (in_delta).frac + (msae.delta).frac >= 100000000
          THEN 100000000
          ELSE 0
        END
    RETURNING aevent_serial_id
         INTO my_event;
  UPDATE exchange_statistic_interval_amount
    SET
      cumulative_value.val = (cumulative_value).val + (in_delta).val
      + CASE
          WHEN (in_delta).frac + (cumulative_value).frac >= 100000000
          THEN 1
          ELSE 0
        END,
      cumulative_value.frac = (cumulative_value).frac + (in_delta).frac
      - CASE
          WHEN (in_delta).frac + (cumulative_value).frac >= 100000000
          THEN 100000000
          ELSE 0
        END
   WHERE imeta_serial_id=my_meta
     AND h_payto=in_h_payto
     AND range=my_rangex;
  IF NOT FOUND
  THEN
    INSERT INTO exchange_statistic_interval_amount
      (imeta_serial_id
      ,h_payto
      ,range
      ,event_delimiter
      ,cumulative_value
      ) VALUES (
       my_meta
      ,in_h_payto
      ,my_rangex
      ,my_event
      ,in_delta);
  END IF;
END $$;
COMMENT ON PROCEDURE exchange_do_bump_amount_interval_stat
  IS 'Updates an amount statistic tracked over an interval';
DROP PROCEDURE IF EXISTS exchange_do_bump_number_stat;
CREATE OR REPLACE PROCEDURE exchange_do_bump_number_stat(
  in_slug TEXT,
  in_h_payto BYTEA,
  in_timestamp TIMESTAMP,
  in_delta INT8
)
LANGUAGE plpgsql
AS $$
BEGIN
  CALL exchange_do_bump_number_bucket_stat (in_slug, in_h_payto, in_timestamp, in_delta);
  CALL exchange_do_bump_number_interval_stat (in_slug, in_h_payto, in_timestamp, in_delta);
END $$;
COMMENT ON PROCEDURE exchange_do_bump_number_stat
  IS 'Updates a numeric statistic (bucket or interval)';
DROP PROCEDURE IF EXISTS exchange_do_bump_amount_stat;
CREATE OR REPLACE PROCEDURE exchange_do_bump_amount_stat(
  in_slug TEXT,
  in_h_payto BYTEA,
  in_timestamp TIMESTAMP,
  in_delta taler_amount
)
LANGUAGE plpgsql
AS $$
BEGIN
  CALL exchange_do_bump_amount_bucket_stat (in_slug, in_h_payto, in_timestamp, in_delta);
  CALL exchange_do_bump_amount_interval_stat (in_slug, in_h_payto, in_timestamp, in_delta);
END $$;
COMMENT ON PROCEDURE exchange_do_bump_amount_stat
  IS 'Updates an amount statistic (bucket or interval)';
DROP FUNCTION IF EXISTS exchange_statistic_interval_number_get;
CREATE OR REPLACE FUNCTION exchange_statistic_interval_number_get (
  IN in_slug TEXT,
  IN in_h_payto BYTEA
)
RETURNS SETOF exchange_statistic_interval_number_get_return_value
LANGUAGE plpgsql
AS $$
DECLARE
  my_time INT8 DEFAULT ROUND(EXTRACT(epoch FROM CURRENT_TIMESTAMP(0)::TIMESTAMP) * 1000000)::INT8 / 1000 / 1000;
  my_ranges INT8[];
  my_range INT8;
  my_delta INT8;
  my_meta INT8;
  my_next_max_serial INT8;
  my_rec RECORD;
  my_irec RECORD;
  my_i INT;
  my_min_serial INT8 DEFAULT NULL;
  my_rval exchange_statistic_interval_number_get_return_value;
BEGIN
  SELECT imeta_serial_id
        ,ranges
        ,precisions
    INTO my_rec
    FROM exchange_statistic_interval_meta
   WHERE slug=in_slug;
  IF NOT FOUND
  THEN
    RETURN;
  END IF;
  my_rval.rvalue = 0;
  my_ranges = my_rec.ranges;
  my_meta = my_rec.imeta_serial_id;
  FOR my_i IN 1..COALESCE(array_length(my_ranges,1),0)
  LOOP
    my_range = my_ranges[my_i];
    SELECT event_delimiter
          ,cumulative_number
      INTO my_irec
      FROM exchange_statistic_interval_counter
     WHERE imeta_serial_id = my_meta
       AND range = my_range
       AND h_payto = in_h_payto;
    IF FOUND
    THEN
      my_min_serial = my_irec.event_delimiter;
      my_rval.rvalue = my_rval.rvalue + my_irec.cumulative_number;
      SELECT SUM(delta) AS delta_sum
        INTO my_irec
        FROM exchange_statistic_counter_event
       WHERE imeta_serial_id = my_meta
         AND h_payto = in_h_payto
         AND slot < my_time - my_range
         AND nevent_serial_id >= my_min_serial;
      IF FOUND AND my_irec.delta_sum IS NOT NULL
      THEN
        my_delta = my_irec.delta_sum;
        my_rval.rvalue = my_rval.rvalue - my_delta;
        SELECT nevent_serial_id
          INTO my_next_max_serial
          FROM exchange_statistic_counter_event
         WHERE imeta_serial_id = my_meta
           AND h_payto = in_h_payto
           AND slot >= my_time - my_range
           AND nevent_serial_id >= my_min_serial
         ORDER BY slot ASC
         LIMIT 1;
        IF FOUND
        THEN
          UPDATE exchange_statistic_interval_counter
             SET cumulative_number = cumulative_number - my_delta,
                 event_delimiter = my_next_max_serial
           WHERE imeta_serial_id = my_meta
             AND h_payto = in_h_payto
             AND range = my_range;
        ELSE
          DELETE FROM exchange_statistic_interval_counter
           WHERE imeta_serial_id = my_meta
             AND h_payto = in_h_payto
             AND range = my_range;
        END IF;
        IF (my_i < array_length(my_ranges,1))
        THEN
          UPDATE exchange_statistic_interval_counter AS usic SET
            cumulative_number = cumulative_number + my_delta,
            event_delimiter = LEAST(usic.event_delimiter,my_min_serial)
           WHERE imeta_serial_id = my_meta
             AND h_payto = in_h_payto
             AND range=my_ranges[my_i+1];
          IF NOT FOUND
          THEN
            INSERT INTO exchange_statistic_interval_counter
              (imeta_serial_id
              ,h_payto
              ,range
              ,event_delimiter
              ,cumulative_number
              ) VALUES (
               my_meta
              ,in_h_payto
              ,my_ranges[my_i+1]
              ,my_min_serial
              ,my_delta);
          END IF;
        ELSE
          DELETE FROM exchange_statistic_counter_event
                WHERE imeta_serial_id = my_meta
                  AND h_payto = in_h_payto
                  AND slot < my_time - my_range;
        END IF;
      END IF;
      my_rval.range = my_range;
      RETURN NEXT my_rval;
    END IF;
  END LOOP;
END $$;
COMMENT ON FUNCTION exchange_statistic_interval_number_get
  IS 'Returns deposit statistic tracking deposited amounts over certain time intervals; we first trim the stored data to only track what is still in-range, and then return the remaining value for each range';
DROP FUNCTION IF EXISTS exchange_statistic_interval_amount_get;
CREATE OR REPLACE FUNCTION exchange_statistic_interval_amount_get (
  IN in_slug TEXT,
  IN in_h_payto BYTEA
)
RETURNS SETOF exchange_statistic_interval_amount_get_return_value
LANGUAGE plpgsql
AS $$
DECLARE
  my_time INT8 DEFAULT ROUND(EXTRACT(epoch FROM CURRENT_TIMESTAMP(0)::TIMESTAMP) * 1000000)::INT8 / 1000 / 1000;
  my_ranges INT8[];
  my_range INT8;
  my_delta_value INT8;
  my_delta_frac INT8;
  my_delta taler_amount;
  my_meta INT8;
  my_next_max_serial INT8;
  my_rec RECORD;
  my_irec RECORD;
  my_jrec RECORD;
  my_i INT;
  my_min_serial INT8 DEFAULT NULL;
  my_rval exchange_statistic_interval_amount_get_return_value;
BEGIN
  SELECT imeta_serial_id
        ,ranges
        ,precisions
    INTO my_rec
    FROM exchange_statistic_interval_meta
   WHERE slug=in_slug;
  IF NOT FOUND
  THEN
    RETURN;
  END IF;
  my_meta = my_rec.imeta_serial_id;
  my_ranges = my_rec.ranges;
  my_rval.rvalue.val = 0;
  my_rval.rvalue.frac = 0;
  FOR my_i IN 1..COALESCE(array_length(my_ranges,1),0)
  LOOP
    my_range = my_ranges[my_i];
    SELECT event_delimiter
          ,cumulative_value
      INTO my_irec
      FROM exchange_statistic_interval_amount
     WHERE imeta_serial_id = my_meta
       AND h_payto = in_h_payto
       AND range = my_range;
    IF FOUND
    THEN
      my_min_serial = my_irec.event_delimiter;
      my_rval.rvalue.val = (my_rval.rvalue).val + (my_irec.cumulative_value).val + (my_irec.cumulative_value).frac / 100000000;
      my_rval.rvalue.frac = (my_rval.rvalue).frac + (my_irec.cumulative_value).frac % 100000000;
      IF (my_rval.rvalue).frac > 100000000
      THEN
        my_rval.rvalue.frac = (my_rval.rvalue).frac - 100000000;
        my_rval.rvalue.val = (my_rval.rvalue).val + 1;
      END IF;
      SELECT SUM((esae.delta).val) AS value_sum
            ,SUM((esae.delta).frac) AS frac_sum
        INTO my_jrec
        FROM exchange_statistic_amount_event esae
       WHERE imeta_serial_id = my_meta
         AND h_payto = in_h_payto
         AND slot < my_time - my_range
         AND aevent_serial_id >= my_min_serial;
      IF FOUND AND my_jrec.value_sum IS NOT NULL
      THEN
        my_delta_value = my_jrec.value_sum + my_jrec.frac_sum / 100000000;
        my_delta_frac = my_jrec.frac_sum % 100000000;
        my_rval.rvalue.val = (my_rval.rvalue).val - my_delta_value;
        IF ((my_rval.rvalue).frac >= my_delta_frac)
        THEN
          my_rval.rvalue.frac = (my_rval.rvalue).frac - my_delta_frac;
        ELSE
          my_rval.rvalue.frac = 100000000 + (my_rval.rvalue).frac - my_delta_frac;
          my_rval.rvalue.val = (my_rval.rvalue).val - 1;
        END IF;
        SELECT aevent_serial_id
          INTO my_next_max_serial
          FROM exchange_statistic_amount_event
         WHERE imeta_serial_id = my_meta
           AND h_payto = in_h_payto
           AND slot >= my_time - my_range
           AND aevent_serial_id >= my_min_serial
         ORDER BY slot ASC
         LIMIT 1;
        IF FOUND
        THEN
          UPDATE exchange_statistic_interval_amount SET
             cumulative_value.val = (cumulative_value).val - my_delta_value
              - CASE
                  WHEN (cumulative_value).frac < my_delta_frac
                  THEN 1
                  ELSE 0
                END,
             cumulative_value.frac = (cumulative_value).frac - my_delta_frac
             + CASE
                 WHEN (cumulative_value).frac < my_delta_frac
                 THEN 100000000
                 ELSE 0
               END,
             event_delimiter = my_next_max_serial
           WHERE imeta_serial_id = my_meta
             AND h_payto = in_h_payto
             AND range = my_range;
        ELSE
          DELETE FROM exchange_statistic_interval_amount
           WHERE imeta_serial_id = my_meta
             AND h_payto = in_h_payto
             AND range = my_range;
        END IF;
        IF (my_i < array_length(my_ranges,1))
        THEN
          UPDATE exchange_statistic_interval_amount AS msia SET
            cumulative_value.val = (cumulative_value).val + my_delta_value
              + CASE
                 WHEN (cumulative_value).frac + my_delta_frac > 100000000
                 THEN 1
                 ELSE 0
               END,
            cumulative_value.frac = (cumulative_value).frac + my_delta_value
              - CASE
                 WHEN (cumulative_value).frac + my_delta_frac > 100000000
                 THEN 100000000
                 ELSE 0
               END,
            event_delimiter = LEAST (msia.event_delimiter,my_min_serial)
           WHERE imeta_serial_id = my_meta
             AND h_payto = in_h_payto
             AND range=my_ranges[my_i+1];
          IF NOT FOUND
          THEN
            my_delta.val = my_delta_value;
            my_delta.frac = my_delta_frac;
            INSERT INTO exchange_statistic_interval_amount
              (imeta_serial_id
              ,h_payto
              ,event_delimiter
              ,range
              ,cumulative_value
              ) VALUES (
               my_meta
              ,in_h_payto
              ,my_min_serial
              ,my_ranges[my_i+1]
              ,my_delta);
          END IF;
        ELSE
          DELETE FROM exchange_statistic_amount_event
                WHERE imeta_serial_id = my_meta
                  AND h_payto = in_h_payto
                  AND slot < my_time - my_range;
        END IF;
      END IF;
      my_rval.range = my_range;
      RETURN NEXT my_rval;
    END IF;
  END LOOP; 
END $$;
COMMENT ON FUNCTION exchange_statistic_interval_amount_get
  IS 'Returns deposit statistic tracking deposited amounts over certain time intervals; we first trim the stored data to only track what is still in-range, and then return the remaining value; multiple values are returned, one per range';
DROP PROCEDURE IF EXISTS exchange_statistic_counter_gc;
CREATE OR REPLACE PROCEDURE exchange_statistic_counter_gc ()
LANGUAGE plpgsql
AS $$
DECLARE
  my_time INT8 DEFAULT ROUND(EXTRACT(epoch FROM CURRENT_TIMESTAMP(0)::TIMESTAMP) * 1000000)::INT8 / 1000 / 1000;
  my_h_payto BYTEA;
  my_rec RECORD;
  my_sum RECORD;
  my_meta INT8;
  my_ranges INT8[];
  my_precisions INT8[];
  my_precision INT4;
  my_i INT4;
  min_slot INT8;
  max_slot INT8;
  end_slot INT8;
  my_total INT8;
BEGIN
  FOR my_h_payto IN
    SELECT DISTINCT h_payto
      FROM exchange_statistic_counter_event
  LOOP
  FOR my_rec IN
    SELECT imeta_serial_id
          ,ranges
          ,precisions
          ,slug
      FROM exchange_statistic_interval_meta
  LOOP
    PERFORM FROM exchange_statistic_interval_number_get (my_rec.slug, my_h_payto);
    my_meta = my_rec.imeta_serial_id;
    my_ranges = my_rec.ranges;
    my_precisions = my_rec.precisions;
    FOR my_i IN 1..COALESCE(array_length(my_ranges,1),0)
    LOOP
      my_precision = my_precisions[my_i];
      IF 1 >= my_precision
      THEN
        CONTINUE;
      END IF;
      IF 1 = my_i
      THEN
        min_slot = 0;
      ELSE
        min_slot = my_ranges[my_i - 1];
      END IF;
      end_slot = my_ranges[my_i];
      LOOP
        EXIT WHEN min_slot >= end_slot;
        max_slot = min_slot + my_precision;
        SELECT SUM(delta) AS total,
               COUNT(*) AS matches,
               MIN(nevent_serial_id) AS rep_serial_id
          INTO my_sum
          FROM exchange_statistic_counter_event
         WHERE h_payto=my_h_payto
           AND imeta_serial_id=my_meta
           AND slot >= my_time - max_slot
           AND slot < my_time - min_slot;
        IF FOUND AND my_sum.matches > 1
        THEN
          my_total = my_sum.total;
          DELETE FROM exchange_statistic_counter_event
           WHERE h_payto=my_h_payto
             AND imeta_serial_id=my_meta
             AND slot >= my_time - max_slot
             AND slot < my_time - min_slot
             AND nevent_serial_id > my_sum.rep_serial_id;
          UPDATE exchange_statistic_counter_event SET
            delta = my_total
           WHERE imeta_serial_id = my_meta
             AND h_payto = my_h_payto
             AND nevent_serial_id = my_sum.rep_serial_id;
        END IF;
        min_slot = min_slot + my_precision;
      END LOOP; 
    END LOOP; 
    DELETE FROM exchange_statistic_counter_event
     WHERE h_payto=my_h_payto
       AND imeta_serial_id=my_meta
       AND slot < my_time - my_ranges[array_length(my_ranges,1)];
  END LOOP; 
  END LOOP; 
END $$;
COMMENT ON PROCEDURE exchange_statistic_counter_gc
  IS 'Performs garbage collection and compaction of the exchange_statistic_counter_event table';
DROP PROCEDURE IF EXISTS exchange_statistic_amount_gc;
CREATE OR REPLACE PROCEDURE exchange_statistic_amount_gc ()
LANGUAGE plpgsql
AS $$
DECLARE
  my_time INT8 DEFAULT ROUND(EXTRACT(epoch FROM CURRENT_TIMESTAMP(0)::TIMESTAMP) * 1000000)::INT8 / 1000 / 1000;
  my_h_payto BYTEA;
  my_rec RECORD;
  my_sum RECORD;
  my_meta INT8;
  my_ranges INT8[];
  my_precisions INT8[];
  my_precision INT4;
  my_i INT4;
  min_slot INT8;
  max_slot INT8;
  end_slot INT8;
  my_total_val INT8;
  my_total_frac INT8;
BEGIN
  FOR my_h_payto IN
    SELECT DISTINCT h_payto
      FROM exchange_statistic_counter_event
  LOOP
  FOR my_rec IN
    SELECT imeta_serial_id
          ,ranges
          ,precisions
          ,slug
      FROM exchange_statistic_interval_meta
  LOOP
    PERFORM FROM exchange_statistic_interval_amount_get (my_rec.slug, my_h_payto);
    my_meta = my_rec.imeta_serial_id;
    my_ranges = my_rec.ranges;
    my_precisions = my_rec.precisions;
    FOR my_i IN 1..COALESCE(array_length(my_ranges,1),0)
    LOOP
      my_precision = my_precisions[my_i];
      IF 1 >= my_precision
      THEN
        CONTINUE;
      END IF;
      IF 1 = my_i
      THEN
        min_slot = 0;
      ELSE
        min_slot = my_ranges[my_i - 1];
      END IF;
      end_slot = my_ranges[my_i];
      LOOP
        EXIT WHEN min_slot >= end_slot;
        max_slot = min_slot + my_precision;
        SELECT SUM((delta).val) AS total_val,
               SUM((delta).frac) AS total_frac,
               COUNT(*) AS matches,
               MIN(aevent_serial_id) AS rep_serial_id
          INTO my_sum
          FROM exchange_statistic_amount_event
         WHERE imeta_serial_id=my_meta
           AND h_payto=my_h_payto
           AND slot >= my_time - max_slot
           AND slot < my_time - max_slot;
        IF FOUND AND my_sum.matches > 1
        THEN
          my_total_frac = my_sum.total_frac % 100000000;
          my_total_val = my_sum.total_val + my_sum.total_frac / 100000000;
          DELETE FROM exchange_statistic_amount_event
           WHERE imeta_serial_id=my_meta
             AND h_payto=my_h_payto
             AND slot >= my_time - max_slot
             AND slot < my_time - max_slot
             AND aevent_serial_id > my_sum.rep_serial_id;
          UPDATE exchange_statistic_amount_event SET
             delta.val = my_total_value
            ,delta.frac = my_total_frac
           WHERE imeta_serial_id = my_meta
             AND h_payto = my_h_payto
             AND aevent_serial_id = my_sum.rep_serial_id;
        END IF;
        min_slot = min_slot + my_precision;
      END LOOP; 
    END LOOP; 
    DELETE FROM exchange_statistic_amount_event
     WHERE h_payto=my_h_payto
       AND imeta_serial_id=my_meta
       AND slot < my_time - my_ranges[array_length(my_ranges,1)];
    END LOOP; 
  END LOOP; 
END $$;
COMMENT ON PROCEDURE exchange_statistic_amount_gc
  IS 'Performs garbage collection and compaction of the exchange_statistic_amount_event table';
DROP PROCEDURE IF EXISTS exchange_statistic_bucket_gc;
CREATE OR REPLACE PROCEDURE exchange_statistic_bucket_gc ()
LANGUAGE plpgsql
AS $$
DECLARE
  my_rec RECORD;
  my_range TEXT;
  my_now INT8;
  my_end INT8;
BEGIN
  my_now = EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0)::TIMESTAMP); 
  FOR my_rec IN
    SELECT bmeta_serial_id
          ,stype
          ,ranges[array_length(ranges,1)] AS range
          ,ages[array_length(ages,1)] AS age
      FROM exchange_statistic_bucket_meta
  LOOP
    my_range = '1 ' || my_rec.range::TEXT;
    my_end = my_now - my_rec.age * EXTRACT(SECONDS FROM (SELECT my_range::INTERVAL)); 
    IF my_rec.stype = 'amount'
    THEN
      DELETE
        FROM exchange_statistic_bucket_amount
       WHERE bmeta_serial_id = my_rec.bmeta_serial_id
         AND bucket_start >= my_end;
    ELSE
      DELETE
        FROM exchange_statistic_bucket_counter
       WHERE bmeta_serial_id = my_rec.bmeta_serial_id
         AND bucket_start >= my_end;
    END IF;
  END LOOP;
END $$;
COMMENT ON PROCEDURE exchange_statistic_bucket_gc
  IS 'Performs garbage collection of the exchange_statistic_bucket_counter and exchange_statistic_bucket_amount tables';
DROP FUNCTION IF EXISTS exchange_drop_customization;
CREATE OR REPLACE FUNCTION exchange_drop_customization (
  IN in_schema TEXT,
  OUT out_found BOOLEAN
)
LANGUAGE plpgsql
AS $$
DECLARE
  my_xpatches TEXT;
BEGIN
  out_found = FALSE;
  FOR my_xpatches IN
    SELECT patch_name
      FROM _v.patches
     WHERE starts_with(patch_name, in_schema || '-')
  LOOP
    PERFORM _v.unregister_patch(my_xpatches);
    out_found = TRUE;
  END LOOP;
  IF out_found
  THEN
    EXECUTE FORMAT('DROP SCHEMA %s CASCADE'
      ,in_schema);
  END IF;
  DELETE
     FROM exchange_statistic_interval_meta
    WHERE origin=in_schema;
  DELETE
     FROM exchange_statistic_bucket_meta
    WHERE origin=in_schema;
END $$;
COMMENT ON FUNCTION exchange_drop_customization
  IS 'Removes all entries related to a particular exchange customization schema';
CREATE OR REPLACE FUNCTION purse_requests_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO
    exchange.purse_actions
    (purse_pub
    ,action_date)
  VALUES
    (NEW.purse_pub
    ,NEW.purse_expiration);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION purse_requests_insert_trigger()
  IS 'When a purse is created, insert it into the purse_action table to take action when the purse expires.';
CREATE OR REPLACE FUNCTION withdraw_delete_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  DELETE FROM exchange.unique_withdraw_blinding_seed
  WHERE blinding_seed = OLD.blinding_seed;
  RETURN OLD;
END $$;
COMMENT ON FUNCTION withdraw_delete_trigger()
  IS 'Delete blinding_seed from unique_withdraw_blinding_seed table.';
CREATE OR REPLACE FUNCTION withdraw_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO exchange.reserve_history
    (reserve_pub
    ,table_name
    ,serial_id
    ) VALUES (
     NEW.reserve_pub
    ,'withdraw'
    ,NEW.withdraw_id
    );
  RETURN NEW;
END $$;
COMMENT ON FUNCTION withdraw_insert_trigger()
  IS 'Replicate withdraw inserts into reserve_history table.';
CREATE OR REPLACE FUNCTION reserves_in_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  INSERT INTO exchange.reserve_history
    (reserve_pub
    ,table_name
    ,serial_id)
  VALUES
    (NEW.reserve_pub
    ,'reserves_in'
    ,NEW.reserve_in_serial_id);
  RETURN NEW;
END $$;
COMMENT ON FUNCTION reserves_in_insert_trigger()
  IS 'Automatically generate reserve history entry.';
CREATE OR REPLACE FUNCTION purse_decision_insert_trigger()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  UPDATE exchange.purse_requests
     SET was_decided=TRUE
   WHERE purse_pub=NEW.purse_pub;
  IF NEW.refunded
  THEN
    INSERT INTO exchange.coin_history
      (coin_pub
      ,table_name
      ,serial_id)
    SELECT
      pd.coin_pub
     ,'purse_decision'
     ,NEW.purse_decision_serial_id
    FROM exchange.purse_deposits pd
    WHERE purse_pub = NEW.purse_pub;
  ELSE
    INSERT INTO exchange.reserve_history
      (reserve_pub
      ,table_name
      ,serial_id)
    SELECT
      reserve_pub
     ,'purse_decision'
     ,NEW.purse_decision_serial_id
    FROM exchange.purse_merges
    WHERE purse_pub=NEW.purse_pub;
  END IF;
  RETURN NEW;
END $$;
COMMENT ON FUNCTION purse_decision_insert_trigger()
  IS 'Automatically generate coin history entry and update decision status for the purse.';
DROP FUNCTION IF EXISTS exchange_do_get_kyc_rules;
CREATE FUNCTION exchange_do_get_kyc_rules(
  IN in_h_payto BYTEA,
  IN in_now INT8,
  IN in_merchant_pub BYTEA, 
  OUT out_target_pub BYTEA, 
  OUT out_reserve_pub BYTEA, 
  OUT out_jnew_rules JSONB 
 )
LANGUAGE plpgsql
AS $$
DECLARE
  my_found BOOL;
BEGIN
  IF in_merchant_pub IS NOT NULL
  THEN
    PERFORM FROM reserves_in
      WHERE reserve_pub=in_merchant_pub
        AND wire_source_h_payto IN
         (SELECT wire_target_h_payto
            FROM wire_targets
           WHERE h_normalized_payto = in_h_payto);
    my_found = FOUND;
  ELSE
    my_found = FALSE;
  END IF;
  IF FOUND
  THEN
    out_reserve_pub = in_merchant_pub;
  ELSE
    SELECT reserve_pub
      INTO out_reserve_pub
      FROM reserves_in
     WHERE wire_source_h_payto IN
      (SELECT wire_target_h_payto
         FROM wire_targets
        WHERE h_normalized_payto = in_h_payto)
     ORDER BY execution_date DESC
     LIMIT 1;
  END IF;
  SELECT target_pub
    INTO out_target_pub
    FROM kyc_targets
   WHERE h_normalized_payto = in_h_payto;
  SELECT jnew_rules
    INTO out_jnew_rules
    FROM legitimization_outcomes
   WHERE h_payto = in_h_payto
     AND COALESCE(expiration_time >= $2, TRUE)
     AND COALESCE(is_active, TRUE)
   ORDER BY expiration_time DESC
   LIMIT 1;
END $$;
DROP PROCEDURE IF EXISTS exchange_do_gc;
CREATE PROCEDURE exchange_do_gc(
  IN in_ancient_date INT8,
  IN in_now INT8)
LANGUAGE plpgsql
AS $$
BEGIN
  CALL exchange_do_main_gc(in_ancient_date,in_now);
  CALL exchange_statistic_amount_gc ();
  CALL exchange_statistic_bucket_gc ();
  CALL exchange_statistic_counter_gc ();
END $$;
COMMENT ON PROCEDURE exchange_do_gc
  IS 'calls all other garbage collection subroutines';
COMMIT;
