skey.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*-------------------------------------------------------------------------
  2. *
  3. * skey.h
  4. * POSTGRES scan key definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/access/skey.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef SKEY_H
  15. #define SKEY_H
  16. #include "access/attnum.h"
  17. #include "access/stratnum.h"
  18. #include "fmgr.h"
  19. /*
  20. * A ScanKey represents the application of a comparison operator between
  21. * a table or index column and a constant. When it's part of an array of
  22. * ScanKeys, the comparison conditions are implicitly ANDed. The index
  23. * column is the left argument of the operator, if it's a binary operator.
  24. * (The data structure can support unary indexable operators too; in that
  25. * case sk_argument would go unused. This is not currently implemented.)
  26. *
  27. * For an index scan, sk_strategy and sk_subtype must be set correctly for
  28. * the operator. When using a ScanKey in a heap scan, these fields are not
  29. * used and may be set to InvalidStrategy/InvalidOid.
  30. *
  31. * If the operator is collation-sensitive, sk_collation must be set
  32. * correctly as well.
  33. *
  34. * A ScanKey can also represent a ScalarArrayOpExpr, that is a condition
  35. * "column op ANY(ARRAY[...])". This is signaled by the SK_SEARCHARRAY
  36. * flag bit. The sk_argument is not a value of the operator's right-hand
  37. * argument type, but rather an array of such values, and the per-element
  38. * comparisons are to be ORed together.
  39. *
  40. * A ScanKey can also represent a condition "column IS NULL" or "column
  41. * IS NOT NULL"; these cases are signaled by the SK_SEARCHNULL and
  42. * SK_SEARCHNOTNULL flag bits respectively. The argument is always NULL,
  43. * and the sk_strategy, sk_subtype, sk_collation, and sk_func fields are
  44. * not used (unless set by the index AM).
  45. *
  46. * SK_SEARCHARRAY, SK_SEARCHNULL and SK_SEARCHNOTNULL are supported only
  47. * for index scans, not heap scans; and not all index AMs support them,
  48. * only those that set amsearcharray or amsearchnulls respectively.
  49. *
  50. * A ScanKey can also represent an ordering operator invocation, that is
  51. * an ordering requirement "ORDER BY indexedcol op constant". This looks
  52. * the same as a comparison operator, except that the operator doesn't
  53. * (usually) yield boolean. We mark such ScanKeys with SK_ORDER_BY.
  54. * SK_SEARCHARRAY, SK_SEARCHNULL, SK_SEARCHNOTNULL cannot be used here.
  55. *
  56. * Note: in some places, ScanKeys are used as a convenient representation
  57. * for the invocation of an access method support procedure. In this case
  58. * sk_strategy/sk_subtype are not meaningful (but sk_collation can be); and
  59. * sk_func may refer to a function that returns something other than boolean.
  60. */
  61. typedef struct ScanKeyData
  62. {
  63. int sk_flags; /* flags, see below */
  64. AttrNumber sk_attno; /* table or index column number */
  65. StrategyNumber sk_strategy; /* operator strategy number */
  66. Oid sk_subtype; /* strategy subtype */
  67. Oid sk_collation; /* collation to use, if needed */
  68. FmgrInfo sk_func; /* lookup info for function to call */
  69. Datum sk_argument; /* data to compare */
  70. } ScanKeyData;
  71. typedef ScanKeyData *ScanKey;
  72. /*
  73. * About row comparisons:
  74. *
  75. * The ScanKey data structure also supports row comparisons, that is ordered
  76. * tuple comparisons like (x, y) > (c1, c2), having the SQL-spec semantics
  77. * "x > c1 OR (x = c1 AND y > c2)". Note that this is currently only
  78. * implemented for btree index searches, not for heapscans or any other index
  79. * type. A row comparison is represented by a "header" ScanKey entry plus
  80. * a separate array of ScanKeys, one for each column of the row comparison.
  81. * The header entry has these properties:
  82. * sk_flags = SK_ROW_HEADER
  83. * sk_attno = index column number for leading column of row comparison
  84. * sk_strategy = btree strategy code for semantics of row comparison
  85. * (ie, < <= > or >=)
  86. * sk_subtype, sk_collation, sk_func: not used
  87. * sk_argument: pointer to subsidiary ScanKey array
  88. * If the header is part of a ScanKey array that's sorted by attno, it
  89. * must be sorted according to the leading column number.
  90. *
  91. * The subsidiary ScanKey array appears in logical column order of the row
  92. * comparison, which may be different from index column order. The array
  93. * elements are like a normal ScanKey array except that:
  94. * sk_flags must include SK_ROW_MEMBER, plus SK_ROW_END in the last
  95. * element (needed since row header does not include a count)
  96. * sk_func points to the btree comparison support function for the
  97. * opclass, NOT the operator's implementation function.
  98. * sk_strategy must be the same in all elements of the subsidiary array,
  99. * that is, the same as in the header entry.
  100. * SK_SEARCHARRAY, SK_SEARCHNULL, SK_SEARCHNOTNULL cannot be used here.
  101. */
  102. /*
  103. * ScanKeyData sk_flags
  104. *
  105. * sk_flags bits 0-15 are reserved for system-wide use (symbols for those
  106. * bits should be defined here). Bits 16-31 are reserved for use within
  107. * individual index access methods.
  108. */
  109. #define SK_ISNULL 0x0001 /* sk_argument is NULL */
  110. #define SK_UNARY 0x0002 /* unary operator (not supported!) */
  111. #define SK_ROW_HEADER 0x0004 /* row comparison header (see above) */
  112. #define SK_ROW_MEMBER 0x0008 /* row comparison member (see above) */
  113. #define SK_ROW_END 0x0010 /* last row comparison member */
  114. #define SK_SEARCHARRAY 0x0020 /* scankey represents ScalarArrayOp */
  115. #define SK_SEARCHNULL 0x0040 /* scankey represents "col IS NULL" */
  116. #define SK_SEARCHNOTNULL 0x0080 /* scankey represents "col IS NOT NULL" */
  117. #define SK_ORDER_BY 0x0100 /* scankey is for ORDER BY op */
  118. /*
  119. * prototypes for functions in access/common/scankey.c
  120. */
  121. extern void ScanKeyInit(ScanKey entry,
  122. AttrNumber attributeNumber,
  123. StrategyNumber strategy,
  124. RegProcedure procedure,
  125. Datum argument);
  126. extern void ScanKeyEntryInitialize(ScanKey entry,
  127. int flags,
  128. AttrNumber attributeNumber,
  129. StrategyNumber strategy,
  130. Oid subtype,
  131. Oid collation,
  132. RegProcedure procedure,
  133. Datum argument);
  134. extern void ScanKeyEntryInitializeWithInfo(ScanKey entry,
  135. int flags,
  136. AttrNumber attributeNumber,
  137. StrategyNumber strategy,
  138. Oid subtype,
  139. Oid collation,
  140. FmgrInfo *finfo,
  141. Datum argument);
  142. #endif /* SKEY_H */