Sample of user-define window function and other things

From: "Hitoshi Harada" <umi(dot)tanuki(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Sample of user-define window function and other things
Date: 2009-01-08 10:19:56
Message-ID: e08cc0400901080219v51e68a8qfa3732a421922bbb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Attached is a simple user-define window function as a test, which
calculates moving avg(). Writing this, I found that even with current
specification (i.e. limited frame clauses), user-define function can
emulate some kinds of moving frame for simple purpose.

CREATE OR REPLACE FUNCTION movavg(float8, int4) RETURNS float8 AS
'$libdir/moving', 'movavg'
LANGUAGE 'c' WINDOW;

SELECT depname, salary, movavg(salary::float8, 1) OVER (
PARTITION BY depname
ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)
FROM empsalary;

And miscellaneous questions:
- CREATE FUNCTION command accepts WINDOW keyword for non-c language
like plpgsql. Don't we need to throw error?
- Is WinGetFuncArgInPartition()'s argument mark_pos required? For the
newbies to window functions, it seems a bit confusing, but
WinSetMarkPos() looks enough for the purpose AFAIK.

Regards,

--
Hitoshi Harada

Attachment Content-Type Size
movavg.c application/octet-stream 2.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Rushabh Lathia 2009-01-08 10:23:21 Segmentation fault on PG 8.4 CVS head
Previous Message Heikki Linnakangas 2009-01-08 10:12:25 Re: Latest version of Hot Standby patch