Re: Defining and Using variables in a postgres function - Mailing list pgsql-general

From Jim Nasby
Subject Re: Defining and Using variables in a postgres function
Date
Msg-id 9894F93D-9B82-45C2-B135-258D150622C1@decibel.org
Whole thread Raw
In response to Defining and Using variables in a postgres function  ("Harpreet Dhaliwal" <harpreet.dhaliwal01@gmail.com>)
List pgsql-general
On Feb 1, 2007, at 12:09 PM, Harpreet Dhaliwal wrote:
> For inserting the id, i need to query a table xyz, fetch the
> maximum id in it, increment it by 1 and store it in tbl_email.
> Right after BEGIN in my function I have a commnet where in I need
> to query the xyz table, fetch the max id and store it in a variable
> and then I can increment this variable and store it in tbl_email.

That's not safe unless you get an exclusive lock on the table before
the select.

Why don't you just use a sequence/serial?

> How should i define this variable first and how to push the result
> of the query fired on table xyz.

SELECT INTO v_max_id
    max(id)
    FROM table
;
--
Jim Nasby                                            jim@nasby.net
EnterpriseDB      http://enterprisedb.com      512.569.9461 (cell)



pgsql-general by date:

Previous
From: Jim Nasby
Date:
Subject: Re: VACUUM ANALYZE taking a long time, %I/O and %CPU very low
Next
From: Jim Nasby
Date:
Subject: Re: SQL to get a table columns comments?