473,836 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

statement level triggers in PostgreSQL , anybody??

Hi,
I am a bit troubled with the row-level triggers which PostgreSQL
uses when using update table cpmmand.
For instance, if the primary key column has values 1,2,3,... and
i want to update the whole column as column = colunm + 1 , then there
is an error. This obviously works on databases such as mssql which has
row level triggers.
Any suggestions so that i do not have to modify the query and
still make it work in the postgresql database ??
Nov 23 '05 #1
4 2266
This has nothing to do with triggers, it has to do with not being able
to defer UNIQUE constraints. The classic solution is:

update SET column = -column;
update SET column = -column+1;

The problem is that you're trying to update the primary key, which is a
bad idea is most situations.

On Thu, May 27, 2004 at 06:28:08AM -0700, rama wrote:
Hi,
I am a bit troubled with the row-level triggers which PostgreSQL
uses when using update table cpmmand.
For instance, if the primary key column has values 1,2,3,... and
i want to update the whole column as column = colunm + 1 , then there
is an error. This obviously works on databases such as mssql which has
row level triggers.
Any suggestions so that i do not have to modify the query and
still make it work in the postgresql database ??

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly
--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAu607Y5T wig3Ge+YRAhSxAK DXI5ylUydrqiQzt kzoccJxx8G3gACb B/DG
t+PnAejlvOsGXxx Ldwz5Twc=
=QSvw
-----END PGP SIGNATURE-----

Nov 23 '05 #2
> This has nothing to do with triggers, it has to do with not being able
to defer UNIQUE constraints. The classic solution is:

update SET column = -column;
update SET column = -column+1;


Thanks for the reply.

Is changing query the only way to do this in postgres?

I would expect some more. IMHO, Postgres should first update all the
values and then check for duplicacy (and not check for duplicacy while
the query is still executing) - kind of saying that it should fire
statement level trigger instead of row level trigger. Most other
databases (Oracle/DB2 etc) behave this way.

The problem is that changing the query may not *always* work. E.g. If
the values in the primary key in the tables are consecutive, not in
any order, and contains negative numbers as well, it will be hard to
come up with a query that would do the needful. Say the table is (col1
is the primary key).

col1 | col2
------+------
-2 | 100
-1 | 100
0 | 100
1 | 100
2 | 100

On this, queries "update temp set col1=col1+1" as well as "update temp
set col1=col1-1" would fail with duplicate key error. Moreover, if the
query is autogenerated, where manual intervention is not possible, it
would be even harder.

So, a robust (and clean) way would be to have postgres support such
statements which would be to say that postgres check for duplicacy at
the end of the statement execution and not in between.

Now, what I want to know is, is there any way to make postgres show
such behaviour using some user level trigger (or any other mechanism)
or this is something which would require changing the postgres code
itself (we are willing to do that as well, but may not be worth the
effort if is somehow possible in the current framework).

Thanks.
Nov 23 '05 #3
It's true, PostgreSQL can only check a unique contraint at the time the
row is inserted and can't defer it to the end of the statement due to
the way they are implemented (using the indexes). Sure, there has been
talk about it but I don't think anyone has done it yet.

The thing is that for the vast majority of applications the primary key
is an opaque handle with no intrinsic meaning so it never needs to be
updated hence the issue never comes up. So I'm afraid that the demand
just isn't that high.

If you want to post a patch though, it'd probably be accepted.

Hope this helps,

On Thu, Jun 03, 2004 at 06:13:17AM -0700, Vikas Rana wrote:
This has nothing to do with triggers, it has to do with not being able
to defer UNIQUE constraints. The classic solution is:

update SET column = -column;
update SET column = -column+1;
Thanks for the reply.

Is changing query the only way to do this in postgres?

--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAw44rY5T wig3Ge+YRAvhaAJ sE1DpGWt/1zhdZUtlphx5BaN CZmgCeIDkL
POHV/pBCphjD4oqC1Yjh HF4=
=QvH3
-----END PGP SIGNATURE-----

Nov 23 '05 #4
It's true, PostgreSQL can only check a unique contraint at the time the
row is inserted and can't defer it to the end of the statement due to
the way they are implemented (using the indexes). Sure, there has been
talk about it but I don't think anyone has done it yet.

The thing is that for the vast majority of applications the primary key
is an opaque handle with no intrinsic meaning so it never needs to be
updated hence the issue never comes up. So I'm afraid that the demand
just isn't that high.

If you want to post a patch though, it'd probably be accepted.

Hope this helps,

On Thu, Jun 03, 2004 at 06:13:17AM -0700, Vikas Rana wrote:
This has nothing to do with triggers, it has to do with not being able
to defer UNIQUE constraints. The classic solution is:

update SET column = -column;
update SET column = -column+1;
Thanks for the reply.

Is changing query the only way to do this in postgres?

--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAw44rY5T wig3Ge+YRAvhaAJ sE1DpGWt/1zhdZUtlphx5BaN CZmgCeIDkL
POHV/pBCphjD4oqC1Yjh HF4=
=QvH3
-----END PGP SIGNATURE-----

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16728
by: Allan Hart | last post by:
Hi.. I'd very much appreciate it if someone would tell me how to translate a statement level trigger written in Oracle to its equivalent (if there is one) in MS SQL Server. Ditto for a row level trigger. If this is an old topic, I apologize. I'm very much a newbie to SQL Server. Regards,
1
9831
by: Nagib Abi Fadel | last post by:
Hi, is there any good trigger tutorial on the net ?? thx __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com
1
2581
by: Ling Xiaoyu | last post by:
Hello there. Can anybody help me with Postgresql triggers? what I need is a trigger which update value of field "tables_rows.total_rows" to rows count of table "zzz" if I insert new row in table "zzz"... CAREATE TABLE zzz ( ... ... );
1
2363
by: Christoph Graf | last post by:
Hi everybody! As far as I have seen you can inherit from a table and get its columns. Is there a possibility to also inherit a tables triggers? When I simply derive a table from another I don't get them (-> they don't trigger). It there is a way please tell me how I do this (and where I can find more information about writing triggers in a OO sense - e.g. depending on which table actually triggers, ...), if not where can I find...
1
1818
by: Campano, Troy | last post by:
Do you know what I did wrong? I'm trying to enable some logging features in postgresql.conf, but now I get this message when I try to connect to the postgresql server... WARNING: statement-level statistics are disabled because parser, planner, or executor statistics are on psql: FATAL: invalid cache id: 30
1
2440
by: SVGK, Raju | last post by:
Hi, I have a table where in lot of triggers were included in that as shown below. How to view/access triggers and procedures from postgresql. I am using postgresql 7.4.1 on solaris. regds -raju
6
2395
by: nd02tsk | last post by:
Hello I am going to do a comparison betweem PgSQL and MySQL replication system. I hear there are some replication projects available for PgSQL. Which are still active and serious, because I hear that some are not active or incomplete? Will any of these projects be merged with PgSQL soon?
0
4949
by: Bruno Lavoie | last post by:
Hello, i'm etablishing a naming convention for a new project under postgresql. For tables, sequences, views, that's ok! I used good naming conventions for this in the past and i'll keep these rules for the new project. but, i plan to use a lot of triggers and functions in this project, but I'm confused! I know that triggers call already created functions. But for tracking i must be able to associate these 2 different object
4
5750
by: --CELKO-- | last post by:
I need to convert a bunch of DB2 triggers to Oracle. Is there any kind of tools for this?
0
9820
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9374
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4016
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.