diff --git a/src/exchangedb/exchange_do_batch_reserves_in_insert.sql b/src/exchangedb/exchange_do_batch_reserves_in_insert.sql index 4e0383f6c..d1cc35d3e 100644 --- a/src/exchangedb/exchange_do_batch_reserves_in_insert.sql +++ b/src/exchangedb/exchange_do_batch_reserves_in_insert.sql @@ -32,7 +32,10 @@ CREATE OR REPLACE FUNCTION exchange_do_batch_reserves_in_insert( OUT ruuid INT8) LANGUAGE plpgsql AS $$ - +DECLARE + curs refcursor; +DECLARE + i RECORD; BEGIN ruuid= 0; out_reserve_found = TRUE; @@ -46,28 +49,35 @@ transaction_duplicate= TRUE; ,in_payto_uri) ON CONFLICT DO NOTHING; - INSERT INTO reserves - (reserve_pub - ,current_balance_val - ,current_balance_frac - ,expiration_date - ,gc_date) - VALUES - (in_reserve_pub - ,in_credit_val - ,in_credit_frac - ,in_expiration_date - ,in_gc_date) - ON CONFLICT DO NOTHING - RETURNING reserves.reserve_uuid INTO ruuid; + OPEN curs FOR + WITH reserve_changes AS ( + INSERT INTO reserves + (reserve_pub + ,current_balance_val + ,current_balance_frac + ,expiration_date + ,gc_date) + VALUES + (in_reserve_pub + ,in_credit_val + ,in_credit_frac + ,in_expiration_date + ,in_gc_date) + ON CONFLICT DO NOTHING + RETURNING reserve_uuid, reserve_pub) + SELECT * FROM reserve_changes; + FETCH FROM curs INTO i; IF FOUND THEN -- We made a change, so the reserve did not previously exist. - out_reserve_found = FALSE; - ELSE - -- We made no change, which means the reserve existed. - out_reserve_found = TRUE; + IF in_reserve_pub = i.reserve_pub + THEN + out_reserve_found = FALSE; + ruuid = i.reserve_uuid; + END IF; END IF; + CLOSE curs; + PERFORM pg_notify(in_notify, NULL); INSERT INTO reserves_in (reserve_pub @@ -92,7 +102,7 @@ transaction_duplicate= TRUE; transaction_duplicate = FALSE; ELSE -- Unhappy... --- RAISE EXCEPTION 'Reserve did not exist, but INSERT into reserves_in gave conflict'; + RAISE EXCEPTION 'Reserve did not exist, but INSERT into reserves_in gave conflict'; transaction_duplicate = TRUE; -- ROLLBACK; END IF; diff --git a/src/exchangedb/pg_batch2_reserves_in_insert.c b/src/exchangedb/pg_batch2_reserves_in_insert.c index a55c518c3..e0208f094 100644 --- a/src/exchangedb/pg_batch2_reserves_in_insert.c +++ b/src/exchangedb/pg_batch2_reserves_in_insert.c @@ -763,6 +763,7 @@ TEH_PG_batch2_reserves_in_insert (void *cls, t_duplicate |= transaction_duplicate[i+1]; t_duplicate |= transaction_duplicate[i+2]; t_duplicate |= transaction_duplicate[i+3]; + // fprintf(stdout, "reserve_uuid : %ld %ld %ld %ld\n", reserve_uuid[i], reserve_uuid[i+1], reserve_uuid[i+2], reserve_uuid[i+3]); i += 4; break; case 3: @@ -792,6 +793,7 @@ TEH_PG_batch2_reserves_in_insert (void *cls, results[i] = (t_duplicate) ? GNUNET_DB_STATUS_SUCCESS_NO_RESULTS : GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; + // fprintf(stdout, "reserve_uuid : %ld %ld\n", reserve_uuid[i], reserve_uuid[i+1]); i += 2; break; case 1: