6 -- A building with a modern TP cable installation where any
7 -- of the wall connectors can be used to plug in phones,
8 -- ethernet interfaces or local office hubs. The backside
9 -- of the wall connectors is wired to one of several patch-
10 -- fields in the building.
12 -- In the patchfields, there are hubs and all the slots
13 -- representing the wall connectors. In addition there are
14 -- slots that can represent a phone line from the central
17 -- Triggers ensure consistency of the patching information.
19 -- Functions are used to build up powerful views that let
20 -- you look behind the wall when looking at a patchfield
27 create unique index Room_rno on Room using btree (roomno bpchar_ops);
34 create unique index WSlot_name on WSlot using btree (slotname bpchar_ops);
39 create unique index PField_name on PField using btree (name text_ops);
46 create unique index PSlot_name on PSlot using btree (slotname bpchar_ops);
53 create unique index PLine_name on PLine using btree (slotname bpchar_ops);
59 create unique index Hub_name on Hub using btree (name bpchar_ops);
66 create unique index HSlot_name on HSlot using btree (slotname bpchar_ops);
67 create index HSlot_hubname on HSlot using btree (hubname bpchar_ops);
72 create unique index System_name on System using btree (name text_ops);
79 create unique index IFace_name on IFace using btree (slotname bpchar_ops);
85 create unique index PHone_name on PHone using btree (slotname bpchar_ops);
86 -- ************************************************************
88 -- * Trigger procedures and functions for the patchfield
91 -- ************************************************************
92 -- ************************************************************
93 -- * AFTER UPDATE on Room
94 -- * - If room no changes let wall slots follow
95 -- ************************************************************
96 create function tg_room_au() returns trigger as '
98 if new.roomno != old.roomno then
99 update WSlot set roomno = new.roomno where roomno = old.roomno;
104 create trigger tg_room_au after update
105 on Room for each row execute procedure tg_room_au();
106 -- ************************************************************
107 -- * AFTER DELETE on Room
108 -- * - delete wall slots in this room
109 -- ************************************************************
110 create function tg_room_ad() returns trigger as '
112 delete from WSlot where roomno = old.roomno;
116 create trigger tg_room_ad after delete
117 on Room for each row execute procedure tg_room_ad();
118 -- ************************************************************
119 -- * BEFORE INSERT or UPDATE on WSlot
120 -- * - Check that room exists
121 -- ************************************************************
122 create function tg_wslot_biu() returns trigger as $$
124 if count(*) = 0 from Room where roomno = new.roomno then
125 raise exception 'Room % does not exist', new.roomno;
130 create trigger tg_wslot_biu before insert or update
131 on WSlot for each row execute procedure tg_wslot_biu();
132 -- ************************************************************
133 -- * AFTER UPDATE on PField
134 -- * - Let PSlots of this field follow
135 -- ************************************************************
136 create function tg_pfield_au() returns trigger as '
138 if new.name != old.name then
139 update PSlot set pfname = new.name where pfname = old.name;
144 create trigger tg_pfield_au after update
145 on PField for each row execute procedure tg_pfield_au();
146 -- ************************************************************
147 -- * AFTER DELETE on PField
148 -- * - Remove all slots of this patchfield
149 -- ************************************************************
150 create function tg_pfield_ad() returns trigger as '
152 delete from PSlot where pfname = old.name;
156 create trigger tg_pfield_ad after delete
157 on PField for each row execute procedure tg_pfield_ad();
158 -- ************************************************************
159 -- * BEFORE INSERT or UPDATE on PSlot
160 -- * - Ensure that our patchfield does exist
161 -- ************************************************************
162 create function tg_pslot_biu() returns trigger as $proc$
167 select into pfrec * from PField where name = ps.pfname;
169 raise exception $$Patchfield "%" does not exist$$, ps.pfname;
173 $proc$ language plpgsql;
174 create trigger tg_pslot_biu before insert or update
175 on PSlot for each row execute procedure tg_pslot_biu();
176 -- ************************************************************
177 -- * AFTER UPDATE on System
178 -- * - If system name changes let interfaces follow
179 -- ************************************************************
180 create function tg_system_au() returns trigger as '
182 if new.name != old.name then
183 update IFace set sysname = new.name where sysname = old.name;
188 create trigger tg_system_au after update
189 on System for each row execute procedure tg_system_au();
190 -- ************************************************************
191 -- * BEFORE INSERT or UPDATE on IFace
192 -- * - set the slotname to IF.sysname.ifname
193 -- ************************************************************
194 create function tg_iface_biu() returns trigger as $$
199 select into sysrec * from system where name = new.sysname;
201 raise exception $q$system "%" does not exist$q$, new.sysname;
203 sname := 'IF.' || new.sysname;
204 sname := sname || '.';
205 sname := sname || new.ifname;
206 if length(sname) > 20 then
207 raise exception 'IFace slotname "%" too long (20 char max)', sname;
209 new.slotname := sname;
213 create trigger tg_iface_biu before insert or update
214 on IFace for each row execute procedure tg_iface_biu();
215 -- ************************************************************
216 -- * AFTER INSERT or UPDATE or DELETE on Hub
217 -- * - insert/delete/rename slots as required
218 -- ************************************************************
219 create function tg_hub_a() returns trigger as '
224 if tg_op = ''INSERT'' then
225 dummy := tg_hub_adjustslots(new.name, 0, new.nslots);
228 if tg_op = ''UPDATE'' then
229 if new.name != old.name then
230 update HSlot set hubname = new.name where hubname = old.name;
232 dummy := tg_hub_adjustslots(new.name, old.nslots, new.nslots);
235 if tg_op = ''DELETE'' then
236 dummy := tg_hub_adjustslots(old.name, old.nslots, 0);
241 create trigger tg_hub_a after insert or update or delete
242 on Hub for each row execute procedure tg_hub_a();
243 -- ************************************************************
244 -- * Support function to add/remove slots of Hub
245 -- ************************************************************
246 create function tg_hub_adjustslots(hname bpchar,
251 if newnslots = oldnslots then
254 if newnslots < oldnslots then
255 delete from HSlot where hubname = hname and slotno > newnslots;
258 for i in oldnslots + 1 .. newnslots loop
259 insert into HSlot (slotname, hubname, slotno, slotlink)
260 values (''HS.dummy'', hname, i, '''');
266 COMMENT ON FUNCTION tg_hub_adjustslots_wrong(bpchar, integer, integer) IS 'function with args';
267 ERROR: function tg_hub_adjustslots_wrong(character, integer, integer) does not exist
268 COMMENT ON FUNCTION tg_hub_adjustslots(bpchar, integer, integer) IS 'function with args';
269 COMMENT ON FUNCTION tg_hub_adjustslots(bpchar, integer, integer) IS NULL;
270 -- ************************************************************
271 -- * BEFORE INSERT or UPDATE on HSlot
272 -- * - prevent from manual manipulation
273 -- * - set the slotname to HS.hubname.slotno
274 -- ************************************************************
275 create function tg_hslot_biu() returns trigger as '
278 xname HSlot.slotname%TYPE;
281 select into hubrec * from Hub where name = new.hubname;
283 raise exception ''no manual manipulation of HSlot'';
285 if new.slotno < 1 or new.slotno > hubrec.nslots then
286 raise exception ''no manual manipulation of HSlot'';
288 if tg_op = ''UPDATE'' and new.hubname != old.hubname then
289 if count(*) > 0 from Hub where name = old.hubname then
290 raise exception ''no manual manipulation of HSlot'';
293 sname := ''HS.'' || trim(new.hubname);
294 sname := sname || ''.'';
295 sname := sname || new.slotno::text;
296 if length(sname) > 20 then
297 raise exception ''HSlot slotname "%" too long (20 char max)'', sname;
299 new.slotname := sname;
303 create trigger tg_hslot_biu before insert or update
304 on HSlot for each row execute procedure tg_hslot_biu();
305 -- ************************************************************
306 -- * BEFORE DELETE on HSlot
307 -- * - prevent from manual manipulation
308 -- ************************************************************
309 create function tg_hslot_bd() returns trigger as '
313 select into hubrec * from Hub where name = old.hubname;
317 if old.slotno > hubrec.nslots then
320 raise exception ''no manual manipulation of HSlot'';
323 create trigger tg_hslot_bd before delete
324 on HSlot for each row execute procedure tg_hslot_bd();
325 -- ************************************************************
326 -- * BEFORE INSERT on all slots
327 -- * - Check name prefix
328 -- ************************************************************
329 create function tg_chkslotname() returns trigger as '
331 if substr(new.slotname, 1, 2) != tg_argv[0] then
332 raise exception ''slotname must begin with %'', tg_argv[0];
337 create trigger tg_chkslotname before insert
338 on PSlot for each row execute procedure tg_chkslotname('PS');
339 create trigger tg_chkslotname before insert
340 on WSlot for each row execute procedure tg_chkslotname('WS');
341 create trigger tg_chkslotname before insert
342 on PLine for each row execute procedure tg_chkslotname('PL');
343 create trigger tg_chkslotname before insert
344 on IFace for each row execute procedure tg_chkslotname('IF');
345 create trigger tg_chkslotname before insert
346 on PHone for each row execute procedure tg_chkslotname('PH');
347 -- ************************************************************
348 -- * BEFORE INSERT or UPDATE on all slots with slotlink
349 -- * - Set slotlink to empty string if NULL value given
350 -- ************************************************************
351 create function tg_chkslotlink() returns trigger as '
353 if new.slotlink isnull then
354 new.slotlink := '''';
359 create trigger tg_chkslotlink before insert or update
360 on PSlot for each row execute procedure tg_chkslotlink();
361 create trigger tg_chkslotlink before insert or update
362 on WSlot for each row execute procedure tg_chkslotlink();
363 create trigger tg_chkslotlink before insert or update
364 on IFace for each row execute procedure tg_chkslotlink();
365 create trigger tg_chkslotlink before insert or update
366 on HSlot for each row execute procedure tg_chkslotlink();
367 create trigger tg_chkslotlink before insert or update
368 on PHone for each row execute procedure tg_chkslotlink();
369 -- ************************************************************
370 -- * BEFORE INSERT or UPDATE on all slots with backlink
371 -- * - Set backlink to empty string if NULL value given
372 -- ************************************************************
373 create function tg_chkbacklink() returns trigger as '
375 if new.backlink isnull then
376 new.backlink := '''';
381 create trigger tg_chkbacklink before insert or update
382 on PSlot for each row execute procedure tg_chkbacklink();
383 create trigger tg_chkbacklink before insert or update
384 on WSlot for each row execute procedure tg_chkbacklink();
385 create trigger tg_chkbacklink before insert or update
386 on PLine for each row execute procedure tg_chkbacklink();
387 -- ************************************************************
388 -- * BEFORE UPDATE on PSlot
389 -- * - do delete/insert instead of update if name changes
390 -- ************************************************************
391 create function tg_pslot_bu() returns trigger as '
393 if new.slotname != old.slotname then
394 delete from PSlot where slotname = old.slotname;
411 create trigger tg_pslot_bu before update
412 on PSlot for each row execute procedure tg_pslot_bu();
413 -- ************************************************************
414 -- * BEFORE UPDATE on WSlot
415 -- * - do delete/insert instead of update if name changes
416 -- ************************************************************
417 create function tg_wslot_bu() returns trigger as '
419 if new.slotname != old.slotname then
420 delete from WSlot where slotname = old.slotname;
437 create trigger tg_wslot_bu before update
438 on WSlot for each row execute procedure tg_Wslot_bu();
439 -- ************************************************************
440 -- * BEFORE UPDATE on PLine
441 -- * - do delete/insert instead of update if name changes
442 -- ************************************************************
443 create function tg_pline_bu() returns trigger as '
445 if new.slotname != old.slotname then
446 delete from PLine where slotname = old.slotname;
463 create trigger tg_pline_bu before update
464 on PLine for each row execute procedure tg_pline_bu();
465 -- ************************************************************
466 -- * BEFORE UPDATE on IFace
467 -- * - do delete/insert instead of update if name changes
468 -- ************************************************************
469 create function tg_iface_bu() returns trigger as '
471 if new.slotname != old.slotname then
472 delete from IFace where slotname = old.slotname;
489 create trigger tg_iface_bu before update
490 on IFace for each row execute procedure tg_iface_bu();
491 -- ************************************************************
492 -- * BEFORE UPDATE on HSlot
493 -- * - do delete/insert instead of update if name changes
494 -- ************************************************************
495 create function tg_hslot_bu() returns trigger as '
497 if new.slotname != old.slotname or new.hubname != old.hubname then
498 delete from HSlot where slotname = old.slotname;
515 create trigger tg_hslot_bu before update
516 on HSlot for each row execute procedure tg_hslot_bu();
517 -- ************************************************************
518 -- * BEFORE UPDATE on PHone
519 -- * - do delete/insert instead of update if name changes
520 -- ************************************************************
521 create function tg_phone_bu() returns trigger as '
523 if new.slotname != old.slotname then
524 delete from PHone where slotname = old.slotname;
539 create trigger tg_phone_bu before update
540 on PHone for each row execute procedure tg_phone_bu();
541 -- ************************************************************
542 -- * AFTER INSERT or UPDATE or DELETE on slot with backlink
543 -- * - Ensure that the opponent correctly points back to us
544 -- ************************************************************
545 create function tg_backlink_a() returns trigger as '
549 if tg_op = ''INSERT'' then
550 if new.backlink != '''' then
551 dummy := tg_backlink_set(new.backlink, new.slotname);
555 if tg_op = ''UPDATE'' then
556 if new.backlink != old.backlink then
557 if old.backlink != '''' then
558 dummy := tg_backlink_unset(old.backlink, old.slotname);
560 if new.backlink != '''' then
561 dummy := tg_backlink_set(new.backlink, new.slotname);
564 if new.slotname != old.slotname and new.backlink != '''' then
565 dummy := tg_slotlink_set(new.backlink, new.slotname);
570 if tg_op = ''DELETE'' then
571 if old.backlink != '''' then
572 dummy := tg_backlink_unset(old.backlink, old.slotname);
578 create trigger tg_backlink_a after insert or update or delete
579 on PSlot for each row execute procedure tg_backlink_a('PS');
580 create trigger tg_backlink_a after insert or update or delete
581 on WSlot for each row execute procedure tg_backlink_a('WS');
582 create trigger tg_backlink_a after insert or update or delete
583 on PLine for each row execute procedure tg_backlink_a('PL');
584 -- ************************************************************
585 -- * Support function to set the opponents backlink field
586 -- * if it does not already point to the requested slot
587 -- ************************************************************
588 create function tg_backlink_set(myname bpchar, blname bpchar)
595 mytype := substr(myname, 1, 2);
596 link := mytype || substr(blname, 1, 2);
597 if link = ''PLPL'' then
599 ''backlink between two phone lines does not make sense'';
601 if link in (''PLWS'', ''WSPL'') then
603 ''direct link of phone line to wall slot not permitted'';
605 if mytype = ''PS'' then
606 select into rec * from PSlot where slotname = myname;
608 raise exception ''% does not exist'', myname;
610 if rec.backlink != blname then
611 update PSlot set backlink = blname where slotname = myname;
615 if mytype = ''WS'' then
616 select into rec * from WSlot where slotname = myname;
618 raise exception ''% does not exist'', myname;
620 if rec.backlink != blname then
621 update WSlot set backlink = blname where slotname = myname;
625 if mytype = ''PL'' then
626 select into rec * from PLine where slotname = myname;
628 raise exception ''% does not exist'', myname;
630 if rec.backlink != blname then
631 update PLine set backlink = blname where slotname = myname;
635 raise exception ''illegal backlink beginning with %'', mytype;
638 -- ************************************************************
639 -- * Support function to clear out the backlink field if
640 -- * it still points to specific slot
641 -- ************************************************************
642 create function tg_backlink_unset(bpchar, bpchar)
650 mytype := substr(myname, 1, 2);
651 if mytype = ''PS'' then
652 select into rec * from PSlot where slotname = myname;
656 if rec.backlink = blname then
657 update PSlot set backlink = '''' where slotname = myname;
661 if mytype = ''WS'' then
662 select into rec * from WSlot where slotname = myname;
666 if rec.backlink = blname then
667 update WSlot set backlink = '''' where slotname = myname;
671 if mytype = ''PL'' then
672 select into rec * from PLine where slotname = myname;
676 if rec.backlink = blname then
677 update PLine set backlink = '''' where slotname = myname;
683 -- ************************************************************
684 -- * AFTER INSERT or UPDATE or DELETE on slot with slotlink
685 -- * - Ensure that the opponent correctly points back to us
686 -- ************************************************************
687 create function tg_slotlink_a() returns trigger as '
691 if tg_op = ''INSERT'' then
692 if new.slotlink != '''' then
693 dummy := tg_slotlink_set(new.slotlink, new.slotname);
697 if tg_op = ''UPDATE'' then
698 if new.slotlink != old.slotlink then
699 if old.slotlink != '''' then
700 dummy := tg_slotlink_unset(old.slotlink, old.slotname);
702 if new.slotlink != '''' then
703 dummy := tg_slotlink_set(new.slotlink, new.slotname);
706 if new.slotname != old.slotname and new.slotlink != '''' then
707 dummy := tg_slotlink_set(new.slotlink, new.slotname);
712 if tg_op = ''DELETE'' then
713 if old.slotlink != '''' then
714 dummy := tg_slotlink_unset(old.slotlink, old.slotname);
720 create trigger tg_slotlink_a after insert or update or delete
721 on PSlot for each row execute procedure tg_slotlink_a('PS');
722 create trigger tg_slotlink_a after insert or update or delete
723 on WSlot for each row execute procedure tg_slotlink_a('WS');
724 create trigger tg_slotlink_a after insert or update or delete
725 on IFace for each row execute procedure tg_slotlink_a('IF');
726 create trigger tg_slotlink_a after insert or update or delete
727 on HSlot for each row execute procedure tg_slotlink_a('HS');
728 create trigger tg_slotlink_a after insert or update or delete
729 on PHone for each row execute procedure tg_slotlink_a('PH');
730 -- ************************************************************
731 -- * Support function to set the opponents slotlink field
732 -- * if it does not already point to the requested slot
733 -- ************************************************************
734 create function tg_slotlink_set(bpchar, bpchar)
743 mytype := substr(myname, 1, 2);
744 link := mytype || substr(blname, 1, 2);
745 if link = ''PHPH'' then
747 ''slotlink between two phones does not make sense'';
749 if link in (''PHHS'', ''HSPH'') then
751 ''link of phone to hub does not make sense'';
753 if link in (''PHIF'', ''IFPH'') then
755 ''link of phone to hub does not make sense'';
757 if link in (''PSWS'', ''WSPS'') then
759 ''slotlink from patchslot to wallslot not permitted'';
761 if mytype = ''PS'' then
762 select into rec * from PSlot where slotname = myname;
764 raise exception ''% does not exist'', myname;
766 if rec.slotlink != blname then
767 update PSlot set slotlink = blname where slotname = myname;
771 if mytype = ''WS'' then
772 select into rec * from WSlot where slotname = myname;
774 raise exception ''% does not exist'', myname;
776 if rec.slotlink != blname then
777 update WSlot set slotlink = blname where slotname = myname;
781 if mytype = ''IF'' then
782 select into rec * from IFace where slotname = myname;
784 raise exception ''% does not exist'', myname;
786 if rec.slotlink != blname then
787 update IFace set slotlink = blname where slotname = myname;
791 if mytype = ''HS'' then
792 select into rec * from HSlot where slotname = myname;
794 raise exception ''% does not exist'', myname;
796 if rec.slotlink != blname then
797 update HSlot set slotlink = blname where slotname = myname;
801 if mytype = ''PH'' then
802 select into rec * from PHone where slotname = myname;
804 raise exception ''% does not exist'', myname;
806 if rec.slotlink != blname then
807 update PHone set slotlink = blname where slotname = myname;
811 raise exception ''illegal slotlink beginning with %'', mytype;
814 -- ************************************************************
815 -- * Support function to clear out the slotlink field if
816 -- * it still points to specific slot
817 -- ************************************************************
818 create function tg_slotlink_unset(bpchar, bpchar)
826 mytype := substr(myname, 1, 2);
827 if mytype = ''PS'' then
828 select into rec * from PSlot where slotname = myname;
832 if rec.slotlink = blname then
833 update PSlot set slotlink = '''' where slotname = myname;
837 if mytype = ''WS'' then
838 select into rec * from WSlot where slotname = myname;
842 if rec.slotlink = blname then
843 update WSlot set slotlink = '''' where slotname = myname;
847 if mytype = ''IF'' then
848 select into rec * from IFace where slotname = myname;
852 if rec.slotlink = blname then
853 update IFace set slotlink = '''' where slotname = myname;
857 if mytype = ''HS'' then
858 select into rec * from HSlot where slotname = myname;
862 if rec.slotlink = blname then
863 update HSlot set slotlink = '''' where slotname = myname;
867 if mytype = ''PH'' then
868 select into rec * from PHone where slotname = myname;
872 if rec.slotlink = blname then
873 update PHone set slotlink = '''' where slotname = myname;
879 -- ************************************************************
880 -- * Describe the backside of a patchfield slot
881 -- ************************************************************
882 create function pslot_backlink_view(bpchar)
890 select into rec * from PSlot where slotname = $1;
894 if rec.backlink = '''' then
897 bltype := substr(rec.backlink, 1, 2);
898 if bltype = ''PL'' then
902 select into rec * from PLine where slotname = "outer".rec.backlink;
903 retval := ''Phone line '' || trim(rec.phonenumber);
904 if rec.comment != '''' then
905 retval := retval || '' ('';
906 retval := retval || rec.comment;
907 retval := retval || '')'';
912 if bltype = ''WS'' then
913 select into rec * from WSlot where slotname = rec.backlink;
914 retval := trim(rec.slotname) || '' in room '';
915 retval := retval || trim(rec.roomno);
916 retval := retval || '' -> '';
917 return retval || wslot_slotlink_view(rec.slotname);
922 -- ************************************************************
923 -- * Describe the front of a patchfield slot
924 -- ************************************************************
925 create function pslot_slotlink_view(bpchar)
932 select into psrec * from PSlot where slotname = $1;
936 if psrec.slotlink = '''' then
939 sltype := substr(psrec.slotlink, 1, 2);
940 if sltype = ''PS'' then
941 retval := trim(psrec.slotlink) || '' -> '';
942 return retval || pslot_backlink_view(psrec.slotlink);
944 if sltype = ''HS'' then
945 retval := comment from Hub H, HSlot HS
946 where HS.slotname = psrec.slotlink
947 and H.name = HS.hubname;
948 retval := retval || '' slot '';
949 retval := retval || slotno::text from HSlot
950 where slotname = psrec.slotlink;
953 return psrec.slotlink;
956 -- ************************************************************
957 -- * Describe the front of a wall connector slot
958 -- ************************************************************
959 create function wslot_slotlink_view(bpchar)
966 select into rec * from WSlot where slotname = $1;
970 if rec.slotlink = '''' then
973 sltype := substr(rec.slotlink, 1, 2);
974 if sltype = ''PH'' then
975 select into rec * from PHone where slotname = rec.slotlink;
976 retval := ''Phone '' || trim(rec.slotname);
977 if rec.comment != '''' then
978 retval := retval || '' ('';
979 retval := retval || rec.comment;
980 retval := retval || '')'';
984 if sltype = ''IF'' then
986 syrow System%RowType;
989 select into ifrow * from IFace where slotname = rec.slotlink;
990 select into syrow * from System where name = ifrow.sysname;
991 retval := syrow.name || '' IF '';
992 retval := retval || ifrow.ifname;
993 if syrow.comment != '''' then
994 retval := retval || '' ('';
995 retval := retval || syrow.comment;
996 retval := retval || '')'';
1001 return rec.slotlink;
1004 -- ************************************************************
1005 -- * View of a patchfield describing backside and patches
1006 -- ************************************************************
1007 create view Pfield_v1 as select PF.pfname, PF.slotname,
1008 pslot_backlink_view(PF.slotname) as backside,
1009 pslot_slotlink_view(PF.slotname) as patch
1012 -- First we build the house - so we create the rooms
1014 insert into Room values ('001', 'Entrance');
1015 insert into Room values ('002', 'Office');
1016 insert into Room values ('003', 'Office');
1017 insert into Room values ('004', 'Technical');
1018 insert into Room values ('101', 'Office');
1019 insert into Room values ('102', 'Conference');
1020 insert into Room values ('103', 'Restroom');
1021 insert into Room values ('104', 'Technical');
1022 insert into Room values ('105', 'Office');
1023 insert into Room values ('106', 'Office');
1025 -- Second we install the wall connectors
1027 insert into WSlot values ('WS.001.1a', '001', '', '');
1028 insert into WSlot values ('WS.001.1b', '001', '', '');
1029 insert into WSlot values ('WS.001.2a', '001', '', '');
1030 insert into WSlot values ('WS.001.2b', '001', '', '');
1031 insert into WSlot values ('WS.001.3a', '001', '', '');
1032 insert into WSlot values ('WS.001.3b', '001', '', '');
1033 insert into WSlot values ('WS.002.1a', '002', '', '');
1034 insert into WSlot values ('WS.002.1b', '002', '', '');
1035 insert into WSlot values ('WS.002.2a', '002', '', '');
1036 insert into WSlot values ('WS.002.2b', '002', '', '');
1037 insert into WSlot values ('WS.002.3a', '002', '', '');
1038 insert into WSlot values ('WS.002.3b', '002', '', '');
1039 insert into WSlot values ('WS.003.1a', '003', '', '');
1040 insert into WSlot values ('WS.003.1b', '003', '', '');
1041 insert into WSlot values ('WS.003.2a', '003', '', '');
1042 insert into WSlot values ('WS.003.2b', '003', '', '');
1043 insert into WSlot values ('WS.003.3a', '003', '', '');
1044 insert into WSlot values ('WS.003.3b', '003', '', '');
1045 insert into WSlot values ('WS.101.1a', '101', '', '');
1046 insert into WSlot values ('WS.101.1b', '101', '', '');
1047 insert into WSlot values ('WS.101.2a', '101', '', '');
1048 insert into WSlot values ('WS.101.2b', '101', '', '');
1049 insert into WSlot values ('WS.101.3a', '101', '', '');
1050 insert into WSlot values ('WS.101.3b', '101', '', '');
1051 insert into WSlot values ('WS.102.1a', '102', '', '');
1052 insert into WSlot values ('WS.102.1b', '102', '', '');
1053 insert into WSlot values ('WS.102.2a', '102', '', '');
1054 insert into WSlot values ('WS.102.2b', '102', '', '');
1055 insert into WSlot values ('WS.102.3a', '102', '', '');
1056 insert into WSlot values ('WS.102.3b', '102', '', '');
1057 insert into WSlot values ('WS.105.1a', '105', '', '');
1058 insert into WSlot values ('WS.105.1b', '105', '', '');
1059 insert into WSlot values ('WS.105.2a', '105', '', '');
1060 insert into WSlot values ('WS.105.2b', '105', '', '');
1061 insert into WSlot values ('WS.105.3a', '105', '', '');
1062 insert into WSlot values ('WS.105.3b', '105', '', '');
1063 insert into WSlot values ('WS.106.1a', '106', '', '');
1064 insert into WSlot values ('WS.106.1b', '106', '', '');
1065 insert into WSlot values ('WS.106.2a', '106', '', '');
1066 insert into WSlot values ('WS.106.2b', '106', '', '');
1067 insert into WSlot values ('WS.106.3a', '106', '', '');
1068 insert into WSlot values ('WS.106.3b', '106', '', '');
1070 -- Now create the patch fields and their slots
1072 insert into PField values ('PF0_1', 'Wallslots basement');
1074 -- The cables for these will be made later, so they are unconnected for now
1076 insert into PSlot values ('PS.base.a1', 'PF0_1', '', '');
1077 insert into PSlot values ('PS.base.a2', 'PF0_1', '', '');
1078 insert into PSlot values ('PS.base.a3', 'PF0_1', '', '');
1079 insert into PSlot values ('PS.base.a4', 'PF0_1', '', '');
1080 insert into PSlot values ('PS.base.a5', 'PF0_1', '', '');
1081 insert into PSlot values ('PS.base.a6', 'PF0_1', '', '');
1083 -- These are already wired to the wall connectors
1085 insert into PSlot values ('PS.base.b1', 'PF0_1', '', 'WS.002.1a');
1086 insert into PSlot values ('PS.base.b2', 'PF0_1', '', 'WS.002.1b');
1087 insert into PSlot values ('PS.base.b3', 'PF0_1', '', 'WS.002.2a');
1088 insert into PSlot values ('PS.base.b4', 'PF0_1', '', 'WS.002.2b');
1089 insert into PSlot values ('PS.base.b5', 'PF0_1', '', 'WS.002.3a');
1090 insert into PSlot values ('PS.base.b6', 'PF0_1', '', 'WS.002.3b');
1091 insert into PSlot values ('PS.base.c1', 'PF0_1', '', 'WS.003.1a');
1092 insert into PSlot values ('PS.base.c2', 'PF0_1', '', 'WS.003.1b');
1093 insert into PSlot values ('PS.base.c3', 'PF0_1', '', 'WS.003.2a');
1094 insert into PSlot values ('PS.base.c4', 'PF0_1', '', 'WS.003.2b');
1095 insert into PSlot values ('PS.base.c5', 'PF0_1', '', 'WS.003.3a');
1096 insert into PSlot values ('PS.base.c6', 'PF0_1', '', 'WS.003.3b');
1098 -- This patchfield will be renamed later into PF0_2 - so its
1099 -- slots references in pfname should follow
1101 insert into PField values ('PF0_X', 'Phonelines basement');
1102 insert into PSlot values ('PS.base.ta1', 'PF0_X', '', '');
1103 insert into PSlot values ('PS.base.ta2', 'PF0_X', '', '');
1104 insert into PSlot values ('PS.base.ta3', 'PF0_X', '', '');
1105 insert into PSlot values ('PS.base.ta4', 'PF0_X', '', '');
1106 insert into PSlot values ('PS.base.ta5', 'PF0_X', '', '');
1107 insert into PSlot values ('PS.base.ta6', 'PF0_X', '', '');
1108 insert into PSlot values ('PS.base.tb1', 'PF0_X', '', '');
1109 insert into PSlot values ('PS.base.tb2', 'PF0_X', '', '');
1110 insert into PSlot values ('PS.base.tb3', 'PF0_X', '', '');
1111 insert into PSlot values ('PS.base.tb4', 'PF0_X', '', '');
1112 insert into PSlot values ('PS.base.tb5', 'PF0_X', '', '');
1113 insert into PSlot values ('PS.base.tb6', 'PF0_X', '', '');
1114 insert into PField values ('PF1_1', 'Wallslots first floor');
1115 insert into PSlot values ('PS.first.a1', 'PF1_1', '', 'WS.101.1a');
1116 insert into PSlot values ('PS.first.a2', 'PF1_1', '', 'WS.101.1b');
1117 insert into PSlot values ('PS.first.a3', 'PF1_1', '', 'WS.101.2a');
1118 insert into PSlot values ('PS.first.a4', 'PF1_1', '', 'WS.101.2b');
1119 insert into PSlot values ('PS.first.a5', 'PF1_1', '', 'WS.101.3a');
1120 insert into PSlot values ('PS.first.a6', 'PF1_1', '', 'WS.101.3b');
1121 insert into PSlot values ('PS.first.b1', 'PF1_1', '', 'WS.102.1a');
1122 insert into PSlot values ('PS.first.b2', 'PF1_1', '', 'WS.102.1b');
1123 insert into PSlot values ('PS.first.b3', 'PF1_1', '', 'WS.102.2a');
1124 insert into PSlot values ('PS.first.b4', 'PF1_1', '', 'WS.102.2b');
1125 insert into PSlot values ('PS.first.b5', 'PF1_1', '', 'WS.102.3a');
1126 insert into PSlot values ('PS.first.b6', 'PF1_1', '', 'WS.102.3b');
1127 insert into PSlot values ('PS.first.c1', 'PF1_1', '', 'WS.105.1a');
1128 insert into PSlot values ('PS.first.c2', 'PF1_1', '', 'WS.105.1b');
1129 insert into PSlot values ('PS.first.c3', 'PF1_1', '', 'WS.105.2a');
1130 insert into PSlot values ('PS.first.c4', 'PF1_1', '', 'WS.105.2b');
1131 insert into PSlot values ('PS.first.c5', 'PF1_1', '', 'WS.105.3a');
1132 insert into PSlot values ('PS.first.c6', 'PF1_1', '', 'WS.105.3b');
1133 insert into PSlot values ('PS.first.d1', 'PF1_1', '', 'WS.106.1a');
1134 insert into PSlot values ('PS.first.d2', 'PF1_1', '', 'WS.106.1b');
1135 insert into PSlot values ('PS.first.d3', 'PF1_1', '', 'WS.106.2a');
1136 insert into PSlot values ('PS.first.d4', 'PF1_1', '', 'WS.106.2b');
1137 insert into PSlot values ('PS.first.d5', 'PF1_1', '', 'WS.106.3a');
1138 insert into PSlot values ('PS.first.d6', 'PF1_1', '', 'WS.106.3b');
1140 -- Now we wire the wall connectors 1a-2a in room 001 to the
1141 -- patchfield. In the second update we make an error, and
1144 update PSlot set backlink = 'WS.001.1a' where slotname = 'PS.base.a1';
1145 update PSlot set backlink = 'WS.001.1b' where slotname = 'PS.base.a3';
1146 select * from WSlot where roomno = '001' order by slotname;
1147 slotname | roomno | slotlink | backlink
1148 ----------------------+----------+----------------------+----------------------
1149 WS.001.1a | 001 | | PS.base.a1
1150 WS.001.1b | 001 | | PS.base.a3
1157 select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
1158 slotname | pfname | slotlink | backlink
1159 ----------------------+--------+----------------------+----------------------
1160 PS.base.a1 | PF0_1 | | WS.001.1a
1161 PS.base.a2 | PF0_1 | |
1162 PS.base.a3 | PF0_1 | | WS.001.1b
1163 PS.base.a4 | PF0_1 | |
1164 PS.base.a5 | PF0_1 | |
1165 PS.base.a6 | PF0_1 | |
1168 update PSlot set backlink = 'WS.001.2a' where slotname = 'PS.base.a3';
1169 select * from WSlot where roomno = '001' order by slotname;
1170 slotname | roomno | slotlink | backlink
1171 ----------------------+----------+----------------------+----------------------
1172 WS.001.1a | 001 | | PS.base.a1
1174 WS.001.2a | 001 | | PS.base.a3
1180 select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
1181 slotname | pfname | slotlink | backlink
1182 ----------------------+--------+----------------------+----------------------
1183 PS.base.a1 | PF0_1 | | WS.001.1a
1184 PS.base.a2 | PF0_1 | |
1185 PS.base.a3 | PF0_1 | | WS.001.2a
1186 PS.base.a4 | PF0_1 | |
1187 PS.base.a5 | PF0_1 | |
1188 PS.base.a6 | PF0_1 | |
1191 update PSlot set backlink = 'WS.001.1b' where slotname = 'PS.base.a2';
1192 select * from WSlot where roomno = '001' order by slotname;
1193 slotname | roomno | slotlink | backlink
1194 ----------------------+----------+----------------------+----------------------
1195 WS.001.1a | 001 | | PS.base.a1
1196 WS.001.1b | 001 | | PS.base.a2
1197 WS.001.2a | 001 | | PS.base.a3
1203 select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
1204 slotname | pfname | slotlink | backlink
1205 ----------------------+--------+----------------------+----------------------
1206 PS.base.a1 | PF0_1 | | WS.001.1a
1207 PS.base.a2 | PF0_1 | | WS.001.1b
1208 PS.base.a3 | PF0_1 | | WS.001.2a
1209 PS.base.a4 | PF0_1 | |
1210 PS.base.a5 | PF0_1 | |
1211 PS.base.a6 | PF0_1 | |
1215 -- Same procedure for 2b-3b but this time updating the WSlot instead
1216 -- of the PSlot. Due to the triggers the result is the same:
1217 -- WSlot and corresponding PSlot point to each other.
1219 update WSlot set backlink = 'PS.base.a4' where slotname = 'WS.001.2b';
1220 update WSlot set backlink = 'PS.base.a6' where slotname = 'WS.001.3a';
1221 select * from WSlot where roomno = '001' order by slotname;
1222 slotname | roomno | slotlink | backlink
1223 ----------------------+----------+----------------------+----------------------
1224 WS.001.1a | 001 | | PS.base.a1
1225 WS.001.1b | 001 | | PS.base.a2
1226 WS.001.2a | 001 | | PS.base.a3
1227 WS.001.2b | 001 | | PS.base.a4
1228 WS.001.3a | 001 | | PS.base.a6
1232 select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
1233 slotname | pfname | slotlink | backlink
1234 ----------------------+--------+----------------------+----------------------
1235 PS.base.a1 | PF0_1 | | WS.001.1a
1236 PS.base.a2 | PF0_1 | | WS.001.1b
1237 PS.base.a3 | PF0_1 | | WS.001.2a
1238 PS.base.a4 | PF0_1 | | WS.001.2b
1239 PS.base.a5 | PF0_1 | |
1240 PS.base.a6 | PF0_1 | | WS.001.3a
1243 update WSlot set backlink = 'PS.base.a6' where slotname = 'WS.001.3b';
1244 select * from WSlot where roomno = '001' order by slotname;
1245 slotname | roomno | slotlink | backlink
1246 ----------------------+----------+----------------------+----------------------
1247 WS.001.1a | 001 | | PS.base.a1
1248 WS.001.1b | 001 | | PS.base.a2
1249 WS.001.2a | 001 | | PS.base.a3
1250 WS.001.2b | 001 | | PS.base.a4
1252 WS.001.3b | 001 | | PS.base.a6
1255 select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
1256 slotname | pfname | slotlink | backlink
1257 ----------------------+--------+----------------------+----------------------
1258 PS.base.a1 | PF0_1 | | WS.001.1a
1259 PS.base.a2 | PF0_1 | | WS.001.1b
1260 PS.base.a3 | PF0_1 | | WS.001.2a
1261 PS.base.a4 | PF0_1 | | WS.001.2b
1262 PS.base.a5 | PF0_1 | |
1263 PS.base.a6 | PF0_1 | | WS.001.3b
1266 update WSlot set backlink = 'PS.base.a5' where slotname = 'WS.001.3a';
1267 select * from WSlot where roomno = '001' order by slotname;
1268 slotname | roomno | slotlink | backlink
1269 ----------------------+----------+----------------------+----------------------
1270 WS.001.1a | 001 | | PS.base.a1
1271 WS.001.1b | 001 | | PS.base.a2
1272 WS.001.2a | 001 | | PS.base.a3
1273 WS.001.2b | 001 | | PS.base.a4
1274 WS.001.3a | 001 | | PS.base.a5
1275 WS.001.3b | 001 | | PS.base.a6
1278 select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
1279 slotname | pfname | slotlink | backlink
1280 ----------------------+--------+----------------------+----------------------
1281 PS.base.a1 | PF0_1 | | WS.001.1a
1282 PS.base.a2 | PF0_1 | | WS.001.1b
1283 PS.base.a3 | PF0_1 | | WS.001.2a
1284 PS.base.a4 | PF0_1 | | WS.001.2b
1285 PS.base.a5 | PF0_1 | | WS.001.3a
1286 PS.base.a6 | PF0_1 | | WS.001.3b
1289 insert into PField values ('PF1_2', 'Phonelines first floor');
1290 insert into PSlot values ('PS.first.ta1', 'PF1_2', '', '');
1291 insert into PSlot values ('PS.first.ta2', 'PF1_2', '', '');
1292 insert into PSlot values ('PS.first.ta3', 'PF1_2', '', '');
1293 insert into PSlot values ('PS.first.ta4', 'PF1_2', '', '');
1294 insert into PSlot values ('PS.first.ta5', 'PF1_2', '', '');
1295 insert into PSlot values ('PS.first.ta6', 'PF1_2', '', '');
1296 insert into PSlot values ('PS.first.tb1', 'PF1_2', '', '');
1297 insert into PSlot values ('PS.first.tb2', 'PF1_2', '', '');
1298 insert into PSlot values ('PS.first.tb3', 'PF1_2', '', '');
1299 insert into PSlot values ('PS.first.tb4', 'PF1_2', '', '');
1300 insert into PSlot values ('PS.first.tb5', 'PF1_2', '', '');
1301 insert into PSlot values ('PS.first.tb6', 'PF1_2', '', '');
1303 -- Fix the wrong name for patchfield PF0_2
1305 update PField set name = 'PF0_2' where name = 'PF0_X';
1306 select * from PSlot order by slotname;
1307 slotname | pfname | slotlink | backlink
1308 ----------------------+--------+----------------------+----------------------
1309 PS.base.a1 | PF0_1 | | WS.001.1a
1310 PS.base.a2 | PF0_1 | | WS.001.1b
1311 PS.base.a3 | PF0_1 | | WS.001.2a
1312 PS.base.a4 | PF0_1 | | WS.001.2b
1313 PS.base.a5 | PF0_1 | | WS.001.3a
1314 PS.base.a6 | PF0_1 | | WS.001.3b
1315 PS.base.b1 | PF0_1 | | WS.002.1a
1316 PS.base.b2 | PF0_1 | | WS.002.1b
1317 PS.base.b3 | PF0_1 | | WS.002.2a
1318 PS.base.b4 | PF0_1 | | WS.002.2b
1319 PS.base.b5 | PF0_1 | | WS.002.3a
1320 PS.base.b6 | PF0_1 | | WS.002.3b
1321 PS.base.c1 | PF0_1 | | WS.003.1a
1322 PS.base.c2 | PF0_1 | | WS.003.1b
1323 PS.base.c3 | PF0_1 | | WS.003.2a
1324 PS.base.c4 | PF0_1 | | WS.003.2b
1325 PS.base.c5 | PF0_1 | | WS.003.3a
1326 PS.base.c6 | PF0_1 | | WS.003.3b
1327 PS.base.ta1 | PF0_2 | |
1328 PS.base.ta2 | PF0_2 | |
1329 PS.base.ta3 | PF0_2 | |
1330 PS.base.ta4 | PF0_2 | |
1331 PS.base.ta5 | PF0_2 | |
1332 PS.base.ta6 | PF0_2 | |
1333 PS.base.tb1 | PF0_2 | |
1334 PS.base.tb2 | PF0_2 | |
1335 PS.base.tb3 | PF0_2 | |
1336 PS.base.tb4 | PF0_2 | |
1337 PS.base.tb5 | PF0_2 | |
1338 PS.base.tb6 | PF0_2 | |
1339 PS.first.a1 | PF1_1 | | WS.101.1a
1340 PS.first.a2 | PF1_1 | | WS.101.1b
1341 PS.first.a3 | PF1_1 | | WS.101.2a
1342 PS.first.a4 | PF1_1 | | WS.101.2b
1343 PS.first.a5 | PF1_1 | | WS.101.3a
1344 PS.first.a6 | PF1_1 | | WS.101.3b
1345 PS.first.b1 | PF1_1 | | WS.102.1a
1346 PS.first.b2 | PF1_1 | | WS.102.1b
1347 PS.first.b3 | PF1_1 | | WS.102.2a
1348 PS.first.b4 | PF1_1 | | WS.102.2b
1349 PS.first.b5 | PF1_1 | | WS.102.3a
1350 PS.first.b6 | PF1_1 | | WS.102.3b
1351 PS.first.c1 | PF1_1 | | WS.105.1a
1352 PS.first.c2 | PF1_1 | | WS.105.1b
1353 PS.first.c3 | PF1_1 | | WS.105.2a
1354 PS.first.c4 | PF1_1 | | WS.105.2b
1355 PS.first.c5 | PF1_1 | | WS.105.3a
1356 PS.first.c6 | PF1_1 | | WS.105.3b
1357 PS.first.d1 | PF1_1 | | WS.106.1a
1358 PS.first.d2 | PF1_1 | | WS.106.1b
1359 PS.first.d3 | PF1_1 | | WS.106.2a
1360 PS.first.d4 | PF1_1 | | WS.106.2b
1361 PS.first.d5 | PF1_1 | | WS.106.3a
1362 PS.first.d6 | PF1_1 | | WS.106.3b
1363 PS.first.ta1 | PF1_2 | |
1364 PS.first.ta2 | PF1_2 | |
1365 PS.first.ta3 | PF1_2 | |
1366 PS.first.ta4 | PF1_2 | |
1367 PS.first.ta5 | PF1_2 | |
1368 PS.first.ta6 | PF1_2 | |
1369 PS.first.tb1 | PF1_2 | |
1370 PS.first.tb2 | PF1_2 | |
1371 PS.first.tb3 | PF1_2 | |
1372 PS.first.tb4 | PF1_2 | |
1373 PS.first.tb5 | PF1_2 | |
1374 PS.first.tb6 | PF1_2 | |
1377 select * from WSlot order by slotname;
1378 slotname | roomno | slotlink | backlink
1379 ----------------------+----------+----------------------+----------------------
1380 WS.001.1a | 001 | | PS.base.a1
1381 WS.001.1b | 001 | | PS.base.a2
1382 WS.001.2a | 001 | | PS.base.a3
1383 WS.001.2b | 001 | | PS.base.a4
1384 WS.001.3a | 001 | | PS.base.a5
1385 WS.001.3b | 001 | | PS.base.a6
1386 WS.002.1a | 002 | | PS.base.b1
1387 WS.002.1b | 002 | | PS.base.b2
1388 WS.002.2a | 002 | | PS.base.b3
1389 WS.002.2b | 002 | | PS.base.b4
1390 WS.002.3a | 002 | | PS.base.b5
1391 WS.002.3b | 002 | | PS.base.b6
1392 WS.003.1a | 003 | | PS.base.c1
1393 WS.003.1b | 003 | | PS.base.c2
1394 WS.003.2a | 003 | | PS.base.c3
1395 WS.003.2b | 003 | | PS.base.c4
1396 WS.003.3a | 003 | | PS.base.c5
1397 WS.003.3b | 003 | | PS.base.c6
1398 WS.101.1a | 101 | | PS.first.a1
1399 WS.101.1b | 101 | | PS.first.a2
1400 WS.101.2a | 101 | | PS.first.a3
1401 WS.101.2b | 101 | | PS.first.a4
1402 WS.101.3a | 101 | | PS.first.a5
1403 WS.101.3b | 101 | | PS.first.a6
1404 WS.102.1a | 102 | | PS.first.b1
1405 WS.102.1b | 102 | | PS.first.b2
1406 WS.102.2a | 102 | | PS.first.b3
1407 WS.102.2b | 102 | | PS.first.b4
1408 WS.102.3a | 102 | | PS.first.b5
1409 WS.102.3b | 102 | | PS.first.b6
1410 WS.105.1a | 105 | | PS.first.c1
1411 WS.105.1b | 105 | | PS.first.c2
1412 WS.105.2a | 105 | | PS.first.c3
1413 WS.105.2b | 105 | | PS.first.c4
1414 WS.105.3a | 105 | | PS.first.c5
1415 WS.105.3b | 105 | | PS.first.c6
1416 WS.106.1a | 106 | | PS.first.d1
1417 WS.106.1b | 106 | | PS.first.d2
1418 WS.106.2a | 106 | | PS.first.d3
1419 WS.106.2b | 106 | | PS.first.d4
1420 WS.106.3a | 106 | | PS.first.d5
1421 WS.106.3b | 106 | | PS.first.d6
1425 -- Install the central phone system and create the phone numbers.
1426 -- They are wired on insert to the patchfields. Again the
1427 -- triggers automatically tell the PSlots to update their
1430 insert into PLine values ('PL.001', '-0', 'Central call', 'PS.base.ta1');
1431 insert into PLine values ('PL.002', '-101', '', 'PS.base.ta2');
1432 insert into PLine values ('PL.003', '-102', '', 'PS.base.ta3');
1433 insert into PLine values ('PL.004', '-103', '', 'PS.base.ta5');
1434 insert into PLine values ('PL.005', '-104', '', 'PS.base.ta6');
1435 insert into PLine values ('PL.006', '-106', '', 'PS.base.tb2');
1436 insert into PLine values ('PL.007', '-108', '', 'PS.base.tb3');
1437 insert into PLine values ('PL.008', '-109', '', 'PS.base.tb4');
1438 insert into PLine values ('PL.009', '-121', '', 'PS.base.tb5');
1439 insert into PLine values ('PL.010', '-122', '', 'PS.base.tb6');
1440 insert into PLine values ('PL.015', '-134', '', 'PS.first.ta1');
1441 insert into PLine values ('PL.016', '-137', '', 'PS.first.ta3');
1442 insert into PLine values ('PL.017', '-139', '', 'PS.first.ta4');
1443 insert into PLine values ('PL.018', '-362', '', 'PS.first.tb1');
1444 insert into PLine values ('PL.019', '-363', '', 'PS.first.tb2');
1445 insert into PLine values ('PL.020', '-364', '', 'PS.first.tb3');
1446 insert into PLine values ('PL.021', '-365', '', 'PS.first.tb5');
1447 insert into PLine values ('PL.022', '-367', '', 'PS.first.tb6');
1448 insert into PLine values ('PL.028', '-501', 'Fax entrance', 'PS.base.ta2');
1449 insert into PLine values ('PL.029', '-502', 'Fax first floor', 'PS.first.ta1');
1451 -- Buy some phones, plug them into the wall and patch the
1452 -- phone lines to the corresponding patchfield slots.
1454 insert into PHone values ('PH.hc001', 'Hicom standard', 'WS.001.1a');
1455 update PSlot set slotlink = 'PS.base.ta1' where slotname = 'PS.base.a1';
1456 insert into PHone values ('PH.hc002', 'Hicom standard', 'WS.002.1a');
1457 update PSlot set slotlink = 'PS.base.ta5' where slotname = 'PS.base.b1';
1458 insert into PHone values ('PH.hc003', 'Hicom standard', 'WS.002.2a');
1459 update PSlot set slotlink = 'PS.base.tb2' where slotname = 'PS.base.b3';
1460 insert into PHone values ('PH.fax001', 'Canon fax', 'WS.001.2a');
1461 update PSlot set slotlink = 'PS.base.ta2' where slotname = 'PS.base.a3';
1463 -- Install a hub at one of the patchfields, plug a computers
1464 -- ethernet interface into the wall and patch it to the hub.
1466 insert into Hub values ('base.hub1', 'Patchfield PF0_1 hub', 16);
1467 insert into System values ('orion', 'PC');
1468 insert into IFace values ('IF', 'orion', 'eth0', 'WS.002.1b');
1469 update PSlot set slotlink = 'HS.base.hub1.1' where slotname = 'PS.base.b2';
1471 -- Now we take a look at the patchfield
1473 select * from PField_v1 where pfname = 'PF0_1' order by slotname;
1474 pfname | slotname | backside | patch
1475 --------+----------------------+----------------------------------------------------------+-----------------------------------------------
1476 PF0_1 | PS.base.a1 | WS.001.1a in room 001 -> Phone PH.hc001 (Hicom standard) | PS.base.ta1 -> Phone line -0 (Central call)
1477 PF0_1 | PS.base.a2 | WS.001.1b in room 001 -> - | -
1478 PF0_1 | PS.base.a3 | WS.001.2a in room 001 -> Phone PH.fax001 (Canon fax) | PS.base.ta2 -> Phone line -501 (Fax entrance)
1479 PF0_1 | PS.base.a4 | WS.001.2b in room 001 -> - | -
1480 PF0_1 | PS.base.a5 | WS.001.3a in room 001 -> - | -
1481 PF0_1 | PS.base.a6 | WS.001.3b in room 001 -> - | -
1482 PF0_1 | PS.base.b1 | WS.002.1a in room 002 -> Phone PH.hc002 (Hicom standard) | PS.base.ta5 -> Phone line -103
1483 PF0_1 | PS.base.b2 | WS.002.1b in room 002 -> orion IF eth0 (PC) | Patchfield PF0_1 hub slot 1
1484 PF0_1 | PS.base.b3 | WS.002.2a in room 002 -> Phone PH.hc003 (Hicom standard) | PS.base.tb2 -> Phone line -106
1485 PF0_1 | PS.base.b4 | WS.002.2b in room 002 -> - | -
1486 PF0_1 | PS.base.b5 | WS.002.3a in room 002 -> - | -
1487 PF0_1 | PS.base.b6 | WS.002.3b in room 002 -> - | -
1488 PF0_1 | PS.base.c1 | WS.003.1a in room 003 -> - | -
1489 PF0_1 | PS.base.c2 | WS.003.1b in room 003 -> - | -
1490 PF0_1 | PS.base.c3 | WS.003.2a in room 003 -> - | -
1491 PF0_1 | PS.base.c4 | WS.003.2b in room 003 -> - | -
1492 PF0_1 | PS.base.c5 | WS.003.3a in room 003 -> - | -
1493 PF0_1 | PS.base.c6 | WS.003.3b in room 003 -> - | -
1496 select * from PField_v1 where pfname = 'PF0_2' order by slotname;
1497 pfname | slotname | backside | patch
1498 --------+----------------------+--------------------------------+------------------------------------------------------------------------
1499 PF0_2 | PS.base.ta1 | Phone line -0 (Central call) | PS.base.a1 -> WS.001.1a in room 001 -> Phone PH.hc001 (Hicom standard)
1500 PF0_2 | PS.base.ta2 | Phone line -501 (Fax entrance) | PS.base.a3 -> WS.001.2a in room 001 -> Phone PH.fax001 (Canon fax)
1501 PF0_2 | PS.base.ta3 | Phone line -102 | -
1502 PF0_2 | PS.base.ta4 | - | -
1503 PF0_2 | PS.base.ta5 | Phone line -103 | PS.base.b1 -> WS.002.1a in room 002 -> Phone PH.hc002 (Hicom standard)
1504 PF0_2 | PS.base.ta6 | Phone line -104 | -
1505 PF0_2 | PS.base.tb1 | - | -
1506 PF0_2 | PS.base.tb2 | Phone line -106 | PS.base.b3 -> WS.002.2a in room 002 -> Phone PH.hc003 (Hicom standard)
1507 PF0_2 | PS.base.tb3 | Phone line -108 | -
1508 PF0_2 | PS.base.tb4 | Phone line -109 | -
1509 PF0_2 | PS.base.tb5 | Phone line -121 | -
1510 PF0_2 | PS.base.tb6 | Phone line -122 | -
1514 -- Finally we want errors
1516 insert into PField values ('PF1_1', 'should fail due to unique index');
1517 ERROR: duplicate key value violates unique constraint "pfield_name"
1518 DETAIL: Key (name)=(PF1_1) already exists.
1519 update PSlot set backlink = 'WS.not.there' where slotname = 'PS.base.a1';
1520 ERROR: WS.not.there does not exist
1521 CONTEXT: PL/pgSQL function tg_backlink_set(character,character) line 30 at RAISE
1522 PL/pgSQL function tg_backlink_a() line 17 at assignment
1523 update PSlot set backlink = 'XX.illegal' where slotname = 'PS.base.a1';
1524 ERROR: illegal backlink beginning with XX
1525 CONTEXT: PL/pgSQL function tg_backlink_set(character,character) line 47 at RAISE
1526 PL/pgSQL function tg_backlink_a() line 17 at assignment
1527 update PSlot set slotlink = 'PS.not.there' where slotname = 'PS.base.a1';
1528 ERROR: PS.not.there does not exist
1529 CONTEXT: PL/pgSQL function tg_slotlink_set(character,character) line 30 at RAISE
1530 PL/pgSQL function tg_slotlink_a() line 17 at assignment
1531 update PSlot set slotlink = 'XX.illegal' where slotname = 'PS.base.a1';
1532 ERROR: illegal slotlink beginning with XX
1533 CONTEXT: PL/pgSQL function tg_slotlink_set(character,character) line 77 at RAISE
1534 PL/pgSQL function tg_slotlink_a() line 17 at assignment
1535 insert into HSlot values ('HS', 'base.hub1', 1, '');
1536 ERROR: duplicate key value violates unique constraint "hslot_name"
1537 DETAIL: Key (slotname)=(HS.base.hub1.1 ) already exists.
1538 insert into HSlot values ('HS', 'base.hub1', 20, '');
1539 ERROR: no manual manipulation of HSlot
1540 CONTEXT: PL/pgSQL function tg_hslot_biu() line 12 at RAISE
1542 ERROR: no manual manipulation of HSlot
1543 CONTEXT: PL/pgSQL function tg_hslot_bd() line 12 at RAISE
1544 insert into IFace values ('IF', 'notthere', 'eth0', '');
1545 ERROR: system "notthere" does not exist
1546 CONTEXT: PL/pgSQL function tg_iface_biu() line 8 at RAISE
1547 insert into IFace values ('IF', 'orion', 'ethernet_interface_name_too_long', '');
1548 ERROR: IFace slotname "IF.orion.ethernet_interface_name_too_long" too long (20 char max)
1549 CONTEXT: PL/pgSQL function tg_iface_biu() line 14 at RAISE
1551 -- The following tests are unrelated to the scenario outlined above;
1552 -- they merely exercise specific parts of PL/pgSQL
1555 -- Test recursion, per bug report 7-Sep-01
1557 CREATE FUNCTION recursion_test(int,int) RETURNS text AS '
1561 rslt = CAST($2 AS TEXT);
1563 rslt = CAST($1 AS TEXT) || '','' || recursion_test($1 - 1, $2);
1566 END;' LANGUAGE plpgsql;
1567 SELECT recursion_test(4,3);
1574 -- Test the FOUND magic variable
1576 CREATE TABLE found_test_tbl (a int);
1577 create function test_found()
1578 returns boolean as '
1581 insert into found_test_tbl values (1);
1583 insert into found_test_tbl values (2);
1586 update found_test_tbl set a = 100 where a = 1;
1588 insert into found_test_tbl values (3);
1591 delete from found_test_tbl where a = 9999; -- matches no rows
1593 insert into found_test_tbl values (4);
1596 for i in 1 .. 10 loop
1597 -- no need to do anything
1600 insert into found_test_tbl values (5);
1603 -- never executes the loop
1604 for i in 2 .. 1 loop
1605 -- no need to do anything
1608 insert into found_test_tbl values (6);
1611 end;' language plpgsql;
1612 select test_found();
1618 select * from found_test_tbl;
1630 -- Test set-returning functions for PL/pgSQL
1632 create function test_table_func_rec() returns setof found_test_tbl as '
1636 FOR rec IN select * from found_test_tbl LOOP
1640 END;' language plpgsql;
1641 select * from test_table_func_rec();
1652 create function test_table_func_row() returns setof found_test_tbl as '
1654 row found_test_tbl%ROWTYPE;
1656 FOR row IN select * from found_test_tbl LOOP
1660 END;' language plpgsql;
1661 select * from test_table_func_row();
1672 create function test_ret_set_scalar(int,int) returns setof int as '
1676 FOR i IN $1 .. $2 LOOP
1680 END;' language plpgsql;
1681 select * from test_ret_set_scalar(1,10);
1683 ---------------------
1696 create function test_ret_set_rec_dyn(int) returns setof record as '
1701 SELECT INTO retval 5, 10, 15;
1705 SELECT INTO retval 50, 5::numeric, ''xxx''::text;
1710 END;' language plpgsql;
1711 SELECT * FROM test_ret_set_rec_dyn(1500) AS (a int, b int, c int);
1718 SELECT * FROM test_ret_set_rec_dyn(5) AS (a int, b numeric, c text);
1725 create function test_ret_rec_dyn(int) returns record as '
1730 SELECT INTO retval 5, 10, 15;
1733 SELECT INTO retval 50, 5::numeric, ''xxx''::text;
1736 END;' language plpgsql;
1737 SELECT * FROM test_ret_rec_dyn(1500) AS (a int, b int, c int);
1743 SELECT * FROM test_ret_rec_dyn(5) AS (a int, b numeric, c text);
1750 -- Test some simple polymorphism cases.
1752 create function f1(x anyelement) returns anyelement as $$
1755 end$$ language plpgsql;
1756 select f1(42) as int, f1(4.5) as num;
1762 select f1(point(3,4)); -- fail for lack of + operator
1763 ERROR: operator does not exist: point + integer
1766 HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
1768 CONTEXT: PL/pgSQL function f1(anyelement) line 3 at RETURN
1769 drop function f1(x anyelement);
1770 create function f1(x anyelement) returns anyarray as $$
1772 return array[x + 1, x + 2];
1773 end$$ language plpgsql;
1774 select f1(42) as int, f1(4.5) as num;
1776 ---------+-----------
1780 drop function f1(x anyelement);
1781 create function f1(x anyarray) returns anyelement as $$
1784 end$$ language plpgsql;
1785 select f1(array[2,4]) as int, f1(array[4.5, 7.7]) as num;
1791 select f1(stavalues1) from pg_statistic; -- fail, can't infer element type
1792 ERROR: cannot determine element type of "anyarray" argument
1793 drop function f1(x anyarray);
1794 create function f1(x anyarray) returns anyarray as $$
1797 end$$ language plpgsql;
1798 select f1(array[2,4]) as int, f1(array[4.5, 7.7]) as num;
1804 select f1(stavalues1) from pg_statistic; -- fail, can't infer element type
1805 ERROR: PL/pgSQL functions cannot accept type anyarray
1806 CONTEXT: compilation of PL/pgSQL function "f1" near line 1
1807 drop function f1(x anyarray);
1808 -- fail, can't infer type:
1809 create function f1(x anyelement) returns anyrange as $$
1811 return array[x + 1, x + 2];
1812 end$$ language plpgsql;
1813 ERROR: cannot determine result data type
1814 DETAIL: A result of type anyrange requires at least one input of type anyrange or anymultirange.
1815 create function f1(x anyrange) returns anyarray as $$
1817 return array[lower(x), upper(x)];
1818 end$$ language plpgsql;
1819 select f1(int4range(42, 49)) as int, f1(float8range(4.5, 7.8)) as num;
1821 ---------+-----------
1825 drop function f1(x anyrange);
1826 create function f1(x anycompatible, y anycompatible) returns anycompatiblearray as $$
1829 end$$ language plpgsql;
1830 select f1(2, 4) as int, f1(2, 4.5) as num;
1836 drop function f1(x anycompatible, y anycompatible);
1837 create function f1(x anycompatiblerange, y anycompatible, z anycompatible) returns anycompatiblearray as $$
1839 return array[lower(x), upper(x), y, z];
1840 end$$ language plpgsql;
1841 select f1(int4range(42, 49), 11, 2::smallint) as int, f1(float8range(4.5, 7.8), 7.8, 11::real) as num;
1843 --------------+------------------
1844 {42,49,11,2} | {4.5,7.8,7.8,11}
1847 select f1(int4range(42, 49), 11, 4.5) as fail; -- range type doesn't fit
1848 ERROR: function f1(int4range, integer, numeric) does not exist
1849 LINE 1: select f1(int4range(42, 49), 11, 4.5) as fail;
1851 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
1852 drop function f1(x anycompatiblerange, y anycompatible, z anycompatible);
1853 -- fail, can't infer type:
1854 create function f1(x anycompatible) returns anycompatiblerange as $$
1856 return array[x + 1, x + 2];
1857 end$$ language plpgsql;
1858 ERROR: cannot determine result data type
1859 DETAIL: A result of type anycompatiblerange requires at least one input of type anycompatiblerange or anycompatiblemultirange.
1860 create function f1(x anycompatiblerange, y anycompatiblearray) returns anycompatiblerange as $$
1863 end$$ language plpgsql;
1864 select f1(int4range(42, 49), array[11]) as int, f1(float8range(4.5, 7.8), array[7]) as num;
1866 ---------+-----------
1870 drop function f1(x anycompatiblerange, y anycompatiblearray);
1871 create function f1(a anyelement, b anyarray,
1872 c anycompatible, d anycompatible,
1873 OUT x anyarray, OUT y anycompatiblearray)
1878 end$$ language plpgsql;
1879 select x, pg_typeof(x), y, pg_typeof(y)
1880 from f1(11, array[1, 2], 42, 34.5);
1881 x | pg_typeof | y | pg_typeof
1882 ----------+-----------+-----------+-----------
1883 {11,1,2} | integer[] | {42,34.5} | numeric[]
1886 select x, pg_typeof(x), y, pg_typeof(y)
1887 from f1(11, array[1, 2], point(1,2), point(3,4));
1888 x | pg_typeof | y | pg_typeof
1889 ----------+-----------+-------------------+-----------
1890 {11,1,2} | integer[] | {"(1,2)","(3,4)"} | point[]
1893 select x, pg_typeof(x), y, pg_typeof(y)
1894 from f1(11, '{1,2}', point(1,2), '(3,4)');
1895 x | pg_typeof | y | pg_typeof
1896 ----------+-----------+-------------------+-----------
1897 {11,1,2} | integer[] | {"(1,2)","(3,4)"} | point[]
1900 select x, pg_typeof(x), y, pg_typeof(y)
1901 from f1(11, array[1, 2.2], 42, 34.5); -- fail
1902 ERROR: function f1(integer, numeric[], integer, numeric) does not exist
1903 LINE 2: from f1(11, array[1, 2.2], 42, 34.5);
1905 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
1906 drop function f1(a anyelement, b anyarray,
1907 c anycompatible, d anycompatible);
1909 -- Test handling of OUT parameters, including polymorphic cases.
1910 -- Note that RETURN is optional with OUT params; we try both ways.
1912 -- wrong way to do it:
1913 create function f1(in i int, out j int) returns int as $$
1916 end$$ language plpgsql;
1917 ERROR: RETURN cannot have a parameter in function with OUT parameters
1920 create function f1(in i int, out j int) as $$
1924 end$$ language plpgsql;
1931 select * from f1(42);
1937 create or replace function f1(inout i int) as $$
1940 end$$ language plpgsql;
1947 select * from f1(42);
1953 drop function f1(int);
1954 create function f1(in i int, out j int) returns setof int as $$
1961 end$$ language plpgsql;
1962 select * from f1(42);
1969 drop function f1(int);
1970 create function f1(in i int, out j int, out k text) as $$
1975 end$$ language plpgsql;
1982 select * from f1(42);
1988 drop function f1(int);
1989 create function f1(in i int, out j int, out k text) returns setof record as $$
1997 end$$ language plpgsql;
1998 select * from f1(42);
2005 drop function f1(int);
2006 create function duplic(in i anyelement, out j anyelement, out k anyarray) as $$
2011 end$$ language plpgsql;
2012 select * from duplic(42);
2018 select * from duplic('foo'::text);
2024 drop function duplic(anyelement);
2025 create function duplic(in i anycompatiblerange, out j anycompatible, out k anycompatiblearray) as $$
2028 k := array[lower(i),upper(i)];
2030 end$$ language plpgsql;
2031 select * from duplic(int4range(42,49));
2037 select * from duplic(textrange('aaa', 'bbb'));
2043 drop function duplic(anycompatiblerange);
2047 create table perform_test (
2051 create function perform_simple_func(int) returns boolean as '
2054 INSERT INTO perform_test VALUES ($1, $1 + 10);
2059 END;' language plpgsql;
2060 create function perform_test_func() returns void as '
2063 INSERT INTO perform_test VALUES (100, 100);
2066 PERFORM perform_simple_func(5);
2069 INSERT INTO perform_test VALUES (100, 100);
2072 PERFORM perform_simple_func(50);
2075 INSERT INTO perform_test VALUES (100, 100);
2079 END;' language plpgsql;
2080 SELECT perform_test_func();
2086 SELECT * FROM perform_test;
2094 drop table perform_test;
2096 -- Test proper snapshot handling in simple expressions
2098 create temp table users(login text, id serial);
2099 create function sp_id_user(a_login text) returns int as $$
2102 select into x id from users where login = a_login;
2103 if found then return x; end if;
2105 end$$ language plpgsql stable;
2106 insert into users values('user1');
2107 select sp_id_user('user1');
2113 select sp_id_user('userx');
2119 create function sp_add_user(a_login text) returns int as $$
2120 declare my_id_user int;
2122 my_id_user = sp_id_user( a_login );
2123 IF my_id_user > 0 THEN
2124 RETURN -1; -- error code for existing user
2126 INSERT INTO users ( login ) VALUES ( a_login );
2127 my_id_user = sp_id_user( a_login );
2128 IF my_id_user = 0 THEN
2129 RETURN -2; -- error code for insertion failure
2132 end$$ language plpgsql;
2133 select sp_add_user('user1');
2139 select sp_add_user('user2');
2145 select sp_add_user('user2');
2151 select sp_add_user('user3');
2157 select sp_add_user('user3');
2163 drop function sp_add_user(text);
2164 drop function sp_id_user(text);
2166 -- tests for refcursors
2168 create table rc_test (a int, b int);
2169 copy rc_test from stdin;
2170 create function return_unnamed_refcursor() returns refcursor as $$
2174 open rc for select a from rc_test;
2177 $$ language plpgsql;
2178 create function use_refcursor(rc refcursor) returns int as $$
2183 rc := return_unnamed_refcursor();
2184 fetch next from rc into x;
2187 $$ language plpgsql;
2188 select use_refcursor(return_unnamed_refcursor());
2194 create function return_refcursor(rc refcursor) returns refcursor as $$
2196 open rc for select a from rc_test;
2199 $$ language plpgsql;
2200 create function refcursor_test1(refcursor) returns refcursor as $$
2202 perform return_refcursor($1);
2205 $$ language plpgsql;
2207 select refcursor_test1('test1');
2213 fetch next in test1;
2219 select refcursor_test1('test2');
2225 fetch all from test2;
2235 fetch next from test1;
2236 ERROR: cursor "test1" does not exist
2237 create function refcursor_test2(int, int) returns boolean as $$
2239 c1 cursor (param1 int, param2 int) for select * from rc_test where a > param1 and b > param2;
2243 fetch c1 into nonsense;
2251 $$ language plpgsql;
2252 select refcursor_test2(20000, 20000) as "Should be false",
2253 refcursor_test2(20, 20) as "Should be true";
2254 Should be false | Should be true
2255 -----------------+----------------
2260 -- tests for cursors with named parameter arguments
2262 create function namedparmcursor_test1(int, int) returns boolean as $$
2264 c1 cursor (param1 int, param12 int) for select * from rc_test where a > param1 and b > param12;
2267 open c1(param12 := $2, param1 := $1);
2268 fetch c1 into nonsense;
2276 $$ language plpgsql;
2277 select namedparmcursor_test1(20000, 20000) as "Should be false",
2278 namedparmcursor_test1(20, 20) as "Should be true";
2279 Should be false | Should be true
2280 -----------------+----------------
2284 -- mixing named and positional argument notations
2285 create function namedparmcursor_test2(int, int) returns boolean as $$
2287 c1 cursor (param1 int, param2 int) for select * from rc_test where a > param1 and b > param2;
2290 open c1(param1 := $1, $2);
2291 fetch c1 into nonsense;
2299 $$ language plpgsql;
2300 select namedparmcursor_test2(20, 20);
2301 namedparmcursor_test2
2302 -----------------------
2306 -- mixing named and positional: param2 is given twice, once in named notation
2307 -- and second time in positional notation. Should throw an error at parse time
2308 create function namedparmcursor_test3() returns void as $$
2310 c1 cursor (param1 int, param2 int) for select * from rc_test where a > param1 and b > param2;
2312 open c1(param2 := 20, 21);
2314 $$ language plpgsql;
2315 ERROR: value for parameter "param2" of cursor "c1" specified more than once
2316 LINE 5: open c1(param2 := 20, 21);
2318 -- mixing named and positional: same as previous test, but param1 is duplicated
2319 create function namedparmcursor_test4() returns void as $$
2321 c1 cursor (param1 int, param2 int) for select * from rc_test where a > param1 and b > param2;
2323 open c1(20, param1 := 21);
2325 $$ language plpgsql;
2326 ERROR: value for parameter "param1" of cursor "c1" specified more than once
2327 LINE 5: open c1(20, param1 := 21);
2329 -- duplicate named parameter, should throw an error at parse time
2330 create function namedparmcursor_test5() returns void as $$
2332 c1 cursor (p1 int, p2 int) for
2333 select * from tenk1 where thousand = p1 and tenthous = p2;
2335 open c1 (p2 := 77, p2 := 42);
2337 $$ language plpgsql;
2338 ERROR: value for parameter "p2" of cursor "c1" specified more than once
2339 LINE 6: open c1 (p2 := 77, p2 := 42);
2341 -- not enough parameters, should throw an error at parse time
2342 create function namedparmcursor_test6() returns void as $$
2344 c1 cursor (p1 int, p2 int) for
2345 select * from tenk1 where thousand = p1 and tenthous = p2;
2349 $$ language plpgsql;
2350 ERROR: not enough arguments for cursor "c1"
2351 LINE 6: open c1 (p2 := 77);
2353 -- division by zero runtime error, the context given in the error message
2354 -- should be sensible
2355 create function namedparmcursor_test7() returns void as $$
2357 c1 cursor (p1 int, p2 int) for
2358 select * from tenk1 where thousand = p1 and tenthous = p2;
2360 open c1 (p2 := 77, p1 := 42/0);
2361 end $$ language plpgsql;
2362 select namedparmcursor_test7();
2363 ERROR: division by zero
2364 CONTEXT: SQL expression "42/0 AS p1, 77 AS p2"
2365 PL/pgSQL function namedparmcursor_test7() line 6 at OPEN
2366 -- check that line comments work correctly within the argument list (there
2367 -- is some special handling of this case in the code: the newline after the
2368 -- comment must be preserved when the argument-evaluating query is
2369 -- constructed, otherwise the comment effectively comments out the next
2371 create function namedparmcursor_test8() returns int4 as $$
2373 c1 cursor (p1 int, p2 int) for
2374 select count(*) from tenk1 where thousand = p1 and tenthous = p2;
2381 end $$ language plpgsql;
2382 select namedparmcursor_test8();
2383 namedparmcursor_test8
2384 -----------------------
2388 -- cursor parameter name can match plpgsql variable or unreserved keyword
2389 create function namedparmcursor_test9(p1 int) returns int4 as $$
2391 c1 cursor (p1 int, p2 int, debug int) for
2392 select count(*) from tenk1 where thousand = p1 and tenthous = p2
2397 open c1 (p1 := p1, p2 := p2, debug := 2);
2400 end $$ language plpgsql;
2401 select namedparmcursor_test9(6);
2402 namedparmcursor_test9
2403 -----------------------
2408 -- tests for "raise" processing
2410 create function raise_test1(int) returns int as $$
2412 raise notice 'This message has too many parameters!', $1;
2415 $$ language plpgsql;
2416 ERROR: too many parameters specified for RAISE
2417 CONTEXT: compilation of PL/pgSQL function "raise_test1" near line 3
2418 create function raise_test2(int) returns int as $$
2420 raise notice 'This message has too few parameters: %, %, %', $1, $1;
2423 $$ language plpgsql;
2424 ERROR: too few parameters specified for RAISE
2425 CONTEXT: compilation of PL/pgSQL function "raise_test2" near line 3
2426 create function raise_test3(int) returns int as $$
2428 raise notice 'This message has no parameters (despite having %% signs in it)!';
2431 $$ language plpgsql;
2432 select raise_test3(1);
2433 NOTICE: This message has no parameters (despite having % signs in it)!
2439 -- Test re-RAISE inside a nested exception block. This case is allowed
2440 -- by Oracle's PL/SQL but was handled differently by PG before 9.1.
2441 CREATE FUNCTION reraise_test() RETURNS void AS $$
2446 WHEN syntax_error THEN
2448 raise notice 'exception % thrown in inner block, reraising', sqlerrm;
2452 raise notice 'RIGHT - exception % caught in inner block', sqlerrm;
2457 raise notice 'WRONG - exception % caught in outer block', sqlerrm;
2459 $$ LANGUAGE plpgsql;
2460 SELECT reraise_test();
2461 NOTICE: exception syntax_error thrown in inner block, reraising
2462 NOTICE: RIGHT - exception syntax_error caught in inner block
2469 -- reject function definitions that contain malformed SQL queries at
2470 -- compile-time, where possible
2472 create function bad_sql1() returns int as $$
2479 end$$ language plpgsql;
2480 ERROR: syntax error at or near "Johnny"
2481 LINE 5: Johnny Yuma;
2483 create function bad_sql2() returns int as $$
2486 for r in select I fought the law, the law won LOOP
2487 raise notice 'in loop';
2490 end;$$ language plpgsql;
2491 ERROR: syntax error at or near "the"
2492 LINE 4: for r in select I fought the law, the law won LOOP
2494 -- a RETURN expression is mandatory, except for void-returning
2495 -- functions, where it is not allowed
2496 create function missing_return_expr() returns int as $$
2499 end;$$ language plpgsql;
2500 ERROR: missing expression at or near ";"
2503 create function void_return_expr() returns void as $$
2506 end;$$ language plpgsql;
2507 ERROR: RETURN cannot have a parameter in function returning void
2510 -- VOID functions are allowed to omit RETURN
2511 create function void_return_expr() returns void as $$
2514 end;$$ language plpgsql;
2515 select void_return_expr();
2521 -- but ordinary functions are not
2522 create function missing_return_expr() returns int as $$
2525 end;$$ language plpgsql;
2526 select missing_return_expr();
2527 ERROR: control reached end of function without RETURN
2528 CONTEXT: PL/pgSQL function missing_return_expr()
2529 drop function void_return_expr();
2530 drop function missing_return_expr();
2532 -- EXECUTE ... INTO test
2534 create table eifoo (i integer, y integer);
2535 create type eitype as (i integer, y integer);
2536 create or replace function execute_into_test(varchar) returns record as $$
2545 execute 'insert into '||$1||' values(10,15)';
2546 execute 'select (row).* from (select row(10,1)::eifoo) s' into _r;
2547 raise notice '% %', _r.i, _r.y;
2548 execute 'select * from '||$1||' limit 1' into _rt;
2549 raise notice '% %', _rt.i, _rt.y;
2550 execute 'select *, 20 from '||$1||' limit 1' into i, j, k;
2551 raise notice '% % %', i, j, k;
2552 execute 'select 1,2' into _v;
2554 end; $$ language plpgsql;
2555 select execute_into_test('eifoo');
2564 drop table eifoo cascade;
2565 drop type eitype cascade;
2567 -- SQLSTATE and SQLERRM test
2569 create function excpt_test1() returns void as $$
2571 raise notice '% %', sqlstate, sqlerrm;
2572 end; $$ language plpgsql;
2573 -- should fail: SQLSTATE and SQLERRM are only in defined EXCEPTION
2575 select excpt_test1();
2576 ERROR: column "sqlstate" does not exist
2580 CONTEXT: PL/pgSQL function excpt_test1() line 3 at RAISE
2581 create function excpt_test2() returns void as $$
2585 raise notice '% %', sqlstate, sqlerrm;
2588 end; $$ language plpgsql;
2590 select excpt_test2();
2591 ERROR: column "sqlstate" does not exist
2595 CONTEXT: PL/pgSQL function excpt_test2() line 5 at RAISE
2596 create function excpt_test3() returns void as $$
2599 raise exception 'user exception';
2600 exception when others then
2601 raise notice 'caught exception % %', sqlstate, sqlerrm;
2603 raise notice '% %', sqlstate, sqlerrm;
2606 when substring_error then
2607 -- this exception handler shouldn't be invoked
2608 raise notice 'unexpected exception: % %', sqlstate, sqlerrm;
2609 when division_by_zero then
2610 raise notice 'caught exception % %', sqlstate, sqlerrm;
2612 raise notice '% %', sqlstate, sqlerrm;
2614 end; $$ language plpgsql;
2615 select excpt_test3();
2616 NOTICE: caught exception P0001 user exception
2617 NOTICE: P0001 user exception
2618 NOTICE: caught exception 22012 division by zero
2619 NOTICE: P0001 user exception
2625 create function excpt_test4() returns text as $$
2628 exception when others then return sqlerrm; end;
2629 end; $$ language plpgsql;
2630 select excpt_test4();
2636 drop function excpt_test1();
2637 drop function excpt_test2();
2638 drop function excpt_test3();
2639 drop function excpt_test4();
2640 -- parameters of raise stmt can be expressions
2641 create function raise_exprs() returns void as $$
2643 a integer[] = '{10,20,30}';
2648 raise notice '%; %; %; %; %; %', a, a[i], c, (select c || 'abc'), row(10,'aaa',NULL,30), NULL;
2649 end;$$ language plpgsql;
2650 select raise_exprs();
2651 NOTICE: {10,20,30}; 20; xyz; xyzabc; (10,aaa,,30); <NULL>
2657 drop function raise_exprs();
2658 -- regression test: verify that multiple uses of same plpgsql datum within
2659 -- a SQL command all get mapped to the same $n parameter. The return value
2660 -- of the SELECT is not important, we only care that it doesn't fail with
2661 -- a complaint about an ungrouped column reference.
2662 create function multi_datum_use(p1 int) returns bool as $$
2667 select into x,y unique1/p1, unique1/$1 from tenk1 group by unique1/p1;
2669 end$$ language plpgsql;
2670 select multi_datum_use(42);
2677 -- Test STRICT limiter in both planned and EXECUTE invocations.
2678 -- Note that a data-modifying query is quasi strict (disallow multi rows)
2679 -- by default in the planned case, but not in EXECUTE.
2681 create temp table foo (f1 int, f2 int);
2682 insert into foo values (1,2), (3,4);
2683 create or replace function stricttest() returns void as $$
2687 insert into foo values(5,6) returning * into x;
2688 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2689 end$$ language plpgsql;
2690 select stricttest();
2691 NOTICE: x.f1 = 5, x.f2 = 6
2697 create or replace function stricttest() returns void as $$
2700 -- should fail due to implicit strict
2701 insert into foo values(7,8),(9,10) returning * into x;
2702 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2703 end$$ language plpgsql;
2704 select stricttest();
2705 ERROR: query returned more than one row
2706 HINT: Make sure the query returns a single row, or use LIMIT 1.
2707 CONTEXT: PL/pgSQL function stricttest() line 5 at SQL statement
2708 create or replace function stricttest() returns void as $$
2712 execute 'insert into foo values(5,6) returning *' into x;
2713 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2714 end$$ language plpgsql;
2715 select stricttest();
2716 NOTICE: x.f1 = 5, x.f2 = 6
2722 create or replace function stricttest() returns void as $$
2725 -- this should work since EXECUTE isn't as picky
2726 execute 'insert into foo values(7,8),(9,10) returning *' into x;
2727 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2728 end$$ language plpgsql;
2729 select stricttest();
2730 NOTICE: x.f1 = 7, x.f2 = 8
2747 create or replace function stricttest() returns void as $$
2751 select * from foo where f1 = 3 into strict x;
2752 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2753 end$$ language plpgsql;
2754 select stricttest();
2755 NOTICE: x.f1 = 3, x.f2 = 4
2761 create or replace function stricttest() returns void as $$
2764 -- should fail, no rows
2765 select * from foo where f1 = 0 into strict x;
2766 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2767 end$$ language plpgsql;
2768 select stricttest();
2769 ERROR: query returned no rows
2770 CONTEXT: PL/pgSQL function stricttest() line 5 at SQL statement
2771 create or replace function stricttest() returns void as $$
2774 -- should fail, too many rows
2775 select * from foo where f1 > 3 into strict x;
2776 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2777 end$$ language plpgsql;
2778 select stricttest();
2779 ERROR: query returned more than one row
2780 HINT: Make sure the query returns a single row, or use LIMIT 1.
2781 CONTEXT: PL/pgSQL function stricttest() line 5 at SQL statement
2782 create or replace function stricttest() returns void as $$
2786 execute 'select * from foo where f1 = 3' into strict x;
2787 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2788 end$$ language plpgsql;
2789 select stricttest();
2790 NOTICE: x.f1 = 3, x.f2 = 4
2796 create or replace function stricttest() returns void as $$
2799 -- should fail, no rows
2800 execute 'select * from foo where f1 = 0' into strict x;
2801 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2802 end$$ language plpgsql;
2803 select stricttest();
2804 ERROR: query returned no rows
2805 CONTEXT: PL/pgSQL function stricttest() line 5 at EXECUTE
2806 create or replace function stricttest() returns void as $$
2809 -- should fail, too many rows
2810 execute 'select * from foo where f1 > 3' into strict x;
2811 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2812 end$$ language plpgsql;
2813 select stricttest();
2814 ERROR: query returned more than one row
2815 CONTEXT: PL/pgSQL function stricttest() line 5 at EXECUTE
2816 drop function stricttest();
2817 -- test printing parameters after failure due to STRICT
2818 set plpgsql.print_strict_params to true;
2819 create or replace function stricttest() returns void as $$
2826 select * from foo where f1 = p1 and f1::text = p3 into strict x;
2827 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2828 end$$ language plpgsql;
2829 select stricttest();
2830 ERROR: query returned no rows
2831 DETAIL: parameters: p1 = '2', p3 = 'foo'
2832 CONTEXT: PL/pgSQL function stricttest() line 8 at SQL statement
2833 create or replace function stricttest() returns void as $$
2837 p3 text := $a$'Valame Dios!' dijo Sancho; 'no le dije yo a vuestra merced que mirase bien lo que hacia?'$a$;
2840 select * from foo where f1 = p1 and f1::text = p3 into strict x;
2841 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2842 end$$ language plpgsql;
2843 select stricttest();
2844 ERROR: query returned no rows
2845 DETAIL: parameters: p1 = '2', p3 = '''Valame Dios!'' dijo Sancho; ''no le dije yo a vuestra merced que mirase bien lo que hacia?'''
2846 CONTEXT: PL/pgSQL function stricttest() line 8 at SQL statement
2847 create or replace function stricttest() returns void as $$
2854 select * from foo where f1 > p1 or f1::text = p3 into strict x;
2855 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2856 end$$ language plpgsql;
2857 select stricttest();
2858 ERROR: query returned more than one row
2859 DETAIL: parameters: p1 = '2', p3 = 'foo'
2860 HINT: Make sure the query returns a single row, or use LIMIT 1.
2861 CONTEXT: PL/pgSQL function stricttest() line 8 at SQL statement
2862 create or replace function stricttest() returns void as $$
2865 -- too many rows, no params
2866 select * from foo where f1 > 3 into strict x;
2867 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2868 end$$ language plpgsql;
2869 select stricttest();
2870 ERROR: query returned more than one row
2871 HINT: Make sure the query returns a single row, or use LIMIT 1.
2872 CONTEXT: PL/pgSQL function stricttest() line 5 at SQL statement
2873 create or replace function stricttest() returns void as $$
2877 execute 'select * from foo where f1 = $1 or f1::text = $2' using 0, 'foo' into strict x;
2878 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2879 end$$ language plpgsql;
2880 select stricttest();
2881 ERROR: query returned no rows
2882 DETAIL: parameters: $1 = '0', $2 = 'foo'
2883 CONTEXT: PL/pgSQL function stricttest() line 5 at EXECUTE
2884 create or replace function stricttest() returns void as $$
2888 execute 'select * from foo where f1 > $1' using 1 into strict x;
2889 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2890 end$$ language plpgsql;
2891 select stricttest();
2892 ERROR: query returned more than one row
2893 DETAIL: parameters: $1 = '1'
2894 CONTEXT: PL/pgSQL function stricttest() line 5 at EXECUTE
2895 create or replace function stricttest() returns void as $$
2898 -- too many rows, no parameters
2899 execute 'select * from foo where f1 > 3' into strict x;
2900 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2901 end$$ language plpgsql;
2902 select stricttest();
2903 ERROR: query returned more than one row
2904 CONTEXT: PL/pgSQL function stricttest() line 5 at EXECUTE
2905 create or replace function stricttest() returns void as $$
2906 -- override the global
2907 #print_strict_params off
2914 select * from foo where f1 > p1 or f1::text = p3 into strict x;
2915 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2916 end$$ language plpgsql;
2917 select stricttest();
2918 ERROR: query returned more than one row
2919 HINT: Make sure the query returns a single row, or use LIMIT 1.
2920 CONTEXT: PL/pgSQL function stricttest() line 10 at SQL statement
2921 reset plpgsql.print_strict_params;
2922 create or replace function stricttest() returns void as $$
2923 -- override the global
2924 #print_strict_params on
2931 select * from foo where f1 > p1 or f1::text = p3 into strict x;
2932 raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
2933 end$$ language plpgsql;
2934 select stricttest();
2935 ERROR: query returned more than one row
2936 DETAIL: parameters: p1 = '2', p3 = 'foo'
2937 HINT: Make sure the query returns a single row, or use LIMIT 1.
2938 CONTEXT: PL/pgSQL function stricttest() line 10 at SQL statement
2939 -- test warnings and errors
2940 set plpgsql.extra_warnings to 'all';
2941 set plpgsql.extra_warnings to 'none';
2942 set plpgsql.extra_errors to 'all';
2943 set plpgsql.extra_errors to 'none';
2944 -- test warnings when shadowing a variable
2945 set plpgsql.extra_warnings to 'shadowed_variables';
2946 -- simple shadowing of input and output parameters
2947 create or replace function shadowtest(in1 int)
2948 returns table (out1 int) as $$
2954 $$ language plpgsql;
2955 WARNING: variable "in1" shadows a previously defined variable
2958 WARNING: variable "out1" shadows a previously defined variable
2961 select shadowtest(1);
2966 set plpgsql.extra_warnings to 'shadowed_variables';
2967 select shadowtest(1);
2972 create or replace function shadowtest(in1 int)
2973 returns table (out1 int) as $$
2979 $$ language plpgsql;
2980 WARNING: variable "in1" shadows a previously defined variable
2983 WARNING: variable "out1" shadows a previously defined variable
2986 select shadowtest(1);
2991 drop function shadowtest(int);
2992 -- shadowing in a second DECLARE block
2993 create or replace function shadowtest()
3002 end$$ language plpgsql;
3003 WARNING: variable "f1" shadows a previously defined variable
3006 drop function shadowtest();
3007 -- several levels of shadowing
3008 create or replace function shadowtest(in1 int)
3017 end$$ language plpgsql;
3018 WARNING: variable "in1" shadows a previously defined variable
3021 WARNING: variable "in1" shadows a previously defined variable
3024 drop function shadowtest(int);
3025 -- shadowing in cursor definitions
3026 create or replace function shadowtest()
3030 c1 cursor (f1 int) for select 1;
3032 end$$ language plpgsql;
3033 WARNING: variable "f1" shadows a previously defined variable
3034 LINE 5: c1 cursor (f1 int) for select 1;
3036 drop function shadowtest();
3037 -- test errors when shadowing a variable
3038 set plpgsql.extra_errors to 'shadowed_variables';
3039 create or replace function shadowtest(f1 int)
3040 returns boolean as $$
3041 declare f1 int; begin return 1; end $$ language plpgsql;
3042 ERROR: variable "f1" shadows a previously defined variable
3043 LINE 3: declare f1 int; begin return 1; end $$ language plpgsql;
3045 select shadowtest(1);
3046 ERROR: function shadowtest(integer) does not exist
3047 LINE 1: select shadowtest(1);
3049 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3050 reset plpgsql.extra_errors;
3051 reset plpgsql.extra_warnings;
3052 create or replace function shadowtest(f1 int)
3053 returns boolean as $$
3054 declare f1 int; begin return 1; end $$ language plpgsql;
3055 select shadowtest(1);
3061 -- runtime extra checks
3062 set plpgsql.extra_warnings to 'too_many_rows';
3066 select v from generate_series(1,2) g(v) into x;
3069 WARNING: query returned more than one row
3070 HINT: Make sure the query returns a single row, or use LIMIT 1.
3071 set plpgsql.extra_errors to 'too_many_rows';
3075 select v from generate_series(1,2) g(v) into x;
3078 ERROR: query returned more than one row
3079 HINT: Make sure the query returns a single row, or use LIMIT 1.
3080 CONTEXT: PL/pgSQL function inline_code_block line 4 at SQL statement
3081 reset plpgsql.extra_errors;
3082 reset plpgsql.extra_warnings;
3083 set plpgsql.extra_warnings to 'strict_multi_assignment';
3090 select 1,2 into x, y;
3091 select 1,2,3 into x, y;
3094 WARNING: number of source and target fields in assignment does not match
3095 DETAIL: strict_multi_assignment check of extra_warnings is active.
3096 HINT: Make sure the query returns the exact list of columns.
3097 WARNING: number of source and target fields in assignment does not match
3098 DETAIL: strict_multi_assignment check of extra_warnings is active.
3099 HINT: Make sure the query returns the exact list of columns.
3100 set plpgsql.extra_errors to 'strict_multi_assignment';
3107 select 1,2 into x, y;
3108 select 1,2,3 into x, y;
3111 ERROR: number of source and target fields in assignment does not match
3112 DETAIL: strict_multi_assignment check of extra_errors is active.
3113 HINT: Make sure the query returns the exact list of columns.
3114 CONTEXT: PL/pgSQL function inline_code_block line 6 at SQL statement
3115 create table test_01(a int, b int, c int);
3116 alter table test_01 drop column a;
3117 -- the check is active only when source table is not empty
3118 insert into test_01 values(10,20);
3124 select * from test_01 into x, y; -- should be ok
3126 select * from test_01 into x; -- should to fail
3130 ERROR: number of source and target fields in assignment does not match
3131 DETAIL: strict_multi_assignment check of extra_errors is active.
3132 HINT: Make sure the query returns the exact list of columns.
3133 CONTEXT: PL/pgSQL function inline_code_block line 8 at SQL statement
3138 select 1, 2 into t; -- should be ok
3140 select 1, 2, 3 into t; -- should fail;
3144 ERROR: number of source and target fields in assignment does not match
3145 DETAIL: strict_multi_assignment check of extra_errors is active.
3146 HINT: Make sure the query returns the exact list of columns.
3147 CONTEXT: PL/pgSQL function inline_code_block line 7 at SQL statement
3152 select 1 into t; -- should fail;
3155 ERROR: number of source and target fields in assignment does not match
3156 DETAIL: strict_multi_assignment check of extra_errors is active.
3157 HINT: Make sure the query returns the exact list of columns.
3158 CONTEXT: PL/pgSQL function inline_code_block line 5 at SQL statement
3160 reset plpgsql.extra_errors;
3161 reset plpgsql.extra_warnings;
3162 -- test scrollable cursor support
3163 create function sc_test() returns setof integer as $$
3165 c scroll cursor for select f1 from int4_tbl;
3169 fetch last from c into x;
3172 fetch prior from c into x;
3176 $$ language plpgsql;
3177 select * from sc_test();
3187 create or replace function sc_test() returns setof integer as $$
3189 c no scroll cursor for select f1 from int4_tbl;
3193 fetch last from c into x;
3196 fetch prior from c into x;
3200 $$ language plpgsql;
3201 select * from sc_test(); -- fails because of NO SCROLL specification
3202 ERROR: cursor can only scan forward
3203 HINT: Declare it with SCROLL option to enable backward scan.
3204 CONTEXT: PL/pgSQL function sc_test() line 7 at FETCH
3205 create or replace function sc_test() returns setof integer as $$
3210 open c scroll for select f1 from int4_tbl;
3211 fetch last from c into x;
3214 fetch prior from c into x;
3218 $$ language plpgsql;
3219 select * from sc_test();
3229 create or replace function sc_test() returns setof integer as $$
3234 open c scroll for execute 'select f1 from int4_tbl';
3235 fetch last from c into x;
3238 fetch relative -2 from c into x;
3242 $$ language plpgsql;
3243 select * from sc_test();
3251 create or replace function sc_test() returns setof integer as $$
3256 open c scroll for execute 'select f1 from int4_tbl';
3257 fetch last from c into x;
3260 move backward 2 from c;
3261 fetch relative -1 from c into x;
3265 $$ language plpgsql;
3266 select * from sc_test();
3273 create or replace function sc_test() returns setof integer as $$
3275 c cursor for select * from generate_series(1, 10);
3280 move relative 2 in c;
3284 fetch next from c into x;
3291 $$ language plpgsql;
3292 select * from sc_test();
3300 create or replace function sc_test() returns setof integer as $$
3302 c cursor for select * from generate_series(1, 10);
3306 move forward all in c;
3307 fetch backward from c into x;
3313 $$ language plpgsql;
3314 select * from sc_test();
3320 drop function sc_test();
3321 -- test qualified variable names
3322 create function pl_qual_names (param1 int) returns void as $$
3331 raise notice 'param1 = %', param1;
3332 raise notice 'pl_qual_names.param1 = %', pl_qual_names.param1;
3333 raise notice 'outerblock.param1 = %', outerblock.param1;
3334 raise notice 'innerblock.param1 = %', innerblock.param1;
3337 $$ language plpgsql;
3338 select pl_qual_names(42);
3340 NOTICE: pl_qual_names.param1 = 42
3341 NOTICE: outerblock.param1 = 1
3342 NOTICE: innerblock.param1 = 2
3348 drop function pl_qual_names(int);
3349 -- tests for RETURN QUERY
3350 create function ret_query1(out int, out int) returns setof record as $$
3355 return query select x + 1, x * 10 from generate_series(0, 10) s (x);
3358 $$ language plpgsql;
3359 select * from ret_query1();
3377 create type record_type as (x text, y int, z boolean);
3378 create or replace function ret_query2(lim int) returns setof record_type as $$
3380 return query select md5(s.x::text), s.x, s.x > 0
3381 from generate_series(-8, lim) s (x) where s.x % 2 = 0;
3383 $$ language plpgsql;
3384 select * from ret_query2(8);
3386 ----------------------------------+----+---
3387 a8d2ec85eaf98407310b72eb73dda247 | -8 | f
3388 596a3d04481816330f07e4f97510c28f | -6 | f
3389 0267aaf632e87a63288a08331f22c7c3 | -4 | f
3390 5d7b9adcbe1c629ec722529dd12e5129 | -2 | f
3391 cfcd208495d565ef66e7dff9f98764da | 0 | f
3392 c81e728d9d4c2f636f067f89cc14862c | 2 | t
3393 a87ff679a2f3e71d9181a67b7542122c | 4 | t
3394 1679091c5a880faf6fb5e6087eb1b2dc | 6 | t
3395 c9f0f895fb98ab9159f51fd0297e236d | 8 | t
3398 -- test EXECUTE USING
3399 create function exc_using(int, text) returns int as $$
3402 for i in execute 'select * from generate_series(1,$1)' using $1+1 loop
3403 raise notice '%', i;
3405 execute 'select $2 + $2*3 + length($1)' into i using $2,$1;
3408 $$ language plpgsql;
3409 select exc_using(5, 'foobar');
3421 drop function exc_using(int, text);
3422 create or replace function exc_using(int) returns void as $$
3427 open c for execute 'select * from generate_series(1,$1)' using $1+1;
3430 exit when not found;
3431 raise notice '%', i;
3436 $$ language plpgsql;
3437 select exc_using(5);
3449 drop function exc_using(int);
3450 -- test FOR-over-cursor
3451 create or replace function forc01() returns void as $$
3453 c cursor(r1 integer, r2 integer)
3454 for select * from generate_series(r1,r2) i;
3456 for select * from generate_series(41,43) i;
3458 for r in c(5,7) loop
3459 raise notice '% from %', r.i, c;
3461 -- again, to test if cursor was closed properly
3462 for r in c(9,10) loop
3463 raise notice '% from %', r.i, c;
3465 -- and test a parameterless cursor
3467 raise notice '% from %', r.i, c2;
3469 -- and try it with a hand-assigned name
3470 raise notice 'after loop, c2 = %', c2;
3471 c2 := 'special_name';
3473 raise notice '% from %', r.i, c2;
3475 raise notice 'after loop, c2 = %', c2;
3476 -- and try it with a generated name
3477 -- (which we can't show in the output because it's variable)
3480 raise notice '%', r.i;
3482 raise notice 'after loop, c2 = %', c2;
3485 $$ language plpgsql;
3495 NOTICE: after loop, c2 = c2
3496 NOTICE: 41 from special_name
3497 NOTICE: 42 from special_name
3498 NOTICE: 43 from special_name
3499 NOTICE: after loop, c2 = special_name
3503 NOTICE: after loop, c2 = <NULL>
3509 -- try updating the cursor's current row
3510 create temp table forc_test as
3511 select n as i, n as j from generate_series(1,10) n;
3512 create or replace function forc01() returns void as $$
3514 c cursor for select * from forc_test;
3517 raise notice '%, %', r.i, r.j;
3518 update forc_test set i = i * 100, j = r.j * 2 where current of c;
3521 $$ language plpgsql;
3538 select * from forc_test;
3553 -- same, with a cursor whose portal name doesn't match variable name
3554 create or replace function forc01() returns void as $$
3556 c refcursor := 'fooled_ya';
3559 open c for select * from forc_test;
3562 exit when not found;
3563 raise notice '%, %', r.i, r.j;
3564 update forc_test set i = i * 100, j = r.j * 2 where current of c;
3567 $$ language plpgsql;
3584 select * from forc_test;
3599 drop function forc01();
3600 -- fail because cursor has no query bound to it
3601 create or replace function forc_bad() returns void as $$
3606 raise notice '%', r.i;
3609 $$ language plpgsql;
3610 ERROR: cursor FOR loop must use a bound cursor variable
3611 LINE 5: for r in c loop
3613 -- test RETURN QUERY EXECUTE
3614 create or replace function return_dquery()
3615 returns setof int as $$
3617 return query execute 'select * from (values(10),(20)) f';
3618 return query execute 'select * from (values($1),($2)) f' using 40,50;
3620 $$ language plpgsql;
3621 select * from return_dquery();
3630 drop function return_dquery();
3631 -- test RETURN QUERY with dropped columns
3632 create table tabwithcols(a int, b int, c int, d int);
3633 insert into tabwithcols values(10,20,30,40),(50,60,70,80);
3634 create or replace function returnqueryf()
3635 returns setof tabwithcols as $$
3637 return query select * from tabwithcols;
3638 return query execute 'select * from tabwithcols';
3640 $$ language plpgsql;
3641 select * from returnqueryf();
3650 alter table tabwithcols drop column b;
3651 select * from returnqueryf();
3660 alter table tabwithcols drop column d;
3661 select * from returnqueryf();
3670 alter table tabwithcols add column d int;
3671 select * from returnqueryf();
3680 drop function returnqueryf();
3681 drop table tabwithcols;
3683 -- Tests for composite-type results
3685 create type compostype as (x int, y varchar);
3686 -- test: use of variable of composite type in return statement
3687 create or replace function compos() returns compostype as $$
3694 $$ language plpgsql;
3701 -- test: use of variable of record type in return statement
3702 create or replace function compos() returns compostype as $$
3706 v := (1, 'hello'::varchar);
3709 $$ language plpgsql;
3716 -- test: use of row expr in return statement
3717 create or replace function compos() returns compostype as $$
3719 return (1, 'hello'::varchar);
3721 $$ language plpgsql;
3728 -- this does not work currently (no implicit casting)
3729 create or replace function compos() returns compostype as $$
3731 return (1, 'hello');
3733 $$ language plpgsql;
3735 ERROR: returned record type does not match expected record type
3736 DETAIL: Returned type unknown does not match expected type character varying in column 2.
3737 CONTEXT: PL/pgSQL function compos() while casting return value to function's return type
3738 -- ... but this does
3739 create or replace function compos() returns compostype as $$
3741 return (1, 'hello')::compostype;
3743 $$ language plpgsql;
3750 drop function compos();
3751 -- test: return a row expr as record.
3752 create or replace function composrec() returns record as $$
3759 $$ language plpgsql;
3766 -- test: return row expr in return statement.
3767 create or replace function composrec() returns record as $$
3769 return (1, 'hello');
3771 $$ language plpgsql;
3778 drop function composrec();
3779 -- test: row expr in RETURN NEXT statement.
3780 create or replace function compos() returns setof compostype as $$
3784 return next (1, 'hello'::varchar);
3786 return next null::compostype;
3787 return next (2, 'goodbye')::compostype;
3789 $$ language plpgsql;
3790 select * from compos();
3800 drop function compos();
3801 -- test: use invalid expr in return statement.
3802 create or replace function compos() returns compostype as $$
3806 $$ language plpgsql;
3808 ERROR: cannot return non-composite value from function returning composite type
3809 CONTEXT: PL/pgSQL function compos() line 3 at RETURN
3810 -- RETURN variable is a different code path ...
3811 create or replace function compos() returns compostype as $$
3812 declare x int := 42;
3816 $$ language plpgsql;
3817 select * from compos();
3818 ERROR: cannot return non-composite value from function returning composite type
3819 CONTEXT: PL/pgSQL function compos() line 4 at RETURN
3820 drop function compos();
3821 -- test: invalid use of composite variable in scalar-returning function
3822 create or replace function compos() returns int as $$
3829 $$ language plpgsql;
3831 ERROR: invalid input syntax for type integer: "(1,hello)"
3832 CONTEXT: PL/pgSQL function compos() while casting return value to function's return type
3833 -- test: invalid use of composite expression in scalar-returning function
3834 create or replace function compos() returns int as $$
3836 return (1, 'hello')::compostype;
3838 $$ language plpgsql;
3840 ERROR: invalid input syntax for type integer: "(1,hello)"
3841 CONTEXT: PL/pgSQL function compos() while casting return value to function's return type
3842 drop function compos();
3843 drop type compostype;
3845 -- Tests for 8.4's new RAISE features
3847 create or replace function raise_test() returns void as $$
3849 raise notice '% % %', 1, 2, 3
3850 using errcode = '55001', detail = 'some detail info', hint = 'some hint';
3851 raise '% % %', 1, 2, 3
3852 using errcode = 'division_by_zero', detail = 'some detail info';
3854 $$ language plpgsql;
3855 select raise_test();
3857 DETAIL: some detail info
3860 DETAIL: some detail info
3861 CONTEXT: PL/pgSQL function raise_test() line 5 at RAISE
3862 -- Since we can't actually see the thrown SQLSTATE in default psql output,
3863 -- test it like this; this also tests re-RAISE
3864 create or replace function raise_test() returns void as $$
3867 using errcode = 'division_by_zero', detail = 'some detail info';
3870 raise notice 'SQLSTATE: % SQLERRM: %', sqlstate, sqlerrm;
3873 $$ language plpgsql;
3874 select raise_test();
3875 NOTICE: SQLSTATE: 22012 SQLERRM: check me
3877 DETAIL: some detail info
3878 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3879 create or replace function raise_test() returns void as $$
3882 using errcode = '1234F', detail = 'some detail info';
3885 raise notice 'SQLSTATE: % SQLERRM: %', sqlstate, sqlerrm;
3888 $$ language plpgsql;
3889 select raise_test();
3890 NOTICE: SQLSTATE: 1234F SQLERRM: check me
3892 DETAIL: some detail info
3893 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3894 -- SQLSTATE specification in WHEN
3895 create or replace function raise_test() returns void as $$
3898 using errcode = '1234F', detail = 'some detail info';
3900 when sqlstate '1234F' then
3901 raise notice 'SQLSTATE: % SQLERRM: %', sqlstate, sqlerrm;
3904 $$ language plpgsql;
3905 select raise_test();
3906 NOTICE: SQLSTATE: 1234F SQLERRM: check me
3908 DETAIL: some detail info
3909 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3910 create or replace function raise_test() returns void as $$
3912 raise division_by_zero using detail = 'some detail info';
3915 raise notice 'SQLSTATE: % SQLERRM: %', sqlstate, sqlerrm;
3918 $$ language plpgsql;
3919 select raise_test();
3920 NOTICE: SQLSTATE: 22012 SQLERRM: division_by_zero
3921 ERROR: division_by_zero
3922 DETAIL: some detail info
3923 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3924 create or replace function raise_test() returns void as $$
3926 raise division_by_zero;
3928 $$ language plpgsql;
3929 select raise_test();
3930 ERROR: division_by_zero
3931 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3932 create or replace function raise_test() returns void as $$
3934 raise sqlstate '1234F';
3936 $$ language plpgsql;
3937 select raise_test();
3939 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3940 create or replace function raise_test() returns void as $$
3942 raise division_by_zero using message = 'custom' || ' message';
3944 $$ language plpgsql;
3945 select raise_test();
3946 ERROR: custom message
3947 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3948 create or replace function raise_test() returns void as $$
3950 raise using message = 'custom' || ' message', errcode = '22012';
3952 $$ language plpgsql;
3953 select raise_test();
3954 ERROR: custom message
3955 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3956 -- conflict on message
3957 create or replace function raise_test() returns void as $$
3959 raise notice 'some message' using message = 'custom' || ' message', errcode = '22012';
3961 $$ language plpgsql;
3962 select raise_test();
3963 ERROR: RAISE option already specified: MESSAGE
3964 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3965 -- conflict on errcode
3966 create or replace function raise_test() returns void as $$
3968 raise division_by_zero using message = 'custom' || ' message', errcode = '22012';
3970 $$ language plpgsql;
3971 select raise_test();
3972 ERROR: RAISE option already specified: ERRCODE
3973 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3974 -- nothing to re-RAISE
3975 create or replace function raise_test() returns void as $$
3979 $$ language plpgsql;
3980 select raise_test();
3981 ERROR: RAISE without parameters cannot be used outside an exception handler
3982 CONTEXT: PL/pgSQL function raise_test() line 3 at RAISE
3983 -- test access to exception data
3984 create function zero_divide() returns int as $$
3989 $$ language plpgsql;
3990 create or replace function raise_test() returns void as $$
3992 raise exception 'custom exception'
3993 using detail = 'some detail of custom exception',
3994 hint = 'some hint related to custom exception';
3996 $$ language plpgsql;
3997 create function stacked_diagnostics_test() returns void as $$
3998 declare _sqlstate text;
4002 perform zero_divide();
4003 exception when others then
4004 get stacked diagnostics
4005 _sqlstate = returned_sqlstate,
4006 _message = message_text,
4007 _context = pg_exception_context;
4008 raise notice 'sqlstate: %, message: %, context: [%]',
4009 _sqlstate, _message, replace(_context, E'\n', ' <- ');
4011 $$ language plpgsql;
4012 select stacked_diagnostics_test();
4013 NOTICE: sqlstate: 22012, message: division by zero, context: [PL/pgSQL function zero_divide() line 4 at RETURN <- SQL statement "SELECT zero_divide()" <- PL/pgSQL function stacked_diagnostics_test() line 6 at PERFORM]
4014 stacked_diagnostics_test
4015 --------------------------
4019 create or replace function stacked_diagnostics_test() returns void as $$
4020 declare _detail text;
4024 perform raise_test();
4025 exception when others then
4026 get stacked diagnostics
4027 _message = message_text,
4028 _detail = pg_exception_detail,
4029 _hint = pg_exception_hint;
4030 raise notice 'message: %, detail: %, hint: %', _message, _detail, _hint;
4032 $$ language plpgsql;
4033 select stacked_diagnostics_test();
4034 NOTICE: message: custom exception, detail: some detail of custom exception, hint: some hint related to custom exception
4035 stacked_diagnostics_test
4036 --------------------------
4040 -- fail, cannot use stacked diagnostics statement outside handler
4041 create or replace function stacked_diagnostics_test() returns void as $$
4042 declare _detail text;
4046 get stacked diagnostics
4047 _message = message_text,
4048 _detail = pg_exception_detail,
4049 _hint = pg_exception_hint;
4050 raise notice 'message: %, detail: %, hint: %', _message, _detail, _hint;
4052 $$ language plpgsql;
4053 select stacked_diagnostics_test();
4054 ERROR: GET STACKED DIAGNOSTICS cannot be used outside an exception handler
4055 CONTEXT: PL/pgSQL function stacked_diagnostics_test() line 6 at GET STACKED DIAGNOSTICS
4056 drop function zero_divide();
4057 drop function stacked_diagnostics_test();
4058 -- check cases where implicit SQLSTATE variable could be confused with
4059 -- SQLSTATE as a keyword, cf bug #5524
4060 create or replace function raise_test() returns void as $$
4064 when sqlstate '22012' then
4065 raise notice using message = sqlstate;
4066 raise sqlstate '22012' using message = 'substitute message';
4068 $$ language plpgsql;
4069 select raise_test();
4071 ERROR: substitute message
4072 CONTEXT: PL/pgSQL function raise_test() line 7 at RAISE
4073 drop function raise_test();
4074 -- test passing column_name, constraint_name, datatype_name, table_name
4075 -- and schema_name error fields
4076 create or replace function stacked_diagnostics_test() returns void as $$
4077 declare _column_name text;
4078 _constraint_name text;
4079 _datatype_name text;
4083 raise exception using
4084 column = '>>some column name<<',
4085 constraint = '>>some constraint name<<',
4086 datatype = '>>some datatype name<<',
4087 table = '>>some table name<<',
4088 schema = '>>some schema name<<';
4089 exception when others then
4090 get stacked diagnostics
4091 _column_name = column_name,
4092 _constraint_name = constraint_name,
4093 _datatype_name = pg_datatype_name,
4094 _table_name = table_name,
4095 _schema_name = schema_name;
4096 raise notice 'column %, constraint %, type %, table %, schema %',
4097 _column_name, _constraint_name, _datatype_name, _table_name, _schema_name;
4099 $$ language plpgsql;
4100 select stacked_diagnostics_test();
4101 NOTICE: column >>some column name<<, constraint >>some constraint name<<, type >>some datatype name<<, table >>some table name<<, schema >>some schema name<<
4102 stacked_diagnostics_test
4103 --------------------------
4107 drop function stacked_diagnostics_test();
4108 -- test variadic functions
4109 create or replace function vari(variadic int[])
4112 for i in array_lower($1,1)..array_upper($1,1) loop
4113 raise notice '%', $1[i];
4115 $$ language plpgsql;
4116 select vari(1,2,3,4,5);
4136 select vari(variadic array[5,6,7]);
4145 drop function vari(int[]);
4147 create or replace function pleast(variadic numeric[])
4148 returns numeric as $$
4149 declare aux numeric = $1[array_lower($1,1)];
4151 for i in array_lower($1,1)+1..array_upper($1,1) loop
4152 if $1[i] < aux then aux := $1[i]; end if;
4156 $$ language plpgsql immutable strict;
4157 select pleast(10,1,2,3,-16);
4163 select pleast(10.2,2.2,-1.1);
4169 select pleast(10.2,10, -20);
4175 select pleast(10,20, -1.0);
4181 -- in case of conflict, non-variadic version is preferred
4182 create or replace function pleast(numeric)
4183 returns numeric as $$
4185 raise notice 'non-variadic function called';
4188 $$ language plpgsql immutable strict;
4190 NOTICE: non-variadic function called
4196 drop function pleast(numeric[]);
4197 drop function pleast(numeric);
4198 -- test table functions
4199 create function tftest(int) returns table(a int, b int) as $$
4201 return query select $1, $1+i from generate_series(1,5) g(i);
4203 $$ language plpgsql immutable strict;
4204 select * from tftest(10);
4214 create or replace function tftest(a1 int) returns table(a int, b int) as $$
4216 a := a1; b := a1 + 1;
4218 a := a1 * 10; b := a1 * 10 + 1;
4221 $$ language plpgsql immutable strict;
4222 select * from tftest(10);
4229 drop function tftest(int);
4230 create or replace function rttest()
4231 returns setof int as $$
4234 return query values(10),(20);
4235 get diagnostics rc = row_count;
4236 raise notice '% %', found, rc;
4237 return query select * from (values(10),(20)) f(a) where false;
4238 get diagnostics rc = row_count;
4239 raise notice '% %', found, rc;
4240 return query execute 'values(10),(20)';
4241 get diagnostics rc = row_count;
4242 raise notice '% %', found, rc;
4243 return query execute 'select * from (values(10),(20)) f(a) where false';
4244 get diagnostics rc = row_count;
4245 raise notice '% %', found, rc;
4247 $$ language plpgsql;
4248 select * from rttest();
4261 drop function rttest();
4262 -- Test for proper cleanup at subtransaction exit. This example
4263 -- exposed a bug in PG 8.2.
4264 CREATE FUNCTION leaker_1(fail BOOL) RETURNS INTEGER AS $$
4269 v_var := (leaker_2(fail)).error_code;
4271 WHEN others THEN RETURN 0;
4275 $$ LANGUAGE plpgsql;
4276 CREATE FUNCTION leaker_2(fail BOOL, OUT error_code INTEGER, OUT new_id INTEGER)
4277 RETURNS RECORD AS $$
4280 RAISE EXCEPTION 'fail ...';
4286 $$ LANGUAGE plpgsql;
4287 SELECT * FROM leaker_1(false);
4293 SELECT * FROM leaker_1(true);
4299 DROP FUNCTION leaker_1(bool);
4300 DROP FUNCTION leaker_2(bool);
4301 -- Test for appropriate cleanup of non-simple expression evaluations
4302 -- (bug in all versions prior to August 2010)
4303 CREATE FUNCTION nonsimple_expr_test() RETURNS text[] AS $$
4309 arr := array[array['foo','bar'], array['baz', 'quux']];
4312 -- use sub-SELECTs to make expressions non-simple
4313 arr[(SELECT i)][(SELECT i+1)] := (SELECT lr);
4316 $$ LANGUAGE plpgsql;
4317 SELECT nonsimple_expr_test();
4319 -------------------------
4320 {{foo,fool},{baz,quux}}
4323 DROP FUNCTION nonsimple_expr_test();
4324 CREATE FUNCTION nonsimple_expr_test() RETURNS integer AS $$
4326 i integer NOT NULL := 0;
4329 i := (SELECT NULL::integer); -- should throw error
4332 i := (SELECT 1::integer);
4336 $$ LANGUAGE plpgsql;
4337 SELECT nonsimple_expr_test();
4339 ---------------------
4343 DROP FUNCTION nonsimple_expr_test();
4345 -- Test cases involving recursion and error recovery in simple expressions
4346 -- (bugs in all versions before October 2010). The problems are most
4347 -- easily exposed by mutual recursion between plpgsql and sql functions.
4349 create function recurse(float8) returns float8 as
4353 return sql_recurse($1 - 1);
4358 $$ language plpgsql;
4359 -- "limit" is to prevent this from being inlined
4360 create function sql_recurse(float8) returns float8 as
4361 $$ select recurse($1) limit 1; $$ language sql;
4368 create function error1(text) returns text language sql as
4369 $$ SELECT relname::text FROM pg_class c WHERE c.oid = $1::regclass $$;
4370 create function error2(p_name_table text) returns text language plpgsql as $$
4372 return error1(p_name_table);
4375 create table public.stuffs (stuff text);
4377 select error2('nonexistent.stuffs');
4378 ERROR: schema "nonexistent" does not exist
4379 CONTEXT: SQL function "error1" statement 1
4380 PL/pgSQL function error2(text) line 3 at RETURN
4382 select error2('public.stuffs');
4389 drop function error2(p_name_table text);
4390 drop function error1(text);
4391 -- Test for proper handling of cast-expression caching
4392 create function sql_to_date(integer) returns date as $$
4393 select $1::text::date
4394 $$ language sql immutable strict;
4395 create cast (integer as date) with function sql_to_date(integer) as assignment;
4396 create function cast_invoker(integer) returns date as $$
4399 end$$ language plpgsql;
4400 select cast_invoker(20150717);
4406 select cast_invoker(20150718); -- second call crashed in pre-release 9.5
4413 select cast_invoker(20150717);
4419 select cast_invoker(20150718);
4426 select cast_invoker(20150718);
4432 select cast_invoker(-1); -- fails
4433 ERROR: invalid input syntax for type date: "-1"
4434 CONTEXT: SQL function "sql_to_date" statement 1
4435 PL/pgSQL function cast_invoker(integer) while casting return value to function's return type
4436 rollback to savepoint s1;
4437 select cast_invoker(20150719);
4443 select cast_invoker(20150720);
4450 drop function cast_invoker(integer);
4451 drop function sql_to_date(integer) cascade;
4452 NOTICE: drop cascades to cast from integer to date
4453 -- Test handling of cast cache inside DO blocks
4454 -- (to check the original crash case, this must be a cast not previously
4455 -- used in this session)
4457 do $$ declare x text[]; begin x := '{1.23, 4.56}'::numeric[]; end $$;
4458 do $$ declare x text[]; begin x := '{1.23, 4.56}'::numeric[]; end $$;
4460 -- Test for consistent reporting of error context
4461 create function fail() returns int language plpgsql as $$
4467 ERROR: division by zero
4468 CONTEXT: SQL expression "1/0"
4469 PL/pgSQL function fail() line 3 at RETURN
4471 ERROR: division by zero
4472 CONTEXT: SQL expression "1/0"
4473 PL/pgSQL function fail() line 3 at RETURN
4474 drop function fail();
4475 -- Test handling of string literals.
4476 set standard_conforming_strings = off;
4477 create or replace function strtest() returns text as $$
4479 raise notice 'foo\\bar\041baz';
4480 return 'foo\\bar\041baz';
4482 $$ language plpgsql;
4483 WARNING: nonstandard use of \\ in a string literal
4484 LINE 3: raise notice 'foo\\bar\041baz';
4486 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
4487 WARNING: nonstandard use of \\ in a string literal
4488 LINE 4: return 'foo\\bar\041baz';
4490 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
4491 WARNING: nonstandard use of \\ in a string literal
4492 LINE 4: return 'foo\\bar\041baz';
4494 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
4497 WARNING: nonstandard use of \\ in a string literal
4498 LINE 1: 'foo\\bar\041baz'
4500 HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
4501 QUERY: 'foo\\bar\041baz'
4507 create or replace function strtest() returns text as $$
4509 raise notice E'foo\\bar\041baz';
4510 return E'foo\\bar\041baz';
4512 $$ language plpgsql;
4520 set standard_conforming_strings = on;
4521 create or replace function strtest() returns text as $$
4523 raise notice 'foo\\bar\041baz\';
4524 return 'foo\\bar\041baz\';
4526 $$ language plpgsql;
4528 NOTICE: foo\\bar\041baz\
4534 create or replace function strtest() returns text as $$
4536 raise notice E'foo\\bar\041baz';
4537 return E'foo\\bar\041baz';
4539 $$ language plpgsql;
4547 drop function strtest();
4548 -- Test anonymous code blocks.
4552 FOR r IN SELECT rtrim(roomno) AS roomno, comment FROM Room ORDER BY roomno
4554 RAISE NOTICE '%, %', r.roomno, r.comment;
4557 NOTICE: 001, Entrance
4560 NOTICE: 004, Technical
4562 NOTICE: 102, Conference
4563 NOTICE: 103, Restroom
4564 NOTICE: 104, Technical
4567 -- these are to check syntax error reporting
4568 DO LANGUAGE plpgsql $$begin return 1; end$$;
4569 ERROR: RETURN cannot have a parameter in function returning void
4570 LINE 1: DO LANGUAGE plpgsql $$begin return 1; end$$;
4575 FOR r IN SELECT rtrim(roomno) AS roomno, foo FROM Room ORDER BY roomno
4577 RAISE NOTICE '%, %', r.roomno, r.comment;
4580 ERROR: column "foo" does not exist
4581 LINE 1: SELECT rtrim(roomno) AS roomno, foo FROM Room ORDER BY roomn...
4583 QUERY: SELECT rtrim(roomno) AS roomno, foo FROM Room ORDER BY roomno
4584 CONTEXT: PL/pgSQL function inline_code_block line 4 at FOR over SELECT rows
4585 -- Check handling of errors thrown from/into anonymous code blocks.
4598 exception when division_by_zero then
4599 raise notice 'caught division by zero';
4604 NOTICE: caught division by zero
4605 NOTICE: caught division by zero
4606 NOTICE: caught division by zero
4607 NOTICE: caught division by zero
4608 NOTICE: caught division by zero
4609 NOTICE: caught division by zero
4610 NOTICE: caught division by zero
4611 NOTICE: caught division by zero
4612 NOTICE: caught division by zero
4613 NOTICE: caught division by zero
4614 -- Check variable scoping -- a var is not available in its own or prior
4615 -- default expressions.
4616 create function scope_test() returns int as $$
4617 declare x int := 42;
4619 declare y int := x + 1;
4625 $$ language plpgsql;
4626 select scope_test();
4632 drop function scope_test();
4633 -- Check handling of conflicts between plpgsql vars and table columns.
4634 set plpgsql.variable_conflict = error;
4635 create function conflict_test() returns setof int8_tbl as $$
4639 for r in select q1,q2 from int8_tbl loop
4643 $$ language plpgsql;
4644 select * from conflict_test();
4645 ERROR: column reference "q1" is ambiguous
4646 LINE 1: select q1,q2 from int8_tbl
4648 DETAIL: It could refer to either a PL/pgSQL variable or a table column.
4649 QUERY: select q1,q2 from int8_tbl
4650 CONTEXT: PL/pgSQL function conflict_test() line 5 at FOR over SELECT rows
4651 create or replace function conflict_test() returns setof int8_tbl as $$
4652 #variable_conflict use_variable
4656 for r in select q1,q2 from int8_tbl loop
4660 $$ language plpgsql;
4661 select * from conflict_test();
4663 ----+-------------------
4665 42 | 4567890123456789
4667 42 | 4567890123456789
4668 42 | -4567890123456789
4671 create or replace function conflict_test() returns setof int8_tbl as $$
4672 #variable_conflict use_column
4676 for r in select q1,q2 from int8_tbl loop
4680 $$ language plpgsql;
4681 select * from conflict_test();
4683 ------------------+-------------------
4685 123 | 4567890123456789
4686 4567890123456789 | 123
4687 4567890123456789 | 4567890123456789
4688 4567890123456789 | -4567890123456789
4691 drop function conflict_test();
4692 -- Check that an unreserved keyword can be used as a variable name
4693 create function unreserved_test() returns int as $$
4697 forward := forward * 2;
4700 $$ language plpgsql;
4701 select unreserved_test();
4707 create or replace function unreserved_test() returns int as $$
4711 return := return + 1;
4714 $$ language plpgsql;
4715 select unreserved_test();
4721 create or replace function unreserved_test() returns int as $$
4725 comment := comment * 2;
4726 comment on function unreserved_test() is 'this is a test';
4729 $$ language plpgsql;
4730 select unreserved_test();
4736 select obj_description('unreserved_test()'::regprocedure, 'pg_proc');
4742 drop function unreserved_test();
4744 -- Test FOREACH over arrays
4746 create function foreach_test(anyarray)
4750 foreach x in array $1
4752 raise notice '%', x;
4755 $$ language plpgsql;
4756 select foreach_test(ARRAY[1,2,3,4]);
4766 select foreach_test(ARRAY[[1,2],[3,4]]);
4776 create or replace function foreach_test(anyarray)
4780 foreach x slice 1 in array $1
4782 raise notice '%', x;
4785 $$ language plpgsql;
4787 select foreach_test(ARRAY[1,2,3,4]);
4788 ERROR: FOREACH ... SLICE loop variable must be of an array type
4789 CONTEXT: PL/pgSQL function foreach_test(anyarray) line 4 at FOREACH over array
4790 select foreach_test(ARRAY[[1,2],[3,4]]);
4791 ERROR: FOREACH ... SLICE loop variable must be of an array type
4792 CONTEXT: PL/pgSQL function foreach_test(anyarray) line 4 at FOREACH over array
4793 create or replace function foreach_test(anyarray)
4797 foreach x slice 1 in array $1
4799 raise notice '%', x;
4802 $$ language plpgsql;
4803 select foreach_test(ARRAY[1,2,3,4]);
4810 select foreach_test(ARRAY[[1,2],[3,4]]);
4818 -- higher level of slicing
4819 create or replace function foreach_test(anyarray)
4823 foreach x slice 2 in array $1
4825 raise notice '%', x;
4828 $$ language plpgsql;
4830 select foreach_test(ARRAY[1,2,3,4]);
4831 ERROR: slice dimension (2) is out of the valid range 0..1
4832 CONTEXT: PL/pgSQL function foreach_test(anyarray) line 4 at FOREACH over array
4834 select foreach_test(ARRAY[[1,2],[3,4]]);
4835 NOTICE: {{1,2},{3,4}}
4841 select foreach_test(ARRAY[[[1,2]],[[3,4]]]);
4849 create type xy_tuple AS (x int, y int);
4850 -- iteration over array of records
4851 create or replace function foreach_test(anyarray)
4855 foreach r in array $1
4857 raise notice '%', r;
4860 $$ language plpgsql;
4861 select foreach_test(ARRAY[(10,20),(40,69),(35,78)]::xy_tuple[]);
4870 select foreach_test(ARRAY[[(10,20),(40,69)],[(35,78),(88,76)]]::xy_tuple[]);
4880 create or replace function foreach_test(anyarray)
4882 declare x int; y int;
4884 foreach x, y in array $1
4886 raise notice 'x = %, y = %', x, y;
4889 $$ language plpgsql;
4890 select foreach_test(ARRAY[(10,20),(40,69),(35,78)]::xy_tuple[]);
4891 NOTICE: x = 10, y = 20
4892 NOTICE: x = 40, y = 69
4893 NOTICE: x = 35, y = 78
4899 select foreach_test(ARRAY[[(10,20),(40,69)],[(35,78),(88,76)]]::xy_tuple[]);
4900 NOTICE: x = 10, y = 20
4901 NOTICE: x = 40, y = 69
4902 NOTICE: x = 35, y = 78
4903 NOTICE: x = 88, y = 76
4909 -- slicing over array of composite types
4910 create or replace function foreach_test(anyarray)
4912 declare x xy_tuple[];
4914 foreach x slice 1 in array $1
4916 raise notice '%', x;
4919 $$ language plpgsql;
4920 select foreach_test(ARRAY[(10,20),(40,69),(35,78)]::xy_tuple[]);
4921 NOTICE: {"(10,20)","(40,69)","(35,78)"}
4927 select foreach_test(ARRAY[[(10,20),(40,69)],[(35,78),(88,76)]]::xy_tuple[]);
4928 NOTICE: {"(10,20)","(40,69)"}
4929 NOTICE: {"(35,78)","(88,76)"}
4935 drop function foreach_test(anyarray);
4938 -- Assorted tests for array subscript assignment
4940 create temp table rtype (id int, ar text[]);
4941 create function arrayassign1() returns text[] language plpgsql as $$
4945 r := row(12, '{foo,bar,baz}')::rtype;
4946 r.ar[2] := 'replace';
4949 select arrayassign1();
4955 select arrayassign1(); -- try again to exercise internal caching
4961 create domain orderedarray as int[2]
4962 constraint sorted check (value[1] < value[2]);
4963 select '{1,2}'::orderedarray;
4969 select '{2,1}'::orderedarray; -- fail
4970 ERROR: value for domain orderedarray violates check constraint "sorted"
4971 create function testoa(x1 int, x2 int, x3 int) returns orderedarray
4972 language plpgsql as $$
4973 declare res orderedarray;
4975 res := array[x1, x2];
4979 select testoa(1,2,3);
4985 select testoa(1,2,3); -- try again to exercise internal caching
4991 select testoa(2,1,3); -- fail at initial assign
4992 ERROR: value for domain orderedarray violates check constraint "sorted"
4993 CONTEXT: PL/pgSQL function testoa(integer,integer,integer) line 4 at assignment
4994 select testoa(1,2,1); -- fail at update
4995 ERROR: value for domain orderedarray violates check constraint "sorted"
4996 CONTEXT: PL/pgSQL function testoa(integer,integer,integer) line 5 at assignment
4997 drop function arrayassign1();
4998 drop function testoa(x1 int, x2 int, x3 int);
5000 -- Test handling of expanded arrays
5002 create function returns_rw_array(int) returns int[]
5003 language plpgsql as $$
5005 begin r := array[$1, $1]; return r; end;
5007 create function consumes_rw_array(int[]) returns int
5008 language plpgsql as $$
5009 begin return $1[1]; end;
5011 select consumes_rw_array(returns_rw_array(42));
5018 explain (verbose, costs off)
5020 (select returns_rw_array(1) as a offset 0) ss,
5021 lateral consumes_rw_array(a) i;
5023 -----------------------------------------------------------------
5025 Output: i.i, (returns_rw_array(1))
5027 Output: returns_rw_array(1)
5028 -> Function Scan on public.consumes_rw_array i
5030 Function Call: consumes_rw_array((returns_rw_array(1)))
5034 (select returns_rw_array(1) as a offset 0) ss,
5035 lateral consumes_rw_array(a) i;
5041 explain (verbose, costs off)
5042 select consumes_rw_array(a), a from returns_rw_array(1) a;
5044 --------------------------------------------
5045 Function Scan on public.returns_rw_array a
5046 Output: consumes_rw_array(a), a
5047 Function Call: returns_rw_array(1)
5050 select consumes_rw_array(a), a from returns_rw_array(1) a;
5051 consumes_rw_array | a
5052 -------------------+-------
5056 explain (verbose, costs off)
5057 select consumes_rw_array(a), a from
5058 (values (returns_rw_array(1)), (returns_rw_array(2))) v(a);
5060 ---------------------------------------------------------------------
5061 Values Scan on "*VALUES*"
5062 Output: consumes_rw_array("*VALUES*".column1), "*VALUES*".column1
5065 select consumes_rw_array(a), a from
5066 (values (returns_rw_array(1)), (returns_rw_array(2))) v(a);
5067 consumes_rw_array | a
5068 -------------------+-------
5074 declare a int[] := array[1,2];
5077 raise notice 'a = %', a;
5081 -- Test access to call stack
5083 create function inner_func(int)
5085 declare _context text;
5087 get diagnostics _context = pg_context;
5088 raise notice '***%***', _context;
5089 -- lets do it again, just for fun..
5090 get diagnostics _context = pg_context;
5091 raise notice '***%***', _context;
5092 raise notice 'lets make sure we didnt break anything';
5095 $$ language plpgsql;
5096 create or replace function outer_func(int)
5101 raise notice 'calling down into inner_func()';
5102 myresult := inner_func($1);
5103 raise notice 'inner_func() done';
5106 $$ language plpgsql;
5107 create or replace function outer_outer_func(int)
5112 raise notice 'calling down into outer_func()';
5113 myresult := outer_func($1);
5114 raise notice 'outer_func() done';
5117 $$ language plpgsql;
5118 select outer_outer_func(10);
5119 NOTICE: calling down into outer_func()
5120 NOTICE: calling down into inner_func()
5121 NOTICE: ***PL/pgSQL function inner_func(integer) line 4 at GET DIAGNOSTICS
5122 PL/pgSQL function outer_func(integer) line 6 at assignment
5123 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5124 NOTICE: ***PL/pgSQL function inner_func(integer) line 7 at GET DIAGNOSTICS
5125 PL/pgSQL function outer_func(integer) line 6 at assignment
5126 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5127 NOTICE: lets make sure we didnt break anything
5128 NOTICE: inner_func() done
5129 NOTICE: outer_func() done
5135 -- repeated call should to work
5136 select outer_outer_func(20);
5137 NOTICE: calling down into outer_func()
5138 NOTICE: calling down into inner_func()
5139 NOTICE: ***PL/pgSQL function inner_func(integer) line 4 at GET DIAGNOSTICS
5140 PL/pgSQL function outer_func(integer) line 6 at assignment
5141 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5142 NOTICE: ***PL/pgSQL function inner_func(integer) line 7 at GET DIAGNOSTICS
5143 PL/pgSQL function outer_func(integer) line 6 at assignment
5144 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5145 NOTICE: lets make sure we didnt break anything
5146 NOTICE: inner_func() done
5147 NOTICE: outer_func() done
5153 drop function outer_outer_func(int);
5154 drop function outer_func(int);
5155 drop function inner_func(int);
5156 -- access to call stack from exception
5157 create function inner_func(int)
5166 when division_by_zero then
5167 get diagnostics _context = pg_context;
5168 raise notice '***%***', _context;
5171 -- lets do it again, just for fun..
5172 get diagnostics _context = pg_context;
5173 raise notice '***%***', _context;
5174 raise notice 'lets make sure we didnt break anything';
5177 $$ language plpgsql;
5178 create or replace function outer_func(int)
5183 raise notice 'calling down into inner_func()';
5184 myresult := inner_func($1);
5185 raise notice 'inner_func() done';
5188 $$ language plpgsql;
5189 create or replace function outer_outer_func(int)
5194 raise notice 'calling down into outer_func()';
5195 myresult := outer_func($1);
5196 raise notice 'outer_func() done';
5199 $$ language plpgsql;
5200 select outer_outer_func(10);
5201 NOTICE: calling down into outer_func()
5202 NOTICE: calling down into inner_func()
5203 NOTICE: ***PL/pgSQL function inner_func(integer) line 10 at GET DIAGNOSTICS
5204 PL/pgSQL function outer_func(integer) line 6 at assignment
5205 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5206 NOTICE: ***PL/pgSQL function inner_func(integer) line 15 at GET DIAGNOSTICS
5207 PL/pgSQL function outer_func(integer) line 6 at assignment
5208 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5209 NOTICE: lets make sure we didnt break anything
5210 NOTICE: inner_func() done
5211 NOTICE: outer_func() done
5217 -- repeated call should to work
5218 select outer_outer_func(20);
5219 NOTICE: calling down into outer_func()
5220 NOTICE: calling down into inner_func()
5221 NOTICE: ***PL/pgSQL function inner_func(integer) line 10 at GET DIAGNOSTICS
5222 PL/pgSQL function outer_func(integer) line 6 at assignment
5223 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5224 NOTICE: ***PL/pgSQL function inner_func(integer) line 15 at GET DIAGNOSTICS
5225 PL/pgSQL function outer_func(integer) line 6 at assignment
5226 PL/pgSQL function outer_outer_func(integer) line 6 at assignment***
5227 NOTICE: lets make sure we didnt break anything
5228 NOTICE: inner_func() done
5229 NOTICE: outer_func() done
5235 drop function outer_outer_func(int);
5236 drop function outer_func(int);
5237 drop function inner_func(int);
5243 assert 1=1; -- should succeed
5248 assert 1=0; -- should fail
5251 ERROR: assertion failed
5252 CONTEXT: PL/pgSQL function inline_code_block line 3 at ASSERT
5255 assert NULL; -- should fail
5258 ERROR: assertion failed
5259 CONTEXT: PL/pgSQL function inline_code_block line 3 at ASSERT
5260 -- check controlling GUC
5261 set plpgsql.check_asserts = off;
5264 assert 1=0; -- won't be tested
5267 reset plpgsql.check_asserts;
5268 -- test custom message
5270 declare var text := 'some value';
5272 assert 1=0, format('assertion failed, var = "%s"', var);
5275 ERROR: assertion failed, var = "some value"
5276 CONTEXT: PL/pgSQL function inline_code_block line 4 at ASSERT
5277 -- ensure assertions are not trapped by 'others'
5280 assert 1=0, 'unhandled assertion';
5281 exception when others then
5285 ERROR: unhandled assertion
5286 CONTEXT: PL/pgSQL function inline_code_block line 3 at ASSERT
5287 -- Test use of plpgsql in a domain check constraint (cf. bug #14414)
5288 create function plpgsql_domain_check(val int) returns boolean as $$
5289 begin return val > 0; end
5290 $$ language plpgsql immutable;
5291 create domain plpgsql_domain as integer check(plpgsql_domain_check(value));
5293 declare v_test plpgsql_domain;
5299 declare v_test plpgsql_domain := 1;
5301 v_test := 0; -- fail
5304 ERROR: value for domain plpgsql_domain violates check constraint "plpgsql_domain_check"
5305 CONTEXT: PL/pgSQL function inline_code_block line 4 at assignment
5306 -- Test handling of expanded array passed to a domain constraint (bug #14472)
5307 create function plpgsql_arr_domain_check(val int[]) returns boolean as $$
5308 begin return val[1] > 0; end
5309 $$ language plpgsql immutable;
5310 create domain plpgsql_arr_domain as int[] check(plpgsql_arr_domain_check(value));
5312 declare v_test plpgsql_arr_domain;
5315 v_test := v_test || 2;
5319 declare v_test plpgsql_arr_domain := array[1];
5321 v_test := 0 || v_test; -- fail
5324 ERROR: value for domain plpgsql_arr_domain violates check constraint "plpgsql_arr_domain_check"
5325 CONTEXT: PL/pgSQL function inline_code_block line 4 at assignment
5327 -- test usage of transition tables in AFTER triggers
5329 CREATE TABLE transition_table_base (id int PRIMARY KEY, val text);
5330 CREATE FUNCTION transition_table_base_ins_func()
5341 EXPLAIN (TIMING off, COSTS off, VERBOSE on)
5342 SELECT * FROM newtable
5344 t = t || l || E'\n';
5351 CREATE TRIGGER transition_table_base_ins_trig
5352 AFTER INSERT ON transition_table_base
5353 REFERENCING OLD TABLE AS oldtable NEW TABLE AS newtable
5355 EXECUTE PROCEDURE transition_table_base_ins_func();
5356 ERROR: OLD TABLE can only be specified for a DELETE or UPDATE trigger
5357 CREATE TRIGGER transition_table_base_ins_trig
5358 AFTER INSERT ON transition_table_base
5359 REFERENCING NEW TABLE AS newtable
5361 EXECUTE PROCEDURE transition_table_base_ins_func();
5362 INSERT INTO transition_table_base VALUES (1, 'One'), (2, 'Two');
5363 INFO: Named Tuplestore Scan
5366 INSERT INTO transition_table_base VALUES (3, 'Three'), (4, 'Four');
5367 INFO: Named Tuplestore Scan
5370 CREATE OR REPLACE FUNCTION transition_table_base_upd_func()
5381 EXPLAIN (TIMING off, COSTS off, VERBOSE on)
5382 SELECT * FROM oldtable ot FULL JOIN newtable nt USING (id)
5384 t = t || l || E'\n';
5391 CREATE TRIGGER transition_table_base_upd_trig
5392 AFTER UPDATE ON transition_table_base
5393 REFERENCING OLD TABLE AS oldtable NEW TABLE AS newtable
5395 EXECUTE PROCEDURE transition_table_base_upd_func();
5396 UPDATE transition_table_base
5397 SET val = '*' || val || '*'
5398 WHERE id BETWEEN 2 AND 3;
5399 INFO: Hash Full Join
5400 Output: COALESCE(ot.id, nt.id), ot.val, nt.val
5401 Hash Cond: (ot.id = nt.id)
5402 -> Named Tuplestore Scan
5403 Output: ot.id, ot.val
5405 Output: nt.id, nt.val
5406 -> Named Tuplestore Scan
5407 Output: nt.id, nt.val
5409 CREATE TABLE transition_table_level1
5411 level1_no serial NOT NULL ,
5412 level1_node_name varchar(255),
5413 PRIMARY KEY (level1_no)
5415 CREATE TABLE transition_table_level2
5417 level2_no serial NOT NULL ,
5418 parent_no int NOT NULL,
5419 level1_node_name varchar(255),
5420 PRIMARY KEY (level2_no)
5422 CREATE TABLE transition_table_status
5425 node_no int NOT NULL,
5427 PRIMARY KEY (level, node_no)
5429 CREATE FUNCTION transition_table_level1_ri_parent_del_func()
5435 PERFORM FROM p JOIN transition_table_level2 c ON c.parent_no = p.level1_no;
5437 RAISE EXCEPTION 'RI error';
5442 CREATE TRIGGER transition_table_level1_ri_parent_del_trigger
5443 AFTER DELETE ON transition_table_level1
5444 REFERENCING OLD TABLE AS p
5445 FOR EACH STATEMENT EXECUTE PROCEDURE
5446 transition_table_level1_ri_parent_del_func();
5447 CREATE FUNCTION transition_table_level1_ri_parent_upd_func()
5454 WITH p AS (SELECT level1_no, sum(delta) cnt
5455 FROM (SELECT level1_no, 1 AS delta FROM i
5457 SELECT level1_no, -1 AS delta FROM d) w
5459 HAVING sum(delta) < 0)
5461 FROM p JOIN transition_table_level2 c ON c.parent_no = p.level1_no
5464 RAISE EXCEPTION 'RI error';
5469 CREATE TRIGGER transition_table_level1_ri_parent_upd_trigger
5470 AFTER UPDATE ON transition_table_level1
5471 REFERENCING OLD TABLE AS d NEW TABLE AS i
5472 FOR EACH STATEMENT EXECUTE PROCEDURE
5473 transition_table_level1_ri_parent_upd_func();
5474 CREATE FUNCTION transition_table_level2_ri_child_insupd_func()
5480 LEFT JOIN transition_table_level1 p
5481 ON p.level1_no IS NOT NULL AND p.level1_no = i.parent_no
5482 WHERE p.level1_no IS NULL;
5484 RAISE EXCEPTION 'RI error';
5489 CREATE TRIGGER transition_table_level2_ri_child_ins_trigger
5490 AFTER INSERT ON transition_table_level2
5491 REFERENCING NEW TABLE AS i
5492 FOR EACH STATEMENT EXECUTE PROCEDURE
5493 transition_table_level2_ri_child_insupd_func();
5494 CREATE TRIGGER transition_table_level2_ri_child_upd_trigger
5495 AFTER UPDATE ON transition_table_level2
5496 REFERENCING NEW TABLE AS i
5497 FOR EACH STATEMENT EXECUTE PROCEDURE
5498 transition_table_level2_ri_child_insupd_func();
5499 -- create initial test data
5500 INSERT INTO transition_table_level1 (level1_no)
5501 SELECT generate_series(1,200);
5502 ANALYZE transition_table_level1;
5503 INSERT INTO transition_table_level2 (level2_no, parent_no)
5504 SELECT level2_no, level2_no / 50 + 1 AS parent_no
5505 FROM generate_series(1,9999) level2_no;
5506 ANALYZE transition_table_level2;
5507 INSERT INTO transition_table_status (level, node_no, status)
5508 SELECT 1, level1_no, 0 FROM transition_table_level1;
5509 INSERT INTO transition_table_status (level, node_no, status)
5510 SELECT 2, level2_no, 0 FROM transition_table_level2;
5511 ANALYZE transition_table_status;
5512 INSERT INTO transition_table_level1(level1_no)
5513 SELECT generate_series(201,1000);
5514 ANALYZE transition_table_level1;
5515 -- behave reasonably if someone tries to modify a transition table
5516 CREATE FUNCTION transition_table_level2_bad_usage_func()
5521 INSERT INTO dx VALUES (1000000, 1000000, 'x');
5525 CREATE TRIGGER transition_table_level2_bad_usage_trigger
5526 AFTER DELETE ON transition_table_level2
5527 REFERENCING OLD TABLE AS dx
5528 FOR EACH STATEMENT EXECUTE PROCEDURE
5529 transition_table_level2_bad_usage_func();
5530 DELETE FROM transition_table_level2
5531 WHERE level2_no BETWEEN 301 AND 305;
5532 ERROR: relation "dx" cannot be the target of a modifying statement
5533 CONTEXT: SQL statement "INSERT INTO dx VALUES (1000000, 1000000, 'x')"
5534 PL/pgSQL function transition_table_level2_bad_usage_func() line 3 at SQL statement
5535 DROP TRIGGER transition_table_level2_bad_usage_trigger
5536 ON transition_table_level2;
5537 -- attempt modifications which would break RI (should all fail)
5538 DELETE FROM transition_table_level1
5539 WHERE level1_no = 25;
5541 CONTEXT: PL/pgSQL function transition_table_level1_ri_parent_del_func() line 6 at RAISE
5542 UPDATE transition_table_level1 SET level1_no = -1
5543 WHERE level1_no = 30;
5545 CONTEXT: PL/pgSQL function transition_table_level1_ri_parent_upd_func() line 15 at RAISE
5546 INSERT INTO transition_table_level2 (level2_no, parent_no)
5547 VALUES (10000, 10000);
5549 CONTEXT: PL/pgSQL function transition_table_level2_ri_child_insupd_func() line 8 at RAISE
5550 UPDATE transition_table_level2 SET parent_no = 2000
5551 WHERE level2_no = 40;
5553 CONTEXT: PL/pgSQL function transition_table_level2_ri_child_insupd_func() line 8 at RAISE
5554 -- attempt modifications which would not break RI (should all succeed)
5555 DELETE FROM transition_table_level1
5556 WHERE level1_no BETWEEN 201 AND 1000;
5557 DELETE FROM transition_table_level1
5558 WHERE level1_no BETWEEN 100000000 AND 100000010;
5559 SELECT count(*) FROM transition_table_level1;
5565 DELETE FROM transition_table_level2
5566 WHERE level2_no BETWEEN 211 AND 220;
5567 SELECT count(*) FROM transition_table_level2;
5573 CREATE TABLE alter_table_under_transition_tables
5578 CREATE FUNCTION alter_table_under_transition_tables_upd_func()
5583 RAISE WARNING 'old table = %, new table = %',
5584 (SELECT string_agg(id || '=' || name, ',') FROM d),
5585 (SELECT string_agg(id || '=' || name, ',') FROM i);
5586 RAISE NOTICE 'one = %', (SELECT 1 FROM alter_table_under_transition_tables LIMIT 1);
5590 -- should fail, TRUNCATE is not compatible with transition tables
5591 CREATE TRIGGER alter_table_under_transition_tables_upd_trigger
5592 AFTER TRUNCATE OR UPDATE ON alter_table_under_transition_tables
5593 REFERENCING OLD TABLE AS d NEW TABLE AS i
5594 FOR EACH STATEMENT EXECUTE PROCEDURE
5595 alter_table_under_transition_tables_upd_func();
5596 ERROR: TRUNCATE triggers with transition tables are not supported
5598 CREATE TRIGGER alter_table_under_transition_tables_upd_trigger
5599 AFTER UPDATE ON alter_table_under_transition_tables
5600 REFERENCING OLD TABLE AS d NEW TABLE AS i
5601 FOR EACH STATEMENT EXECUTE PROCEDURE
5602 alter_table_under_transition_tables_upd_func();
5603 INSERT INTO alter_table_under_transition_tables
5604 VALUES (1, '1'), (2, '2'), (3, '3');
5605 UPDATE alter_table_under_transition_tables
5606 SET name = name || name;
5607 WARNING: old table = 1=1,2=2,3=3, new table = 1=11,2=22,3=33
5609 -- now change 'name' to an integer to see what happens...
5610 ALTER TABLE alter_table_under_transition_tables
5611 ALTER COLUMN name TYPE int USING name::integer;
5612 UPDATE alter_table_under_transition_tables
5613 SET name = (name::text || name::text)::integer;
5614 WARNING: old table = 1=11,2=22,3=33, new table = 1=1111,2=2222,3=3333
5616 -- now drop column 'name'
5617 ALTER TABLE alter_table_under_transition_tables
5619 UPDATE alter_table_under_transition_tables
5621 ERROR: column "name" does not exist
5622 LINE 1: (SELECT string_agg(id || '=' || name, ',') FROM d)
5624 QUERY: (SELECT string_agg(id || '=' || name, ',') FROM d)
5625 CONTEXT: PL/pgSQL function alter_table_under_transition_tables_upd_func() line 3 at RAISE
5627 -- Test multiple reference to a transition table
5629 CREATE TABLE multi_test (i int);
5630 INSERT INTO multi_test VALUES (1);
5631 CREATE OR REPLACE FUNCTION multi_test_trig() RETURNS trigger
5632 LANGUAGE plpgsql AS $$
5634 RAISE NOTICE 'count = %', (SELECT COUNT(*) FROM new_test);
5635 RAISE NOTICE 'count union = %',
5637 FROM (SELECT * FROM new_test UNION ALL SELECT * FROM new_test) ss);
5640 CREATE TRIGGER my_trigger AFTER UPDATE ON multi_test
5641 REFERENCING NEW TABLE AS new_test OLD TABLE as old_test
5642 FOR EACH STATEMENT EXECUTE PROCEDURE multi_test_trig();
5643 UPDATE multi_test SET i = i;
5645 NOTICE: count union = 2
5646 DROP TABLE multi_test;
5647 DROP FUNCTION multi_test_trig();
5649 -- Check type parsing and record fetching from partitioned tables
5651 CREATE TABLE partitioned_table (a int, b text) PARTITION BY LIST (a);
5652 CREATE TABLE pt_part1 PARTITION OF partitioned_table FOR VALUES IN (1);
5653 CREATE TABLE pt_part2 PARTITION OF partitioned_table FOR VALUES IN (2);
5654 INSERT INTO partitioned_table VALUES (1, 'Row 1');
5655 INSERT INTO partitioned_table VALUES (2, 'Row 2');
5656 CREATE OR REPLACE FUNCTION get_from_partitioned_table(partitioned_table.a%type)
5657 RETURNS partitioned_table AS $$
5659 a_val partitioned_table.a%TYPE;
5660 result partitioned_table%ROWTYPE;
5663 SELECT * INTO result FROM partitioned_table WHERE a = a_val;
5665 END; $$ LANGUAGE plpgsql;
5666 NOTICE: type reference partitioned_table.a%TYPE converted to integer
5667 SELECT * FROM get_from_partitioned_table(1) AS t;
5673 CREATE OR REPLACE FUNCTION list_partitioned_table()
5674 RETURNS SETOF partitioned_table.a%TYPE AS $$
5676 row partitioned_table%ROWTYPE;
5677 a_val partitioned_table.a%TYPE;
5679 FOR row IN SELECT * FROM partitioned_table ORDER BY a LOOP
5684 END; $$ LANGUAGE plpgsql;
5685 NOTICE: type reference partitioned_table.a%TYPE converted to integer
5686 SELECT * FROM list_partitioned_table() AS t;
5694 -- Check argument name is used instead of $n in error message
5696 CREATE FUNCTION fx(x WSlot) RETURNS void AS $$
5698 GET DIAGNOSTICS x = ROW_COUNT;
5700 END; $$ LANGUAGE plpgsql;
5701 ERROR: "x" is not a scalar variable
5702 LINE 3: GET DIAGNOSTICS x = ROW_COUNT;