-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocal_test.sql
More file actions
101 lines (73 loc) · 3.3 KB
/
Copy pathlocal_test.sql
File metadata and controls
101 lines (73 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
-- Reinstall extension
DROP EXTENSION IF EXISTS pg_kalam CASCADE;
CREATE EXTENSION pg_kalam;
-- Create the foreign server
CREATE SERVER IF NOT EXISTS kalam_server FOREIGN DATA WRAPPER pg_kalam;
-- ============================================================
-- SCHEMA OPT-IN: only 'app' is Kalam-managed
-- ============================================================
-- Enable the 'app' schema for Kalam (creates PG schema + Kalam namespace)
SELECT kalam.enable_schema('app', 'user');
-- ============================================================
-- REGULAR PG TABLE (not intercepted – public is NOT enabled)
-- ============================================================
CREATE TABLE public.vanilla_pg (id SERIAL PRIMARY KEY, info TEXT);
INSERT INTO public.vanilla_pg (info) VALUES ('this is a normal PG table');
SELECT * FROM public.vanilla_pg;
-- ============================================================
-- KALAM TABLE: native CREATE TABLE in enabled schema
-- ============================================================
CREATE TABLE app.profiles (
id TEXT PRIMARY KEY,
name TEXT,
age INTEGER
);
-- ^ Event trigger intercepts: drops regular table, creates Kalam table + foreign table
-- Check the foreign table exists
SELECT column_name, data_type FROM information_schema.columns
WHERE table_schema = 'app' AND table_name = 'profiles'
ORDER BY ordinal_position;
-- ============================================================
-- NATIVE DML: INSERT / SELECT / UPDATE / DELETE
-- ============================================================
SET kalam.user_id = 'user-alice';
INSERT INTO app.profiles (id, name, age) VALUES ('p1', 'Alice', 30);
INSERT INTO app.profiles (id, name, age) VALUES ('p2', 'Bob', 25);
SELECT * FROM app.profiles;
UPDATE app.profiles SET age = 31 WHERE id = 'p1';
SELECT id, name, age FROM app.profiles WHERE id = 'p1';
DELETE FROM app.profiles WHERE id = 'p2';
SELECT id, name, age FROM app.profiles;
-- ============================================================
-- NATIVE ALTER TABLE: adds column on both PG + Kalam
-- ============================================================
ALTER TABLE app.profiles ADD COLUMN email TEXT;
SELECT column_name, data_type FROM information_schema.columns
WHERE table_schema = 'app' AND table_name = 'profiles'
ORDER BY ordinal_position;
-- ============================================================
-- STREAM TABLE via GUC override
-- ============================================================
SET kalam.table_type = 'stream';
SET kalam.stream_ttl_seconds = '7200';
CREATE TABLE app.events (
id TEXT PRIMARY KEY,
event_type TEXT,
payload TEXT
);
RESET kalam.table_type;
RESET kalam.stream_ttl_seconds;
SET kalam.user_id = 'service-a';
INSERT INTO app.events (id, event_type, payload) VALUES ('e1', 'click', '{"x":10}');
SELECT * FROM app.events;
-- ============================================================
-- DROP via DROP FOREIGN TABLE (native)
-- ============================================================
DROP FOREIGN TABLE app.events;
-- ^ sql_drop trigger auto-drops Kalam table
DROP FOREIGN TABLE app.profiles;
SELECT table_name FROM information_schema.tables WHERE table_schema = 'app';
-- Clean up the vanilla PG table
DROP TABLE public.vanilla_pg;
-- Disable the schema (no more interception)
SELECT kalam.disable_schema('app');