Skip to content

Commit 41f862b

Browse files
committed
As mentioned above, here is my contrib/tablefunc patch. It includes
three functions which exercise the tablefunc API. show_all_settings() - returns the same information as SHOW ALL, but as a query result normal_rand(int numvals, float8 mean, float8 stddev, int seed) - returns a set of normally distributed float8 values - This routine implements Algorithm P (Polar method for normal deviates) from Knuth's _The_Art_of_Computer_Programming_, Volume 2, 3rd ed., pages 122-126. Knuth cites his source as "The polar method", G. E. P. Box, M. E. Muller, and G. Marsaglia, _Annals_Math,_Stat._ 29 (1958), 610-611. crosstabN(text sql) - returns a set of row_name plus N category value columns - crosstab2(), crosstab3(), and crosstab4() are defined for you, but you can create additional crosstab functions per directions in the README. Joe Conway
1 parent 23a8b77 commit 41f862b

File tree

7 files changed

+1081
-0
lines changed

7 files changed

+1081
-0
lines changed

contrib/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ string -
186186
C-like input/output conversion routines for strings
187187
by Massimo Dal Zotto <dz@cs.unitn.it>
188188

189+
tablefunc -
190+
Examples of tables returning functions
191+
by Joe Conway <mail@joeconway.com>
192+
189193
tips/apache_logging -
190194
Getting Apache to log to PostgreSQL
191195
by Terry Mackintosh <terry@terrym.com>

contrib/tablefunc/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
subdir = contrib/tablefunc
2+
top_builddir = ../..
3+
include $(top_builddir)/src/Makefile.global
4+
5+
MODULES = tablefunc
6+
DATA_built = tablefunc.sql
7+
DOCS = README.tablefunc
8+
9+
include $(top_srcdir)/contrib/contrib-global.mk

contrib/tablefunc/README.tablefunc

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/*
2+
* tablefunc
3+
*
4+
* Sample to demonstrate C functions which return setof scalar
5+
* and setof composite.
6+
* Joe Conway <mail@joeconway.com>
7+
*
8+
* Copyright 2002 by PostgreSQL Global Development Group
9+
*
10+
* Permission to use, copy, modify, and distribute this software and its
11+
* documentation for any purpose, without fee, and without a written agreement
12+
* is hereby granted, provided that the above copyright notice and this
13+
* paragraph and the following two paragraphs appear in all copies.
14+
*
15+
* IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
16+
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
17+
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
18+
* DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
19+
* POSSIBILITY OF SUCH DAMAGE.
20+
*
21+
* THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
22+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23+
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
24+
* ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
25+
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26+
*
27+
*/
28+
Version 0.1 (20 July, 2002):
29+
First release
30+
31+
Release Notes:
32+
33+
Version 0.1
34+
- initial release
35+
36+
Installation:
37+
Place these files in a directory called 'tablefunc' under 'contrib' in the
38+
PostgreSQL source tree. Then run:
39+
40+
make
41+
make install
42+
43+
You can use tablefunc.sql to create the functions in your database of choice, e.g.
44+
45+
psql -U postgres template1 < tablefunc.sql
46+
47+
installs following functions into database template1:
48+
49+
show_all_settings()
50+
- returns the same information as SHOW ALL, but as a query result
51+
52+
normal_rand(int numvals, float8 mean, float8 stddev, int seed)
53+
- returns a set of normally distributed float8 values
54+
55+
crosstabN(text sql)
56+
- returns a set of row_name plus N category value columns
57+
- crosstab2(), crosstab3(), and crosstab4() are defined for you,
58+
but you can create additional crosstab functions per the instructions
59+
in the documentation below.
60+
61+
Documentation
62+
==================================================================
63+
Name
64+
65+
show_all_settings() - returns the same information as SHOW ALL,
66+
but as a query result.
67+
68+
Synopsis
69+
70+
show_all_settings()
71+
72+
Inputs
73+
74+
none
75+
76+
Outputs
77+
78+
Returns setof tablefunc_config_settings which is defined by:
79+
CREATE VIEW tablefunc_config_settings AS
80+
SELECT
81+
''::TEXT AS name,
82+
''::TEXT AS setting;
83+
84+
Example usage
85+
86+
test=# select * from show_all_settings();
87+
name | setting
88+
-------------------------------+---------------------------------------
89+
australian_timezones | off
90+
authentication_timeout | 60
91+
checkpoint_segments | 3
92+
.
93+
.
94+
.
95+
wal_debug | 0
96+
wal_files | 0
97+
wal_sync_method | fdatasync
98+
(94 rows)
99+
100+
==================================================================
101+
Name
102+
103+
normal_rand(int, float8, float8, int) - returns a set of normally
104+
distributed float8 values
105+
106+
Synopsis
107+
108+
normal_rand(int numvals, float8 mean, float8 stddev, int seed)
109+
110+
Inputs
111+
112+
numvals
113+
the number of random values to be returned from the function
114+
115+
mean
116+
the mean of the normal distribution of values
117+
118+
stddev
119+
the standard deviation of the normal distribution of values
120+
121+
seed
122+
a seed value for the pseudo-random number generator
123+
124+
Outputs
125+
126+
Returns setof float8, where the returned set of random values are normally
127+
distributed (Gaussian distribution)
128+
129+
Example usage
130+
131+
test=# SELECT * FROM
132+
test=# normal_rand(1000, 5, 3, EXTRACT(SECONDS FROM CURRENT_TIME(0))::int);
133+
normal_rand
134+
----------------------
135+
1.56556322244898
136+
9.10040991424657
137+
5.36957140345079
138+
-0.369151492880995
139+
0.283600703686639
140+
.
141+
.
142+
.
143+
4.82992125404908
144+
9.71308014517282
145+
2.49639286969028
146+
(1000 rows)
147+
148+
Returns 1000 values with a mean of 5 and a standard deviation of 3.
149+
150+
==================================================================
151+
Name
152+
153+
crosstabN(text) - returns a set of row_name plus N category value columns
154+
155+
Synopsis
156+
157+
crosstabN(text sql)
158+
159+
Inputs
160+
161+
sql
162+
163+
A SQL statement which produces the source set of data. The SQL statement
164+
must return one row_name column, one category column, and one value
165+
column.
166+
167+
e.g. provided sql must produce a set something like:
168+
169+
row_name cat value
170+
----------+-------+-------
171+
row1 cat1 val1
172+
row1 cat2 val2
173+
row1 cat3 val3
174+
row1 cat4 val4
175+
row2 cat1 val5
176+
row2 cat2 val6
177+
row2 cat3 val7
178+
row2 cat4 val8
179+
180+
Outputs
181+
182+
Returns setof tablefunc_crosstab_N, which is defined by:
183+
184+
CREATE VIEW tablefunc_crosstab_N AS
185+
SELECT
186+
''::TEXT AS row_name,
187+
''::TEXT AS category_1,
188+
''::TEXT AS category_2,
189+
.
190+
.
191+
.
192+
''::TEXT AS category_N;
193+
194+
for the default installed functions, where N is 2, 3, or 4.
195+
196+
e.g. the provided crosstab2 function produces a set something like:
197+
<== values columns ==>
198+
row_name category_1 category_2
199+
---------+------------+------------
200+
row1 val1 val2
201+
row2 val5 val6
202+
203+
Notes
204+
205+
1. The sql result must be ordered by 1,2.
206+
207+
2. The number of values columns depends on the tuple description
208+
of the function's declared return type.
209+
210+
3. Missing values (i.e. not enough adjacent rows of same row_name to
211+
fill the number of result values columns) are filled in with nulls.
212+
213+
4. Extra values (i.e. too many adjacent rows of same row_name to fill
214+
the number of result values columns) are skipped.
215+
216+
5. Rows with all nulls in the values columns are skipped.
217+
218+
6. The installed defaults are for illustration purposes. You
219+
can create your own return types and functions based on the
220+
crosstab() function of the installed library.
221+
222+
The return type must have a first column that matches the data
223+
type of the sql set used as its source. The subsequent category
224+
columns must have the same data type as the value column of the
225+
sql result set.
226+
227+
Create a VIEW to define your return type, similar to the VIEWS
228+
in the provided installation script. Then define a unique function
229+
name accepting one text parameter and returning setof your_view_name.
230+
For example, if your source data produces row_names that are TEXT,
231+
and values that are FLOAT8, and you want 5 category columns:
232+
233+
CREATE VIEW my_crosstab_float8_5_cols AS
234+
SELECT
235+
''::TEXT AS row_name,
236+
0::FLOAT8 AS category_1,
237+
0::FLOAT8 AS category_2,
238+
0::FLOAT8 AS category_3,
239+
0::FLOAT8 AS category_4,
240+
0::FLOAT8 AS category_5;
241+
242+
CREATE OR REPLACE FUNCTION crosstab_float8_5_cols(text)
243+
RETURNS setof my_crosstab_float8_5_cols
244+
AS '$libdir/tablefunc','crosstab' LANGUAGE 'c' STABLE STRICT;
245+
246+
Example usage
247+
248+
create table ct(id serial, rowclass text, rowid text, attribute text, value text);
249+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att1','val1');
250+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att2','val2');
251+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att3','val3');
252+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att4','val4');
253+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att1','val5');
254+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att2','val6');
255+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att3','val7');
256+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att4','val8');
257+
258+
select * from crosstab3(
259+
'select rowid, attribute, value
260+
from ct
261+
where rowclass = ''group1''
262+
and (attribute = ''att2'' or attribute = ''att3'') order by 1,2;');
263+
264+
row_name | category_1 | category_2 | category_3
265+
----------+------------+------------+------------
266+
test1 | val2 | val3 |
267+
test2 | val6 | val7 |
268+
(2 rows)
269+
270+
==================================================================
271+
-- Joe Conway
272+

contrib/tablefunc/tablefunc-test.sql

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--
2+
-- show_all_settings()
3+
--
4+
SELECT * FROM show_all_settings();
5+
6+
--
7+
-- normal_rand()
8+
--
9+
SELECT * FROM normal_rand(100, 250, 5, EXTRACT(SECONDS FROM CURRENT_TIME(0))::int);
10+
11+
--
12+
-- crosstab()
13+
--
14+
create table ct(id serial, rowclass text, rowid text, attribute text, value text);
15+
16+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att1','val1');
17+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att2','val2');
18+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att3','val3');
19+
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att4','val4');
20+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att1','val5');
21+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att2','val6');
22+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att3','val7');
23+
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att4','val8');
24+
insert into ct(rowclass, rowid, attribute, value) values('group2','test3','att1','val1');
25+
insert into ct(rowclass, rowid, attribute, value) values('group2','test3','att2','val2');
26+
insert into ct(rowclass, rowid, attribute, value) values('group2','test3','att3','val3');
27+
insert into ct(rowclass, rowid, attribute, value) values('group2','test4','att1','val4');
28+
insert into ct(rowclass, rowid, attribute, value) values('group2','test4','att2','val5');
29+
insert into ct(rowclass, rowid, attribute, value) values('group2','test4','att3','val6');
30+
31+
select * from crosstab2('select rowid, attribute, value from ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') order by 1,2;');
32+
select * from crosstab3('select rowid, attribute, value from ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') order by 1,2;');
33+
select * from crosstab4('select rowid, attribute, value from ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') order by 1,2;');
34+
35+
select * from crosstab2('select rowid, attribute, value from ct where rowclass = ''group1'' order by 1,2;');
36+
select * from crosstab3('select rowid, attribute, value from ct where rowclass = ''group1'' order by 1,2;');
37+
select * from crosstab4('select rowid, attribute, value from ct where rowclass = ''group1'' order by 1,2;');
38+
39+
select * from crosstab2('select rowid, attribute, value from ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') order by 1,2;');
40+
select * from crosstab3('select rowid, attribute, value from ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') order by 1,2;');
41+
select * from crosstab4('select rowid, attribute, value from ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') order by 1,2;');
42+
43+
select * from crosstab2('select rowid, attribute, value from ct where rowclass = ''group2'' order by 1,2;');
44+
select * from crosstab3('select rowid, attribute, value from ct where rowclass = ''group2'' order by 1,2;');
45+
select * from crosstab4('select rowid, attribute, value from ct where rowclass = ''group2'' order by 1,2;');
46+
47+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy