PostgreSQL: UPDATE a table using ORDER BY

John Reeve | July 23rd, 2007

MySQL is great in that it really lets you get away with a lot. Something I’ve found frequently valuable is the ability to update a table in a particular order, such as when you want a column to have a particular numerical sequence, and you want that sequence to share the same order as another field. Let me give you an example.

In many of our tables we allow site administrators to sort elements (rows) according to a certain priority (in this case, 1 being the highest priority). For one particular client, they wanted the ability to sort their list of team members according to priority. This allowed them to order their employees according to any criteria they desire (say, in this case, seniority, or rank). However, recently they asked us to sort their employee table according to name. Under MySQL this would be easy. You could run the following:

SET @p=1;
UPDATE team SET priority = (@p:=@p+1) ORDER BY name ASC;

Piece of cake: each employee is given a certain priority according to name. However, under PostgreSQL, this is much more challenging, and I couldn’t find any easy solution on the web. It turns out, the solution requires the use of views, rules, and sequences, things that are foreign to most MySQL users and all but the more advanced PostgreSQL users. Here are the following steps:

  1. You must first create a view on the table that you want to update in the particular order that you want to update by:
  2. CREATE VIEW view_team AS SELECT * FROM team ORDER BY name ASC;

  3. To run the update on the view you just created, you have to create a rule telling the view how to interpret UPDATE queries you’re going to run on it:
  4. CREATE RULE rule_team AS ON UPDATE TO view_team DO INSTEAD UPDATE team SET priority = NEW.priority WHERE id = NEW.id;

    This specifies that when you try to run an update on the view_team view, it will apply those changes to the team table where we want those changes to show up. You can change more than one field in this rule by adding more fields to the UPDATE part of that query, but since we are only applying changes to the priority field, this will suffice.

  5. Create a sequence. This will update each row in sequence, and serves as an alternative to using user-defined variables, as we did in MySQL:
  6. CREATE SEQUENCE team_priority_seq;

  7. Update the view:
  8. UPDATE view_team SET priority = nextval('team_priority_seq');

  9. Now unless you want to keep the view, rule, and sequence around, you can drop them:

    DROP SEQUENCE team_priority_seq;
    DROP RULE rule_team ON view_team;
    DROP VIEW view_team;

Hopefully if you followed those steps, your table should be sorted in exactly the order you want. Here is everything all at once:

CREATE VIEW view_team AS SELECT * FROM team ORDER BY name;
CREATE RULE rule_team AS ON UPDATE TO view_team DO INSTEAD
UPDATE team SET
priority = NEW.priority
WHERE id = NEW.id;
CREATE SEQUENCE team_priority_seq;
UPDATE view_team SET priority = nextval('team_priority_seq');
DROP SEQUENCE team_priority_seq;
DROP RULE rule_team ON view_team;
DROP VIEW view_team;

2 Responses to “PostgreSQL: UPDATE a table using ORDER BY”

  1. Juergen says:

    This might be a bit much overhead for a one time usage. This might do the trick. Don’t know about the performance.

    update team t3 
    set priority  = t2.newPrio
    from
    (
    	select name, nextval( 'team_priority_seq' ) newPrio
    	from 
    	(select name from team order by name) T1
    ) t2 
    where t3.name = t2.name;
  2. Tagôre says:

    My sugestion is:

    update tasks_task t1
    set "order" = t2.newOrder
    from (
    		SELECT
    			id,
    			ROW_NUMBER () OVER (ORDER BY "name") as newOrder
    		from tasks_task
    ) t2
    where t1.id = t2.id;

Leave a Reply

Intervals Blog

A collection of useful tips, tales and opinions based on decades of collective experience designing and developing web sites and web-based applications.

What is Intervals?

Intervals is online time, task and project management software built by and for web designers, developers and creatives.
Learn more…

John Reeve
Author Profile
John Reeve

John is a co-founder, web designer and developer at Pelago. His blog posts are inspired by everyday encounters with designers, developers, creatives and small businesses in general. John is an avid reader and road cyclist.
» More about John
» Read posts by John

Jennifer Payne
Author Profile
Jennifer Payne

Jennifer is the Director of Quality and Efficiency at Pelago. Her blog posts are based largely on her experience working with teams to improve harmony and productivity. Jennifer is a cat person.
» More about Jennifer
» Read posts by Jennifer

Michael Payne
Author Profile
Michael Payne

Michael is a co-founder and product architect at Pelago. His contributions stem from experiences managing the development process behind web sites and web-based applications such as Intervals. Michael drives a 1990 Volkswagen Carat with a rebuilt 2.4 liter engine from GoWesty.
» More about Michael
» Read posts by Michael

help.myintervals.com
Videos, tips & tricks