473,786 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parameter in trigger function

Hello everyone,

Several columns in sereval tables in my DB should always be lowercase.
I now have a simple function:

create or replace function code_lower() returns trigger as '
begin
NEW.code := lower(NEW.code) ;
return NEW;
end'
language 'plpgsql';

which I call with a trigger like this:

create trigger my_trigger
before insert or update on my_table
execute procedure code_lower();

This will successfully lower() a field named 'code' in the table
'mytable' or any other table to which I point it. But some of my tables
have fields which should be lower()ed that have names other than
'code'. Writing a function for every of these field seems stupid, so I
tried to give the trigger arguments. Code like this

NEW.$1 := lower(NEW.$1)

won't work, all I get is error messages :-( The doc says this should be
OK (http://www.postgresql.org/docs/7.3/static/triggers.html) but Google
mostly says the opposite. Is this possible at all? How do I read the
TriggerData structure from whithin a pl/pgsql function?

TIA!

---------------------------(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

Nov 11 '05 #1
2 6209
"Jules Alberts" <ju***********@ arbodienst-limburg.nl> writes:
Code like this
NEW.$1 := lower(NEW.$1)
won't work, all I get is error messages :-(
Can't do that in plpgsql; it does not like run-time specification of
table or field names. You could try using EXECUTE but I doubt that
will work either for an assignment to NEW.

I'd suggest using pltcl, which is more amenable to run-time
determination of field names.
mostly says the opposite. Is this possible at all? How do I read the
TriggerData structure from whithin a pl/pgsql function?


You are looking for TG_ARGV[], though this is not your biggest
problem...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #2
Jules Alberts wrote:
Hello everyone,

Several columns in sereval tables in my DB should always be lowercase.
I now have a simple function:

create or replace function code_lower() returns trigger as '
begin
NEW.code := lower(NEW.code) ;
return NEW;
end'
language 'plpgsql';

which I call with a trigger like this:

create trigger my_trigger
before insert or update on my_table
execute procedure code_lower();

This will successfully lower() a field named 'code' in the table
'mytable' or any other table to which I point it. But some of my tables
have fields which should be lower()ed that have names other than
'code'. Writing a function for every of these field seems stupid, so I
tried to give the trigger arguments. Code like this

NEW.$1 := lower(NEW.$1)

won't work, all I get is error messages :-( The doc says this should be
OK (http://www.postgresql.org/docs/7.3/static/triggers.html) but Google
mostly says the opposite. Is this possible at all? How do I read the
TriggerData structure from whithin a pl/pgsql function?


It might seem stupid on the first look, but let's look again. PL/pgSQL
is a language that makes heavy use of cached query plans. Let's assume
your sample function above is triggered on it's first call in a session
for a table where "code" is an attribute of type "text". It will prepare
an SPI plan with the query "SELECT lower($1)" and tell the parser and
planner that the parameter $1 is of type "text". This plan is saved and
never touched again during the lifetime of your connection.

Now whenever your trigger function is called, it will put NEW.code into
a Datum array and call SPI_execp() for the above prepared plan. If you
now install the same trigger function on a table where "code" is of type
"name", this will not work because of a parameter type mismatch.

So it's even better not only to create separate functions per field
name, it's best to create a separate function for every single trigger.

You can alternatively use a language with fine control over SPI plan
caching like PL/Tcl. But then you loose exactly that optimization and
your trigger has to parse and plan this "SELECT lower('quotedva l')" on
every single invocation. Do that only if you are sure to have ample
spare cpu cycles.
Jan

--
#============== =============== =============== =============== ===========#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#============== =============== =============== ====== Ja******@Yahoo. com #
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #3

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

Similar topics

1
3211
by: Alex Vinokur | last post by:
========================= Windows 2000 Professional Digital Mars C/C++ 8.36 STLport 4.5.3 ========================= I have got a problem with compilation of the following piece of code using Digital Mars C/C++ compiler. There was no problem with this code while using GNU g++ 3.2.
1
4185
by: Dunc | last post by:
I'm new to Postgres, and getting nowhere with a PL/Perl trigger that I'm trying to write - hopefully, someone can give me some insight into what I'm doing wrong. My trigger is designed to reformat / standardize phone numbers and it looks like this: CREATE or REPLACE FUNCTION fixphone() RETURNS trigger AS $$ $number .= $_TD->{new}{phone}; $number =~ s/(-|\.|\(|\)| )//g; $number .= substr($number,0,3) . "." .
2
8993
by: eczino | last post by:
I currently have a web form posting back to a SQL table using a Stored Procedure. Part of this SP is that it pulls data from another table and inserts a new row into the registration table. I want to have a trigger on the registration table that will fire when the row is inserted which will use the sp_send_cdosysmail sproc to send an e-mail to the user. However, I want to be able to include the value of one of the fields within the...
4
2136
by: Jules Alberts | last post by:
Hello everyone, I'm working on a tiny trigger function that needs to ensure that all values entered in a field are lowercase'd. I can't use pl/pgsql because I have a dozen different columns (with different names) that need a trigger that does this and pl'pgsql can't expand variable names to fieldnames. Writing a dozen functions (one per columnname) is /way/ too blunt so I tried pl/tcl (which I don't know): ...
1
4462
by: Barbara Lindsey | last post by:
I am a postgres newbie. I am trying to create a trigger that will put a copy of a record into a backup table before update or delete. As I understand it, in order to do this I must have a function created to do this task. The function I am trying to create is as follows: CREATE FUNCTION customer_bak_proc(integer) RETURNS boolean as 'INSERT INTO customer_bak (SELECT * from customer where id = $1 )' LANGUAGE 'SQL';
6
5116
by: alederer | last post by:
Hallo! I have a table tstest(ts char(13) for bit data constraint a unique). This column is filled in a trigger with generate_unique(). In a application (CLI), I have the values of this column as timestamp representation: e.g.: select timestamp(ts) from tstest 1
8
2638
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or something entirely different? The docs only mention regular triggers being executed alfabetically.
1
2995
by: Mario A. Soto Cordones | last post by:
Hi. anybody know how to sen a parameter an a trigger. thank for your help Mario Soto
1
7275
by: djdevx | last post by:
Dear all PostgreSQL xperts! Hi, I am newbie in PostgreSQL. I am currently developing an app that use PostgreSQL as a BackEnd Database. Now I am having problem with the function trigger since last week. Here's the case: create table nr_tr (tr_type character(3), period character(6), nr_tr integer); create table h_trsale(nr_tr integer, sale_dt datetime, desc text); create table h_trbuy (nr_tr integer, sale_dt datetime, desc text);
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
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
8992
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...
0
5398
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
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
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.