Solved

Postgres Update Triggers (an update in an update)

Posted on 2011-03-09
2
1,053 Views
Last Modified: 2012-05-11
Postgres 9.0

I want to have a trigger for an update on TableA, in that trigger, part of the process is to update other rows in TableA.

What is the best way/method to do this without causing a loop.

Simple example, Table A has field "is_master" which I want only 1 record of x,y,z  to have is_master = 't'. When a new row is inserted and is x,y,z (and also newer version), I want for new row to have is_master = 't', and the old row is_master = null.

The table already has a unique constraint on (x,y,z,is_master).

Somehow, I need to set old is_master to null so I can insert new row with is_master = 't';
0
Comment
Question by:freshgrill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 35089539
I would create a stored procedure for the update in do inside of it:

update all is_master to null
insert new row with is_master ='t'

surround this with a transaction + COMMIT / ROLLBACK and you done....
0
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 35096893
I think the constraint won't let you put a AFTER (by default) trigger with that action but if you have a id column on that table you could use OLD.id in a BEFORE  trigger to identify existing x,y,z and set is_master = 0 for that table.id then set is_master = 1 WHERE  table.id = NEW.id
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question