Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2015 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a6654100d61a6a01c595 to your computer and use it in GitHub Desktop.
Save anonymous/a6654100d61a6a01c595 to your computer and use it in GitHub Desktop.
psql (9.4.1)
Type "help" for help.
test=# create table bug_or_feature (id bigserial not null primary key, created timestamp with time zone not null default now(), priority int not null, state text not null);
CREATE TABLE
test=# create index on bug_or_feature (priority DESC, created);
CREATE INDEX
test=# insert into bug_or_feature (priority, state) values (0, 'inactive');
INSERT 0 1
test=# insert into bug_or_feature (priority, state) values (0, 'inactive');
INSERT 0 1
test=# insert into bug_or_feature (priority, state) values (0, 'inactive');
INSERT 0 1
test=# select * from bug_or_feature;
id | created | priority | state
----+-------------------------------+----------+----------
1 | 2015-03-01 20:13:49.070046+01 | 0 | inactive
2 | 2015-03-01 20:13:50.382009+01 | 0 | inactive
3 | 2015-03-01 20:13:58.077502+01 | 0 | inactive
(3 rows)
test=# update bug_or_feature set state = 'active' from (select id from bug_or_feature where state = 'inactive' order by priority desc, created limit 1 for update) job;
UPDATE 3
test=# select * from bug_or_feature;
id | created | priority | state
----+-------------------------------+----------+--------
1 | 2015-03-01 20:13:49.070046+01 | 0 | active
2 | 2015-03-01 20:13:50.382009+01 | 0 | active
3 | 2015-03-01 20:13:58.077502+01 | 0 | active
(3 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment