simplify batch insert: no cursor where not required, replace out_reserve_found with ruuid being allowed to remain NULL

This commit is contained in:
Christian Grothoff 2023-04-22 15:02:47 +02:00
parent c3fc8c5e55
commit acbee86745
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
2 changed files with 25 additions and 125 deletions

View File

@ -26,21 +26,11 @@ CREATE OR REPLACE FUNCTION exchange_do_batch_reserves_in_insert(
IN in_wire_source_h_payto BYTEA, IN in_wire_source_h_payto BYTEA,
IN in_payto_uri VARCHAR, IN in_payto_uri VARCHAR,
IN in_notify TEXT, IN in_notify TEXT,
OUT out_reserve_found0 BOOLEAN,
OUT transaction_duplicate0 BOOLEAN, OUT transaction_duplicate0 BOOLEAN,
OUT ruuid0 INT8) OUT ruuid0 INT8)
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
DECLARE
curs REFCURSOR;
DECLARE
i RECORD;
DECLARE
curs_trans REFCURSOR;
BEGIN BEGIN
ruuid0 = 0;
out_reserve_found0 = TRUE;
transaction_duplicate0 = TRUE;
INSERT INTO wire_targets INSERT INTO wire_targets
(wire_target_h_payto (wire_target_h_payto
@ -50,8 +40,6 @@ BEGIN
,in_payto_uri) ,in_payto_uri)
ON CONFLICT DO NOTHING; ON CONFLICT DO NOTHING;
OPEN curs FOR
WITH reserve_changes AS (
INSERT INTO reserves INSERT INTO reserves
(reserve_pub (reserve_pub
,current_balance_val ,current_balance_val
@ -65,20 +53,9 @@ BEGIN
,in_reserve_expiration ,in_reserve_expiration
,in_gc_date) ,in_gc_date)
ON CONFLICT DO NOTHING ON CONFLICT DO NOTHING
RETURNING reserve_uuid, reserve_pub) RETURNING reserve_uuid
SELECT reserve_uuid, reserve_pub FROM reserve_changes; INTO ruuid0;
FETCH FROM curs INTO i;
IF FOUND
THEN
-- We made a change, so the reserve did not previously exist.
out_reserve_found0 = FALSE;
ruuid0 = i.reserve_uuid;
END IF;
CLOSE curs;
OPEN curs_trans FOR
WITH reserve_transaction AS(
INSERT INTO reserves_in INSERT INTO reserves_in
(reserve_pub (reserve_pub
,wire_reference ,wire_reference
@ -95,21 +72,15 @@ BEGIN
,in_exchange_account_name ,in_exchange_account_name
,in_wire_source_h_payto ,in_wire_source_h_payto
,in_execution_date) ,in_execution_date)
ON CONFLICT DO NOTHING ON CONFLICT DO NOTHING;
RETURNING reserve_pub)
SELECT reserve_pub FROM reserve_transaction;
FETCH FROM curs_trans INTO i; transaction_duplicate0 = NOT FOUND;
IF FOUND IF FOUND
THEN THEN
transaction_duplicate0 = FALSE;
EXECUTE FORMAT ( EXECUTE FORMAT (
'NOTIFY %s' 'NOTIFY %s'
,in_notify); ,in_notify);
END IF; END IF;
CLOSE curs_trans;
RETURN; RETURN;
END $$; END $$;
@ -135,8 +106,6 @@ CREATE OR REPLACE FUNCTION exchange_do_batch2_reserves_insert(
IN in1_wire_source_h_payto BYTEA, IN in1_wire_source_h_payto BYTEA,
IN in1_payto_uri VARCHAR, IN in1_payto_uri VARCHAR,
IN in1_notify TEXT, IN in1_notify TEXT,
OUT out_reserve_found0 BOOLEAN,
OUT out_reserve_found1 BOOLEAN,
OUT transaction_duplicate0 BOOLEAN, OUT transaction_duplicate0 BOOLEAN,
OUT transaction_duplicate1 BOOLEAN, OUT transaction_duplicate1 BOOLEAN,
OUT ruuid0 INT8, OUT ruuid0 INT8,
@ -154,10 +123,6 @@ DECLARE
BEGIN BEGIN
transaction_duplicate0 = TRUE; transaction_duplicate0 = TRUE;
transaction_duplicate1 = TRUE; transaction_duplicate1 = TRUE;
out_reserve_found0 = TRUE;
out_reserve_found1 = TRUE;
ruuid0=0;
ruuid1=0;
INSERT INTO wire_targets INSERT INTO wire_targets
(wire_target_h_payto (wire_target_h_payto
@ -207,7 +172,6 @@ BEGIN
IF in0_reserve_pub = i.reserve_pub IF in0_reserve_pub = i.reserve_pub
THEN THEN
ruuid0 = i.reserve_uuid; ruuid0 = i.reserve_uuid;
out_reserve_found0 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -215,7 +179,6 @@ BEGIN
IF in1_reserve_pub = i.reserve_pub IF in1_reserve_pub = i.reserve_pub
THEN THEN
ruuid1 = i.reserve_uuid; ruuid1 = i.reserve_uuid;
out_reserve_found1 = FALSE;
END IF; END IF;
EXIT loop_reserve; EXIT loop_reserve;
END CASE; END CASE;
@ -332,10 +295,6 @@ CREATE OR REPLACE FUNCTION exchange_do_batch4_reserves_insert(
IN in3_wire_source_h_payto BYTEA, IN in3_wire_source_h_payto BYTEA,
IN in3_payto_uri VARCHAR, IN in3_payto_uri VARCHAR,
IN in3_notify TEXT, IN in3_notify TEXT,
OUT out_reserve_found0 BOOLEAN,
OUT out_reserve_found1 BOOLEAN,
OUT out_reserve_found2 BOOLEAN,
OUT out_reserve_found3 BOOLEAN,
OUT transaction_duplicate0 BOOLEAN, OUT transaction_duplicate0 BOOLEAN,
OUT transaction_duplicate1 BOOLEAN, OUT transaction_duplicate1 BOOLEAN,
OUT transaction_duplicate2 BOOLEAN, OUT transaction_duplicate2 BOOLEAN,
@ -359,14 +318,6 @@ BEGIN
transaction_duplicate1=TRUE; transaction_duplicate1=TRUE;
transaction_duplicate2=TRUE; transaction_duplicate2=TRUE;
transaction_duplicate3=TRUE; transaction_duplicate3=TRUE;
out_reserve_found0 = TRUE;
out_reserve_found1 = TRUE;
out_reserve_found2 = TRUE;
out_reserve_found3 = TRUE;
ruuid0=0;
ruuid1=0;
ruuid2=0;
ruuid3=0;
INSERT INTO wire_targets INSERT INTO wire_targets
(wire_target_h_payto (wire_target_h_payto
@ -430,7 +381,6 @@ BEGIN
IF in0_reserve_pub = i.reserve_pub IF in0_reserve_pub = i.reserve_pub
THEN THEN
ruuid0 = i.reserve_uuid; ruuid0 = i.reserve_uuid;
out_reserve_found0 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -439,7 +389,6 @@ BEGIN
IF in1_reserve_pub = i.reserve_pub IF in1_reserve_pub = i.reserve_pub
THEN THEN
ruuid1 = i.reserve_uuid; ruuid1 = i.reserve_uuid;
out_reserve_found1 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -448,7 +397,6 @@ BEGIN
IF in2_reserve_pub = i.reserve_pub IF in2_reserve_pub = i.reserve_pub
THEN THEN
ruuid2 = i.reserve_uuid; ruuid2 = i.reserve_uuid;
out_reserve_found2 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -456,7 +404,6 @@ BEGIN
IF in3_reserve_pub = i.reserve_pub IF in3_reserve_pub = i.reserve_pub
THEN THEN
ruuid3 = i.reserve_uuid; ruuid3 = i.reserve_uuid;
out_reserve_found3 = FALSE;
END IF; END IF;
EXIT loop_reserve; EXIT loop_reserve;
END CASE; END CASE;
@ -645,14 +592,6 @@ CREATE OR REPLACE FUNCTION exchange_do_batch8_reserves_insert(
IN in7_wire_source_h_payto BYTEA, IN in7_wire_source_h_payto BYTEA,
IN in7_payto_uri VARCHAR, IN in7_payto_uri VARCHAR,
IN in7_notify TEXT, IN in7_notify TEXT,
OUT out_reserve_found0 BOOLEAN,
OUT out_reserve_found1 BOOLEAN,
OUT out_reserve_found2 BOOLEAN,
OUT out_reserve_found3 BOOLEAN,
OUT out_reserve_found4 BOOLEAN,
OUT out_reserve_found5 BOOLEAN,
OUT out_reserve_found6 BOOLEAN,
OUT out_reserve_found7 BOOLEAN,
OUT transaction_duplicate0 BOOLEAN, OUT transaction_duplicate0 BOOLEAN,
OUT transaction_duplicate1 BOOLEAN, OUT transaction_duplicate1 BOOLEAN,
OUT transaction_duplicate2 BOOLEAN, OUT transaction_duplicate2 BOOLEAN,
@ -691,22 +630,6 @@ BEGIN
transaction_duplicate5=TRUE; transaction_duplicate5=TRUE;
transaction_duplicate6=TRUE; transaction_duplicate6=TRUE;
transaction_duplicate7=TRUE; transaction_duplicate7=TRUE;
out_reserve_found0 = TRUE;
out_reserve_found1 = TRUE;
out_reserve_found2 = TRUE;
out_reserve_found3 = TRUE;
out_reserve_found4 = TRUE;
out_reserve_found5 = TRUE;
out_reserve_found6 = TRUE;
out_reserve_found7 = TRUE;
ruuid0=0;
ruuid1=0;
ruuid2=0;
ruuid3=0;
ruuid4=0;
ruuid5=0;
ruuid6=0;
ruuid7=0;
INSERT INTO wire_targets INSERT INTO wire_targets
(wire_target_h_payto (wire_target_h_payto
@ -803,7 +726,6 @@ BEGIN
IF in0_reserve_pub = i.reserve_pub IF in0_reserve_pub = i.reserve_pub
THEN THEN
ruuid0 = i.reserve_uuid; ruuid0 = i.reserve_uuid;
out_reserve_found0 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -812,7 +734,6 @@ BEGIN
IF in1_reserve_pub = i.reserve_pub IF in1_reserve_pub = i.reserve_pub
THEN THEN
ruuid1 = i.reserve_uuid; ruuid1 = i.reserve_uuid;
out_reserve_found1 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -821,7 +742,6 @@ BEGIN
IF in2_reserve_pub = i.reserve_pub IF in2_reserve_pub = i.reserve_pub
THEN THEN
ruuid2 = i.reserve_uuid; ruuid2 = i.reserve_uuid;
out_reserve_found2 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -830,7 +750,6 @@ BEGIN
IF in3_reserve_pub = i.reserve_pub IF in3_reserve_pub = i.reserve_pub
THEN THEN
ruuid3 = i.reserve_uuid; ruuid3 = i.reserve_uuid;
out_reserve_found3 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -839,7 +758,6 @@ BEGIN
IF in4_reserve_pub = i.reserve_pub IF in4_reserve_pub = i.reserve_pub
THEN THEN
ruuid4 = i.reserve_uuid; ruuid4 = i.reserve_uuid;
out_reserve_found4 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -848,7 +766,6 @@ BEGIN
IF in5_reserve_pub = i.reserve_pub IF in5_reserve_pub = i.reserve_pub
THEN THEN
ruuid5 = i.reserve_uuid; ruuid5 = i.reserve_uuid;
out_reserve_found5 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -857,7 +774,6 @@ BEGIN
IF in6_reserve_pub = i.reserve_pub IF in6_reserve_pub = i.reserve_pub
THEN THEN
ruuid6 = i.reserve_uuid; ruuid6 = i.reserve_uuid;
out_reserve_found6 = FALSE;
CONTINUE loop_reserve; CONTINUE loop_reserve;
END IF; END IF;
CONTINUE loop_k; CONTINUE loop_k;
@ -865,7 +781,6 @@ BEGIN
IF in7_reserve_pub = i.reserve_pub IF in7_reserve_pub = i.reserve_pub
THEN THEN
ruuid7 = i.reserve_uuid; ruuid7 = i.reserve_uuid;
out_reserve_found7 = FALSE;
END IF; END IF;
EXIT loop_reserve; EXIT loop_reserve;
END CASE; END CASE;

View File

@ -113,12 +113,12 @@ struct ReserveRecord
* index @a index * index @a index
*/ */
#define RR_RESULT_PARAM(rr,index) \ #define RR_RESULT_PARAM(rr,index) \
GNUNET_PQ_result_spec_bool ("conflicted" TALER_S (index), \
&rr[index].conflicts), \
GNUNET_PQ_result_spec_bool ("transaction_duplicate" TALER_S (index), \ GNUNET_PQ_result_spec_bool ("transaction_duplicate" TALER_S (index), \
&rr[index].transaction_duplicate), \ &rr[index].transaction_duplicate), \
GNUNET_PQ_result_spec_allow_null ( \
GNUNET_PQ_result_spec_uint64 ("reserve_uuid" TALER_S (index), \ GNUNET_PQ_result_spec_uint64 ("reserve_uuid" TALER_S (index), \
&rr[index].reserve_uuid) &rr[index].reserve_uuid), \
&rr[index].conflicts)
/** /**
@ -151,8 +151,7 @@ insert1 (struct PostgresClosure *pg,
PREPARE (pg, PREPARE (pg,
"batch1_reserve_create", "batch1_reserve_create",
"SELECT " "SELECT "
" out_reserve_found0 AS conflicted0" " transaction_duplicate0 AS transaction_duplicate0"
",transaction_duplicate0 AS transaction_duplicate0"
",ruuid0 AS reserve_uuid0" ",ruuid0 AS reserve_uuid0"
" FROM exchange_do_batch_reserves_in_insert" " FROM exchange_do_batch_reserves_in_insert"
" ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);"); " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);");
@ -210,9 +209,7 @@ insert2 (struct PostgresClosure *pg,
PREPARE (pg, PREPARE (pg,
"batch2_reserve_create", "batch2_reserve_create",
"SELECT" "SELECT"
" out_reserve_found0 AS conflicted0" " transaction_duplicate0"
",out_reserve_found1 AS conflicted1"
",transaction_duplicate0"
",transaction_duplicate1" ",transaction_duplicate1"
",ruuid0 AS reserve_uuid0" ",ruuid0 AS reserve_uuid0"
",ruuid1 AS reserve_uuid1" ",ruuid1 AS reserve_uuid1"
@ -279,11 +276,7 @@ insert4 (struct PostgresClosure *pg,
PREPARE (pg, PREPARE (pg,
"batch4_reserve_create", "batch4_reserve_create",
"SELECT" "SELECT"
" out_reserve_found0 AS conflicted0" " transaction_duplicate0"
",out_reserve_found1 AS conflicted1"
",out_reserve_found2 AS conflicted2"
",out_reserve_found3 AS conflicted3"
",transaction_duplicate0"
",transaction_duplicate1" ",transaction_duplicate1"
",transaction_duplicate2" ",transaction_duplicate2"
",transaction_duplicate3" ",transaction_duplicate3"
@ -363,15 +356,7 @@ insert8 (struct PostgresClosure *pg,
PREPARE (pg, PREPARE (pg,
"batch8_reserve_create", "batch8_reserve_create",
"SELECT" "SELECT"
" out_reserve_found0 AS conflicted0" " transaction_duplicate0"
",out_reserve_found1 AS conflicted1"
",out_reserve_found2 AS conflicted2"
",out_reserve_found3 AS conflicted3"
",out_reserve_found4 AS conflicted4"
",out_reserve_found5 AS conflicted5"
",out_reserve_found6 AS conflicted6"
",out_reserve_found7 AS conflicted7"
",transaction_duplicate0"
",transaction_duplicate1" ",transaction_duplicate1"
",transaction_duplicate2" ",transaction_duplicate2"
",transaction_duplicate3" ",transaction_duplicate3"