Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #487 from rodo/remove_drop_indexes
Browse files Browse the repository at this point in the history
Replace DROP INDEX with conditional creation in PostgreSQL backends
  • Loading branch information
leplatrem committed Oct 22, 2015
2 parents 94c2bea + 1477c25 commit 495c074
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Contributors
* Michiel de Jong <michiel@unhosted.org>
* Nicolas Perriault <nperriault@mozilla.com>
* Rémy Hubscher <rhubscher@mozilla.com>
* Rodolphe Quiédeville <rodolphe@quiedeville.org>
* Tarek Ziade <tarek@mozilla.com>
84 changes: 76 additions & 8 deletions cliquet/storage/postgresql/schema.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
--
-- Automated script, we do not need NOTICE and WARNING
--
SET client_min_messages TO ERROR;
--
-- Convert timestamps to milliseconds epoch integer
--
CREATE OR REPLACE FUNCTION as_epoch(ts TIMESTAMP) RETURNS BIGINT AS $$
Expand Down Expand Up @@ -26,13 +30,53 @@ CREATE TABLE IF NOT EXISTS records (

PRIMARY KEY (id, parent_id, collection_id)
);
--
-- CREATE INDEX IF NOT EXISTS will be available in PostgreSQL 9.5
-- http://www.postgresql.org/docs/9.5/static/sql-createindex.html
DO $$
BEGIN

DROP INDEX IF EXISTS idx_records_parent_id_collection_id_last_modified;
CREATE UNIQUE INDEX idx_records_parent_id_collection_id_last_modified
IF NOT EXISTS (
SELECT 1 FROM pg_indexes
WHERE indexname = 'idx_records_parent_id_collection_id_last_modified'
AND tablename = 'records'
) THEN

CREATE UNIQUE INDEX idx_records_parent_id_collection_id_last_modified
ON records(parent_id, collection_id, last_modified DESC);
DROP INDEX IF EXISTS idx_records_last_modified_epoch;
CREATE INDEX idx_records_last_modified_epoch ON records(as_epoch(last_modified));

END IF;
END$$;

DO $$
BEGIN

IF NOT EXISTS (
SELECT 1 FROM pg_indexes
WHERE indexname = 'idx_records_parent_id_collection_id_last_modified'
AND tablename = 'records'
) THEN

CREATE UNIQUE INDEX idx_records_parent_id_collection_id_last_modified
ON records(parent_id, collection_id, last_modified DESC);

END IF;
END$$;

DO $$
BEGIN

IF NOT EXISTS (
SELECT 1 FROM pg_indexes
WHERE indexname = 'idx_records_last_modified_epoch'
AND tablename = 'records'
) THEN

CREATE INDEX idx_records_last_modified_epoch
ON records(as_epoch(last_modified));

END IF;
END$$;

--
-- Deleted records, without data.
Expand All @@ -45,12 +89,36 @@ CREATE TABLE IF NOT EXISTS deleted (

PRIMARY KEY (id, parent_id, collection_id)
);
DROP INDEX IF EXISTS idx_deleted_parent_id_collection_id_last_modified;
CREATE UNIQUE INDEX idx_deleted_parent_id_collection_id_last_modified

DO $$
BEGIN

IF NOT EXISTS (
SELECT 1 FROM pg_indexes
WHERE indexname = 'idx_deleted_parent_id_collection_id_last_modified'
AND tablename = 'deleted'
) THEN

CREATE UNIQUE INDEX idx_deleted_parent_id_collection_id_last_modified
ON deleted(parent_id, collection_id, last_modified DESC);
DROP INDEX IF EXISTS idx_deleted_last_modified_epoch;
CREATE INDEX idx_deleted_last_modified_epoch ON deleted(as_epoch(last_modified));

END IF;
END$$;

DO $$
BEGIN

IF NOT EXISTS (
SELECT 1 FROM pg_indexes
WHERE indexname = 'idx_deleted_last_modified_epoch'
AND tablename = 'deleted'
) THEN

CREATE INDEX idx_deleted_last_modified_epoch
ON deleted(as_epoch(last_modified));

END IF;
END$$;

--
-- Helper that returns the current collection timestamp.
Expand Down

0 comments on commit 495c074

Please sign in to comment.