Datasets:
File size: 65,789 Bytes
6493056 | 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 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 | <!DOCTYPE html>
<html data-theme="light" lang="en"><head>
<title>PostgreSQL: Documentation: 18: 65.2. GiST Indexes</title>
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport"/>
<meta content="text/xhtml; charset=utf-8" http-equiv="Content-Type"/>
<meta content="#336791" name="theme-color"/>
<meta content="The PostgreSQL Global Development Group" name="copyright"/>
<meta content="/docs/18/gist.html" property="og:url"/>
<meta content="article" property="og:type"/>
<meta content="2026-02-26T14:07:09" property="article:published_time"/>
<meta content="/media/img/about/press/elephant.png" property="og:image"/>
<meta content="65.2. GiST Indexes" property="og:title"/>
<meta content="65.2.&nbsp;GiST Indexes # 65.2.1. Introduction 65.2.2. Built-in Operator Classes 65.2.3. Extensibility 65.2.4. Implementation 65.2.5. Examples 65.2.1.&nbsp;Introduction # GiST stands for …" property="og:description"/>
<meta content="PostgreSQL Documentation" property="og:site_name"/>
<link href="/media/css/fontawesome.css?54a05eb2" rel="stylesheet"/>
<link href="/media/css/bootstrap-4.4.1.min.css" rel="stylesheet"/>
<link href="/favicon.ico" rel="shortcut icon"/>
<link href="/dyncss/base.css?54a05eb2" rel="stylesheet" type="text/css"/>
<script src="/media/js/theme.js?54a05eb2"></script>
<link href="/docs/current/gist.html" rel="canonical"/>
</head>
<body>
<div class="container-fluid">
<div class="row justify-content-md-center">
<div class="col">
<!-- Header -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand p-0" href="/">
<img alt="PostgreSQL Elephant Logo" class="logo" src="/media/img/about/press/elephant.png"/>
</a>
<input aria-controls="pgNavbar" aria-expanded="false" aria-label="Toggle navigation" id="navbar-toggler" type="checkbox"/>
<label class="navbar-toggler" for="navbar-toggler" id="navbar-toggler-label" tabindex="0"><span class="navbar-toggler-icon"></span></label>
<div class="navbar-collapse" id="pgNavbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item p-2"><a href="/" title="Home">Home</a></li>
<li class="nav-item p-2"><a href="/about/" title="About">About</a></li>
<li class="nav-item p-2"><a href="/download/" title="Download">Download</a></li>
<li class="nav-item p-2"><a href="/docs/" title="Documentation">Documentation</a></li>
<li class="nav-item p-2"><a href="/community/" title="Community">Community</a></li>
<li class="nav-item p-2"><a href="/developer/" title="Developers">Developers</a></li>
<li class="nav-item p-2"><a href="/support/" title="Support">Support</a></li>
<li class="nav-item p-2"><a href="/about/donate/" title="Donate">Donate</a></li>
<li class="nav-item p-2"><a href="/account/" title="Your account">Your account</a></li>
</ul>
<form action="/search/" method="get" role="search">
<div class="input-group">
<input accesskey="s" class="form-control" id="q" maxlength="255" name="q" placeholder="Search for..." size="20" type="text"/>
<span class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="fas fa-search"></i></button>
</span>
</div><!-- /input-group -->
</form>
<form class="form-inline" id="form-theme">
<button class="btn btn-default ml-1" id="btn-theme" type="button"><i class="fas fa-moon"></i></button>
</form>
</div>
</nav>
</div>
</div>
<div class="row justify-content-center pg-shout-box">
<div class="col text-white text-center">February 26, 2026: <a href="/about/news/postgresql-183-179-1613-1517-and-1422-released-3246/">
PostgreSQL 18.3, 17.9, 16.13, 15.17, and 14.22 Released!
</a>
</div>
</div>
</div>
<div class="container-fluid margin">
<div class="row">
<div class="col-11" id="pgContentWrap">
<div class="row">
<div class="col-md-6 mb-2">
<div class="row">
<div class="col">
<div>
<a href="/docs/" title="Documentation">Documentation</a> → <a href="/docs/18/index.html">PostgreSQL 18</a>
</div>
</div>
</div>
<div class="row">
<div class="col">
Supported Versions:
<a class="docs-version-selected" href="/docs/current/gist.html" title="PostgreSQL 18 - 65.2. GiST Indexes">Current</a>
(<a class="docs-version-selected" href="/docs/18/gist.html" title="PostgreSQL 18 - 65.2. GiST Indexes">18</a>)
/
<a href="/docs/17/gist.html" title="PostgreSQL 17 - 65.2. GiST Indexes">17</a>
/
<a href="/docs/16/gist.html" title="PostgreSQL 16 - 65.2. GiST Indexes">16</a>
/
<a href="/docs/15/gist.html" title="PostgreSQL 15 - 65.2. GiST Indexes">15</a>
/
<a href="/docs/14/gist.html" title="PostgreSQL 14 - 65.2. GiST Indexes">14</a>
</div>
</div>
<div class="row">
<div class="col">
Development Versions:
<a href="/docs/devel/gist.html" rel="nofollow" title="PostgreSQL devel - 65.2. GiST Indexes">devel</a>
</div>
</div>
<div class="row">
<div class="col-12">
Unsupported versions:
<a href="/docs/13/gist.html" rel="nofollow" title="PostgreSQL 13 - 65.2. GiST Indexes">13</a>
/
<a href="/docs/12/gist.html" rel="nofollow" title="PostgreSQL 12 - 65.2. GiST Indexes">12</a>
/
<a href="/docs/11/gist.html" rel="nofollow" title="PostgreSQL 11 - 65.2. GiST Indexes">11</a>
/
<a href="/docs/10/gist.html" rel="nofollow" title="PostgreSQL 10 - 65.2. GiST Indexes">10</a>
/
<a href="/docs/9.6/gist.html" rel="nofollow" title="PostgreSQL 9.6 - 65.2. GiST Indexes">9.6</a>
/
<a href="/docs/9.5/gist.html" rel="nofollow" title="PostgreSQL 9.5 - 65.2. GiST Indexes">9.5</a>
/
<a href="/docs/9.4/gist.html" rel="nofollow" title="PostgreSQL 9.4 - 65.2. GiST Indexes">9.4</a>
/
<a href="/docs/9.3/gist.html" rel="nofollow" title="PostgreSQL 9.3 - 65.2. GiST Indexes">9.3</a>
/
<a href="/docs/9.2/gist.html" rel="nofollow" title="PostgreSQL 9.2 - 65.2. GiST Indexes">9.2</a>
/
<a href="/docs/9.1/gist.html" rel="nofollow" title="PostgreSQL 9.1 - 65.2. GiST Indexes">9.1</a>
/
<a href="/docs/9.0/gist.html" rel="nofollow" title="PostgreSQL 9.0 - 65.2. GiST Indexes">9.0</a>
/
<a href="/docs/8.4/gist.html" rel="nofollow" title="PostgreSQL 8.4 - 65.2. GiST Indexes">8.4</a>
/
<a href="/docs/8.3/gist.html" rel="nofollow" title="PostgreSQL 8.3 - 65.2. GiST Indexes">8.3</a>
/
<a href="/docs/8.2/gist.html" rel="nofollow" title="PostgreSQL 8.2 - 65.2. GiST Indexes">8.2</a>
/
<a href="/docs/8.1/gist.html" rel="nofollow" title="PostgreSQL 8.1 - 65.2. GiST Indexes">8.1</a>
/
<a href="/docs/8.0/gist.html" rel="nofollow" title="PostgreSQL 8.0 - 65.2. GiST Indexes">8.0</a>
/
<a href="/docs/7.4/gist.html" rel="nofollow" title="PostgreSQL 7.4 - 65.2. GiST Indexes">7.4</a>
/
<a href="/docs/7.3/gist.html" rel="nofollow" title="PostgreSQL 7.3 - 65.2. GiST Indexes">7.3</a>
/
<a href="/docs/7.2/gist.html" rel="nofollow" title="PostgreSQL 7.2 - 65.2. GiST Indexes">7.2</a>
/
<a href="/docs/7.1/gist.html" rel="nofollow" title="PostgreSQL 7.1 - 65.2. GiST Indexes">7.1</a>
</div>
</div>
</div>
<div class="col-md-6 col-lg-5 offset-lg-1">
<form action="/search/" id="docSearchForm" method="get" role="search">
<input name="u" type="hidden" value="/docs/18/"/>
<div class="input-group">
<input class="form-control" id="q" name="q" placeholder="Search the documentation for..." type="text"/>
<span class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="fas fa-search"></i></button>
</span>
</div><!-- /input-group -->
</form>
</div>
</div>
<div id="docContent">
<div class="navheader">
<table summary="Navigation header" width="100%">
<tbody><tr>
<th align="center" colspan="5">65.2. GiST Indexes</th>
</tr>
<tr>
<td align="left" width="10%"><a accesskey="p" href="/docs/current/static/btree.html" title="65.1. B-Tree Indexes">Prev</a> </td>
<td align="left" width="10%"><a accesskey="u" href="/docs/current/static/indextypes.html" title="Chapter 65. Built-in Index Access Methods">Up</a></td>
<th align="center" width="60%">Chapter 65. Built-in Index Access Methods</th>
<td align="right" width="10%"><a accesskey="h" href="/docs/current/static/index.html" title="PostgreSQL 18.3 Documentation">Home</a></td>
<td align="right" width="10%"> <a accesskey="n" href="/docs/current/static/spgist.html" title="65.3. SP-GiST Indexes">Next</a></td>
</tr>
</tbody></table>
<hr/>
</div>
<div class="sect1" id="GIST">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both">65.2. GiST Indexes <a class="id_link" href="#GIST">#</a></h2>
</div>
</div>
</div>
<div class="toc">
<dl class="toc">
<dt><span class="sect2"><a href="/docs/current/static/gist.html#GIST-INTRO">65.2.1. Introduction</a></span></dt>
<dt><span class="sect2"><a href="/docs/current/static/gist.html#GIST-BUILTIN-OPCLASSES">65.2.2. Built-in Operator Classes</a></span></dt>
<dt><span class="sect2"><a href="/docs/current/static/gist.html#GIST-EXTENSIBILITY">65.2.3. Extensibility</a></span></dt>
<dt><span class="sect2"><a href="/docs/current/static/gist.html#GIST-IMPLEMENTATION">65.2.4. Implementation</a></span></dt>
<dt><span class="sect2"><a href="/docs/current/static/gist.html#GIST-EXAMPLES">65.2.5. Examples</a></span></dt>
</dl>
</div><a class="indexterm" id="id-1.10.17.3.2" name="id-1.10.17.3.2"></a>
<div class="sect2" id="GIST-INTRO">
<div class="titlepage">
<div>
<div>
<h3 class="title">65.2.1. Introduction <a class="id_link" href="#GIST-INTRO">#</a></h3>
</div>
</div>
</div>
<p><acronym class="acronym">GiST</acronym> stands for Generalized Search Tree. It is a balanced, tree-structured access method, that acts as a base template in which to implement arbitrary indexing schemes. B-trees, R-trees and many other indexing schemes can be implemented in <acronym class="acronym">GiST</acronym>.</p>
<p>One advantage of <acronym class="acronym">GiST</acronym> is that it allows the development of custom data types with the appropriate access methods, by an expert in the domain of the data type, rather than a database expert.</p>
<p>Some of the information here is derived from the University of California at Berkeley's GiST Indexing Project <a class="ulink" href="/_404.html" target="_top">web site</a> and Marcel Kornacker's thesis, <a class="ulink" href="/_404.html" target="_top">Access Methods for Next-Generation Database Systems</a>. The <acronym class="acronym">GiST</acronym> implementation in <span class="productname">PostgreSQL</span> is primarily maintained by Teodor Sigaev and Oleg Bartunov, and there is more information on their <a class="ulink" href="/_404.html" target="_top">web site</a>.</p>
</div>
<div class="sect2" id="GIST-BUILTIN-OPCLASSES">
<div class="titlepage">
<div>
<div>
<h3 class="title">65.2.2. Built-in Operator Classes <a class="id_link" href="#GIST-BUILTIN-OPCLASSES">#</a></h3>
</div>
</div>
</div>
<p>The core <span class="productname">PostgreSQL</span> distribution includes the <acronym class="acronym">GiST</acronym> operator classes shown in <a class="xref" href="/docs/current/static/gist.html#GIST-BUILTIN-OPCLASSES-TABLE" title="Table 65.1. Built-in GiST Operator Classes">Table 65.1</a>. (Some of the optional modules described in <a class="xref" href="/docs/current/static/contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Appendix F</a> provide additional <acronym class="acronym">GiST</acronym> operator classes.)</p>
<div class="table" id="GIST-BUILTIN-OPCLASSES-TABLE">
<p class="title"><strong>Table 65.1. Built-in <acronym class="acronym">GiST</acronym> Operator Classes</strong></p>
<div class="table-contents">
<table border="1" class="table" summary="Built-in GiST Operator Classes">
<colgroup>
<col class="col1"/>
<col class="col2"/>
<col class="col3"/>
</colgroup>
<thead>
<tr>
<th>Name</th>
<th>Indexable Operators</th>
<th>Ordering Operators</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="12" valign="middle"><code class="literal">box_ops</code></td>
<td><code class="literal"><< (box, box)</code></td>
<td rowspan="12" valign="middle"><code class="literal"><-> (box, point)</code></td>
</tr>
<tr>
<td><code class="literal">&< (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">&& (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">&> (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">>> (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">~= (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">@> (box, box)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">&<| (box, box)</code></td>
</tr>
<tr>
<td><code class="literal"><<| (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">|>> (box, box)</code></td>
</tr>
<tr>
<td><code class="literal">|&> (box, box)</code></td>
</tr>
<tr>
<td rowspan="12" valign="middle"><code class="literal">circle_ops</code></td>
<td><code class="literal"><< (circle, circle)</code></td>
<td rowspan="12" valign="middle"><code class="literal"><-> (circle, point)</code></td>
</tr>
<tr>
<td><code class="literal">&< (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">&> (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">>> (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">@> (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">~= (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">&& (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">|>> (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal"><<| (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">&<| (circle, circle)</code></td>
</tr>
<tr>
<td><code class="literal">|&> (circle, circle)</code></td>
</tr>
<tr>
<td rowspan="11" valign="middle"><code class="literal">inet_ops</code></td>
<td><code class="literal"><< (inet, inet)</code></td>
<td rowspan="11" valign="middle"> </td>
</tr>
<tr>
<td><code class="literal"><<= (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal">>> (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal">>>= (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal">= (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal"><> (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal">< (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal"><= (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal">> (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal">>= (inet, inet)</code></td>
</tr>
<tr>
<td><code class="literal">&& (inet, inet)</code></td>
</tr>
<tr>
<td rowspan="18" valign="middle"><code class="literal">multirange_ops</code></td>
<td><code class="literal">= (anymultirange, anymultirange)</code></td>
<td rowspan="18" valign="middle"> </td>
</tr>
<tr>
<td><code class="literal">&& (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">&& (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">@> (anymultirange, anyelement)</code></td>
</tr>
<tr>
<td><code class="literal">@> (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">@> (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal"><< (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal"><< (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">>> (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">>> (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">&< (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">&< (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">&> (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">&> (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">-|- (anymultirange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">-|- (anymultirange, anyrange)</code></td>
</tr>
<tr>
<td rowspan="8" valign="middle"><code class="literal">point_ops</code></td>
<td><code class="literal">|>> (point, point)</code></td>
<td rowspan="8" valign="middle"><code class="literal"><-> (point, point)</code></td>
</tr>
<tr>
<td><code class="literal"><< (point, point)</code></td>
</tr>
<tr>
<td><code class="literal">>> (point, point)</code></td>
</tr>
<tr>
<td><code class="literal"><<| (point, point)</code></td>
</tr>
<tr>
<td><code class="literal">~= (point, point)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (point, box)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (point, polygon)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (point, circle)</code></td>
</tr>
<tr>
<td rowspan="12" valign="middle"><code class="literal">poly_ops</code></td>
<td><code class="literal"><< (polygon, polygon)</code></td>
<td rowspan="12" valign="middle"><code class="literal"><-> (polygon, point)</code></td>
</tr>
<tr>
<td><code class="literal">&< (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">&> (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">>> (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">@> (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">~= (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">&& (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal"><<| (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">&<| (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">|&> (polygon, polygon)</code></td>
</tr>
<tr>
<td><code class="literal">|>> (polygon, polygon)</code></td>
</tr>
<tr>
<td rowspan="18" valign="middle"><code class="literal">range_ops</code></td>
<td><code class="literal">= (anyrange, anyrange)</code></td>
<td rowspan="18" valign="middle"> </td>
</tr>
<tr>
<td><code class="literal">&& (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">&& (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">@> (anyrange, anyelement)</code></td>
</tr>
<tr>
<td><code class="literal">@> (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">@> (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal"><@ (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal"><< (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal"><< (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">>> (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">>> (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">&< (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">&< (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">&> (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">&> (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td><code class="literal">-|- (anyrange, anyrange)</code></td>
</tr>
<tr>
<td><code class="literal">-|- (anyrange, anymultirange)</code></td>
</tr>
<tr>
<td rowspan="2" valign="middle"><code class="literal">tsquery_ops</code></td>
<td><code class="literal"><@ (tsquery, tsquery)</code></td>
<td rowspan="2" valign="middle"> </td>
</tr>
<tr>
<td><code class="literal">@> (tsquery, tsquery)</code></td>
</tr>
<tr>
<td valign="middle"><code class="literal">tsvector_ops</code></td>
<td><code class="literal">@@ (tsvector, tsquery)</code></td>
<td> </td>
</tr>
</tbody>
</table>
</div>
</div><br class="table-break"/>
<p>For historical reasons, the <code class="literal">inet_ops</code> operator class is not the default class for types <code class="type">inet</code> and <code class="type">cidr</code>. To use it, mention the class name in <code class="command">CREATE INDEX</code>, for example</p>
<pre class="programlisting">CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
</pre>
</div>
<div class="sect2" id="GIST-EXTENSIBILITY">
<div class="titlepage">
<div>
<div>
<h3 class="title">65.2.3. Extensibility <a class="id_link" href="#GIST-EXTENSIBILITY">#</a></h3>
</div>
</div>
</div>
<p>Traditionally, implementing a new index access method meant a lot of difficult work. It was necessary to understand the inner workings of the database, such as the lock manager and Write-Ahead Log. The <acronym class="acronym">GiST</acronym> interface has a high level of abstraction, requiring the access method implementer only to implement the semantics of the data type being accessed. The <acronym class="acronym">GiST</acronym> layer itself takes care of concurrency, logging and searching the tree structure.</p>
<p>This extensibility should not be confused with the extensibility of the other standard search trees in terms of the data they can handle. For example, <span class="productname">PostgreSQL</span> supports extensible B-trees and hash indexes. That means that you can use <span class="productname">PostgreSQL</span> to build a B-tree or hash over any data type you want. But B-trees only support range predicates (<code class="literal"><</code>, <code class="literal">=</code>, <code class="literal">></code>), and hash indexes only support equality queries.</p>
<p>So if you index, say, an image collection with a <span class="productname">PostgreSQL</span> B-tree, you can only issue queries such as <span class="quote">“<span class="quote">is imagex equal to imagey</span>”</span>, <span class="quote">“<span class="quote">is imagex less than imagey</span>”</span> and <span class="quote">“<span class="quote">is imagex greater than imagey</span>”</span>. Depending on how you define <span class="quote">“<span class="quote">equals</span>”</span>, <span class="quote">“<span class="quote">less than</span>”</span> and <span class="quote">“<span class="quote">greater than</span>”</span> in this context, this could be useful. However, by using a <acronym class="acronym">GiST</acronym> based index, you could create ways to ask domain-specific questions, perhaps <span class="quote">“<span class="quote">find all images of horses</span>”</span> or <span class="quote">“<span class="quote">find all over-exposed images</span>”</span>.</p>
<p>All it takes to get a <acronym class="acronym">GiST</acronym> access method up and running is to implement several user-defined methods, which define the behavior of keys in the tree. Of course these methods have to be pretty fancy to support fancy queries, but for all the standard queries (B-trees, R-trees, etc.) they're relatively straightforward. In short, <acronym class="acronym">GiST</acronym> combines extensibility along with generality, code reuse, and a clean interface.</p>
<p>There are five methods that an index operator class for <acronym class="acronym">GiST</acronym> must provide, and seven that are optional. Correctness of the index is ensured by proper implementation of the <code class="function">same</code>, <code class="function">consistent</code> and <code class="function">union</code> methods, while efficiency (size and speed) of the index will depend on the <code class="function">penalty</code> and <code class="function">picksplit</code> methods. Two optional methods are <code class="function">compress</code> and <code class="function">decompress</code>, which allow an index to have internal tree data of a different type than the data it indexes. The leaves are to be of the indexed data type, while the other tree nodes can be of any C struct (but you still have to follow <span class="productname">PostgreSQL</span> data type rules here, see about <code class="literal">varlena</code> for variable sized data). If the tree's internal data type exists at the SQL level, the <code class="literal">STORAGE</code> option of the <code class="command">CREATE OPERATOR CLASS</code> command can be used. The optional eighth method is <code class="function">distance</code>, which is needed if the operator class wishes to support ordered scans (nearest-neighbor searches). The optional ninth method <code class="function">fetch</code> is needed if the operator class wishes to support index-only scans, except when the <code class="function">compress</code> method is omitted. The optional tenth method <code class="function">options</code> is needed if the operator class has user-specified parameters. The optional eleventh method <code class="function">sortsupport</code> is used to speed up building a <acronym class="acronym">GiST</acronym> index. The optional twelfth method <code class="function">stratnum</code> is used to translate compare types (from <code class="filename">src/include/access/cmptype.h</code>) into strategy numbers used by the operator class. This lets the core code look up operators for temporal constraint indexes.</p>
<div class="variablelist">
<dl class="variablelist">
<dt><span class="term"><code class="function">consistent</code></span></dt>
<dd>
<p>Given an index entry <code class="literal">p</code> and a query value <code class="literal">q</code>, this function determines whether the index entry is <span class="quote">“<span class="quote">consistent</span>”</span> with the query; that is, could the predicate <span class="quote">“<span class="quote"><em class="replaceable"><code>indexed_column</code></em> <em class="replaceable"><code>indexable_operator</code></em> <code class="literal">q</code></span>”</span> be true for any row represented by the index entry? For a leaf index entry this is equivalent to testing the indexable condition, while for an internal tree node this determines whether it is necessary to scan the subtree of the index represented by the tree node. When the result is <code class="literal">true</code>, a <code class="literal">recheck</code> flag must also be returned. This indicates whether the predicate is certainly true or only possibly true. If <code class="literal">recheck</code> = <code class="literal">false</code> then the index has tested the predicate condition exactly, whereas if <code class="literal">recheck</code> = <code class="literal">true</code> the row is only a candidate match. In that case the system will automatically evaluate the <em class="replaceable"><code>indexable_operator</code></em> against the actual row value to see if it is really a match. This convention allows <acronym class="acronym">GiST</acronym> to support both lossless and lossy index structures.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_consistent(internal, data_type, smallint, oid, internal)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_consistent);
Datum
my_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
data_type *query = PG_GETARG_DATA_TYPE_P(1);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
/* Oid subtype = PG_GETARG_OID(3); */
bool *recheck = (bool *) PG_GETARG_POINTER(4);
data_type *key = DatumGetDataType(entry->key);
bool retval;
/*
* determine return value as a function of strategy, key and query.
*
* Use GIST_LEAF(entry) to know where you're called in the index tree,
* which comes handy when supporting the = operator for example (you could
* check for non empty union() in non-leaf nodes and equality in leaf
* nodes).
*/
*recheck = true; /* or false if check is exact */
PG_RETURN_BOOL(retval);
}
</pre>
<p>Here, <code class="varname">key</code> is an element in the index and <code class="varname">query</code> the value being looked up in the index. The <code class="literal">StrategyNumber</code> parameter indicates which operator of your operator class is being applied — it matches one of the operator numbers in the <code class="command">CREATE OPERATOR CLASS</code> command.</p>
<p>Depending on which operators you have included in the class, the data type of <code class="varname">query</code> could vary with the operator, since it will be whatever type is on the right-hand side of the operator, which might be different from the indexed data type appearing on the left-hand side. (The above code skeleton assumes that only one type is possible; if not, fetching the <code class="varname">query</code> argument value would have to depend on the operator.) It is recommended that the SQL declaration of the <code class="function">consistent</code> function use the opclass's indexed data type for the <code class="varname">query</code> argument, even though the actual type might be something else depending on the operator.</p>
</dd>
<dt><span class="term"><code class="function">union</code></span></dt>
<dd>
<p>This method consolidates information in the tree. Given a set of entries, this function generates a new index entry that represents all the given entries.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_union(internal, internal)
RETURNS storage_type
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_union);
Datum
my_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GISTENTRY *ent = entryvec->vector;
data_type *out,
*tmp,
*old;
int numranges,
i = 0;
numranges = entryvec->n;
tmp = DatumGetDataType(ent[0].key);
out = tmp;
if (numranges == 1)
{
out = data_type_deep_copy(tmp);
PG_RETURN_DATA_TYPE_P(out);
}
for (i = 1; i < numranges; i++)
{
old = out;
tmp = DatumGetDataType(ent[i].key);
out = my_union_implementation(out, tmp);
}
PG_RETURN_DATA_TYPE_P(out);
}
</pre>
<p>As you can see, in this skeleton we're dealing with a data type where <code class="literal">union(X, Y, Z) = union(union(X, Y), Z)</code>. It's easy enough to support data types where this is not the case, by implementing the proper union algorithm in this <acronym class="acronym">GiST</acronym> support method.</p>
<p>The result of the <code class="function">union</code> function must be a value of the index's storage type, whatever that is (it might or might not be different from the indexed column's type). The <code class="function">union</code> function should return a pointer to newly <code class="function">palloc()</code>ed memory. You can't just return the input value as-is, even if there is no type change.</p>
<p>As shown above, the <code class="function">union</code> function's first <code class="type">internal</code> argument is actually a <code class="structname">GistEntryVector</code> pointer. The second argument is a pointer to an integer variable, which can be ignored. (It used to be required that the <code class="function">union</code> function store the size of its result value into that variable, but this is no longer necessary.)</p>
</dd>
<dt><span class="term"><code class="function">compress</code></span></dt>
<dd>
<p>Converts a data item into a format suitable for physical storage in an index page. If the <code class="function">compress</code> method is omitted, data items are stored in the index without modification.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_compress(internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_compress);
Datum
my_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
if (entry->leafkey)
{
/* replace entry->key with a compressed version */
compressed_data_type *compressed_data = palloc(sizeof(compressed_data_type));
/* fill *compressed_data from entry->key ... */
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(compressed_data),
entry->rel, entry->page, entry->offset, FALSE);
}
else
{
/* typically we needn't do anything with non-leaf entries */
retval = entry;
}
PG_RETURN_POINTER(retval);
}
</pre>
<p>You have to adapt <em class="replaceable"><code>compressed_data_type</code></em> to the specific type you're converting to in order to compress your leaf nodes, of course.</p>
</dd>
<dt><span class="term"><code class="function">decompress</code></span></dt>
<dd>
<p>Converts the stored representation of a data item into a format that can be manipulated by the other GiST methods in the operator class. If the <code class="function">decompress</code> method is omitted, it is assumed that the other GiST methods can work directly on the stored data format. (<code class="function">decompress</code> is not necessarily the reverse of the <code class="function">compress</code> method; in particular, if <code class="function">compress</code> is lossy then it's impossible for <code class="function">decompress</code> to exactly reconstruct the original data. <code class="function">decompress</code> is not necessarily equivalent to <code class="function">fetch</code>, either, since the other GiST methods might not require full reconstruction of the data.)</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_decompress(internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_decompress);
Datum
my_decompress(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
}
</pre>
<p>The above skeleton is suitable for the case where no decompression is needed. (But, of course, omitting the method altogether is even easier, and is recommended in such cases.)</p>
</dd>
<dt><span class="term"><code class="function">penalty</code></span></dt>
<dd>
<p>Returns a value indicating the <span class="quote">“<span class="quote">cost</span>”</span> of inserting the new entry into a particular branch of the tree. Items will be inserted down the path of least <code class="function">penalty</code> in the tree. Values returned by <code class="function">penalty</code> should be non-negative. If a negative value is returned, it will be treated as zero.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_penalty(internal, internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT; -- in some cases penalty functions need not be strict
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_penalty);
Datum
my_penalty(PG_FUNCTION_ARGS)
{
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
float *penalty = (float *) PG_GETARG_POINTER(2);
data_type *orig = DatumGetDataType(origentry->key);
data_type *new = DatumGetDataType(newentry->key);
*penalty = my_penalty_implementation(orig, new);
PG_RETURN_POINTER(penalty);
}
</pre>
<p>For historical reasons, the <code class="function">penalty</code> function doesn't just return a <code class="type">float</code> result; instead it has to store the value at the location indicated by the third argument. The return value per se is ignored, though it's conventional to pass back the address of that argument.</p>
<p>The <code class="function">penalty</code> function is crucial to good performance of the index. It'll get used at insertion time to determine which branch to follow when choosing where to add the new entry in the tree. At query time, the more balanced the index, the quicker the lookup.</p>
</dd>
<dt><span class="term"><code class="function">picksplit</code></span></dt>
<dd>
<p>When an index page split is necessary, this function decides which entries on the page are to stay on the old page, and which are to move to the new page.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_picksplit(internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_picksplit);
Datum
my_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber maxoff = entryvec->n - 1;
GISTENTRY *ent = entryvec->vector;
int i,
nbytes;
OffsetNumber *left,
*right;
data_type *tmp_union;
data_type *unionL;
data_type *unionR;
GISTENTRY **raw_entryvec;
maxoff = entryvec->n - 1;
nbytes = (maxoff + 1) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
left = v->spl_left;
v->spl_nleft = 0;
v->spl_right = (OffsetNumber *) palloc(nbytes);
right = v->spl_right;
v->spl_nright = 0;
unionL = NULL;
unionR = NULL;
/* Initialize the raw entry vector. */
raw_entryvec = (GISTENTRY **) malloc(entryvec->n * sizeof(void *));
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
raw_entryvec[i] = &(entryvec->vector[i]);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
int real_index = raw_entryvec[i] - entryvec->vector;
tmp_union = DatumGetDataType(entryvec->vector[real_index].key);
Assert(tmp_union != NULL);
/*
* Choose where to put the index entries and update unionL and unionR
* accordingly. Append the entries to either v->spl_left or
* v->spl_right, and care about the counters.
*/
if (my_choice_is_left(unionL, curl, unionR, curr))
{
if (unionL == NULL)
unionL = tmp_union;
else
unionL = my_union_implementation(unionL, tmp_union);
*left = real_index;
++left;
++(v->spl_nleft);
}
else
{
/*
* Same on the right
*/
}
}
v->spl_ldatum = DataTypeGetDatum(unionL);
v->spl_rdatum = DataTypeGetDatum(unionR);
PG_RETURN_POINTER(v);
}
</pre>
<p>Notice that the <code class="function">picksplit</code> function's result is delivered by modifying the passed-in <code class="structname">v</code> structure. The return value per se is ignored, though it's conventional to pass back the address of <code class="structname">v</code>.</p>
<p>Like <code class="function">penalty</code>, the <code class="function">picksplit</code> function is crucial to good performance of the index. Designing suitable <code class="function">penalty</code> and <code class="function">picksplit</code> implementations is where the challenge of implementing well-performing <acronym class="acronym">GiST</acronym> indexes lies.</p>
</dd>
<dt><span class="term"><code class="function">same</code></span></dt>
<dd>
<p>Returns true if two index entries are identical, false otherwise. (An <span class="quote">“<span class="quote">index entry</span>”</span> is a value of the index's storage type, not necessarily the original indexed column's type.)</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_same(storage_type, storage_type, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_same);
Datum
my_same(PG_FUNCTION_ARGS)
{
prefix_range *v1 = PG_GETARG_PREFIX_RANGE_P(0);
prefix_range *v2 = PG_GETARG_PREFIX_RANGE_P(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = my_eq(v1, v2);
PG_RETURN_POINTER(result);
}
</pre>
<p>For historical reasons, the <code class="function">same</code> function doesn't just return a Boolean result; instead it has to store the flag at the location indicated by the third argument. The return value per se is ignored, though it's conventional to pass back the address of that argument.</p>
</dd>
<dt><span class="term"><code class="function">distance</code></span></dt>
<dd>
<p>Given an index entry <code class="literal">p</code> and a query value <code class="literal">q</code>, this function determines the index entry's <span class="quote">“<span class="quote">distance</span>”</span> from the query value. This function must be supplied if the operator class contains any ordering operators. A query using the ordering operator will be implemented by returning index entries with the smallest <span class="quote">“<span class="quote">distance</span>”</span> values first, so the results must be consistent with the operator's semantics. For a leaf index entry the result just represents the distance to the index entry; for an internal tree node, the result must be the smallest distance that any child entry could have.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_distance(internal, data_type, smallint, oid, internal)
RETURNS float8
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_distance);
Datum
my_distance(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
data_type *query = PG_GETARG_DATA_TYPE_P(1);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
/* Oid subtype = PG_GETARG_OID(3); */
/* bool *recheck = (bool *) PG_GETARG_POINTER(4); */
data_type *key = DatumGetDataType(entry->key);
double retval;
/*
* determine return value as a function of strategy, key and query.
*/
PG_RETURN_FLOAT8(retval);
}
</pre>
<p>The arguments to the <code class="function">distance</code> function are identical to the arguments of the <code class="function">consistent</code> function.</p>
<p>Some approximation is allowed when determining the distance, so long as the result is never greater than the entry's actual distance. Thus, for example, distance to a bounding box is usually sufficient in geometric applications. For an internal tree node, the distance returned must not be greater than the distance to any of the child nodes. If the returned distance is not exact, the function must set <code class="literal">*recheck</code> to true. (This is not necessary for internal tree nodes; for them, the calculation is always assumed to be inexact.) In this case the executor will calculate the accurate distance after fetching the tuple from the heap, and reorder the tuples if necessary.</p>
<p>If the distance function returns <code class="literal">*recheck = true</code> for any leaf node, the original ordering operator's return type must be <code class="type">float8</code> or <code class="type">float4</code>, and the distance function's result values must be comparable to those of the original ordering operator, since the executor will sort using both distance function results and recalculated ordering-operator results. Otherwise, the distance function's result values can be any finite <code class="type">float8</code> values, so long as the relative order of the result values matches the order returned by the ordering operator. (Infinity and minus infinity are used internally to handle cases such as nulls, so it is not recommended that <code class="function">distance</code> functions return these values.)</p>
</dd>
<dt><span class="term"><code class="function">fetch</code></span></dt>
<dd>
<p>Converts the compressed index representation of a data item into the original data type, for index-only scans. The returned data must be an exact, non-lossy copy of the originally indexed value.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_fetch(internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>The argument is a pointer to a <code class="structname">GISTENTRY</code> struct. On entry, its <code class="structfield">key</code> field contains a non-NULL leaf datum in compressed form. The return value is another <code class="structname">GISTENTRY</code> struct, whose <code class="structfield">key</code> field contains the same datum in its original, uncompressed form. If the opclass's compress function does nothing for leaf entries, the <code class="function">fetch</code> method can return the argument as-is. Or, if the opclass does not have a compress function, the <code class="function">fetch</code> method can be omitted as well, since it would necessarily be a no-op.</p>
<p>The matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_fetch);
Datum
my_fetch(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
input_data_type *in = DatumGetPointer(entry->key);
fetched_data_type *fetched_data;
GISTENTRY *retval;
retval = palloc(sizeof(GISTENTRY));
fetched_data = palloc(sizeof(fetched_data_type));
/*
* Convert 'fetched_data' into the a Datum of the original datatype.
*/
/* fill *retval from fetched_data. */
gistentryinit(*retval, PointerGetDatum(converted_datum),
entry->rel, entry->page, entry->offset, FALSE);
PG_RETURN_POINTER(retval);
}
</pre>
<p>If the compress method is lossy for leaf entries, the operator class cannot support index-only scans, and must not define a <code class="function">fetch</code> function.</p>
</dd>
<dt><span class="term"><code class="function">options</code></span></dt>
<dd>
<p>Allows definition of user-visible parameters that control operator class behavior.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_options(internal)
RETURNS void
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>The function is passed a pointer to a <code class="structname">local_relopts</code> struct, which needs to be filled with a set of operator class specific options. The options can be accessed from other support functions using the <code class="literal">PG_HAS_OPCLASS_OPTIONS()</code> and <code class="literal">PG_GET_OPCLASS_OPTIONS()</code> macros.</p>
<p>An example implementation of my_options() and parameters use from other support functions are given below:</p>
<pre class="programlisting">typedef enum MyEnumType
{
MY_ENUM_ON,
MY_ENUM_OFF,
MY_ENUM_AUTO
} MyEnumType;
typedef struct
{
int32 vl_len_; /* varlena header (do not touch directly!) */
int int_param; /* integer parameter */
double real_param; /* real parameter */
MyEnumType enum_param; /* enum parameter */
int str_param; /* string parameter */
} MyOptionsStruct;
/* String representation of enum values */
static relopt_enum_elt_def myEnumValues[] =
{
{"on", MY_ENUM_ON},
{"off", MY_ENUM_OFF},
{"auto", MY_ENUM_AUTO},
{(const char *) NULL} /* list terminator */
};
static char *str_param_default = "default";
/*
* Sample validator: checks that string is not longer than 8 bytes.
*/
static void
validate_my_string_relopt(const char *value)
{
if (strlen(value) > 8)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("str_param must be at most 8 bytes")));
}
/*
* Sample filler: switches characters to lower case.
*/
static Size
fill_my_string_relopt(const char *value, void *ptr)
{
char *tmp = str_tolower(value, strlen(value), DEFAULT_COLLATION_OID);
int len = strlen(tmp);
if (ptr)
strcpy(ptr, tmp);
pfree(tmp);
return len + 1;
}
PG_FUNCTION_INFO_V1(my_options);
Datum
my_options(PG_FUNCTION_ARGS)
{
local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
init_local_reloptions(relopts, sizeof(MyOptionsStruct));
add_local_int_reloption(relopts, "int_param", "integer parameter",
100, 0, 1000000,
offsetof(MyOptionsStruct, int_param));
add_local_real_reloption(relopts, "real_param", "real parameter",
1.0, 0.0, 1000000.0,
offsetof(MyOptionsStruct, real_param));
add_local_enum_reloption(relopts, "enum_param", "enum parameter",
myEnumValues, MY_ENUM_ON,
"Valid values are: \"on\", \"off\" and \"auto\".",
offsetof(MyOptionsStruct, enum_param));
add_local_string_reloption(relopts, "str_param", "string parameter",
str_param_default,
&validate_my_string_relopt,
&fill_my_string_relopt,
offsetof(MyOptionsStruct, str_param));
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(my_compress);
Datum
my_compress(PG_FUNCTION_ARGS)
{
int int_param = 100;
double real_param = 1.0;
MyEnumType enum_param = MY_ENUM_ON;
char *str_param = str_param_default;
/*
* Normally, when opclass contains 'options' method, then options are always
* passed to support functions. However, if you add 'options' method to
* existing opclass, previously defined indexes have no options, so the
* check is required.
*/
if (PG_HAS_OPCLASS_OPTIONS())
{
MyOptionsStruct *options = (MyOptionsStruct *) PG_GET_OPCLASS_OPTIONS();
int_param = options->int_param;
real_param = options->real_param;
enum_param = options->enum_param;
str_param = GET_STRING_RELOPTION(options, str_param);
}
/* the rest implementation of support function */
}
</pre>
<p>Since the representation of the key in <acronym class="acronym">GiST</acronym> is flexible, it may depend on user-specified parameters. For instance, the length of key signature may be specified. See <code class="literal">gtsvector_options()</code> for example.</p>
</dd>
<dt><span class="term"><code class="function">sortsupport</code></span></dt>
<dd>
<p>Returns a comparator function to sort data in a way that preserves locality. It is used by <code class="command">CREATE INDEX</code> and <code class="command">REINDEX</code> commands. The quality of the created index depends on how well the sort order determined by the comparator function preserves locality of the inputs.</p>
<p>The <code class="function">sortsupport</code> method is optional. If it is not provided, <code class="command">CREATE INDEX</code> builds the index by inserting each tuple to the tree using the <code class="function">penalty</code> and <code class="function">picksplit</code> functions, which is much slower.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_sortsupport(internal)
RETURNS void
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>The argument is a pointer to a <code class="structname">SortSupport</code> struct. At a minimum, the function must fill in its comparator field. The comparator takes three arguments: two Datums to compare, and a pointer to the <code class="structname">SortSupport</code> struct. The Datums are the two indexed values in the format that they are stored in the index; that is, in the format returned by the <code class="function">compress</code> method. The full API is defined in <code class="filename">src/include/utils/sortsupport.h</code>.</p>
<p>The matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_sortsupport);
static int
my_fastcmp(Datum x, Datum y, SortSupport ssup)
{
/* establish order between x and y by computing some sorting value z */
int z1 = ComputeSpatialCode(x);
int z2 = ComputeSpatialCode(y);
return z1 == z2 ? 0 : z1 > z2 ? 1 : -1;
}
Datum
my_sortsupport(PG_FUNCTION_ARGS)
{
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
ssup->comparator = my_fastcmp;
PG_RETURN_VOID();
}
</pre>
</dd>
<dt><span class="term"><code class="function">translate_cmptype</code></span></dt>
<dd>
<p>Given a <code class="literal">CompareType</code> value from <code class="filename">src/include/access/cmptype.h</code>, returns a strategy number used by this operator class for matching functionality. The function should return <code class="literal">InvalidStrategy</code> if the operator class has no matching strategy.</p>
<p>This is used for temporal index constraints (i.e., <code class="literal">PRIMARY KEY</code> and <code class="literal">UNIQUE</code>). If the operator class provides this function and it returns results for <code class="literal">COMPARE_EQ</code>, it can be used in the non-<code class="literal">WITHOUT OVERLAPS</code> part(s) of an index constraint.</p>
<p>This support function corresponds to the index access method callback function <code class="structfield">amtranslatecmptype</code> (see <a class="xref" href="/docs/current/static/index-functions.html" title="63.2. Index Access Method Functions">Section 63.2</a>). The <code class="structfield">amtranslatecmptype</code> callback function for GiST indexes merely calls down to the <code class="function">translate_cmptype</code> support function of the respective operator family, since the GiST index access method has no fixed strategy numbers itself.</p>
<p>The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:</p>
<pre class="programlisting">CREATE OR REPLACE FUNCTION my_translate_cmptype(integer)
RETURNS smallint
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</pre>
<p>And the operator family registration must look like this:</p>
<pre class="programlisting">ALTER OPERATOR FAMILY my_opfamily USING gist ADD
FUNCTION 12 ("any", "any") my_translate_cmptype(int);
</pre>
<p>The matching code in the C module could then follow this skeleton:</p>
<pre class="programlisting">PG_FUNCTION_INFO_V1(my_translate_cmptype);
Datum
my_translate_cmptype(PG_FUNCTION_ARGS)
{
CompareType cmptype = PG_GETARG_INT32(0);
StrategyNumber ret = InvalidStrategy;
switch (cmptype)
{
case COMPARE_EQ:
ret = BTEqualStrategyNumber;
}
PG_RETURN_UINT16(ret);
}
</pre>
<p>One translation function is provided by <span class="productname">PostgreSQL</span>: <code class="literal">gist_translate_cmptype_common</code> is for operator classes that use the <code class="literal">RT*StrategyNumber</code> constants. The <code class="literal">btree_gist</code> extension defines a second translation function, <code class="literal">gist_translate_cmptype_btree</code>, for operator classes that use the <code class="literal">BT*StrategyNumber</code> constants.</p>
</dd>
</dl>
</div>
<p>All the GiST support methods are normally called in short-lived memory contexts; that is, <code class="varname">CurrentMemoryContext</code> will get reset after each tuple is processed. It is therefore not very important to worry about pfree'ing everything you palloc. However, in some cases it's useful for a support method to cache data across repeated calls. To do that, allocate the longer-lived data in <code class="literal">fcinfo->flinfo->fn_mcxt</code>, and keep a pointer to it in <code class="literal">fcinfo->flinfo->fn_extra</code>. Such data will survive for the life of the index operation (e.g., a single GiST index scan, index build, or index tuple insertion). Be careful to pfree the previous value when replacing a <code class="literal">fn_extra</code> value, or the leak will accumulate for the duration of the operation.</p>
</div>
<div class="sect2" id="GIST-IMPLEMENTATION">
<div class="titlepage">
<div>
<div>
<h3 class="title">65.2.4. Implementation <a class="id_link" href="#GIST-IMPLEMENTATION">#</a></h3>
</div>
</div>
</div>
<div class="sect3" id="GIST-BUFFERING-BUILD">
<div class="titlepage">
<div>
<div>
<h4 class="title">65.2.4.1. GiST Index Build Methods <a class="id_link" href="#GIST-BUFFERING-BUILD">#</a></h4>
</div>
</div>
</div>
<p>The simplest way to build a GiST index is just to insert all the entries, one by one. This tends to be slow for large indexes, because if the index tuples are scattered across the index and the index is large enough to not fit in cache, a lot of random I/O will be needed. <span class="productname">PostgreSQL</span> supports two alternative methods for initial build of a GiST index: <em class="firstterm">sorted</em> and <em class="firstterm">buffered</em> modes.</p>
<p>The sorted method is only available if each of the opclasses used by the index provides a <code class="function">sortsupport</code> function, as described in <a class="xref" href="/docs/current/static/gist.html#GIST-EXTENSIBILITY" title="65.2.3. Extensibility">Section 65.2.3</a>. If they do, this method is usually the best, so it is used by default.</p>
<p>The buffered method works by not inserting tuples directly into the index right away. It can dramatically reduce the amount of random I/O needed for non-ordered data sets. For well-ordered data sets the benefit is smaller or non-existent, because only a small number of pages receive new tuples at a time, and those pages fit in cache even if the index as a whole does not.</p>
<p>The buffered method needs to call the <code class="function">penalty</code> function more often than the simple method does, which consumes some extra CPU resources. Also, the buffers need temporary disk space, up to the size of the resulting index. Buffering can also influence the quality of the resulting index, in both positive and negative directions. That influence depends on various factors, like the distribution of the input data and the operator class implementation.</p>
<p>If sorting is not possible, then by default a GiST index build switches to the buffering method when the index size reaches <a class="xref" href="/docs/current/static/runtime-config-query.html#GUC-EFFECTIVE-CACHE-SIZE">effective_cache_size</a>. Buffering can be manually forced or prevented by the <code class="literal">buffering</code> parameter to the CREATE INDEX command. The default behavior is good for most cases, but turning buffering off might speed up the build somewhat if the input data is ordered.</p>
</div>
</div>
<div class="sect2" id="GIST-EXAMPLES">
<div class="titlepage">
<div>
<div>
<h3 class="title">65.2.5. Examples <a class="id_link" href="#GIST-EXAMPLES">#</a></h3>
</div>
</div>
</div>
<p>The <span class="productname">PostgreSQL</span> source distribution includes several examples of index methods implemented using <acronym class="acronym">GiST</acronym>. The core system currently provides text search support (indexing for <code class="type">tsvector</code> and <code class="type">tsquery</code>) as well as R-Tree equivalent functionality for some of the built-in geometric data types (see <code class="filename">src/backend/access/gist/gistproc.c</code>). The following <code class="filename">contrib</code> modules also contain <acronym class="acronym">GiST</acronym> operator classes:</p>
<div class="variablelist">
<dl class="variablelist">
<dt><span class="term"><code class="filename">btree_gist</code></span></dt>
<dd>
<p>B-tree equivalent functionality for several data types</p>
</dd>
<dt><span class="term"><code class="filename">cube</code></span></dt>
<dd>
<p>Indexing for multidimensional cubes</p>
</dd>
<dt><span class="term"><code class="filename">hstore</code></span></dt>
<dd>
<p>Module for storing (key, value) pairs</p>
</dd>
<dt><span class="term"><code class="filename">intarray</code></span></dt>
<dd>
<p>RD-Tree for one-dimensional array of int4 values</p>
</dd>
<dt><span class="term"><code class="filename">ltree</code></span></dt>
<dd>
<p>Indexing for tree-like structures</p>
</dd>
<dt><span class="term"><code class="filename">pg_trgm</code></span></dt>
<dd>
<p>Text similarity using trigram matching</p>
</dd>
<dt><span class="term"><code class="filename">seg</code></span></dt>
<dd>
<p>Indexing for <span class="quote">“<span class="quote">float ranges</span>”</span></p>
</dd>
</dl>
</div>
</div>
</div>
<div class="navfooter">
<hr/>
<table summary="Navigation footer" width="100%">
<tbody><tr>
<td align="left" width="40%"><a accesskey="p" href="/docs/current/static/btree.html" title="65.1. B-Tree Indexes">Prev</a> </td>
<td align="center" width="20%"><a accesskey="u" href="/docs/current/static/indextypes.html" title="Chapter 65. Built-in Index Access Methods">Up</a></td>
<td align="right" width="40%"> <a accesskey="n" href="/docs/current/static/spgist.html" title="65.3. SP-GiST Indexes">Next</a></td>
</tr>
<tr>
<td align="left" valign="top" width="40%">65.1. B-Tree Indexes </td>
<td align="center" width="20%"><a accesskey="h" href="/docs/current/static/index.html" title="PostgreSQL 18.3 Documentation">Home</a></td>
<td align="right" valign="top" width="40%"> 65.3. SP-GiST Indexes</td>
</tr>
</tbody></table>
</div>
</div>
<div id="docComments">
<h2>Submit correction</h2>
<p>
If you see anything in the documentation that is not correct, does not match
your experience with the particular feature or requires further clarification,
please use
<a href="/account/comments/new/18/gist.html/" rel="nofollow">this form</a>
to report a documentation issue.
</p>
</div>
</div> <!-- pgContentWrap -->
</div>
</div>
<!-- Footer -->
<footer id="footer">
<!-- Copyright -->
<div class="container">
<a href="/about/policies/">Policies</a> |
<a href="/about/policies/coc/">Code of Conduct</a> |
<a href="/about/">About PostgreSQL</a> |
<a href="/about/contact/">Contact</a><br/>
<p>Copyright © 1996-2026 The PostgreSQL Global Development Group</p>
</div>
</footer>
<script src="/media/js/main.js?54a05eb2"></script>
</body></html> |