Skip to content

Instantly share code, notes, and snippets.

@jensraaby
Last active July 22, 2021 13:36
Show Gist options
  • Save jensraaby/d5aa87b58f492143878e1b1c4fae5816 to your computer and use it in GitHub Desktop.
Save jensraaby/d5aa87b58f492143878e1b1c4fae5816 to your computer and use it in GitHub Desktop.
TIL Postgres

Indexes

https://www.postgresql.org/docs/9.1/sql-createindex.html

Concurrently

Indexing locks up the table for writes/deletes but allows reads. You can use CONCURRENTLY to prevent the locking

Replacing primary keys

Creating a new primary key involves a unique index; dropping the old primary key constraint; then adding the index as a new primary key constraint.

CREATE UNIQUE INDEX idxname ON tablename(col1,col2);

ALTER TABLE tablename DROP CONSTRAINT tablename_pkey;
ALTER TABLE tablename ADD CONSTRAINT tablename_pkey PRIMARY KEY USING INDEX idxname; 

Listing all constraints

https://dba.stackexchange.com/questions/214863/how-to-list-all-constraints-of-a-table-in-postgresql/243625

Creating composite primary keys

Order the fields so that the most restrictive filter first (i.e. the field that varies the most) (mpxn,msn,date) would result in (date,msn,mpxn)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment