If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Global values in PLpgSQL

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-10-09, 22:22
gary002g gary002g is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Exclamation Global values in PLpgSQL

I am working on the performance problem with the database application.
I've got several PLpgSQL functions that are doing bulk processing of the data.
They are calling some other functions in the loop which, in their own turn, calling third level functions. These, third level functions use some static data.
It is not constant but for for the particular installation it practically never changes. It is currently stored in couple static tables which are used in joins by queries. If I can eliminate these joins, I can shave off 150-170 ms for every query execution which adds up to 2-2.5 minutes for the top level.

If someone know a way to somehow reads this static data into a set of environment variables which can later be used by all underlying functions without passing them in as parameters (there are too many to pass),
please speak up. Your input will be greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 06-12-09, 08:13
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,297
Quote:
Originally Posted by gary002g
If someone know a way to somehow reads this static data into a set of environment variables which can later be used by all underlying functions without passing them in as parameters (there are too many to pass),
please speak up. Your input will be greatly appreciated.
You can define variables in postgresql.conf

PostgreSQL: Documentation: Manuals: PostgreSQL 8.3: Customized Options

In postgresql.conf you could define:
Code:
custom_variable_classes = 'myvars'
myvars.value_1 = 'foo'
myvars.value_2 = 'bar'
This can then be selected inside a SQL statement:
Code:
select current_setting('myvars.value_1');
Setting the variable classes in postgresql.conf requires a server restart though
Reply With Quote
  #3 (permalink)  
Old 06-12-09, 14:28
gary002g gary002g is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Quote:
Originally Posted by shammat
You can define variables in postgresql.conf

PostgreSQL: Documentation: Manuals: PostgreSQL 8.3: Customized Options

In postgresql.conf you could define:
Code:
custom_variable_classes = 'myvars'
myvars.value_1 = 'foo'
myvars.value_2 = 'bar'
This can then be selected inside a SQL statement:
Code:
select current_setting('myvars.value_1');
Setting the variable classes in postgresql.conf requires a server restart though
Hm-m-m, How can I use it in a query, though?
For example right now I have a query that selects events that occurred within a test period of the specified timestamp:
Code:
SELECT e.* FROM events e, metric_parameters p
WHERE e.event_ts BETWEEN $1 - p.value::INTERVAL AND $1 + p.value::INTERVAL
AND p.key = 'test_period';

If I rewrite the query this way:
Code:
SELECT e.* FROM events e
WHERE e.event_ts BETWEEN $1 - INTERVAL '15 minute' AND $1 + INTERVAL '15 minute';

I am gaining 25% on performance.
Reply With Quote
  #4 (permalink)  
Old 06-12-09, 16:05
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,297
Quote:
Originally Posted by gary002g
Hm-m-m, How can I use it in a query, though?
I don't understand your question. I was showing an example using it in a query.

Did you try casting the variable?
Code:
custom_variable_classes = 'myvars'
myvars.interval_1 = '15 minute'
Code:
SELECT e.* FROM events e
WHERE e.event_ts BETWEEN $1 - (cast(current_settings('myvars.interval_1') as interval) AND $1 + (cast(current_settings('myvars.interval_1') as interval)
Reply With Quote
  #5 (permalink)  
Old 06-18-09, 01:49
gary002g gary002g is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Quote:
Originally Posted by shammat
I don't understand your question. I was showing an example using it in a query.

Did you try casting the variable?
Code:
custom_variable_classes = 'myvars'
myvars.interval_1 = '15 minute'
Code:
SELECT e.* FROM events e
WHERE e.event_ts BETWEEN $1 - (cast(current_settings('myvars.interval_1') as interval) AND $1 + (cast(current_settings('myvars.interval_1') as interval)
Thank you, it works.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On