1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
--
-- MISC
--
-- directory paths and dlsuffix are passed to us in environment variables
\getenv abs_srcdir PG_ABS_SRCDIR
\getenv abs_builddir PG_ABS_BUILDDIR
\getenv libdir PG_LIBDIR
\getenv dlsuffix PG_DLSUFFIX
\set regresslib :libdir '/regress' :dlsuffix
CREATE FUNCTION overpaid(emp)
RETURNS bool
AS :'regresslib'
LANGUAGE C STRICT;
CREATE FUNCTION reverse_name(name)
RETURNS name
AS :'regresslib'
LANGUAGE C STRICT;
--
-- BTREE
--
UPDATE onek
SET unique1 = onek.unique1 + 1;
UPDATE onek
SET unique1 = onek.unique1 - 1;
--
-- BTREE partial
--
-- UPDATE onek2
-- SET unique1 = onek2.unique1 + 1;
--UPDATE onek2
-- SET unique1 = onek2.unique1 - 1;
--
-- BTREE shutting out non-functional updates
--
-- the following two tests seem to take a long time on some
-- systems. This non-func update stuff needs to be examined
-- more closely. - jolly (2/22/96)
--
SELECT two, stringu1, ten, string4
INTO TABLE tmp
FROM onek;
UPDATE tmp
SET stringu1 = reverse_name(onek.stringu1)
FROM onek
WHERE onek.stringu1 = 'JBAAAA' and
onek.stringu1 = tmp.stringu1;
UPDATE tmp
SET stringu1 = reverse_name(onek2.stringu1)
FROM onek2
WHERE onek2.stringu1 = 'JCAAAA' and
onek2.stringu1 = tmp.stringu1;
DROP TABLE tmp;
--UPDATE person*
-- SET age = age + 1;
--UPDATE person*
-- SET age = age + 3
-- WHERE name = 'linda';
--
-- copy
--
\set filename :abs_builddir '/results/onek.data'
COPY onek TO :'filename';
CREATE TEMP TABLE onek_copy (LIKE onek);
COPY onek_copy FROM :'filename';
SELECT * FROM onek EXCEPT ALL SELECT * FROM onek_copy;
unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
(0 rows)
SELECT * FROM onek_copy EXCEPT ALL SELECT * FROM onek;
unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
(0 rows)
\set filename :abs_builddir '/results/stud_emp.data'
COPY BINARY stud_emp TO :'filename';
CREATE TEMP TABLE stud_emp_copy (LIKE stud_emp);
COPY BINARY stud_emp_copy FROM :'filename';
SELECT * FROM stud_emp_copy;
name | age | location | salary | manager | gpa | percent
-------+-----+------------+--------+---------+-----+---------
jeff | 23 | (8,7.7) | 600 | sharon | 3.5 |
cim | 30 | (10.5,4.7) | 400 | | 3.4 |
linda | 19 | (0.9,6.1) | 100 | | 2.9 |
(3 rows)
--
-- test data for postquel functions
--
CREATE TABLE hobbies_r (
name text,
person text
);
CREATE TABLE equipment_r (
name text,
hobby text
);
INSERT INTO hobbies_r (name, person)
SELECT 'posthacking', p.name
FROM person* p
WHERE p.name = 'mike' or p.name = 'jeff';
INSERT INTO hobbies_r (name, person)
SELECT 'basketball', p.name
FROM person p
WHERE p.name = 'joe' or p.name = 'sally';
INSERT INTO hobbies_r (name) VALUES ('skywalking');
INSERT INTO equipment_r (name, hobby) VALUES ('advil', 'posthacking');
INSERT INTO equipment_r (name, hobby) VALUES ('peet''s coffee', 'posthacking');
INSERT INTO equipment_r (name, hobby) VALUES ('hightops', 'basketball');
INSERT INTO equipment_r (name, hobby) VALUES ('guts', 'skywalking');
--
-- postquel functions
--
CREATE FUNCTION hobbies(person)
RETURNS setof hobbies_r
AS 'select * from hobbies_r where person = $1.name'
LANGUAGE SQL;
CREATE FUNCTION hobby_construct(text, text)
RETURNS hobbies_r
AS 'select $1 as name, $2 as hobby'
LANGUAGE SQL;
CREATE FUNCTION hobby_construct_named(name text, hobby text)
RETURNS hobbies_r
AS 'select name, hobby'
LANGUAGE SQL;
CREATE FUNCTION hobbies_by_name(hobbies_r.name%TYPE)
RETURNS hobbies_r.person%TYPE
AS 'select person from hobbies_r where name = $1'
LANGUAGE SQL;
NOTICE: type reference hobbies_r.name%TYPE converted to text
NOTICE: type reference hobbies_r.person%TYPE converted to text
CREATE FUNCTION equipment(hobbies_r)
RETURNS setof equipment_r
AS 'select * from equipment_r where hobby = $1.name'
LANGUAGE SQL;
CREATE FUNCTION equipment_named(hobby hobbies_r)
RETURNS setof equipment_r
AS 'select * from equipment_r where equipment_r.hobby = equipment_named.hobby.name'
LANGUAGE SQL;
CREATE FUNCTION equipment_named_ambiguous_1a(hobby hobbies_r)
RETURNS setof equipment_r
AS 'select * from equipment_r where hobby = equipment_named_ambiguous_1a.hobby.name'
LANGUAGE SQL;
CREATE FUNCTION equipment_named_ambiguous_1b(hobby hobbies_r)
RETURNS setof equipment_r
AS 'select * from equipment_r where equipment_r.hobby = hobby.name'
LANGUAGE SQL;
CREATE FUNCTION equipment_named_ambiguous_1c(hobby hobbies_r)
RETURNS setof equipment_r
AS 'select * from equipment_r where hobby = hobby.name'
LANGUAGE SQL;
CREATE FUNCTION equipment_named_ambiguous_2a(hobby text)
RETURNS setof equipment_r
AS 'select * from equipment_r where hobby = equipment_named_ambiguous_2a.hobby'
LANGUAGE SQL;
CREATE FUNCTION equipment_named_ambiguous_2b(hobby text)
RETURNS setof equipment_r
AS 'select * from equipment_r where equipment_r.hobby = hobby'
LANGUAGE SQL;
--
-- mike does post_hacking,
-- joe and sally play basketball, and
-- everyone else does nothing.
--
SELECT p.name, name(p.hobbies) FROM ONLY person p;
name | name
-------+-------------
mike | posthacking
joe | basketball
sally | basketball
(3 rows)
--
-- as above, but jeff also does post_hacking.
--
SELECT p.name, name(p.hobbies) FROM person* p;
name | name
-------+-------------
mike | posthacking
joe | basketball
sally | basketball
jeff | posthacking
(4 rows)
--
-- the next two queries demonstrate how functions generate bogus duplicates.
-- this is a "feature" ..
--
SELECT DISTINCT hobbies_r.name, name(hobbies_r.equipment) FROM hobbies_r
ORDER BY 1,2;
name | name
-------------+---------------
basketball | hightops
posthacking | advil
posthacking | peet's coffee
skywalking | guts
(4 rows)
SELECT hobbies_r.name, (hobbies_r.equipment).name FROM hobbies_r;
name | name
-------------+---------------
posthacking | advil
posthacking | peet's coffee
posthacking | advil
posthacking | peet's coffee
basketball | hightops
basketball | hightops
skywalking | guts
(7 rows)
--
-- mike needs advil and peet's coffee,
-- joe and sally need hightops, and
-- everyone else is fine.
--
SELECT p.name, name(p.hobbies), name(equipment(p.hobbies)) FROM ONLY person p;
name | name | name
-------+-------------+---------------
mike | posthacking | advil
mike | posthacking | peet's coffee
joe | basketball | hightops
sally | basketball | hightops
(4 rows)
--
-- as above, but jeff needs advil and peet's coffee as well.
--
SELECT p.name, name(p.hobbies), name(equipment(p.hobbies)) FROM person* p;
name | name | name
-------+-------------+---------------
mike | posthacking | advil
mike | posthacking | peet's coffee
joe | basketball | hightops
sally | basketball | hightops
jeff | posthacking | advil
jeff | posthacking | peet's coffee
(6 rows)
--
-- just like the last two, but make sure that the target list fixup and
-- unflattening is being done correctly.
--
SELECT name(equipment(p.hobbies)), p.name, name(p.hobbies) FROM ONLY person p;
name | name | name
---------------+-------+-------------
advil | mike | posthacking
peet's coffee | mike | posthacking
hightops | joe | basketball
hightops | sally | basketball
(4 rows)
SELECT (p.hobbies).equipment.name, p.name, name(p.hobbies) FROM person* p;
name | name | name
---------------+-------+-------------
advil | mike | posthacking
peet's coffee | mike | posthacking
hightops | joe | basketball
hightops | sally | basketball
advil | jeff | posthacking
peet's coffee | jeff | posthacking
(6 rows)
SELECT (p.hobbies).equipment.name, name(p.hobbies), p.name FROM ONLY person p;
name | name | name
---------------+-------------+-------
advil | posthacking | mike
peet's coffee | posthacking | mike
hightops | basketball | joe
hightops | basketball | sally
(4 rows)
SELECT name(equipment(p.hobbies)), name(p.hobbies), p.name FROM person* p;
name | name | name
---------------+-------------+-------
advil | posthacking | mike
peet's coffee | posthacking | mike
hightops | basketball | joe
hightops | basketball | sally
advil | posthacking | jeff
peet's coffee | posthacking | jeff
(6 rows)
SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer')));
name
------
guts
(1 row)
SELECT name(equipment(hobby_construct_named(text 'skywalking', text 'mer')));
name
------
guts
(1 row)
SELECT name(equipment_named(hobby_construct_named(text 'skywalking', text 'mer')));
name
------
guts
(1 row)
SELECT name(equipment_named_ambiguous_1a(hobby_construct_named(text 'skywalking', text 'mer')));
name
------
guts
(1 row)
SELECT name(equipment_named_ambiguous_1b(hobby_construct_named(text 'skywalking', text 'mer')));
name
------
guts
(1 row)
SELECT name(equipment_named_ambiguous_1c(hobby_construct_named(text 'skywalking', text 'mer')));
name
------
guts
(1 row)
SELECT name(equipment_named_ambiguous_2a(text 'skywalking'));
name
------
guts
(1 row)
SELECT name(equipment_named_ambiguous_2b(text 'skywalking'));
name
---------------
advil
peet's coffee
hightops
guts
(4 rows)
SELECT hobbies_by_name('basketball');
hobbies_by_name
-----------------
joe
(1 row)
SELECT name, overpaid(emp.*) FROM emp;
name | overpaid
--------+----------
sharon | t
sam | t
bill | t
jeff | f
cim | f
linda | f
(6 rows)
--
-- Try a few cases with SQL-spec row constructor expressions
--
SELECT * FROM equipment(ROW('skywalking', 'mer'));
name | hobby
------+------------
guts | skywalking
(1 row)
SELECT name(equipment(ROW('skywalking', 'mer')));
name
------
guts
(1 row)
SELECT *, name(equipment(h.*)) FROM hobbies_r h;
name | person | name
-------------+--------+---------------
posthacking | mike | advil
posthacking | mike | peet's coffee
posthacking | jeff | advil
posthacking | jeff | peet's coffee
basketball | joe | hightops
basketball | sally | hightops
skywalking | | guts
(7 rows)
SELECT *, (equipment(CAST((h.*) AS hobbies_r))).name FROM hobbies_r h;
name | person | name
-------------+--------+---------------
posthacking | mike | advil
posthacking | mike | peet's coffee
posthacking | jeff | advil
posthacking | jeff | peet's coffee
basketball | joe | hightops
basketball | sally | hightops
skywalking | | guts
(7 rows)
--
-- functional joins
--
--
-- instance rules
--
--
-- rewrite rules
--
|