Add attachment_idx column, constraint and populate it

This commit is contained in:
towards-a-new-leftypol 2024-02-28 00:29:28 -05:00
parent e73293c4e0
commit 7cb508eb8a
2 changed files with 16 additions and 1 deletions

View File

@ -0,0 +1,11 @@
WITH indexed_attachments AS (
SELECT
attachment_id,
ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY attachment_id) AS index
FROM
attachments
)
UPDATE attachments
SET attachment_idx = indexed_attachments.index
FROM indexed_attachments
WHERE attachments.attachment_id = indexed_attachments.attachment_id;

View File

@ -121,12 +121,16 @@ CREATE TABLE IF NOT EXISTS attachments
, board_filename text NOT NULL
, spoiler boolean NOT NULL DEFAULT true
, file_size_bytes int
, attachment_idx int NOT NULL
, CONSTRAINT post_fk FOREIGN KEY (post_id) REFERENCES posts (post_id) ON DELETE CASCADE
, CONSTRAINT unique_post_attachment_idx UNIQUE (post_id, attachment_idx)
);
CREATE INDEX attachments_creation_time_idx ON attachments (creation_time);
CREATE INDEX attachments_post_id_idx ON attachments (post_id);
CREATE INDEX attachments_sha256_hash_idx ON attachments (sha256_hash);
--
CREATE INDEX attachments_attachment_idx_idx ON attachments (attachment_idx);
-- Index using the bktree extension for quickly getting the closest phashes
CREATE INDEX attachments_phash_bktree_index ON attachments USING spgist (phash bktree_ops);