I'm having a problem with a value coming out of a loop.
CREATE OR REPLACE FUNCTION funmessagesperm intotal()
RETURNS int8 AS
'
DECLARE
this_rServer record;
this_rSum record;
this_iSum bigint;
this_iTotal bigint;
this_iMsgsPerMi n bigint;
this_sQuery varchar(500);
BEGIN
this_iTotal := 0;
FOR this_rServer IN SELECT iId FROM tblServers LOOP
this_sQuery := \'
SELECT SUM( iNumSent ) AS iSum
FROM tblBatchHistory _\' || this_rServer.ii d || \'
WHERE tStamp > now() - interval \'\'5 min\'\';
\';
FOR this_rSum IN EXECUTE this_sQuery LOOP
this_iSum := this_rSum.isum;
END LOOP;
this_iTotal := this_iTotal + this_iSum;
END LOOP;
this_iMsgsPerMi n := this_iTotal / 5;
IF this_iMsgsPerMi n IS NULL THEN
this_iMsgsPerMi n := 0;
END IF;
RETURN this_iMsgsPerMi n;
END;
'
LANGUAGE 'plpgsql';
If I return this_iSum or this_iTotal in the loop, I get a value. If I
return directly after the loop, the value is NULL. I can't figure out
why it's doing this. The value comes out of the inside for loop just
fine, but not the outer loop.
What's going on?
-Josh
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html 6 2621
On Fri, Oct 15, 2004 at 10:56:06PM -0500, Josh Close wrote: FOR this_rSum IN EXECUTE this_sQuery LOOP this_iSum := this_rSum.isum; END LOOP; this_iTotal := this_iTotal + this_iSum;
[snip]
If I return this_iSum or this_iTotal in the loop, I get a value. If I return directly after the loop, the value is NULL. I can't figure out why it's doing this. The value comes out of the inside for loop just fine, but not the outer loop.
Add some RAISE INFO statements to print variables' values at key
places. I wonder if one of your SUMs is returning NULL, causing
your addition to evaluate to NULL. If so, then perhaps you should
use COALESCE to turn those NULLs into zeros.
If I've misunderstood the problem then please clarify.
--
Michael Fuhr http://www.fuhr.org/~mfuhr/
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
On Sat, 16 Oct 2004 00:59:34 -0600, Michael Fuhr <mi**@fuhr.or g> wrote: Add some RAISE INFO statements to print variables' values at key places. I wonder if one of your SUMs is returning NULL, causing your addition to evaluate to NULL. If so, then perhaps you should use COALESCE to turn those NULLs into zeros.
If I've misunderstood the problem then please clarify.
-- Michael Fuhr http://www.fuhr.org/~mfuhr/
Thanks. That's exactly what was happening. I'm still new to postgres.
This is actually the first function I've wrote. I didn't even know
about RAISE or COALESCE. I added them both and it works great now!
Thanks.
-Josh
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html
On Sat, Oct 16, 2004 at 09:30:32AM -0500, Josh Close wrote: On Sat, 16 Oct 2004 00:59:34 -0600, Michael Fuhr <mi**@fuhr.or g> wrote: Add some RAISE INFO statements to print variables' values at key places. I wonder if one of your SUMs is returning NULL, causing your addition to evaluate to NULL. If so, then perhaps you should use COALESCE to turn those NULLs into zeros.
Thanks. That's exactly what was happening. I'm still new to postgres.
Glad you got it working.
A question about your design: you appear to have a tblBatchHistory _X
table for each iId value in tblServers. Is there a reason for doing
that instead of having a single tblBatchHistory table with a foreign
key reference to tblServers?
--
Michael Fuhr http://www.fuhr.org/~mfuhr/
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
On Sat, 16 Oct 2004 10:20:35 -0600, Michael Fuhr <mi**@fuhr.or g> wrote: Glad you got it working.
A question about your design: you appear to have a tblBatchHistory _X table for each iId value in tblServers. Is there a reason for doing that instead of having a single tblBatchHistory table with a foreign key reference to tblServers?
I didn't write it, but it's to avoid locking. Each table is for a
different server. They are all writing at the same time, and I guess
it speeds up the inserts to have them in different tables. It makes
querying them a little bit more tricky, but it's not bad.
-Josh
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster
Josh Close <na****@gmail.c om> writes: I didn't write it, but it's to avoid locking. Each table is for a different server. They are all writing at the same time, and I guess it speeds up the inserts to have them in different tables.
Uh, not in Postgres. Perhaps you're thinking of another database system?
In fact I suspect it's slowing down your system somewhat.
--
greg
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match
On 17 Oct 2004 01:24:27 -0400, Greg Stark <gs*****@mit.ed u> wrote: Uh, not in Postgres. Perhaps you're thinking of another database system? In fact I suspect it's slowing down your system somewhat.
-- greg
So, there is no locking taking place during inserts at all? Or updates?
Also, where would I find some more basic info on stuff like this? In
the postgres docs?
-Josh
---------------------------(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Julie May |
last post by:
I have 90% of my function working and I know to get the next 10% it is justa matter of getting the quotations and the escaping quotations correct. Here is the portion that does work:
<working code>
-- Function: public.get_factory_ytd()
CREATE or replace FUNCTION public.get_factory_ytd() RETURNS setof record AS'
declare
ytd_record record;
d_date record;
begin
for d_date in select distinct delivery_date from load_info_table order...
|
by: Bill Moran |
last post by:
I've got a bit of a strange problem that's causing me some MAJOR
headaches.
I'm developing the server-side of a large database application in
PostgreSQL. This consists of a C daemon, and a LOT of stored
functions in the database.
I'm developing this in conjunction with another company, who is
developing the the client side. I've got a 7.4 server that I'm
developing on, and once a day I push my changes up to a common
|
by: Igor Shevchenko |
last post by:
Hi.
I've got a problem with using cursor in a plpgsql function. Cursor is created
via DECLARE, it's SELECT query has placeholders. I use PostgreSQL 7.4.2.
Here's a problematic plpgsql function:
create or replace function add_messages_to_folder(integer,refcursor) returns
integer security definer
as '
|
by: Mark Cave-Ayland |
last post by:
Hi everyone,
I'm trying to write a recursive plpgsql function in PostgreSQL 7.4.2
that given a tree node id (ictid) will return all the nodes below it in
the tree, one row per node. When I try and execute the function I get
the following error message:
CONTEXT: PL/pgSQL function "findsubcategories" line 15 at for over
select rows
PL/pgSQL function "findsubcategories" line 15 at for over select rows
|
by: Karl O. Pinc |
last post by:
I want to return multiple values, but not a set, only a single row,
from a
plpgsql function and I can't seem to get it to work. (I suppose I'd be
happy to return a set, but I can't seem to make that work either.
Anyway,
what's wrong with this?)
Version is:
$ rpm -q postgresql
| |
by: Karl O. Pinc |
last post by:
FYI, mostly. But I do have questions as to how to write code
that will continue to work in subsequent postgresql versions.
See code below.
begintest() uses EXIT to exit a BEGIN block from within nested
loops. No problem.
begintest2() simplifies this, omitting the nested loops.
Still no problem.
|
by: Daniel Martini |
last post by:
Hi all,
I'm currently coding some functions in plpgsql for generating
reports out of records in a table. Problem is: NULL values in
records make the complete function fail.
Here is a simple test case (original is more complex with a multi-
table query in the for qres in select... part):
create table test(
id serial,
|
by: twinklyblue |
last post by:
Hi thescripts team,
I would like to ask for your help once again regarding returning set of records in plpgsql. I followed examples presented in other sites regarding this function but my script doesnt work.
here's my code
create or replace function find_postal(varchar, varchar, varchar) returns setof record as
'
declare
ret_row record;
|
by: lazybee26 |
last post by:
Hello –
I’m trying to findout a better solution to this approach.
Currently if I have to return columns from multiple tables, I have to define my own TYPE and then return SETOF that type in the function. I’ve provided an example below.
Now, if I have to add a column to the select query, I have drop the existing TYPE definition, create a new TYPE with the new column added to it, and then modify the function sql to return this...
|
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...
|
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,...
| |
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |