@@ -344,6 +344,12 @@ SELECT '[1.234, 5.678]'::floatrange;
344344 function in this example.
345345 </para>
346346
347+ <para>
348+ Defining your own range type also allows you to specify a different
349+ subtype B-tree operator class or collation to use, so as to change the sort
350+ ordering that determines which values fall into a given range.
351+ </para>
352+
347353 <para>
348354 If the subtype is considered to have discrete rather than continuous
349355 values, the <command>CREATE TYPE</> command should specify a
@@ -365,29 +371,40 @@ SELECT '[1.234, 5.678]'::floatrange;
365371 </para>
366372
367373 <para>
368- Defining your own range type also allows you to specify a different
369- subtype B-tree operator class or collation to use, so as to change the sort
370- ordering that determines which values fall into a given range.
374+ In addition, any range type that is meant to be used with GiST or SP-GiST
375+ indexes should define a subtype difference, or <literal>subtype_diff</>,
376+ function. (The index will still work without <literal>subtype_diff</>,
377+ but it is likely to be considerably less efficient than if a difference
378+ function is provided.) The subtype difference function takes two input
379+ values of the subtype, and returns their difference
380+ (i.e., <replaceable>X</> minus <replaceable>Y</>) represented as
381+ a <type>float8</> value. In our example above, the
382+ function <function>float8mi</> that underlies the regular <type>float8</>
383+ minus operator can be used; but for any other subtype, some type
384+ conversion would be necessary. Some creative thought about how to
385+ represent differences as numbers might be needed, too. To the greatest
386+ extent possible, the <literal>subtype_diff</> function should agree with
387+ the sort ordering implied by the selected operator class and collation;
388+ that is, its result should be positive whenever its first argument is
389+ greater than its second according to the sort ordering.
371390 </para>
372391
373392 <para>
374- In addition, any range type that is meant to be used with GiST or SP-GiST indexes
375- should define a subtype difference, or <literal>subtype_diff</>, function.
376- (the index will still work without <literal>subtype_diff</>, but it is
377- likely to be considerably less efficient than if a difference function is
378- provided.) The subtype difference function takes two input values of the
379- subtype, and returns their difference (i.e., <replaceable>X</> minus
380- <replaceable>Y</>) represented as a <type>float8</> value. In our example
381- above, the function that underlies the regular <type>float8</> minus
382- operator can be used; but for any other subtype, some type conversion would
383- be necessary. Some creative thought about how to represent differences as
384- numbers might be needed, too. To the greatest extent possible, the
385- <literal>subtype_diff</> function should agree with the sort ordering
386- implied by the selected operator class and collation; that is, its result
387- should be positive whenever its first argument is greater than its second
388- according to the sort ordering.
393+ A less-oversimplified example of a <literal>subtype_diff</> function is:
389394 </para>
390395
396+ <programlisting>
397+ CREATE FUNCTION time_subtype_diff(x time, y time) RETURNS float8 AS
398+ 'SELECT EXTRACT(EPOCH FROM (x - y))' LANGUAGE sql STRICT IMMUTABLE;
399+
400+ CREATE TYPE timerange AS RANGE (
401+ subtype = time,
402+ subtype_diff = time_subtype_diff
403+ );
404+
405+ SELECT '[11:10, 23:00]'::timerange;
406+ </programlisting>
407+
391408 <para>
392409 See <xref linkend="SQL-CREATETYPE"> for more information about creating
393410 range types.
0 commit comments