prepare tables for DD31

This commit is contained in:
Christian Grothoff 2022-09-18 18:04:41 +02:00
parent 18a2fae3b5
commit da69fd9c72
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
3 changed files with 234 additions and 0 deletions

View File

@ -382,6 +382,177 @@ BEGIN
END
$$;
--------------------------- reserves_open_requests -------------------------------
CREATE OR REPLACE FUNCTION create_table_reserves_open_requests(
IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR default 'reserves_open_requests';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I'
'(open_request_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE / PRIMARY KEY'
',reserve_pub BYTEA NOT NULL' -- REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
',request_timestamp INT8 NOT NULL'
',expiration_date INT8 NOT NULL'
',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
',reserve_payment_val INT8 NOT NULL'
',reserve_payment_frac INT4 NOT NULL'
',requested_purse_limit INT4 NOT NULL'
') %s ;'
,table_name
,'PARTITION BY HASH (reserve_pub)'
,shard_suffix
);
table_name = concat_ws('_', table_name, shard_suffix);
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_reserve_open_uuid_index '
'ON ' || table_name || ' '
'(open_request_uuid);'
);
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_reserve_pub_index '
'ON ' || table_name || ' '
'(reserve_pub);'
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_reserves_open_request_partition(
IN partition_suffix VARCHAR
)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
EXECUTE FORMAT (
'ALTER TABLE reserves_open_requests_' || partition_suffix || ' '
'ADD CONSTRAINT reserves_open_requests_' || partition_suffix || '_by_uuid '
'PRIMARY KEY (open_request_uuid),'
'ADD CONSTRAINT reserves_open_requests_' || partition_suffix || '_by_time '
'UNIQUE (reserve_pub,request_timestamp)'
);
END
$$;
--------------------------- reserves_open_deposits -------------------------------
CREATE OR REPLACE FUNCTION create_table_reserves_open_deposits(
IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR default 'reserves_open_deposits';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I'
'(reserve_open_deposit_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE / PRIMARY KEY'
',reserve_pub BYTEA NOT NULL' -- REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
',request_timestamp INT8 NOT NULL'
',coin_pub BYTEA NOT NULL CHECK (LENGTH(coin_pub)=32)'
',coin_sig BYTEA NOT NULL CHECK (LENGTH(coin_sig)=64)'
',contribution_val INT8 NOT NULL'
',contribution_frac INT4 NOT NULL'
') %s ;'
,table_name
,'PARTITION BY HASH (coin_pub)'
,shard_suffix
);
table_name = concat_ws('_', table_name, shard_suffix);
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_uuid '
'ON ' || table_name || ' '
'(reserve_open_deposit_uuid);'
);
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_reserve '
'ON ' || table_name || ' '
'(reserve_pub,request_timestamp);'
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_reserves_open_deposits_partition(
IN partition_suffix VARCHAR
)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
EXECUTE FORMAT (
'ALTER TABLE reserves_open_deposits_' || partition_suffix || ' '
'ADD CONSTRAINT reserves_open_deposits_' || partition_suffix || '_coin_unique '
'PRIMARY KEY (coin_pub,reserve_pub)'
);
END
$$;
--------------------------- reserves_close_requests -------------------------------
CREATE OR REPLACE FUNCTION create_table_reserves_close_requests(
IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
table_name VARCHAR default 'reserves_close_requests';
BEGIN
PERFORM create_partitioned_table(
'CREATE TABLE IF NOT EXISTS %I'
'(close_request_uuid BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE / PRIMARY KEY'
',reserve_pub BYTEA NOT NULL' -- REFERENCES reserves (reserve_pub) ON DELETE CASCADE'
',execution_date INT8 NOT NULL'
',reserve_sig BYTEA NOT NULL CHECK (LENGTH(reserve_sig)=64)'
',wire_target_h_payto BYTEA CHECK (LENGTH(wire_target_h_payto)=32)'
') %s ;'
,table_name
,'PARTITION BY HASH (reserve_pub)'
,shard_suffix
);
table_name = concat_ws('_', table_name, shard_suffix);
EXECUTE FORMAT (
'CREATE INDEX IF NOT EXISTS ' || table_name || '_by_close_request_uuid_index '
'ON ' || table_name || ' '
'(close_request_uuid);'
);
END
$$;
CREATE OR REPLACE FUNCTION add_constraints_to_reserves_close_requests_partition(
IN partition_suffix VARCHAR
)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
EXECUTE FORMAT (
'ALTER TABLE reserves_close_requests_' || partition_suffix || ' '
'ADD CONSTRAINT reserves_close_' || partition_suffix || '_close_request_uuid_pkey '
'PRIMARY KEY (close_request_uuid)'
);
END
$$;
---------------------------- reserves_out -------------------------------
CREATE OR REPLACE FUNCTION create_table_reserves_out(

View File

@ -233,6 +233,58 @@ CREATE TABLE IF NOT EXISTS reserves_close_default
SELECT add_constraints_to_reserves_close_partition('default');
-- ------------------------------ reserves_open_requests ----------------------------------------
SELECT create_table_reserves_open_requests();
COMMENT ON TABLE reserves_open_requests
IS 'requests to keep a reserve open';
COMMENT ON COLUMN reserves_open_requests.reserve_payment_val
IS 'Funding to pay for the request from the reserve balance itself.';
CREATE TABLE IF NOT EXISTS reserves_open_requests_default
PARTITION OF reserves_open_requests
FOR VALUES WITH (MODULUS 1, REMAINDER 0);
SELECT add_constraints_to_reserves_open_request_partition('default');
-- ------------------------------ reserves_open_deposits ----------------------------------------
SELECT create_table_reserves_open_deposits();
COMMENT ON TABLE reserves_open_deposits
IS 'coin contributions paying for a reserve to remain open';
COMMENT ON COLUMN reserves_open_deposits.request_timestamp
IS 'Identifies the specific reserve open request being paid for.';
CREATE TABLE IF NOT EXISTS reserves_open_deposits_default
PARTITION OF reserves_open_deposits
FOR VALUES WITH (MODULUS 1, REMAINDER 0);
SELECT add_constraints_to_reserves_open_deposits_partition('default');
-- ------------------------------ reserves_close_requests ----------------------------------------
SELECT create_table_reserves_close_requests();
COMMENT ON TABLE reserves_close_requests
IS 'explicit requests by clients to affect an immediate closure of a reserve';
COMMENT ON COLUMN reserves_close_requests.wire_target_h_payto
IS 'Identifies the credited bank account. Optional.';
CREATE TABLE IF NOT EXISTS reserves_close_requests_default
PARTITION OF reserves_close_requests
FOR VALUES WITH (MODULUS 1, REMAINDER 0);
SELECT add_constraints_to_reserves_close_requests_partition('default');
-- ------------------------------ reserves_out ----------------------------------------
SELECT create_table_reserves_out();

View File

@ -42,8 +42,19 @@ BEGIN
PERFORM add_constraints_to_reserves_in_partition(shard_suffix);
PERFORM create_table_reserves_close(shard_suffix);
PERFORM add_constraints_to_reserves_close_partition(shard_suffix);
PERFORM create_table_reserves_open_requests(shard_suffix);
PERFORM add_constraints_to_reserves_open_request_partition(shard_suffix);
PERFORM create_table_reserves_open_deposits(shard_suffix);
PERFORM add_constraints_to_reserves_open_deposits_partition(shard_suffix);
PERFORM create_table_reserves_close_requests(shard_suffix);
PERFORM add_constraints_to_reserves_close_requests_partition(shard_suffix);
PERFORM create_table_reserves_out(shard_suffix);
PERFORM add_constraints_to_reserves_out_partition(shard_suffix);
PERFORM create_table_reserves_out_by_reserve(shard_suffix);