1 -- PostRR - src/postrr.sql
2 -- Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 -- All rights reserved.
4 --
5 -- Redistribution and use in source and binary forms, with or without
6 -- modification, are permitted provided that the following conditions
7 -- are met:
8 -- 1. Redistributions of source code must retain the above copyright
9 -- notice, this list of conditions and the following disclaimer.
10 -- 2. Redistributions in binary form must reproduce the above copyright
11 -- notice, this list of conditions and the following disclaimer in the
12 -- documentation and/or other materials provided with the distribution.
13 --
14 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 -- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 -- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 -- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 -- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 -- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 -- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 -- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 -- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 --
27 -- PostRR - PostgreSQL Round-Robin Extension
28 --
30 -- suppress messages like 'return type foo is only a shell'
31 SET client_min_messages TO WARNING;
33 BEGIN;
35 CREATE SCHEMA postrr;
37 CREATE SEQUENCE postrr.tsid INCREMENT BY 1
38 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 NO CYCLE;
40 CREATE TABLE postrr.rrtimeslices (
41 tsid integer NOT NULL PRIMARY KEY
42 DEFAULT nextval('postrr.tsid'::regclass)
43 CHECK (0 < tsid),
44 tslen integer NOT NULL,
45 tsnum integer NOT NULL
46 );
48 CREATE OR REPLACE FUNCTION PostRR_Version()
49 RETURNS cstring
50 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'postrr_version'
51 LANGUAGE 'C' IMMUTABLE;
53 CREATE TYPE RRTimeslice;
55 CREATE OR REPLACE FUNCTION RRTimeslice_validate(integer)
56 RETURNS cstring
57 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_validate'
58 LANGUAGE 'C' IMMUTABLE STRICT;
60 -- this will abort the transaction in case the expected internal length does
61 -- not match the actual length
62 SELECT RRTimeslice_validate(16);
64 CREATE OR REPLACE FUNCTION RRTimeslice_in(cstring, oid, integer)
65 RETURNS RRTimeslice
66 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_in'
67 LANGUAGE 'C' IMMUTABLE STRICT;
69 CREATE OR REPLACE FUNCTION RRTimeslice_out(RRTimeslice)
70 RETURNS cstring
71 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_out'
72 LANGUAGE 'C' IMMUTABLE STRICT;
74 CREATE OR REPLACE FUNCTION RRTimeslice_typmodin(cstring[])
75 RETURNS integer
76 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_typmodin'
77 LANGUAGE 'C' IMMUTABLE STRICT;
79 CREATE OR REPLACE FUNCTION RRTimeslice_typmodout(integer)
80 RETURNS cstring
81 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_typmodout'
82 LANGUAGE 'C' IMMUTABLE STRICT;
84 CREATE TYPE RRTimeslice (
85 INTERNALLENGTH = 16,
86 INPUT = RRTimeslice_in,
87 OUTPUT = RRTimeslice_out,
88 TYPMOD_IN = RRTimeslice_typmodin,
89 TYPMOD_OUT = RRTimeslice_typmodout,
90 ALIGNMENT = double,
91 STORAGE = plain
92 );
94 CREATE OR REPLACE FUNCTION RRTimeslice(rrtimeslice, integer, boolean)
95 RETURNS rrtimeslice
96 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_to_rrtimeslice'
97 LANGUAGE 'C' IMMUTABLE STRICT;
99 CREATE CAST (rrtimeslice AS rrtimeslice)
100 WITH FUNCTION RRTimeslice(rrtimeslice, integer, boolean)
101 AS IMPLICIT;
103 CREATE OR REPLACE FUNCTION Tstamp(rrtimeslice)
104 RETURNS timestamp
105 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_to_timestamp'
106 LANGUAGE 'C' IMMUTABLE STRICT;
108 CREATE CAST (rrtimeslice AS timestamp)
109 WITH FUNCTION Tstamp(rrtimeslice);
110 -- EXPLICIT
112 CREATE OR REPLACE FUNCTION rrtimeslice_seq_eq(rrtimeslice, rrtimeslice)
113 RETURNS boolean
114 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_eq'
115 LANGUAGE 'C' IMMUTABLE STRICT;
117 CREATE OR REPLACE FUNCTION rrtimeslice_seq_ne(rrtimeslice, rrtimeslice)
118 RETURNS boolean
119 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_ne'
120 LANGUAGE 'C' IMMUTABLE STRICT;
122 CREATE OR REPLACE FUNCTION rrtimeslice_seq_lt(rrtimeslice, rrtimeslice)
123 RETURNS boolean
124 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_lt'
125 LANGUAGE 'C' IMMUTABLE STRICT;
127 CREATE OR REPLACE FUNCTION rrtimeslice_seq_le(rrtimeslice, rrtimeslice)
128 RETURNS boolean
129 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_le'
130 LANGUAGE 'C' IMMUTABLE STRICT;
132 CREATE OR REPLACE FUNCTION rrtimeslice_seq_gt(rrtimeslice, rrtimeslice)
133 RETURNS boolean
134 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_gt'
135 LANGUAGE 'C' IMMUTABLE STRICT;
137 CREATE OR REPLACE FUNCTION rrtimeslice_seq_ge(rrtimeslice, rrtimeslice)
138 RETURNS boolean
139 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_ge'
140 LANGUAGE 'C' IMMUTABLE STRICT;
142 CREATE OR REPLACE FUNCTION rrtimeslice_seq_cmp(rrtimeslice, rrtimeslice)
143 RETURNS integer
144 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_cmp'
145 LANGUAGE 'C' IMMUTABLE STRICT;
147 CREATE OR REPLACE FUNCTION rrtimeslice_seq_hash(rrtimeslice)
148 RETURNS integer
149 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_seq_hash'
150 LANGUAGE 'C' IMMUTABLE STRICT;
152 CREATE OPERATOR = (
153 LEFTARG = RRTimeslice,
154 RIGHTARG = RRTimeslice,
155 PROCEDURE = rrtimeslice_seq_eq,
156 COMMUTATOR = =,
157 NEGATOR = <>,
158 RESTRICT = eqsel
159 );
161 CREATE OPERATOR <> (
162 LEFTARG = RRTimeslice,
163 RIGHTARG = RRTimeslice,
164 PROCEDURE = rrtimeslice_seq_ne,
165 COMMUTATOR = <>,
166 NEGATOR = =,
167 RESTRICT = neqsel
168 );
170 CREATE OPERATOR < (
171 LEFTARG = RRTimeslice,
172 RIGHTARG = RRTimeslice,
173 PROCEDURE = rrtimeslice_seq_lt,
174 COMMUTATOR = >,
175 NEGATOR = <=,
176 RESTRICT = scalarltsel
177 );
179 CREATE OPERATOR <= (
180 LEFTARG = RRTimeslice,
181 RIGHTARG = RRTimeslice,
182 PROCEDURE = rrtimeslice_seq_le,
183 COMMUTATOR = >=,
184 NEGATOR = <,
185 RESTRICT = scalarltsel
186 );
188 CREATE OPERATOR > (
189 LEFTARG = RRTimeslice,
190 RIGHTARG = RRTimeslice,
191 PROCEDURE = rrtimeslice_seq_gt,
192 COMMUTATOR = <,
193 NEGATOR = >=,
194 RESTRICT = scalargtsel
195 );
197 CREATE OPERATOR >= (
198 LEFTARG = RRTimeslice,
199 RIGHTARG = RRTimeslice,
200 PROCEDURE = rrtimeslice_seq_ge,
201 COMMUTATOR = <=,
202 NEGATOR = >,
203 RESTRICT = scalargtsel
204 );
206 CREATE OPERATOR CLASS rrtimeslice_ops
207 DEFAULT FOR TYPE RRTimeslice USING btree AS
208 OPERATOR 1 < ,
209 OPERATOR 2 <= ,
210 OPERATOR 3 = ,
211 OPERATOR 4 >= ,
212 OPERATOR 5 > ,
213 FUNCTION 1 rrtimeslice_seq_cmp(rrtimeslice, rrtimeslice);
215 CREATE OPERATOR CLASS rrtimeslice_hash_ops
216 FOR TYPE RRTimeslice USING hash AS
217 OPERATOR 1 = ,
218 FUNCTION 1 rrtimeslice_seq_hash(rrtimeslice);
220 CREATE TYPE CData;
222 CREATE OR REPLACE FUNCTION CData_validate(integer)
223 RETURNS cstring
224 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_validate'
225 LANGUAGE 'C' IMMUTABLE STRICT;
227 -- this will abort the transaction in case the expected internal length does
228 -- not match the actual length
229 SELECT CData_validate(24);
231 CREATE OR REPLACE FUNCTION CData_in(cstring, oid, integer)
232 RETURNS CData
233 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_in'
234 LANGUAGE 'C' IMMUTABLE STRICT;
236 CREATE OR REPLACE FUNCTION CData_out(CData)
237 RETURNS cstring
238 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_out'
239 LANGUAGE 'C' IMMUTABLE STRICT;
241 CREATE OR REPLACE FUNCTION CData_typmodin(cstring[])
242 RETURNS integer
243 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_typmodin'
244 LANGUAGE 'C' IMMUTABLE STRICT;
246 CREATE OR REPLACE FUNCTION CData_typmodout(integer)
247 RETURNS cstring
248 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_typmodout'
249 LANGUAGE 'C' IMMUTABLE STRICT;
251 CREATE TYPE CData (
252 INTERNALLENGTH = 24,
253 INPUT = CData_in,
254 OUTPUT = CData_out,
255 TYPMOD_IN = CData_typmodin,
256 TYPMOD_OUT = CData_typmodout,
257 ALIGNMENT = double,
258 STORAGE = plain
259 );
261 CREATE OR REPLACE FUNCTION CData(cdata, integer, boolean)
262 RETURNS cdata
263 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_to_cdata'
264 LANGUAGE 'C' IMMUTABLE STRICT;
266 CREATE CAST (cdata AS cdata)
267 WITH FUNCTION CData(cdata, integer, boolean)
268 AS IMPLICIT;
270 CREATE CAST (numeric AS cdata)
271 WITH INOUT
272 AS ASSIGNMENT;
274 CREATE OR REPLACE FUNCTION CData(integer, integer, boolean)
275 RETURNS cdata
276 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'int32_to_cdata'
277 LANGUAGE 'C' IMMUTABLE STRICT;
279 CREATE CAST (integer AS cdata)
280 WITH FUNCTION CData(integer, integer, boolean)
281 AS ASSIGNMENT;
283 CREATE OR REPLACE FUNCTION Float8(cdata)
284 RETURNS double precision
285 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_to_float8'
286 LANGUAGE 'C' IMMUTABLE STRICT;
288 CREATE CAST (cdata AS double precision)
289 WITH FUNCTION Float8(cdata);
290 -- EXPLICIT
292 CREATE OR REPLACE FUNCTION CData_update(cdata, cdata)
293 RETURNS cdata
294 AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'cdata_update'
295 LANGUAGE 'C' IMMUTABLE STRICT;
297 COMMIT;
299 SET client_min_messages TO DEFAULT;
301 -- vim: set tw=78 sw=4 ts=4 noexpandtab :