From 6fc9158647619cf50a8bf2c7ade9f7f1b4aa274c Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Tue, 12 Nov 2024 11:41:03 -0800 Subject: [PATCH] DataSync: Avoid CREATE TABLE large_pg_notifications if not needed --- IHP/DataSync/ChangeNotifications.hs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/IHP/DataSync/ChangeNotifications.hs b/IHP/DataSync/ChangeNotifications.hs index a8ad25d38..8a38d59d4 100644 --- a/IHP/DataSync/ChangeNotifications.hs +++ b/IHP/DataSync/ChangeNotifications.hs @@ -98,12 +98,19 @@ createNotificationFunction table = [i| WHEN duplicate_function THEN null; - CREATE UNLOGGED TABLE IF NOT EXISTS large_pg_notifications ( - id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL, - payload TEXT DEFAULT null, - created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL - ); - CREATE INDEX IF NOT EXISTS large_pg_notifications_created_at_index ON large_pg_notifications (created_at); + IF NOT EXISTS ( + SELECT FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + WHERE c.relname = 'large_pg_notifications' + AND n.nspname = 'public' + ) THEN + CREATE UNLOGGED TABLE large_pg_notifications ( + id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL, + payload TEXT DEFAULT NULL, + created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL + ); + CREATE INDEX large_pg_notifications_created_at_index ON large_pg_notifications (created_at); + END IF; END; $$ |]