Skip to content

Commit

Permalink
fix: revert back to previous 'FirstGap()' query. It yields the same r…
Browse files Browse the repository at this point in the history
…esult and is much quicker.
  • Loading branch information
jaensen committed Dec 15, 2024
1 parent 480704f commit 71c9b3a
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions Circles.Index.Postgres/PostgresDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,20 @@ SELECT MAX(""blockNumber"") as block_number FROM ""System_Block""

public long? FirstGap()
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString)
{
CommandTimeout = 120
};
using var connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString);
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

NpgsqlCommand cmd = connection.CreateCommand();
cmd.CommandText = $@"
SELECT first_gap
FROM generate_series(
(SELECT MIN(""blockNumber"") FROM ""System_Block""),
(SELECT MAX(""blockNumber"") FROM ""System_Block"")
) AS first_gap
LEFT JOIN ""System_Block"" sb ON first_gap = sb.""blockNumber""
WHERE sb.""blockNumber"" IS NULL
SELECT (prev.""blockNumber"" + 1) AS gap_start
FROM (
SELECT ""blockNumber"", LEAD(""blockNumber"") OVER (ORDER BY ""blockNumber"") AS next_block_number
FROM (
SELECT ""blockNumber"" FROM ""System_Block"" ORDER BY ""blockNumber"" DESC LIMIT 1000000
) AS sub
) AS prev
WHERE prev.next_block_number - prev.""blockNumber"" > 1
ORDER BY gap_start
LIMIT 1;
";

Expand Down Expand Up @@ -336,7 +334,7 @@ public async Task DeleteFromBlockOnwards(long reorgAt)
await using var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync();

await using var transaction = await connection.BeginTransactionAsync(IsolationLevel.Serializable);
await using var transaction = await connection.BeginTransactionAsync();
try
{
foreach (var table in Schema.Tables.Values)
Expand Down

0 comments on commit 71c9b3a

Please sign in to comment.