Skip to content

Commit

Permalink
fix: Improve partition creation on realtime.send (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco authored Dec 20, 2024
1 parent 7b1373f commit 821bd77
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ if config_env() == :prod do
end

if config_env() != :test do
config :logger, level: System.get_env("LOG_LEVEL", "info") |> String.to_existing_atom()

platform = if System.get_env("AWS_EXECUTION_ENV") == "AWS_ECS_FARGATE", do: :aws, else: :fly

config :realtime,
Expand Down Expand Up @@ -245,5 +247,3 @@ if System.get_env("LOGS_ENGINE") == "logflare" do
discard_threshold: 1_000,
backends: [LogflareLogger.HttpBackend]
end

config :logger, level: System.get_env("LOG_LEVEL", "info") |> String.to_existing_atom()
6 changes: 4 additions & 2 deletions lib/realtime/tenants/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ defmodule Realtime.Tenants.Migrations do
MessagesPartitioning,
MessagesUsingUuid,
FixSendFunction,
RecreateEntityIndexUsingBtree
RecreateEntityIndexUsingBtree,
FixSendFunctionPartitionCreation
}

@migrations [
Expand Down Expand Up @@ -124,7 +125,8 @@ defmodule Realtime.Tenants.Migrations do
{20_241_030_150_047, MessagesPartitioning},
{20_241_108_114_728, MessagesUsingUuid},
{20_241_121_104_152, FixSendFunction},
{20_241_130_184_212, RecreateEntityIndexUsingBtree}
{20_241_130_184_212, RecreateEntityIndexUsingBtree},
{20_241_220_035_512, FixSendFunctionPartitionCreation}
]

defstruct [:tenant_external_id, :settings]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Realtime.Tenants.Migrations.FixSendFunctionPartitionCreation do
@moduledoc false
use Ecto.Migration

# We missed the schema prefix of `realtime.` in the create table partition statement
def change do
execute("""
CREATE OR REPLACE FUNCTION realtime.send(payload jsonb, event text, topic text, private boolean DEFAULT true)
RETURNS void
AS $$
DECLARE
partition_name text;
partition_start timestamp;
partition_end timestamp;
BEGIN
partition_start := date_trunc('day', NOW());
partition_end := partition_start + interval '1 day';
partition_name := 'messages_' || to_char(partition_start, 'YYYY_MM_DD');
BEGIN
EXECUTE format(
'CREATE TABLE IF NOT EXISTS realtime.%I PARTITION OF realtime.messages FOR VALUES FROM (%L) TO (%L)',
partition_name,
partition_start,
partition_end
);
EXCEPTION WHEN duplicate_table THEN
-- Ignore; table already exists
END;
INSERT INTO realtime.messages (payload, event, topic, private, extension)
VALUES (payload, event, topic, private, 'broadcast');
END;
$$
LANGUAGE plpgsql;
""")
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.33.70",
version: "2.33.71",
elixir: "~> 1.17.3",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit 821bd77

Please sign in to comment.