Solved

Postgres Update Triggers (an update in an update)

Posted on 2011-03-09
2
1,028 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
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 39

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now