aboutsummaryrefslogtreecommitdiff
path: root/doc/src/sgml/ltree.sgml
blob: f98555c31acaea44c02f93ed0b005b9cd55e018b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
<sect1 id="ltree">
 <title>ltree</title>
 
 <indexterm zone="ltree">
  <primary>ltree</primary>
 </indexterm>

 <para>
  <literal>ltree</literal> is a PostgreSQL module that contains implementation 
  of data types, indexed access methods and queries for data organized as a 
  tree-like structures.
 </para>

 <sect2>
  <title>Definitions</title>
  <para>
   A <firstterm>label</firstterm> of a node is a sequence of one or more words 
   separated by blank character '_' and containing letters and digits ( for 
   example, [a-zA-Z0-9] for C locale). The length of a label is limited by 256 
   bytes.
  </para>
  <para>
   Example: 'Countries', 'Personal_Services'
  </para>
  <para>
   A <firstterm>label path</firstterm> of a node is a sequence of one or more 
   dot-separated labels l1.l2...ln, represents path from root to the node. The 
   length of a label path is limited by 65Kb, but size &lt;= 2Kb is preferrable. 
   We consider it's not a strict limitation (maximal size of label path for 
   DMOZ catalogue - <ulink url="http://www.dmoz.org"></ulink>, is about 240 
   bytes!)
  </para>
  <para>
   Example: <literal>'Top.Countries.Europe.Russia'</literal>
  </para>
  <para>
   We introduce several datatypes:
  </para>
  <itemizedlist>
   <listitem>
    <para>
     <literal>ltree</literal> - is a datatype for label path.
    </para>
   </listitem> 
   <listitem>
    <para>
     <literal>ltree[]</literal> - is a datatype for arrays of ltree.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>lquery</literal>
     - is a path expression that has regular expression in the label path and
     used for ltree matching. Star symbol (*) is used to specify any number of
     labels (levels) and could be used at the beginning and the end of lquery,
     for example, '*.Europe.*'.
    </para>
    <para>
     The following quantifiers are recognized for '*' (like in Perl):
    </para>
    <itemizedlist>
     <listitem>
      <para>{n}    Match exactly n levels</para>
     </listitem>
     <listitem>
      <para>{n,}   Match at least n levels</para>
     </listitem>
     <listitem>
      <para>{n,m}  Match at least n but not more than m levels</para>
     </listitem>
     <listitem>
      <para>{,m}   Match at maximum m levels (eq. to {0,m})</para>
     </listitem>
    </itemizedlist>
    <para>
     It is possible to use several modifiers at the end of a label:
    </para>
    <itemizedlist>
     <listitem>
      <para>@     Do case-insensitive label matching</para>
     </listitem>
     <listitem>
      <para>*     Do prefix matching for a label</para>
     </listitem>
     <listitem>
      <para>%     Don't account word separator '_' in label matching, that is 
                  'Russian%' would match 'Russian_nations', but not 'Russian'
      </para>
     </listitem>
    </itemizedlist>
   
    <para>
     <literal>lquery</literal> can contain logical '!' (NOT) at the beginning 
     of the label and '|' (OR) to specify possible alternatives for label 
     matching.
    </para>
    <para> 
     Example of <literal>lquery</literal>:
    </para>
    <programlisting> 
     Top.*{0,2}.sport*@.!football|tennis.Russ*|Spain
     a)  b)     c)      d)               e)
    </programlisting>
    <para>
    A label path should
    </para>
    <orderedlist numeration='loweralpha'>
     <listitem>
      <para>
       begin from a node with label 'Top'
      </para>
     </listitem>
     <listitem>
      <para>
       and following zero or 2 labels until
      </para>
     </listitem>
     <listitem>
      <para>
       a node with label beginning from case-insensitive prefix 'sport'
      </para>
     </listitem>
     <listitem>
      <para>
       following node with label not matched 'football' or 'tennis' and
      </para>
     </listitem>
     <listitem>
      <para>
       end on node with label beginning from 'Russ' or strictly matched
        'Spain'.
      </para>
     </listitem>
    </orderedlist>

   </listitem>

   <listitem>
    <para><literal>ltxtquery</literal>
    - is a datatype for label searching (like type 'query' for full text
    searching, see contrib/tsearch). It's possible to use modifiers @,%,* at
    the end of word. The meaning of modifiers are the same as for lquery.
    </para>
    <para>
     Example: <literal>'Europe &amp; Russia*@ &amp; !Transportation'</literal>
    </para>
    <para>
     Search paths contain words 'Europe' and 'Russia*' (case-insensitive) and
     not 'Transportation'. Notice, the order of words as they appear in label
     path is not important !
    </para>
   </listitem>

  </itemizedlist>
 </sect2>

 <sect2>
  <title>Operations</title>
  <para>
   The following operations are defined for type ltree:
  </para>

  <itemizedlist>
   <listitem>
    <para>
     <literal>&lt;,&gt;,&lt;=,&gt;=,=, &lt;&gt;</literal>
     - Have their usual meanings. Comparison is doing in the order of direct
     tree traversing, children of a node are sorted lexicographic.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree @&gt; ltree</literal>
      - returns TRUE if left argument is an ancestor of right argument (or
      equal).
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree &lt;@ ltree </literal>
     - returns TRUE if left argument is a descendant of right argument (or
     equal).
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree ~ lquery, lquery ~ ltree</literal>
     - return TRUE if node represented by ltree satisfies lquery.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree ? lquery[], lquery ? ltree[]</literal>
     - return TRUE if node represented by ltree satisfies at least one lquery
       from array.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree @ ltxtquery, ltxtquery @ ltree</literal>
     - return TRUE if node represented by ltree satisfies ltxtquery.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree || ltree, ltree || text, text || ltree</literal>
     - return concatenated ltree.
    </para>
   </listitem>
  </itemizedlist>
  
  <para>
   Operations for arrays of ltree (<literal>ltree[]</literal>):
  </para>
  <itemizedlist>
   <listitem>
    <para>
     <literal>ltree[] @&gt; ltree, ltree &lt;@ ltree[]</literal>
     - returns TRUE if array ltree[] contains an ancestor of ltree.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree @&gt; ltree[], ltree[] &lt;@ ltree</literal>
     - returns TRUE if array ltree[] contains a descendant of ltree.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree[] ~ lquery, lquery ~ ltree[]</literal>
     - returns TRUE if array ltree[] contains label paths matched lquery.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree[] ? lquery[], lquery[] ? ltree[]</literal>
     - returns TRUE if array ltree[] contains label paths matched atleaset one
       lquery from array.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree[] @ ltxtquery, ltxtquery @ ltree[]</literal>
     - returns TRUE if array ltree[] contains label paths matched ltxtquery
     (full text search).
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree[] ?@&gt; ltree, ltree ?&lt;@ ltree[], ltree[] ?~ lquery, ltree[] ?@ ltxtquery</literal>

     - returns first element of array ltree[] satisfies corresponding condition
     and NULL in vice versa.
    </para>
   </listitem>
  </itemizedlist>
 </sect2>

 <sect2>
  <title>Remark</title>

  <para>
   Operations <literal>&lt;@</literal>, <literal>@&gt;</literal>, <literal>@</literal> and 
   <literal>~</literal> have analogues - <literal>^&lt;@, ^@&gt;, ^@, ^~,</literal> which don't use
   indices!
  </para>
 </sect2>

 <sect2>
  <title>Indices</title>
  <para>
   Various indices could be created to speed up execution of operations:
  </para>

  <itemizedlist>
   <listitem>
    <para>
     B-tree index over ltree: <literal>&lt;, &lt;=, =, &gt;=, &gt;</literal>
    </para>
   </listitem>
   <listitem>
    <para>
     GiST index over ltree: <literal>&lt;, &lt;=, =, &gt;=, &gt;, @&gt;, &lt;@, @, ~, ?</literal>
    </para>
    <para>
     Example:
    </para>
    <programlisting>
     CREATE INDEX path_gist_idx ON test USING GIST (path);
    </programlisting>
   </listitem>
   <listitem>
    <para>GiST index over ltree[]:
     <literal>ltree[]&lt;@ ltree, ltree @&gt; ltree[], @, ~, ?.</literal>
    </para>
    <para>
     Example:
    </para>
    <programlisting>
    CREATE INDEX path_gist_idx ON test USING GIST (array_path);
    </programlisting>
    <para>
     Notices: This index is lossy.
    </para>
   </listitem>
  </itemizedlist>
 </sect2>

 <sect2>
  <title>Functions</title>
  
  <itemizedlist>
   <listitem>
    <para>
     <literal>ltree subltree(ltree, start, end)</literal>
      returns subpath of ltree from start (inclusive) until the end.
    </para>
    <programlisting>
        # select subltree('Top.Child1.Child2',1,2);
        subltree
        --------
        Child1
    </programlisting>
   </listitem>
   <listitem>
    <para>
     <literal>ltree subpath(ltree, OFFSET,LEN)</literal> and 
     <literal>ltree subpath(ltree, OFFSET)</literal>
      returns subpath of ltree from OFFSET (inclusive) with length LEN.
      If OFFSET is negative returns subpath starts that far from the end 
      of the path.  If LENGTH is omitted, returns everything to the end
      of the path.  If LENGTH is negative, leaves that many labels off 
      the end of the path.
    </para>
    <programlisting>
        # select subpath('Top.Child1.Child2',1,2);
        subpath
        -------
        Child1.Child2
    
        # select subpath('Top.Child1.Child2',-2,1);
        subpath 
        ---------
        Child1
    </programlisting>
   </listitem>
   <listitem>
    <para>
     <literal>int4 nlevel(ltree)</literal> - returns level of the node.
    </para>
    <programlisting>
        # select nlevel('Top.Child1.Child2');
        nlevel 
        --------
          3
    </programlisting>
    <para>
     Note, that arguments start, end, OFFSET, LEN have meaning of level of the
     node !
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>int4 index(ltree,ltree)</literal> and 
     <literal>int4 index(ltree,ltree,OFFSET)</literal>
     returns number of level of the first occurence of second argument in first
     one beginning from OFFSET. if OFFSET is negative, than search begins from |
     OFFSET| levels from the end of the path.
    </para>
    <programlisting>
     SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',3);
      index
     -------
          6
     SELECT index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-4);
      index  
     -------
          9
    </programlisting>
   </listitem>
   <listitem>
    <para>
     <literal>ltree text2ltree(text)</literal> and
     <literal>text ltree2text(text)</literal> cast functions for ltree and text.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>ltree lca(ltree,ltree,...) (up to 8 arguments)</literal> and
     <literal>ltree lca(ltree[])</literal> Returns Lowest Common Ancestor (lca).
    </para>
    <programlisting>
        # select lca('1.2.2.3','1.2.3.4.5.6');
        lca 
        -----
         1.2
        # select lca('{la.2.3,1.2.3.4.5.6}') is null;
        ?column? 
        ----------
           f
    </programlisting>
   </listitem>
  </itemizedlist>
 </sect2>

 <sect2>
  <title>Installation</title>
  <programlisting>
  cd contrib/ltree
  make
  make install
  make installcheck
  </programlisting>
 </sect2>

 <sect2>
  <title>Example</title>
  <programlisting>
 createdb ltreetest
 psql ltreetest &lt; /usr/local/pgsql/share/contrib/ltree.sql
 psql ltreetest &lt; ltreetest.sql
  </programlisting>

  <para>
Now, we have a database ltreetest populated with a data describing hierarchy
shown below:
  </para>

  <programlisting>

 
                            TOP
                         /   |  \     
                 Science Hobbies Collections  
                     /       |              \
            Astronomy   Amateurs_Astronomy Pictures
               /  \                            |
    Astrophysics  Cosmology                Astronomy
                                            /  |    \
                                     Galaxies Stars Astronauts
  </programlisting>
  <para>
   Inheritance:
  </para>

  <programlisting>
ltreetest=# select path from test where path &lt;@ 'Top.Science';
                path                
------------------------------------
 Top.Science
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
(4 rows)
  </programlisting>
  <para>
   Matching:
  </para>
  <programlisting>
ltreetest=# select path from test where path ~ '*.Astronomy.*';
                     path                      
-----------------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
 Top.Collections.Pictures.Astronomy
 Top.Collections.Pictures.Astronomy.Stars
 Top.Collections.Pictures.Astronomy.Galaxies
 Top.Collections.Pictures.Astronomy.Astronauts
(7 rows)
ltreetest=# select path from test where path ~ '*.!pictures@.*.Astronomy.*';
                path                
------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
(3 rows)
  </programlisting>
  <para>
   Full text search:
  </para>
  <programlisting>
ltreetest=# select path from test where path @ 'Astro*% &amp; !pictures@';
                path                
------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
 Top.Hobbies.Amateurs_Astronomy
(4 rows)

ltreetest=# select path from test where path @ 'Astro* &amp; !pictures@';
                path                
------------------------------------
 Top.Science.Astronomy
 Top.Science.Astronomy.Astrophysics
 Top.Science.Astronomy.Cosmology
(3 rows)
  </programlisting>
  <para>
   Using Functions:
  </para>
  <programlisting>
ltreetest=# select subpath(path,0,2)||'Space'||subpath(path,2) from test where path &lt;@ 'Top.Science.Astronomy';
                 ?column?                 
------------------------------------------
 Top.Science.Space.Astronomy
 Top.Science.Space.Astronomy.Astrophysics
 Top.Science.Space.Astronomy.Cosmology
(3 rows)
We could create SQL-function:
CREATE FUNCTION ins_label(ltree, int4, text) RETURNS ltree 
AS 'select subpath($1,0,$2) || $3 || subpath($1,$2);'
LANGUAGE SQL IMMUTABLE;
  </programlisting>
  <para>
   and previous select could be rewritten as:
  </para>

  <programlisting>
ltreetest=# select ins_label(path,2,'Space') from test where path &lt;@ 'Top.Science.Astronomy';
                ins_label                 
------------------------------------------
 Top.Science.Space.Astronomy
 Top.Science.Space.Astronomy.Astrophysics
 Top.Science.Space.Astronomy.Cosmology
(3 rows)
  </programlisting>

  <para>
   Or with another arguments:
  </para>

  <programlisting>
CREATE FUNCTION ins_label(ltree, ltree, text) RETURNS ltree
AS 'select subpath($1,0,nlevel($2)) || $3 || subpath($1,nlevel($2));'
LANGUAGE SQL IMMUTABLE;

ltreetest=# select ins_label(path,'Top.Science'::ltree,'Space') from test where path &lt;@ 'Top.Science.Astronomy';
                ins_label                 
------------------------------------------
 Top.Science.Space.Astronomy
 Top.Science.Space.Astronomy.Astrophysics
 Top.Science.Space.Astronomy.Cosmology
(3 rows)
  </programlisting>
 </sect2>

 <sect2>
  <title>Additional data</title>
  <para>
   To get more feeling from our ltree module you could download
   dmozltree-eng.sql.gz (about 3Mb tar.gz archive containing 300,274 nodes),
   available from 
   <ulink url="http://www.sai.msu.su/~megera/postgres/gist/ltree/"></ulink>
   dmozltree-eng.sql.gz, which is DMOZ catalogue, prepared for use with ltree.
   Setup your test database (dmoz), load ltree module and issue command:
  </para>
  <programlisting>
   zcat dmozltree-eng.sql.gz| psql dmoz
  </programlisting>
  <para>
   Data will be loaded into database dmoz and all indices will be created.
  </para>
 </sect2>

 <sect2>
  <title>Benchmarks</title>
  <para>
   All runs were performed on my IBM ThinkPad T21 (256 MB RAM, 750Mhz) using DMOZ
   data, containing 300,274 nodes (see above for download link). We used some
   basic queries typical for walking through catalog.
  </para>

  <sect3>
   <title>Queries</title>
   <itemizedlist>
    <listitem>
     <para>
      Q0: Count all rows (sort of base time for comparison)
     </para>
     <programlisting>
     select count(*) from dmoz;
      count  
     --------
      300274
     (1 row)
     </programlisting>
    </listitem>
    <listitem>
     <para>
      Q1: Get direct children (without inheritance)
     </para>
     <programlisting>
     select path from dmoz where path ~ 'Top.Adult.Arts.Animation.*{1}';
                    path                
     -----------------------------------
      Top.Adult.Arts.Animation.Cartoons
      Top.Adult.Arts.Animation.Anime
     (2 rows)
     </programlisting>
    </listitem>
    <listitem>
     <para>
      Q2: The same as Q1 but with counting of successors
     </para>
     <programlisting>
     select path as parentpath , (select count(*)-1 from dmoz where path &lt;@
     p.path) as count from dmoz p where path ~ 'Top.Adult.Arts.Animation.*{1}';
                 parentpath             | count 
     -----------------------------------+-------
      Top.Adult.Arts.Animation.Cartoons |     2
      Top.Adult.Arts.Animation.Anime    |    61
     (2 rows)
     </programlisting>
    </listitem>
    <listitem>
     <para>
      Q3: Get all parents
     </para>
     <programlisting>
     select path from dmoz where path @&gt; 'Top.Adult.Arts.Animation' order by
     path asc;
                path           
     --------------------------
      Top
      Top.Adult
      Top.Adult.Arts
      Top.Adult.Arts.Animation
     (4 rows)
     </programlisting>
    </listitem>
    <listitem>
     <para>
      Q4: Get all parents with counting of children
     </para>
     <programlisting>
     select path, (select count(*)-1 from dmoz where path &lt;@ p.path) as count
     from dmoz p where path @&gt; 'Top.Adult.Arts.Animation' order by path asc;
                path           | count  
     --------------------------+--------
      Top                      | 300273
      Top.Adult                |   4913
      Top.Adult.Arts           |    339
      Top.Adult.Arts.Animation |     65
     (4 rows)
     </programlisting>
    </listitem>
    <listitem>
     <para>
      Q5: Get all children with levels
     </para>
     <programlisting>
     select path, nlevel(path) - nlevel('Top.Adult.Arts.Animation') as level
     from dmoz where path ~ 'Top.Adult.Arts.Animation.*{1,2}' order by path asc;
                           path                      | level 
     ------------------------------------------------+-------
      Top.Adult.Arts.Animation.Anime                 |     1
      Top.Adult.Arts.Animation.Anime.Fan_Works       |     2
      Top.Adult.Arts.Animation.Anime.Games           |     2
      Top.Adult.Arts.Animation.Anime.Genres          |     2
      Top.Adult.Arts.Animation.Anime.Image_Galleries |     2
      Top.Adult.Arts.Animation.Anime.Multimedia      |     2
      Top.Adult.Arts.Animation.Anime.Resources       |     2
      Top.Adult.Arts.Animation.Anime.Titles          |     2
      Top.Adult.Arts.Animation.Cartoons              |     1
      Top.Adult.Arts.Animation.Cartoons.AVS          |     2
      Top.Adult.Arts.Animation.Cartoons.Members      |     2
     (11 rows)
     </programlisting>
    </listitem>
   </itemizedlist>
  </sect3>

  <sect3>
   <title>Timings</title>
   <programlisting>
+---------------------------------------------+
|Query|Rows|Time (ms) index|Time (ms) no index|
|-----+----+---------------+------------------|
|   Q0|   1|             NA|           1453.44|
|-----+----+---------------+------------------|
|   Q1|   2|           0.49|           1001.54|
|-----+----+---------------+------------------|
|   Q2|   2|           1.48|           3009.39|
|-----+----+---------------+------------------|
|   Q3|   4|           0.55|            906.98|
|-----+----+---------------+------------------|
|   Q4|   4|       24385.07|           4951.91|
|-----+----+---------------+------------------|
|   Q5|  11|           0.85|           1003.23|
+---------------------------------------------+
   </programlisting>
   <para>
    Timings without indices were obtained using operations which doesn't use
    indices (see above)
   </para>
  </sect3>

  <sect3>
   <title>Remarks</title>
   <para>
    We didn't run full-scale tests, also we didn't present (yet) data for
    operations with arrays of ltree (ltree[]) and full text searching. We'll
    appreciate your input. So far, below some (rather obvious) results:
   </para>
   <itemizedlist>
    <listitem>
     <para>
      Indices does help execution of queries
     </para>
    </listitem>
    <listitem>
     <para>
      Q4 performs bad because one needs to read almost all data from the HDD
     </para>
    </listitem>
   </itemizedlist>
  </sect3>
 </sect2>
 <sect2>
  <title>Some Backgrounds</title>
  <para>
   The approach we use for ltree is much like one we used in our other GiST based
   contrib modules (intarray, tsearch, tree, btree_gist, rtree_gist). Theoretical
   background is available in papers referenced from our GiST development page
   (<ulink url="http://www.sai.msu.su/~megera/postgres/gist"></ulink>).
  </para>
  <para>
   A hierarchical data structure (tree) is a set of nodes. Each node has a
   signature (LPS) of a fixed size, which is a hashed label path of that node.
   Traversing a tree we could *certainly* prune branches if
  </para>
  <programlisting>
   LQS (bitwise AND) LPS != LQS
  </programlisting>
  <para>
   where LQS is a signature of lquery or ltxtquery, obtained in the same way as
   LPS.
  </para>
  <programlisting>
   ltree[]:
  </programlisting>
  <para>
   For array of ltree LPS is a bitwise OR-ed signatures of *ALL* children
   reachable from that node. Signatures are stored in RD-tree, implemented using
   GiST, which provides indexed access.
  </para>
  <programlisting>
   ltree:
  </programlisting>
  <para>
   For ltree we store LPS in a B-tree, implemented using GiST. Each node entry is
   represented by (left_bound, signature, right_bound), so that we could speedup
   operations <literal>&lt;, &lt;=, =, &gt;=, &gt;</literal> using left_bound, right_bound and prune branches of
   a tree using signature.
  </para>
 </sect2>
 <sect2>
  <title>Authors</title>
  <para>
   All work was done by Teodor Sigaev (<email>teodor@stack.net</email>) and 
   Oleg Bartunov (<email>oleg@sai.msu.su</email>). See 
   <ulink url="http://www.sai.msu.su/~megera/postgres/gist"></ulink> for
   additional information. Authors would like to thank Eugeny Rodichev for 
   helpful discussions. Comments and bug reports are welcome.
  </para>
 </sect2>
</sect1>