Skip to content

Commit a47f74e

Browse files
committed
Removed redundant stream position from streams tables
1 parent ea04a9c commit a47f74e

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/packages/emmett-postgresql/src/eventStore/schema/appendToStream.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ export const appendToStreamSQL = rawSql(
4848
v_transaction_id := pg_current_xact_id();
4949
5050
IF v_expected_stream_position IS NULL THEN
51-
SELECT COALESCE(stream_position, 0) INTO v_expected_stream_position
52-
FROM ${streamsTable.name}
53-
WHERE stream_id = v_stream_id AND partition = v_partition AND is_archived = FALSE;
51+
SELECT COALESCE(
52+
(SELECT stream_position
53+
FROM ${streamsTable.name}
54+
WHERE stream_id = v_stream_id
55+
AND partition = v_partition
56+
AND is_archived = FALSE
57+
LIMIT 1),
58+
0
59+
) INTO v_expected_stream_position;
5460
END IF;
5561
5662
v_next_stream_position := v_expected_stream_position + array_upper(v_messages_data, 1);

src/packages/emmett-postgresql/src/eventStore/schema/tables.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ export const streamsTableSQL = rawSql(
1515
stream_type TEXT NOT NULL,
1616
stream_metadata JSONB NOT NULL,
1717
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
18-
PRIMARY KEY (stream_id, stream_position, partition, is_archived),
19-
UNIQUE (stream_id, partition, is_archived)
20-
) PARTITION BY LIST (partition);`,
18+
PRIMARY KEY (stream_id, partition, is_archived)
19+
) PARTITION BY LIST (partition);
20+
21+
CREATE UNIQUE INDEX idx_streams_unique
22+
ON ${streamsTable.name}(stream_id, partition, is_archived)
23+
INCLUDE (stream_position);`,
2124
);
2225

2326
export const messagesTableSQL = rawSql(

src/packages/emmett-sqlite/src/eventStore/schema/tables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const streamsTableSQL = sql(
1616
stream_type TEXT NOT NULL,
1717
stream_metadata JSONB NOT NULL,
1818
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
19-
PRIMARY KEY (stream_id, stream_position, partition, is_archived),
19+
PRIMARY KEY (stream_id, partition, is_archived),
2020
UNIQUE (stream_id, partition, is_archived)
2121
);`,
2222
);

0 commit comments

Comments
 (0)