PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nbtutils.c File Reference
#include "postgres.h"
#include <time.h>
#include "access/nbtree.h"
#include "access/reloptions.h"
#include "access/relscan.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for nbtutils.c:

Go to the source code of this file.

Data Structures

struct  BTSortArrayContext
 
struct  BTOneVacInfo
 
struct  BTVacInfo
 

Typedefs

typedef struct BTSortArrayContext BTSortArrayContext
 
typedef struct BTOneVacInfo BTOneVacInfo
 
typedef struct BTVacInfo BTVacInfo
 

Functions

static Datum _bt_find_extreme_element (IndexScanDesc scan, ScanKey skey, StrategyNumber strat, Datum *elems, int nelems)
 
static int _bt_sort_array_elements (IndexScanDesc scan, ScanKey skey, bool reverse, Datum *elems, int nelems)
 
static int _bt_compare_array_elements (const void *a, const void *b, void *arg)
 
static bool _bt_compare_scankey_args (IndexScanDesc scan, ScanKey op, ScanKey leftarg, ScanKey rightarg, bool *result)
 
static bool _bt_fix_scankey_strategy (ScanKey skey, int16 *indoption)
 
static void _bt_mark_scankey_required (ScanKey skey)
 
static bool _bt_check_rowcompare (ScanKey skey, IndexTuple tuple, TupleDesc tupdesc, ScanDirection dir, bool *continuescan)
 
ScanKey _bt_mkscankey (Relation rel, IndexTuple itup)
 
ScanKey _bt_mkscankey_nodata (Relation rel)
 
void _bt_freeskey (ScanKey skey)
 
void _bt_freestack (BTStack stack)
 
void _bt_preprocess_array_keys (IndexScanDesc scan)
 
void _bt_start_array_keys (IndexScanDesc scan, ScanDirection dir)
 
bool _bt_advance_array_keys (IndexScanDesc scan, ScanDirection dir)
 
void _bt_mark_array_keys (IndexScanDesc scan)
 
void _bt_restore_array_keys (IndexScanDesc scan)
 
void _bt_preprocess_keys (IndexScanDesc scan)
 
IndexTuple _bt_checkkeys (IndexScanDesc scan, Page page, OffsetNumber offnum, ScanDirection dir, bool *continuescan)
 
void _bt_killitems (IndexScanDesc scan)
 
BTCycleId _bt_vacuum_cycleid (Relation rel)
 
BTCycleId _bt_start_vacuum (Relation rel)
 
void _bt_end_vacuum (Relation rel)
 
void _bt_end_vacuum_callback (int code, Datum arg)
 
Size BTreeShmemSize (void)
 
void BTreeShmemInit (void)
 
byteabtoptions (Datum reloptions, bool validate)
 
bool btproperty (Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
 

Variables

static BTVacInfobtvacinfo
 

Typedef Documentation

Function Documentation

bool _bt_advance_array_keys ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 547 of file nbtutils.c.

References _bt_parallel_advance_array_keys(), BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, BTArrayKeyInfo::cur_elem, BTArrayKeyInfo::elem_values, i, NULL, BTArrayKeyInfo::num_elems, BTScanOpaqueData::numArrayKeys, IndexScanDescData::opaque, IndexScanDescData::parallel_scan, BTArrayKeyInfo::scan_key, ScanDirectionIsBackward, and ScanKeyData::sk_argument.

Referenced by btgetbitmap(), and btgettuple().

548 {
549  BTScanOpaque so = (BTScanOpaque) scan->opaque;
550  bool found = false;
551  int i;
552 
553  /*
554  * We must advance the last array key most quickly, since it will
555  * correspond to the lowest-order index column among the available
556  * qualifications. This is necessary to ensure correct ordering of output
557  * when there are multiple array keys.
558  */
559  for (i = so->numArrayKeys - 1; i >= 0; i--)
560  {
561  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
562  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
563  int cur_elem = curArrayKey->cur_elem;
564  int num_elems = curArrayKey->num_elems;
565 
566  if (ScanDirectionIsBackward(dir))
567  {
568  if (--cur_elem < 0)
569  {
570  cur_elem = num_elems - 1;
571  found = false; /* need to advance next array key */
572  }
573  else
574  found = true;
575  }
576  else
577  {
578  if (++cur_elem >= num_elems)
579  {
580  cur_elem = 0;
581  found = false; /* need to advance next array key */
582  }
583  else
584  found = true;
585  }
586 
587  curArrayKey->cur_elem = cur_elem;
588  skey->sk_argument = curArrayKey->elem_values[cur_elem];
589  if (found)
590  break;
591  }
592 
593  /* advance parallel scan */
594  if (scan->parallel_scan != NULL)
596 
597  return found;
598 }
ParallelIndexScanDesc parallel_scan
Definition: relscan.h:139
Datum * elem_values
Definition: nbtree.h:372
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
int cur_elem
Definition: nbtree.h:369
int numArrayKeys
Definition: nbtree.h:384
ScanKey arrayKeyData
Definition: nbtree.h:383
int num_elems
Definition: nbtree.h:371
#define NULL
Definition: c.h:229
void _bt_parallel_advance_array_keys(IndexScanDesc scan)
Definition: nbtree.c:889
BTArrayKeyInfo * arrayKeys
Definition: nbtree.h:388
int i
Datum sk_argument
Definition: skey.h:72
int scan_key
Definition: nbtree.h:368
static bool _bt_check_rowcompare ( ScanKey  skey,
IndexTuple  tuple,
TupleDesc  tupdesc,
ScanDirection  dir,
bool continuescan 
)
static

Definition at line 1553 of file nbtutils.c.

References Assert, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, DatumGetInt32, DatumGetPointer, elog, ERROR, FunctionCall2Coll(), index_getattr, result, ScanDirectionIsBackward, ScanDirectionIsForward, ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, SK_BT_REQBKWD, SK_BT_REQFWD, ScanKeyData::sk_collation, ScanKeyData::sk_flags, ScanKeyData::sk_func, SK_ISNULL, SK_ROW_END, SK_ROW_MEMBER, and ScanKeyData::sk_strategy.

Referenced by _bt_checkkeys().

1555 {
1556  ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1557  int32 cmpresult = 0;
1558  bool result;
1559 
1560  /* First subkey should be same as the header says */
1561  Assert(subkey->sk_attno == skey->sk_attno);
1562 
1563  /* Loop over columns of the row condition */
1564  for (;;)
1565  {
1566  Datum datum;
1567  bool isNull;
1568 
1569  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1570 
1571  datum = index_getattr(tuple,
1572  subkey->sk_attno,
1573  tupdesc,
1574  &isNull);
1575 
1576  if (isNull)
1577  {
1578  if (subkey->sk_flags & SK_BT_NULLS_FIRST)
1579  {
1580  /*
1581  * Since NULLs are sorted before non-NULLs, we know we have
1582  * reached the lower limit of the range of values for this
1583  * index attr. On a backward scan, we can stop if this qual
1584  * is one of the "must match" subset. We can stop regardless
1585  * of whether the qual is > or <, so long as it's required,
1586  * because it's not possible for any future tuples to pass. On
1587  * a forward scan, however, we must keep going, because we may
1588  * have initially positioned to the start of the index.
1589  */
1590  if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1592  *continuescan = false;
1593  }
1594  else
1595  {
1596  /*
1597  * Since NULLs are sorted after non-NULLs, we know we have
1598  * reached the upper limit of the range of values for this
1599  * index attr. On a forward scan, we can stop if this qual is
1600  * one of the "must match" subset. We can stop regardless of
1601  * whether the qual is > or <, so long as it's required,
1602  * because it's not possible for any future tuples to pass. On
1603  * a backward scan, however, we must keep going, because we
1604  * may have initially positioned to the end of the index.
1605  */
1606  if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1608  *continuescan = false;
1609  }
1610 
1611  /*
1612  * In any case, this indextuple doesn't match the qual.
1613  */
1614  return false;
1615  }
1616 
1617  if (subkey->sk_flags & SK_ISNULL)
1618  {
1619  /*
1620  * Unlike the simple-scankey case, this isn't a disallowed case.
1621  * But it can never match. If all the earlier row comparison
1622  * columns are required for the scan direction, we can stop the
1623  * scan, because there can't be another tuple that will succeed.
1624  */
1625  if (subkey != (ScanKey) DatumGetPointer(skey->sk_argument))
1626  subkey--;
1627  if ((subkey->sk_flags & SK_BT_REQFWD) &&
1629  *continuescan = false;
1630  else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1632  *continuescan = false;
1633  return false;
1634  }
1635 
1636  /* Perform the test --- three-way comparison not bool operator */
1637  cmpresult = DatumGetInt32(FunctionCall2Coll(&subkey->sk_func,
1638  subkey->sk_collation,
1639  datum,
1640  subkey->sk_argument));
1641 
1642  if (subkey->sk_flags & SK_BT_DESC)
1643  cmpresult = -cmpresult;
1644 
1645  /* Done comparing if unequal, else advance to next column */
1646  if (cmpresult != 0)
1647  break;
1648 
1649  if (subkey->sk_flags & SK_ROW_END)
1650  break;
1651  subkey++;
1652  }
1653 
1654  /*
1655  * At this point cmpresult indicates the overall result of the row
1656  * comparison, and subkey points to the deciding column (or the last
1657  * column if the result is "=").
1658  */
1659  switch (subkey->sk_strategy)
1660  {
1661  /* EQ and NE cases aren't allowed here */
1662  case BTLessStrategyNumber:
1663  result = (cmpresult < 0);
1664  break;
1666  result = (cmpresult <= 0);
1667  break;
1669  result = (cmpresult >= 0);
1670  break;
1672  result = (cmpresult > 0);
1673  break;
1674  default:
1675  elog(ERROR, "unrecognized RowCompareType: %d",
1676  (int) subkey->sk_strategy);
1677  result = 0; /* keep compiler quiet */
1678  break;
1679  }
1680 
1681  if (!result)
1682  {
1683  /*
1684  * Tuple fails this qual. If it's a required qual for the current
1685  * scan direction, then we can conclude no further tuples will pass,
1686  * either. Note we have to look at the deciding column, not
1687  * necessarily the first or last column of the row condition.
1688  */
1689  if ((subkey->sk_flags & SK_BT_REQFWD) &&
1691  *continuescan = false;
1692  else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
1694  *continuescan = false;
1695  }
1696 
1697  return result;
1698 }
#define SK_ROW_MEMBER
Definition: skey.h:118
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
#define DatumGetInt32(X)
Definition: postgres.h:478
return result
Definition: formatting.c:1618
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
signed int int32
Definition: c.h:256
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define SK_ROW_END
Definition: skey.h:119
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
#define ERROR
Definition: elog.h:43
StrategyNumber sk_strategy
Definition: skey.h:68
ScanKeyData * ScanKey
Definition: skey.h:75
FmgrInfo sk_func
Definition: skey.h:71
#define SK_ISNULL
Definition: skey.h:115
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:428
uintptr_t Datum
Definition: postgres.h:372
#define SK_BT_REQFWD
Definition: nbtree.h:424
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:675
#define SK_BT_DESC
Definition: nbtree.h:427
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
#define DatumGetPointer(X)
Definition: postgres.h:555
Oid sk_collation
Definition: skey.h:70
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define SK_BT_REQBKWD
Definition: nbtree.h:425
Datum sk_argument
Definition: skey.h:72
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
AttrNumber sk_attno
Definition: skey.h:67
IndexTuple _bt_checkkeys ( IndexScanDesc  scan,
Page  page,
OffsetNumber  offnum,
ScanDirection  dir,
bool continuescan 
)

Definition at line 1359 of file nbtutils.c.

References _bt_check_rowcompare(), Assert, DatumGetBool, FunctionCall2Coll(), IndexScanDescData::ignore_killed_tuples, index_getattr, IndexScanDescData::indexRelation, ItemIdIsDead, BTScanOpaqueData::keyData, NULL, BTScanOpaqueData::numberOfKeys, IndexScanDescData::opaque, P_FIRSTDATAKEY, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetSpecialPointer, RelationGetDescr, ScanDirectionIsBackward, ScanDirectionIsForward, ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_NULLS_FIRST, SK_BT_REQBKWD, SK_BT_REQFWD, ScanKeyData::sk_collation, ScanKeyData::sk_flags, ScanKeyData::sk_func, SK_ISNULL, SK_ROW_HEADER, SK_SEARCHNOTNULL, SK_SEARCHNULL, and test().

Referenced by _bt_readpage().

1362 {
1363  ItemId iid = PageGetItemId(page, offnum);
1364  bool tuple_alive;
1365  IndexTuple tuple;
1366  TupleDesc tupdesc;
1367  BTScanOpaque so;
1368  int keysz;
1369  int ikey;
1370  ScanKey key;
1371 
1372  *continuescan = true; /* default assumption */
1373 
1374  /*
1375  * If the scan specifies not to return killed tuples, then we treat a
1376  * killed tuple as not passing the qual. Most of the time, it's a win to
1377  * not bother examining the tuple's index keys, but just return
1378  * immediately with continuescan = true to proceed to the next tuple.
1379  * However, if this is the last tuple on the page, we should check the
1380  * index keys to prevent uselessly advancing to the next page.
1381  */
1382  if (scan->ignore_killed_tuples && ItemIdIsDead(iid))
1383  {
1384  /* return immediately if there are more tuples on the page */
1385  if (ScanDirectionIsForward(dir))
1386  {
1387  if (offnum < PageGetMaxOffsetNumber(page))
1388  return NULL;
1389  }
1390  else
1391  {
1393 
1394  if (offnum > P_FIRSTDATAKEY(opaque))
1395  return NULL;
1396  }
1397 
1398  /*
1399  * OK, we want to check the keys so we can set continuescan correctly,
1400  * but we'll return NULL even if the tuple passes the key tests.
1401  */
1402  tuple_alive = false;
1403  }
1404  else
1405  tuple_alive = true;
1406 
1407  tuple = (IndexTuple) PageGetItem(page, iid);
1408 
1409  tupdesc = RelationGetDescr(scan->indexRelation);
1410  so = (BTScanOpaque) scan->opaque;
1411  keysz = so->numberOfKeys;
1412 
1413  for (key = so->keyData, ikey = 0; ikey < keysz; key++, ikey++)
1414  {
1415  Datum datum;
1416  bool isNull;
1417  Datum test;
1418 
1419  /* row-comparison keys need special processing */
1420  if (key->sk_flags & SK_ROW_HEADER)
1421  {
1422  if (_bt_check_rowcompare(key, tuple, tupdesc, dir, continuescan))
1423  continue;
1424  return NULL;
1425  }
1426 
1427  datum = index_getattr(tuple,
1428  key->sk_attno,
1429  tupdesc,
1430  &isNull);
1431 
1432  if (key->sk_flags & SK_ISNULL)
1433  {
1434  /* Handle IS NULL/NOT NULL tests */
1435  if (key->sk_flags & SK_SEARCHNULL)
1436  {
1437  if (isNull)
1438  continue; /* tuple satisfies this qual */
1439  }
1440  else
1441  {
1443  if (!isNull)
1444  continue; /* tuple satisfies this qual */
1445  }
1446 
1447  /*
1448  * Tuple fails this qual. If it's a required qual for the current
1449  * scan direction, then we can conclude no further tuples will
1450  * pass, either.
1451  */
1452  if ((key->sk_flags & SK_BT_REQFWD) &&
1454  *continuescan = false;
1455  else if ((key->sk_flags & SK_BT_REQBKWD) &&
1457  *continuescan = false;
1458 
1459  /*
1460  * In any case, this indextuple doesn't match the qual.
1461  */
1462  return NULL;
1463  }
1464 
1465  if (isNull)
1466  {
1467  if (key->sk_flags & SK_BT_NULLS_FIRST)
1468  {
1469  /*
1470  * Since NULLs are sorted before non-NULLs, we know we have
1471  * reached the lower limit of the range of values for this
1472  * index attr. On a backward scan, we can stop if this qual
1473  * is one of the "must match" subset. We can stop regardless
1474  * of whether the qual is > or <, so long as it's required,
1475  * because it's not possible for any future tuples to pass. On
1476  * a forward scan, however, we must keep going, because we may
1477  * have initially positioned to the start of the index.
1478  */
1479  if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1481  *continuescan = false;
1482  }
1483  else
1484  {
1485  /*
1486  * Since NULLs are sorted after non-NULLs, we know we have
1487  * reached the upper limit of the range of values for this
1488  * index attr. On a forward scan, we can stop if this qual is
1489  * one of the "must match" subset. We can stop regardless of
1490  * whether the qual is > or <, so long as it's required,
1491  * because it's not possible for any future tuples to pass. On
1492  * a backward scan, however, we must keep going, because we
1493  * may have initially positioned to the end of the index.
1494  */
1495  if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
1497  *continuescan = false;
1498  }
1499 
1500  /*
1501  * In any case, this indextuple doesn't match the qual.
1502  */
1503  return NULL;
1504  }
1505 
1506  test = FunctionCall2Coll(&key->sk_func, key->sk_collation,
1507  datum, key->sk_argument);
1508 
1509  if (!DatumGetBool(test))
1510  {
1511  /*
1512  * Tuple fails this qual. If it's a required qual for the current
1513  * scan direction, then we can conclude no further tuples will
1514  * pass, either.
1515  *
1516  * Note: because we stop the scan as soon as any required equality
1517  * qual fails, it is critical that equality quals be used for the
1518  * initial positioning in _bt_first() when they are available. See
1519  * comments in _bt_first().
1520  */
1521  if ((key->sk_flags & SK_BT_REQFWD) &&
1523  *continuescan = false;
1524  else if ((key->sk_flags & SK_BT_REQBKWD) &&
1526  *continuescan = false;
1527 
1528  /*
1529  * In any case, this indextuple doesn't match the qual.
1530  */
1531  return NULL;
1532  }
1533  }
1534 
1535  /* Check for failure due to it being a killed tuple. */
1536  if (!tuple_alive)
1537  return NULL;
1538 
1539  /* If we get here, the tuple passes all index quals. */
1540  return tuple;
1541 }
static bool _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc, ScanDirection dir, bool *continuescan)
Definition: nbtutils.c:1553
static void test(void)
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
#define RelationGetDescr(relation)
Definition: rel.h:429
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:205
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:67
bool ignore_killed_tuples
Definition: relscan.h:100
Relation indexRelation
Definition: relscan.h:89
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
IndexTupleData * IndexTuple
Definition: itup.h:53
FmgrInfo sk_func
Definition: skey.h:71
#define DatumGetBool(X)
Definition: postgres.h:399
#define SK_SEARCHNOTNULL
Definition: skey.h:122
#define SK_ISNULL
Definition: skey.h:115
#define SK_ROW_HEADER
Definition: skey.h:117
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:428
uintptr_t Datum
Definition: postgres.h:372
#define SK_BT_REQFWD
Definition: nbtree.h:424
int sk_flags
Definition: skey.h:66
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
int numberOfKeys
Definition: nbtree.h:379
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
ScanKey keyData
Definition: nbtree.h:380
Oid sk_collation
Definition: skey.h:70
#define SK_BT_REQBKWD
Definition: nbtree.h:425
Datum sk_argument
Definition: skey.h:72
#define SK_SEARCHNULL
Definition: skey.h:121
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
AttrNumber sk_attno
Definition: skey.h:67
static int _bt_compare_array_elements ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 499 of file nbtutils.c.

References BTSortArrayContext::collation, compare(), DatumGetInt32, BTSortArrayContext::flinfo, FunctionCall2Coll(), and BTSortArrayContext::reverse.

Referenced by _bt_sort_array_elements().

500 {
501  Datum da = *((const Datum *) a);
502  Datum db = *((const Datum *) b);
504  int32 compare;
505 
506  compare = DatumGetInt32(FunctionCall2Coll(&cxt->flinfo,
507  cxt->collation,
508  da, db));
509  if (cxt->reverse)
510  compare = -compare;
511  return compare;
512 }
#define DatumGetInt32(X)
Definition: postgres.h:478
FmgrInfo flinfo
Definition: nbtutils.c:32
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
signed int int32
Definition: c.h:256
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
uintptr_t Datum
Definition: postgres.h:372
void * arg
static bool _bt_compare_scankey_args ( IndexScanDesc  scan,
ScanKey  op,
ScanKey  leftarg,
ScanKey  rightarg,
bool result 
)
static

Definition at line 1035 of file nbtutils.c.

References Assert, BTCommuteStrategyNumber, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, DatumGetBool, elog, ERROR, FunctionCall2Coll(), get_opcode(), get_opfamily_member(), IndexScanDescData::indexRelation, InvalidOid, OidFunctionCall2Coll(), OidIsValid, RelationData::rd_opcintype, RelationData::rd_opfamily, RegProcedureIsValid, ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, ScanKeyData::sk_collation, ScanKeyData::sk_flags, ScanKeyData::sk_func, SK_ISNULL, SK_SEARCHNOTNULL, SK_SEARCHNULL, ScanKeyData::sk_strategy, and ScanKeyData::sk_subtype.

Referenced by _bt_preprocess_keys().

1038 {
1039  Relation rel = scan->indexRelation;
1040  Oid lefttype,
1041  righttype,
1042  optype,
1043  opcintype,
1044  cmp_op;
1045  StrategyNumber strat;
1046 
1047  /*
1048  * First, deal with cases where one or both args are NULL. This should
1049  * only happen when the scankeys represent IS NULL/NOT NULL conditions.
1050  */
1051  if ((leftarg->sk_flags | rightarg->sk_flags) & SK_ISNULL)
1052  {
1053  bool leftnull,
1054  rightnull;
1055 
1056  if (leftarg->sk_flags & SK_ISNULL)
1057  {
1059  leftnull = true;
1060  }
1061  else
1062  leftnull = false;
1063  if (rightarg->sk_flags & SK_ISNULL)
1064  {
1065  Assert(rightarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
1066  rightnull = true;
1067  }
1068  else
1069  rightnull = false;
1070 
1071  /*
1072  * We treat NULL as either greater than or less than all other values.
1073  * Since true > false, the tests below work correctly for NULLS LAST
1074  * logic. If the index is NULLS FIRST, we need to flip the strategy.
1075  */
1076  strat = op->sk_strategy;
1077  if (op->sk_flags & SK_BT_NULLS_FIRST)
1078  strat = BTCommuteStrategyNumber(strat);
1079 
1080  switch (strat)
1081  {
1082  case BTLessStrategyNumber:
1083  *result = (leftnull < rightnull);
1084  break;
1086  *result = (leftnull <= rightnull);
1087  break;
1088  case BTEqualStrategyNumber:
1089  *result = (leftnull == rightnull);
1090  break;
1092  *result = (leftnull >= rightnull);
1093  break;
1095  *result = (leftnull > rightnull);
1096  break;
1097  default:
1098  elog(ERROR, "unrecognized StrategyNumber: %d", (int) strat);
1099  *result = false; /* keep compiler quiet */
1100  break;
1101  }
1102  return true;
1103  }
1104 
1105  /*
1106  * The opfamily we need to worry about is identified by the index column.
1107  */
1108  Assert(leftarg->sk_attno == rightarg->sk_attno);
1109 
1110  opcintype = rel->rd_opcintype[leftarg->sk_attno - 1];
1111 
1112  /*
1113  * Determine the actual datatypes of the ScanKey arguments. We have to
1114  * support the convention that sk_subtype == InvalidOid means the opclass
1115  * input type; this is a hack to simplify life for ScanKeyInit().
1116  */
1117  lefttype = leftarg->sk_subtype;
1118  if (lefttype == InvalidOid)
1119  lefttype = opcintype;
1120  righttype = rightarg->sk_subtype;
1121  if (righttype == InvalidOid)
1122  righttype = opcintype;
1123  optype = op->sk_subtype;
1124  if (optype == InvalidOid)
1125  optype = opcintype;
1126 
1127  /*
1128  * If leftarg and rightarg match the types expected for the "op" scankey,
1129  * we can use its already-looked-up comparison function.
1130  */
1131  if (lefttype == opcintype && righttype == optype)
1132  {
1134  op->sk_collation,
1135  leftarg->sk_argument,
1136  rightarg->sk_argument));
1137  return true;
1138  }
1139 
1140  /*
1141  * Otherwise, we need to go to the syscache to find the appropriate
1142  * operator. (This cannot result in infinite recursion, since no
1143  * indexscan initiated by syscache lookup will use cross-data-type
1144  * operators.)
1145  *
1146  * If the sk_strategy was flipped by _bt_fix_scankey_strategy, we have to
1147  * un-flip it to get the correct opfamily member.
1148  */
1149  strat = op->sk_strategy;
1150  if (op->sk_flags & SK_BT_DESC)
1151  strat = BTCommuteStrategyNumber(strat);
1152 
1153  cmp_op = get_opfamily_member(rel->rd_opfamily[leftarg->sk_attno - 1],
1154  lefttype,
1155  righttype,
1156  strat);
1157  if (OidIsValid(cmp_op))
1158  {
1159  RegProcedure cmp_proc = get_opcode(cmp_op);
1160 
1161  if (RegProcedureIsValid(cmp_proc))
1162  {
1164  op->sk_collation,
1165  leftarg->sk_argument,
1166  rightarg->sk_argument));
1167  return true;
1168  }
1169  }
1170 
1171  /* Can't make the comparison */
1172  *result = false; /* suppress compiler warnings */
1173  return false;
1174 }
Oid sk_subtype
Definition: skey.h:69
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
regproc RegProcedure
Definition: c.h:395
uint16 StrategyNumber
Definition: stratnum.h:22
return result
Definition: formatting.c:1618
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
Relation indexRelation
Definition: relscan.h:89
#define BTCommuteStrategyNumber(strat)
Definition: nbtree.h:215
#define ERROR
Definition: elog.h:43
StrategyNumber sk_strategy
Definition: skey.h:68
#define RegProcedureIsValid(p)
Definition: c.h:540
FmgrInfo sk_func
Definition: skey.h:71
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
#define DatumGetBool(X)
Definition: postgres.h:399
#define SK_SEARCHNOTNULL
Definition: skey.h:122
Oid * rd_opfamily
Definition: rel.h:182
#define SK_ISNULL
Definition: skey.h:115
Datum OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1651
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:428
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:675
#define SK_BT_DESC
Definition: nbtree.h:427
Oid sk_collation
Definition: skey.h:70
#define elog
Definition: elog.h:219
Oid * rd_opcintype
Definition: rel.h:183
#define BTLessStrategyNumber
Definition: stratnum.h:29
Datum sk_argument
Definition: skey.h:72
#define SK_SEARCHNULL
Definition: skey.h:121
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
AttrNumber sk_attno
Definition: skey.h:67
void _bt_end_vacuum ( Relation  rel)

Definition at line 1964 of file nbtutils.c.

References LockRelId::dbId, i, LockInfoData::lockRelId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), BTVacInfo::num_vacuums, RelationData::rd_lockInfo, LockRelId::relId, BTOneVacInfo::relid, and BTVacInfo::vacuums.

Referenced by _bt_end_vacuum_callback(), and btbulkdelete().

1965 {
1966  int i;
1967 
1968  LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1969 
1970  /* Find the array entry */
1971  for (i = 0; i < btvacinfo->num_vacuums; i++)
1972  {
1973  BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1974 
1975  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1976  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1977  {
1978  /* Remove it by shifting down the last entry */
1979  *vac = btvacinfo->vacuums[btvacinfo->num_vacuums - 1];
1981  break;
1982  }
1983  }
1984 
1985  LWLockRelease(BtreeVacuumLock);
1986 }
LockRelId lockRelId
Definition: rel.h:44
static BTVacInfo * btvacinfo
Definition: nbtutils.c:1860
LockRelId relid
Definition: nbtutils.c:1848
Oid dbId
Definition: rel.h:39
BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtutils.c:1857
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LockInfoData rd_lockInfo
Definition: rel.h:117
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int num_vacuums
Definition: nbtutils.c:1855
int i
Oid relId
Definition: rel.h:38
void _bt_end_vacuum_callback ( int  code,
Datum  arg 
)

Definition at line 1992 of file nbtutils.c.

References _bt_end_vacuum(), and DatumGetPointer.

Referenced by btbulkdelete().

1993 {
1995 }
void _bt_end_vacuum(Relation rel)
Definition: nbtutils.c:1964
#define DatumGetPointer(X)
Definition: postgres.h:555
void * arg
static Datum _bt_find_extreme_element ( IndexScanDesc  scan,
ScanKey  skey,
StrategyNumber  strat,
Datum elems,
int  nelems 
)
static

Definition at line 364 of file nbtutils.c.

References Assert, DatumGetBool, elog, ERROR, fmgr_info(), FunctionCall2Coll(), get_opcode(), get_opfamily_member(), i, IndexScanDescData::indexRelation, InvalidOid, OidIsValid, RelationData::rd_opcintype, RelationData::rd_opfamily, RegProcedureIsValid, result, ScanKeyData::sk_attno, ScanKeyData::sk_collation, and ScanKeyData::sk_subtype.

Referenced by _bt_preprocess_array_keys().

367 {
368  Relation rel = scan->indexRelation;
369  Oid elemtype,
370  cmp_op;
371  RegProcedure cmp_proc;
372  FmgrInfo flinfo;
373  Datum result;
374  int i;
375 
376  /*
377  * Determine the nominal datatype of the array elements. We have to
378  * support the convention that sk_subtype == InvalidOid means the opclass
379  * input type; this is a hack to simplify life for ScanKeyInit().
380  */
381  elemtype = skey->sk_subtype;
382  if (elemtype == InvalidOid)
383  elemtype = rel->rd_opcintype[skey->sk_attno - 1];
384 
385  /*
386  * Look up the appropriate comparison operator in the opfamily.
387  *
388  * Note: it's possible that this would fail, if the opfamily is
389  * incomplete, but it seems quite unlikely that an opfamily would omit
390  * non-cross-type comparison operators for any datatype that it supports
391  * at all.
392  */
393  cmp_op = get_opfamily_member(rel->rd_opfamily[skey->sk_attno - 1],
394  elemtype,
395  elemtype,
396  strat);
397  if (!OidIsValid(cmp_op))
398  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
399  strat, elemtype, elemtype,
400  rel->rd_opfamily[skey->sk_attno - 1]);
401  cmp_proc = get_opcode(cmp_op);
402  if (!RegProcedureIsValid(cmp_proc))
403  elog(ERROR, "missing oprcode for operator %u", cmp_op);
404 
405  fmgr_info(cmp_proc, &flinfo);
406 
407  Assert(nelems > 0);
408  result = elems[0];
409  for (i = 1; i < nelems; i++)
410  {
411  if (DatumGetBool(FunctionCall2Coll(&flinfo,
412  skey->sk_collation,
413  elems[i],
414  result)))
415  result = elems[i];
416  }
417 
418  return result;
419 }
Oid sk_subtype
Definition: skey.h:69
Definition: fmgr.h:56
regproc RegProcedure
Definition: c.h:395
return result
Definition: formatting.c:1618
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
Relation indexRelation
Definition: relscan.h:89
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define RegProcedureIsValid(p)
Definition: c.h:540
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
#define DatumGetBool(X)
Definition: postgres.h:399
Oid * rd_opfamily
Definition: rel.h:182
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
#define Assert(condition)
Definition: c.h:675
Oid sk_collation
Definition: skey.h:70
int i
#define elog
Definition: elog.h:219
Oid * rd_opcintype
Definition: rel.h:183
AttrNumber sk_attno
Definition: skey.h:67
static bool _bt_fix_scankey_strategy ( ScanKey  skey,
int16 indoption 
)
static

Definition at line 1199 of file nbtutils.c.

References Assert, BTCommuteStrategyNumber, BTEqualStrategyNumber, BTGreaterStrategyNumber, BTLessStrategyNumber, DatumGetPointer, InvalidOid, ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_INDOPTION_SHIFT, SK_BT_NULLS_FIRST, ScanKeyData::sk_collation, ScanKeyData::sk_flags, SK_ISNULL, SK_ROW_END, SK_ROW_HEADER, SK_ROW_MEMBER, SK_SEARCHNOTNULL, SK_SEARCHNULL, ScanKeyData::sk_strategy, and ScanKeyData::sk_subtype.

Referenced by _bt_preprocess_keys().

1200 {
1201  int addflags;
1202 
1203  addflags = indoption[skey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1204 
1205  /*
1206  * We treat all btree operators as strict (even if they're not so marked
1207  * in pg_proc). This means that it is impossible for an operator condition
1208  * with a NULL comparison constant to succeed, and we can reject it right
1209  * away.
1210  *
1211  * However, we now also support "x IS NULL" clauses as search conditions,
1212  * so in that case keep going. The planner has not filled in any
1213  * particular strategy in this case, so set it to BTEqualStrategyNumber
1214  * --- we can treat IS NULL as an equality operator for purposes of search
1215  * strategy.
1216  *
1217  * Likewise, "x IS NOT NULL" is supported. We treat that as either "less
1218  * than NULL" in a NULLS LAST index, or "greater than NULL" in a NULLS
1219  * FIRST index.
1220  *
1221  * Note: someday we might have to fill in sk_collation from the index
1222  * column's collation. At the moment this is a non-issue because we'll
1223  * never actually call the comparison operator on a NULL.
1224  */
1225  if (skey->sk_flags & SK_ISNULL)
1226  {
1227  /* SK_ISNULL shouldn't be set in a row header scankey */
1228  Assert(!(skey->sk_flags & SK_ROW_HEADER));
1229 
1230  /* Set indoption flags in scankey (might be done already) */
1231  skey->sk_flags |= addflags;
1232 
1233  /* Set correct strategy for IS NULL or NOT NULL search */
1234  if (skey->sk_flags & SK_SEARCHNULL)
1235  {
1237  skey->sk_subtype = InvalidOid;
1238  skey->sk_collation = InvalidOid;
1239  }
1240  else if (skey->sk_flags & SK_SEARCHNOTNULL)
1241  {
1242  if (skey->sk_flags & SK_BT_NULLS_FIRST)
1244  else
1246  skey->sk_subtype = InvalidOid;
1247  skey->sk_collation = InvalidOid;
1248  }
1249  else
1250  {
1251  /* regular qual, so it cannot be satisfied */
1252  return false;
1253  }
1254 
1255  /* Needn't do the rest */
1256  return true;
1257  }
1258 
1259  /* Adjust strategy for DESC, if we didn't already */
1260  if ((addflags & SK_BT_DESC) && !(skey->sk_flags & SK_BT_DESC))
1262  skey->sk_flags |= addflags;
1263 
1264  /* If it's a row header, fix row member flags and strategies similarly */
1265  if (skey->sk_flags & SK_ROW_HEADER)
1266  {
1267  ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1268 
1269  for (;;)
1270  {
1271  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1272  addflags = indoption[subkey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
1273  if ((addflags & SK_BT_DESC) && !(subkey->sk_flags & SK_BT_DESC))
1274  subkey->sk_strategy = BTCommuteStrategyNumber(subkey->sk_strategy);
1275  subkey->sk_flags |= addflags;
1276  if (subkey->sk_flags & SK_ROW_END)
1277  break;
1278  subkey++;
1279  }
1280  }
1281 
1282  return true;
1283 }
#define SK_BT_INDOPTION_SHIFT
Definition: nbtree.h:426
Oid sk_subtype
Definition: skey.h:69
#define SK_ROW_MEMBER
Definition: skey.h:118
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define SK_ROW_END
Definition: skey.h:119
#define BTCommuteStrategyNumber(strat)
Definition: nbtree.h:215
StrategyNumber sk_strategy
Definition: skey.h:68
ScanKeyData * ScanKey
Definition: skey.h:75
#define SK_SEARCHNOTNULL
Definition: skey.h:122
#define SK_ISNULL
Definition: skey.h:115
#define SK_ROW_HEADER
Definition: skey.h:117
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:428
#define InvalidOid
Definition: postgres_ext.h:36
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:675
#define SK_BT_DESC
Definition: nbtree.h:427
#define DatumGetPointer(X)
Definition: postgres.h:555
Oid sk_collation
Definition: skey.h:70
#define BTLessStrategyNumber
Definition: stratnum.h:29
Datum sk_argument
Definition: skey.h:72
#define SK_SEARCHNULL
Definition: skey.h:121
#define BTEqualStrategyNumber
Definition: stratnum.h:31
AttrNumber sk_attno
Definition: skey.h:67
void _bt_freeskey ( ScanKey  skey)

Definition at line 155 of file nbtutils.c.

References pfree().

Referenced by _bt_doinsert(), _bt_load(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

156 {
157  pfree(skey);
158 }
void pfree(void *pointer)
Definition: mcxt.c:950
void _bt_freestack ( BTStack  stack)

Definition at line 164 of file nbtutils.c.

References BTStackData::bts_parent, NULL, and pfree().

Referenced by _bt_doinsert(), and _bt_first().

165 {
166  BTStack ostack;
167 
168  while (stack != NULL)
169  {
170  ostack = stack;
171  stack = stack->bts_parent;
172  pfree(ostack);
173  }
174 }
void pfree(void *pointer)
Definition: mcxt.c:950
#define NULL
Definition: c.h:229
struct BTStackData * bts_parent
Definition: nbtree.h:256
void _bt_killitems ( IndexScanDesc  scan)

Definition at line 1732 of file nbtutils.c.

References _bt_getbuf(), _bt_relbuf(), Assert, BT_READ, BTP_HAS_GARBAGE, BTScanPosIsPinned, BTScanPosIsValid, buf, BTScanPosData::buf, BUFFER_LOCK_UNLOCK, BufferGetPage, BufferIsValid, BTScanPosData::currPage, BTScanOpaqueData::currPos, BTScanPosData::firstItem, BTScanPosItem::heapTid, i, BTScanPosItem::indexOffset, IndexScanDescData::indexRelation, ItemIdMarkDead, ItemPointerEquals(), BTScanPosData::items, BTScanOpaqueData::killedItems, LockBuffer(), BTScanPosData::lsn, MarkBufferDirtyHint(), BTScanOpaqueData::numKilled, OffsetNumberNext, IndexScanDescData::opaque, P_FIRSTDATAKEY, PageGetItem, PageGetItemId, PageGetLSN, PageGetMaxOffsetNumber, PageGetSpecialPointer, and IndexTupleData::t_tid.

Referenced by _bt_steppage(), btendscan(), btrescan(), and btrestrpos().

1733 {
1734  BTScanOpaque so = (BTScanOpaque) scan->opaque;
1735  Page page;
1736  BTPageOpaque opaque;
1737  OffsetNumber minoff;
1738  OffsetNumber maxoff;
1739  int i;
1740  int numKilled = so->numKilled;
1741  bool killedsomething = false;
1742 
1744 
1745  /*
1746  * Always reset the scan state, so we don't look for same items on other
1747  * pages.
1748  */
1749  so->numKilled = 0;
1750 
1751  if (BTScanPosIsPinned(so->currPos))
1752  {
1753  /*
1754  * We have held the pin on this page since we read the index tuples,
1755  * so all we need to do is lock it. The pin will have prevented
1756  * re-use of any TID on the page, so there is no need to check the
1757  * LSN.
1758  */
1759  LockBuffer(so->currPos.buf, BT_READ);
1760 
1761  page = BufferGetPage(so->currPos.buf);
1762  }
1763  else
1764  {
1765  Buffer buf;
1766 
1767  /* Attempt to re-read the buffer, getting pin and lock. */
1768  buf = _bt_getbuf(scan->indexRelation, so->currPos.currPage, BT_READ);
1769 
1770  /* It might not exist anymore; in which case we can't hint it. */
1771  if (!BufferIsValid(buf))
1772  return;
1773 
1774  page = BufferGetPage(buf);
1775  if (PageGetLSN(page) == so->currPos.lsn)
1776  so->currPos.buf = buf;
1777  else
1778  {
1779  /* Modified while not pinned means hinting is not safe. */
1780  _bt_relbuf(scan->indexRelation, buf);
1781  return;
1782  }
1783  }
1784 
1785  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
1786  minoff = P_FIRSTDATAKEY(opaque);
1787  maxoff = PageGetMaxOffsetNumber(page);
1788 
1789  for (i = 0; i < numKilled; i++)
1790  {
1791  int itemIndex = so->killedItems[i];
1792  BTScanPosItem *kitem = &so->currPos.items[itemIndex];
1793  OffsetNumber offnum = kitem->indexOffset;
1794 
1795  Assert(itemIndex >= so->currPos.firstItem &&
1796  itemIndex <= so->currPos.lastItem);
1797  if (offnum < minoff)
1798  continue; /* pure paranoia */
1799  while (offnum <= maxoff)
1800  {
1801  ItemId iid = PageGetItemId(page, offnum);
1802  IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
1803 
1804  if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
1805  {
1806  /* found the item */
1807  ItemIdMarkDead(iid);
1808  killedsomething = true;
1809  break; /* out of inner search loop */
1810  }
1811  offnum = OffsetNumberNext(offnum);
1812  }
1813  }
1814 
1815  /*
1816  * Since this can be redone later if needed, mark as dirty hint.
1817  *
1818  * Whenever we mark anything LP_DEAD, we also set the page's
1819  * BTP_HAS_GARBAGE flag, which is likewise just a hint.
1820  */
1821  if (killedsomething)
1822  {
1823  opaque->btpo_flags |= BTP_HAS_GARBAGE;
1824  MarkBufferDirtyHint(so->currPos.buf, true);
1825  }
1826 
1828 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
BTScanPosItem items[MaxIndexTuplesPerPage]
Definition: nbtree.h:328
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3379
#define ItemIdMarkDead(itemId)
Definition: itemid.h:178
Buffer _bt_getbuf(Relation rel, BlockNumber blkno, int access)
Definition: nbtpage.c:570
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:205
ItemPointerData t_tid
Definition: itup.h:37
BlockNumber currPage
Definition: nbtree.h:299
#define BTScanPosIsValid(scanpos)
Definition: nbtree.h:350
OffsetNumber indexOffset
Definition: nbtree.h:290
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:67
Relation indexRelation
Definition: relscan.h:89
uint16 OffsetNumber
Definition: off.h:24
#define BT_READ
Definition: nbtree.h:238
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
static char * buf
Definition: pg_test_fsync.c:65
#define BTScanPosIsPinned(scanpos)
Definition: nbtree.h:333
IndexTupleData * IndexTuple
Definition: itup.h:53
int firstItem
Definition: nbtree.h:324
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:720
int * killedItems
Definition: nbtree.h:392
#define Assert(condition)
Definition: c.h:675
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
ItemPointerData heapTid
Definition: nbtree.h:289
#define PageGetLSN(page)
Definition: bufpage.h:363
BTScanPosData currPos
Definition: nbtree.h:413
int i
XLogRecPtr lsn
Definition: nbtree.h:298
Buffer buf
Definition: nbtree.h:296
int Buffer
Definition: buf.h:23
#define BTP_HAS_GARBAGE
Definition: nbtree.h:76
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
void _bt_mark_array_keys ( IndexScanDesc  scan)

Definition at line 606 of file nbtutils.c.

References BTScanOpaqueData::arrayKeys, BTArrayKeyInfo::cur_elem, i, BTArrayKeyInfo::mark_elem, BTScanOpaqueData::numArrayKeys, and IndexScanDescData::opaque.

607 {
608  BTScanOpaque so = (BTScanOpaque) scan->opaque;
609  int i;
610 
611  for (i = 0; i < so->numArrayKeys; i++)
612  {
613  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
614 
615  curArrayKey->mark_elem = curArrayKey->cur_elem;
616  }
617 }
int mark_elem
Definition: nbtree.h:370
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
int cur_elem
Definition: nbtree.h:369
int numArrayKeys
Definition: nbtree.h:384
BTArrayKeyInfo * arrayKeys
Definition: nbtree.h:388
int i
static void _bt_mark_scankey_required ( ScanKey  skey)
static

Definition at line 1301 of file nbtutils.c.

References Assert, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, DatumGetPointer, elog, ERROR, ScanKeyData::sk_argument, ScanKeyData::sk_attno, SK_BT_REQBKWD, SK_BT_REQFWD, ScanKeyData::sk_flags, SK_ROW_HEADER, SK_ROW_MEMBER, and ScanKeyData::sk_strategy.

Referenced by _bt_preprocess_keys().

1302 {
1303  int addflags;
1304 
1305  switch (skey->sk_strategy)
1306  {
1307  case BTLessStrategyNumber:
1309  addflags = SK_BT_REQFWD;
1310  break;
1311  case BTEqualStrategyNumber:
1312  addflags = SK_BT_REQFWD | SK_BT_REQBKWD;
1313  break;
1316  addflags = SK_BT_REQBKWD;
1317  break;
1318  default:
1319  elog(ERROR, "unrecognized StrategyNumber: %d",
1320  (int) skey->sk_strategy);
1321  addflags = 0; /* keep compiler quiet */
1322  break;
1323  }
1324 
1325  skey->sk_flags |= addflags;
1326 
1327  if (skey->sk_flags & SK_ROW_HEADER)
1328  {
1329  ScanKey subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
1330 
1331  /* First subkey should be same column/operator as the header */
1332  Assert(subkey->sk_flags & SK_ROW_MEMBER);
1333  Assert(subkey->sk_attno == skey->sk_attno);
1334  Assert(subkey->sk_strategy == skey->sk_strategy);
1335  subkey->sk_flags |= addflags;
1336  }
1337 }
#define SK_ROW_MEMBER
Definition: skey.h:118
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define ERROR
Definition: elog.h:43
StrategyNumber sk_strategy
Definition: skey.h:68
ScanKeyData * ScanKey
Definition: skey.h:75
#define SK_ROW_HEADER
Definition: skey.h:117
#define SK_BT_REQFWD
Definition: nbtree.h:424
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:675
#define DatumGetPointer(X)
Definition: postgres.h:555
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define SK_BT_REQBKWD
Definition: nbtree.h:425
Datum sk_argument
Definition: skey.h:72
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
AttrNumber sk_attno
Definition: skey.h:67
ScanKey _bt_mkscankey ( Relation  rel,
IndexTuple  itup 
)

Definition at line 62 of file nbtutils.c.

References arg, BTORDER_PROC, i, index_getattr, index_getprocinfo(), InvalidOid, InvalidStrategy, palloc(), RelationData::rd_indcollation, RelationData::rd_indoption, RelationGetDescr, RelationGetNumberOfAttributes, ScanKeyEntryInitializeWithInfo(), SK_BT_INDOPTION_SHIFT, and SK_ISNULL.

Referenced by _bt_doinsert(), _bt_pagedel(), bt_right_page_check_scankey(), and bt_target_page_check().

63 {
64  ScanKey skey;
65  TupleDesc itupdesc;
66  int natts;
67  int16 *indoption;
68  int i;
69 
70  itupdesc = RelationGetDescr(rel);
71  natts = RelationGetNumberOfAttributes(rel);
72  indoption = rel->rd_indoption;
73 
74  skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
75 
76  for (i = 0; i < natts; i++)
77  {
78  FmgrInfo *procinfo;
79  Datum arg;
80  bool null;
81  int flags;
82 
83  /*
84  * We can use the cached (default) support procs since no cross-type
85  * comparison can be needed.
86  */
87  procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
88  arg = index_getattr(itup, i + 1, itupdesc, &null);
89  flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
91  flags,
92  (AttrNumber) (i + 1),
94  InvalidOid,
95  rel->rd_indcollation[i],
96  procinfo,
97  arg);
98  }
99 
100  return skey;
101 }
#define SK_BT_INDOPTION_SHIFT
Definition: nbtree.h:426
signed short int16
Definition: c.h:255
#define InvalidStrategy
Definition: stratnum.h:24
Definition: fmgr.h:56
#define BTORDER_PROC
Definition: nbtree.h:229
int16 * rd_indoption
Definition: rel.h:186
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
void ScanKeyEntryInitializeWithInfo(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, FmgrInfo *finfo, Datum argument)
Definition: scankey.c:101
#define RelationGetDescr(relation)
Definition: rel.h:429
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:423
Oid * rd_indcollation
Definition: rel.h:193
ScanKeyData * ScanKey
Definition: skey.h:75
#define SK_ISNULL
Definition: skey.h:115
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
void * palloc(Size size)
Definition: mcxt.c:849
int i
void * arg
int16 AttrNumber
Definition: attnum.h:21
ScanKey _bt_mkscankey_nodata ( Relation  rel)

Definition at line 115 of file nbtutils.c.

References BTORDER_PROC, i, index_getprocinfo(), InvalidOid, InvalidStrategy, palloc(), RelationData::rd_indcollation, RelationData::rd_indoption, RelationGetNumberOfAttributes, ScanKeyEntryInitializeWithInfo(), SK_BT_INDOPTION_SHIFT, and SK_ISNULL.

Referenced by _bt_load(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

116 {
117  ScanKey skey;
118  int natts;
119  int16 *indoption;
120  int i;
121 
122  natts = RelationGetNumberOfAttributes(rel);
123  indoption = rel->rd_indoption;
124 
125  skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
126 
127  for (i = 0; i < natts; i++)
128  {
129  FmgrInfo *procinfo;
130  int flags;
131 
132  /*
133  * We can use the cached (default) support procs since no cross-type
134  * comparison can be needed.
135  */
136  procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
137  flags = SK_ISNULL | (indoption[i] << SK_BT_INDOPTION_SHIFT);
139  flags,
140  (AttrNumber) (i + 1),
142  InvalidOid,
143  rel->rd_indcollation[i],
144  procinfo,
145  (Datum) 0);
146  }
147 
148  return skey;
149 }
#define SK_BT_INDOPTION_SHIFT
Definition: nbtree.h:426
signed short int16
Definition: c.h:255
#define InvalidStrategy
Definition: stratnum.h:24
Definition: fmgr.h:56
#define BTORDER_PROC
Definition: nbtree.h:229
int16 * rd_indoption
Definition: rel.h:186
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
void ScanKeyEntryInitializeWithInfo(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, FmgrInfo *finfo, Datum argument)
Definition: scankey.c:101
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:423
Oid * rd_indcollation
Definition: rel.h:193
ScanKeyData * ScanKey
Definition: skey.h:75
#define SK_ISNULL
Definition: skey.h:115
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
void * palloc(Size size)
Definition: mcxt.c:849
int i
int16 AttrNumber
Definition: attnum.h:21
void _bt_preprocess_array_keys ( IndexScanDesc  scan)

Definition at line 192 of file nbtutils.c.

References _bt_find_extreme_element(), _bt_sort_array_elements(), ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), ARR_ELEMTYPE, BTScanOpaqueData::arrayContext, BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, Assert, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, cur, CurrentMemoryContext, DatumGetArrayTypeP, deconstruct_array(), BTArrayKeyInfo::elem_values, elog, ERROR, get_typlenbyvalalign(), i, IndexScanDescData::indexRelation, INDOPTION_DESC, IndexScanDescData::keyData, MemoryContextReset(), MemoryContextSwitchTo(), NULL, BTArrayKeyInfo::num_elems, BTScanOpaqueData::numArrayKeys, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, palloc(), palloc0(), RelationData::rd_indoption, BTArrayKeyInfo::scan_key, SK_ISNULL, SK_ROW_HEADER, SK_SEARCHARRAY, SK_SEARCHNOTNULL, and SK_SEARCHNULL.

Referenced by btrescan().

193 {
194  BTScanOpaque so = (BTScanOpaque) scan->opaque;
195  int numberOfKeys = scan->numberOfKeys;
196  int16 *indoption = scan->indexRelation->rd_indoption;
197  int numArrayKeys;
198  ScanKey cur;
199  int i;
200  MemoryContext oldContext;
201 
202  /* Quick check to see if there are any array keys */
203  numArrayKeys = 0;
204  for (i = 0; i < numberOfKeys; i++)
205  {
206  cur = &scan->keyData[i];
207  if (cur->sk_flags & SK_SEARCHARRAY)
208  {
209  numArrayKeys++;
210  Assert(!(cur->sk_flags & (SK_ROW_HEADER | SK_SEARCHNULL | SK_SEARCHNOTNULL)));
211  /* If any arrays are null as a whole, we can quit right now. */
212  if (cur->sk_flags & SK_ISNULL)
213  {
214  so->numArrayKeys = -1;
215  so->arrayKeyData = NULL;
216  return;
217  }
218  }
219  }
220 
221  /* Quit if nothing to do. */
222  if (numArrayKeys == 0)
223  {
224  so->numArrayKeys = 0;
225  so->arrayKeyData = NULL;
226  return;
227  }
228 
229  /*
230  * Make a scan-lifespan context to hold array-associated data, or reset it
231  * if we already have one from a previous rescan cycle.
232  */
233  if (so->arrayContext == NULL)
235  "BTree array context",
237  else
239 
240  oldContext = MemoryContextSwitchTo(so->arrayContext);
241 
242  /* Create modifiable copy of scan->keyData in the workspace context */
243  so->arrayKeyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
244  memcpy(so->arrayKeyData,
245  scan->keyData,
246  scan->numberOfKeys * sizeof(ScanKeyData));
247 
248  /* Allocate space for per-array data in the workspace context */
249  so->arrayKeys = (BTArrayKeyInfo *) palloc0(numArrayKeys * sizeof(BTArrayKeyInfo));
250 
251  /* Now process each array key */
252  numArrayKeys = 0;
253  for (i = 0; i < numberOfKeys; i++)
254  {
255  ArrayType *arrayval;
256  int16 elmlen;
257  bool elmbyval;
258  char elmalign;
259  int num_elems;
260  Datum *elem_values;
261  bool *elem_nulls;
262  int num_nonnulls;
263  int j;
264 
265  cur = &so->arrayKeyData[i];
266  if (!(cur->sk_flags & SK_SEARCHARRAY))
267  continue;
268 
269  /*
270  * First, deconstruct the array into elements. Anything allocated
271  * here (including a possibly detoasted array value) is in the
272  * workspace context.
273  */
274  arrayval = DatumGetArrayTypeP(cur->sk_argument);
275  /* We could cache this data, but not clear it's worth it */
277  &elmlen, &elmbyval, &elmalign);
278  deconstruct_array(arrayval,
279  ARR_ELEMTYPE(arrayval),
280  elmlen, elmbyval, elmalign,
281  &elem_values, &elem_nulls, &num_elems);
282 
283  /*
284  * Compress out any null elements. We can ignore them since we assume
285  * all btree operators are strict.
286  */
287  num_nonnulls = 0;
288  for (j = 0; j < num_elems; j++)
289  {
290  if (!elem_nulls[j])
291  elem_values[num_nonnulls++] = elem_values[j];
292  }
293 
294  /* We could pfree(elem_nulls) now, but not worth the cycles */
295 
296  /* If there's no non-nulls, the scan qual is unsatisfiable */
297  if (num_nonnulls == 0)
298  {
299  numArrayKeys = -1;
300  break;
301  }
302 
303  /*
304  * If the comparison operator is not equality, then the array qual
305  * degenerates to a simple comparison against the smallest or largest
306  * non-null array element, as appropriate.
307  */
308  switch (cur->sk_strategy)
309  {
312  cur->sk_argument =
315  elem_values, num_nonnulls);
316  continue;
318  /* proceed with rest of loop */
319  break;
322  cur->sk_argument =
325  elem_values, num_nonnulls);
326  continue;
327  default:
328  elog(ERROR, "unrecognized StrategyNumber: %d",
329  (int) cur->sk_strategy);
330  break;
331  }
332 
333  /*
334  * Sort the non-null elements and eliminate any duplicates. We must
335  * sort in the same ordering used by the index column, so that the
336  * successive primitive indexscans produce data in index order.
337  */
338  num_elems = _bt_sort_array_elements(scan, cur,
339  (indoption[cur->sk_attno - 1] & INDOPTION_DESC) != 0,
340  elem_values, num_nonnulls);
341 
342  /*
343  * And set up the BTArrayKeyInfo data.
344  */
345  so->arrayKeys[numArrayKeys].scan_key = i;
346  so->arrayKeys[numArrayKeys].num_elems = num_elems;
347  so->arrayKeys[numArrayKeys].elem_values = elem_values;
348  numArrayKeys++;
349  }
350 
351  so->numArrayKeys = numArrayKeys;
352 
353  MemoryContextSwitchTo(oldContext);
354 }
signed short int16
Definition: c.h:255
int16 * rd_indoption
Definition: rel.h:186
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
MemoryContext arrayContext
Definition: nbtree.h:389
static int _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey, bool reverse, Datum *elems, int nelems)
Definition: nbtutils.c:431
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:1989
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
struct cursor * cur
Definition: ecpg.c:28
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
Datum * elem_values
Definition: nbtree.h:372
#define SK_SEARCHARRAY
Definition: skey.h:120
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
Relation indexRelation
Definition: relscan.h:89
static Datum _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey, StrategyNumber strat, Datum *elems, int nelems)
Definition: nbtutils.c:364
#define ERROR
Definition: elog.h:43
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
int numArrayKeys
Definition: nbtree.h:384
ScanKeyData * ScanKey
Definition: skey.h:75
#define SK_SEARCHNOTNULL
Definition: skey.h:122
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define SK_ISNULL
Definition: skey.h:115
ScanKey arrayKeyData
Definition: nbtree.h:383
#define SK_ROW_HEADER
Definition: skey.h:117
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
int num_elems
Definition: nbtree.h:371
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define INDOPTION_DESC
Definition: pg_index.h:99
ScanKey keyData
Definition: relscan.h:93
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
void * palloc(Size size)
Definition: mcxt.c:849
BTArrayKeyInfo * arrayKeys
Definition: nbtree.h:388
int i
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define ARR_ELEMTYPE(a)
Definition: array.h:273
int scan_key
Definition: nbtree.h:368
#define SK_SEARCHNULL
Definition: skey.h:121
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
#define DatumGetArrayTypeP(X)
Definition: array.h:242
void _bt_preprocess_keys ( IndexScanDesc  scan)

Definition at line 745 of file nbtutils.c.

References _bt_compare_scankey_args(), _bt_fix_scankey_strategy(), _bt_mark_scankey_required(), BTScanOpaqueData::arrayKeyData, Assert, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BTMaxStrategyNumber, cur, elog, ERROR, i, IndexScanDescData::indexRelation, IndexScanDescData::keyData, BTScanOpaqueData::keyData, NULL, IndexScanDescData::numberOfKeys, BTScanOpaqueData::numberOfKeys, IndexScanDescData::opaque, BTScanOpaqueData::qual_ok, RelationData::rd_indoption, ScanKeyData::sk_flags, SK_ROW_HEADER, and SK_SEARCHNULL.

Referenced by _bt_first(), and _bt_restore_array_keys().

746 {
747  BTScanOpaque so = (BTScanOpaque) scan->opaque;
748  int numberOfKeys = scan->numberOfKeys;
749  int16 *indoption = scan->indexRelation->rd_indoption;
750  int new_numberOfKeys;
751  int numberOfEqualCols;
752  ScanKey inkeys;
753  ScanKey outkeys;
754  ScanKey cur;
756  bool test_result;
757  int i,
758  j;
759  AttrNumber attno;
760 
761  /* initialize result variables */
762  so->qual_ok = true;
763  so->numberOfKeys = 0;
764 
765  if (numberOfKeys < 1)
766  return; /* done if qual-less scan */
767 
768  /*
769  * Read so->arrayKeyData if array keys are present, else scan->keyData
770  */
771  if (so->arrayKeyData != NULL)
772  inkeys = so->arrayKeyData;
773  else
774  inkeys = scan->keyData;
775 
776  outkeys = so->keyData;
777  cur = &inkeys[0];
778  /* we check that input keys are correctly ordered */
779  if (cur->sk_attno < 1)
780  elog(ERROR, "btree index keys must be ordered by attribute");
781 
782  /* We can short-circuit most of the work if there's just one key */
783  if (numberOfKeys == 1)
784  {
785  /* Apply indoption to scankey (might change sk_strategy!) */
786  if (!_bt_fix_scankey_strategy(cur, indoption))
787  so->qual_ok = false;
788  memcpy(outkeys, cur, sizeof(ScanKeyData));
789  so->numberOfKeys = 1;
790  /* We can mark the qual as required if it's for first index col */
791  if (cur->sk_attno == 1)
792  _bt_mark_scankey_required(outkeys);
793  return;
794  }
795 
796  /*
797  * Otherwise, do the full set of pushups.
798  */
799  new_numberOfKeys = 0;
800  numberOfEqualCols = 0;
801 
802  /*
803  * Initialize for processing of keys for attr 1.
804  *
805  * xform[i] points to the currently best scan key of strategy type i+1; it
806  * is NULL if we haven't yet found such a key for this attr.
807  */
808  attno = 1;
809  memset(xform, 0, sizeof(xform));
810 
811  /*
812  * Loop iterates from 0 to numberOfKeys inclusive; we use the last pass to
813  * handle after-last-key processing. Actual exit from the loop is at the
814  * "break" statement below.
815  */
816  for (i = 0;; cur++, i++)
817  {
818  if (i < numberOfKeys)
819  {
820  /* Apply indoption to scankey (might change sk_strategy!) */
821  if (!_bt_fix_scankey_strategy(cur, indoption))
822  {
823  /* NULL can't be matched, so give up */
824  so->qual_ok = false;
825  return;
826  }
827  }
828 
829  /*
830  * If we are at the end of the keys for a particular attr, finish up
831  * processing and emit the cleaned-up keys.
832  */
833  if (i == numberOfKeys || cur->sk_attno != attno)
834  {
835  int priorNumberOfEqualCols = numberOfEqualCols;
836 
837  /* check input keys are correctly ordered */
838  if (i < numberOfKeys && cur->sk_attno < attno)
839  elog(ERROR, "btree index keys must be ordered by attribute");
840 
841  /*
842  * If = has been specified, all other keys can be eliminated as
843  * redundant. If we have a case like key = 1 AND key > 2, we can
844  * set qual_ok to false and abandon further processing.
845  *
846  * We also have to deal with the case of "key IS NULL", which is
847  * unsatisfiable in combination with any other index condition. By
848  * the time we get here, that's been classified as an equality
849  * check, and we've rejected any combination of it with a regular
850  * equality condition; but not with other types of conditions.
851  */
852  if (xform[BTEqualStrategyNumber - 1])
853  {
854  ScanKey eq = xform[BTEqualStrategyNumber - 1];
855 
856  for (j = BTMaxStrategyNumber; --j >= 0;)
857  {
858  ScanKey chk = xform[j];
859 
860  if (!chk || j == (BTEqualStrategyNumber - 1))
861  continue;
862 
863  if (eq->sk_flags & SK_SEARCHNULL)
864  {
865  /* IS NULL is contradictory to anything else */
866  so->qual_ok = false;
867  return;
868  }
869 
870  if (_bt_compare_scankey_args(scan, chk, eq, chk,
871  &test_result))
872  {
873  if (!test_result)
874  {
875  /* keys proven mutually contradictory */
876  so->qual_ok = false;
877  return;
878  }
879  /* else discard the redundant non-equality key */
880  xform[j] = NULL;
881  }
882  /* else, cannot determine redundancy, keep both keys */
883  }
884  /* track number of attrs for which we have "=" keys */
885  numberOfEqualCols++;
886  }
887 
888  /* try to keep only one of <, <= */
889  if (xform[BTLessStrategyNumber - 1]
890  && xform[BTLessEqualStrategyNumber - 1])
891  {
892  ScanKey lt = xform[BTLessStrategyNumber - 1];
893  ScanKey le = xform[BTLessEqualStrategyNumber - 1];
894 
895  if (_bt_compare_scankey_args(scan, le, lt, le,
896  &test_result))
897  {
898  if (test_result)
899  xform[BTLessEqualStrategyNumber - 1] = NULL;
900  else
901  xform[BTLessStrategyNumber - 1] = NULL;
902  }
903  }
904 
905  /* try to keep only one of >, >= */
906  if (xform[BTGreaterStrategyNumber - 1]
907  && xform[BTGreaterEqualStrategyNumber - 1])
908  {
909  ScanKey gt = xform[BTGreaterStrategyNumber - 1];
910  ScanKey ge = xform[BTGreaterEqualStrategyNumber - 1];
911 
912  if (_bt_compare_scankey_args(scan, ge, gt, ge,
913  &test_result))
914  {
915  if (test_result)
916  xform[BTGreaterEqualStrategyNumber - 1] = NULL;
917  else
918  xform[BTGreaterStrategyNumber - 1] = NULL;
919  }
920  }
921 
922  /*
923  * Emit the cleaned-up keys into the outkeys[] array, and then
924  * mark them if they are required. They are required (possibly
925  * only in one direction) if all attrs before this one had "=".
926  */
927  for (j = BTMaxStrategyNumber; --j >= 0;)
928  {
929  if (xform[j])
930  {
931  ScanKey outkey = &outkeys[new_numberOfKeys++];
932 
933  memcpy(outkey, xform[j], sizeof(ScanKeyData));
934  if (priorNumberOfEqualCols == attno - 1)
936  }
937  }
938 
939  /*
940  * Exit loop here if done.
941  */
942  if (i == numberOfKeys)
943  break;
944 
945  /* Re-initialize for new attno */
946  attno = cur->sk_attno;
947  memset(xform, 0, sizeof(xform));
948  }
949 
950  /* check strategy this key's operator corresponds to */
951  j = cur->sk_strategy - 1;
952 
953  /* if row comparison, push it directly to the output array */
954  if (cur->sk_flags & SK_ROW_HEADER)
955  {
956  ScanKey outkey = &outkeys[new_numberOfKeys++];
957 
958  memcpy(outkey, cur, sizeof(ScanKeyData));
959  if (numberOfEqualCols == attno - 1)
961 
962  /*
963  * We don't support RowCompare using equality; such a qual would
964  * mess up the numberOfEqualCols tracking.
965  */
966  Assert(j != (BTEqualStrategyNumber - 1));
967  continue;
968  }
969 
970  /* have we seen one of these before? */
971  if (xform[j] == NULL)
972  {
973  /* nope, so remember this scankey */
974  xform[j] = cur;
975  }
976  else
977  {
978  /* yup, keep only the more restrictive key */
979  if (_bt_compare_scankey_args(scan, cur, cur, xform[j],
980  &test_result))
981  {
982  if (test_result)
983  xform[j] = cur;
984  else if (j == (BTEqualStrategyNumber - 1))
985  {
986  /* key == a && key == b, but a != b */
987  so->qual_ok = false;
988  return;
989  }
990  /* else old key is more restrictive, keep it */
991  }
992  else
993  {
994  /*
995  * We can't determine which key is more restrictive. Keep the
996  * previous one in xform[j] and push this one directly to the
997  * output array.
998  */
999  ScanKey outkey = &outkeys[new_numberOfKeys++];
1000 
1001  memcpy(outkey, cur, sizeof(ScanKeyData));
1002  if (numberOfEqualCols == attno - 1)
1003  _bt_mark_scankey_required(outkey);
1004  }
1005  }
1006  }
1007 
1008  so->numberOfKeys = new_numberOfKeys;
1009 }
signed short int16
Definition: c.h:255
static void _bt_mark_scankey_required(ScanKey skey)
Definition: nbtutils.c:1301
int16 * rd_indoption
Definition: rel.h:186
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
struct cursor * cur
Definition: ecpg.c:28
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
Relation indexRelation
Definition: relscan.h:89
#define ERROR
Definition: elog.h:43
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
ScanKey arrayKeyData
Definition: nbtree.h:383
#define SK_ROW_HEADER
Definition: skey.h:117
static bool _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption)
Definition: nbtutils.c:1199
int sk_flags
Definition: skey.h:66
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int numberOfKeys
Definition: nbtree.h:379
ScanKey keyData
Definition: relscan.h:93
ScanKey keyData
Definition: nbtree.h:380
int i
#define BTMaxStrategyNumber
Definition: stratnum.h:35
static bool _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op, ScanKey leftarg, ScanKey rightarg, bool *result)
Definition: nbtutils.c:1035
#define elog
Definition: elog.h:219
#define BTLessStrategyNumber
Definition: stratnum.h:29
int16 AttrNumber
Definition: attnum.h:21
#define SK_SEARCHNULL
Definition: skey.h:121
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
void _bt_restore_array_keys ( IndexScanDesc  scan)

Definition at line 625 of file nbtutils.c.

References _bt_preprocess_keys(), BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, Assert, BTArrayKeyInfo::cur_elem, BTArrayKeyInfo::elem_values, i, BTArrayKeyInfo::mark_elem, BTScanOpaqueData::numArrayKeys, IndexScanDescData::opaque, BTScanOpaqueData::qual_ok, BTArrayKeyInfo::scan_key, and ScanKeyData::sk_argument.

Referenced by btrestrpos().

626 {
627  BTScanOpaque so = (BTScanOpaque) scan->opaque;
628  bool changed = false;
629  int i;
630 
631  /* Restore each array key to its position when the mark was set */
632  for (i = 0; i < so->numArrayKeys; i++)
633  {
634  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
635  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
636  int mark_elem = curArrayKey->mark_elem;
637 
638  if (curArrayKey->cur_elem != mark_elem)
639  {
640  curArrayKey->cur_elem = mark_elem;
641  skey->sk_argument = curArrayKey->elem_values[mark_elem];
642  changed = true;
643  }
644  }
645 
646  /*
647  * If we changed any keys, we must redo _bt_preprocess_keys. That might
648  * sound like overkill, but in cases with multiple keys per index column
649  * it seems necessary to do the full set of pushups.
650  */
651  if (changed)
652  {
653  _bt_preprocess_keys(scan);
654  /* The mark should have been set on a consistent set of keys... */
655  Assert(so->qual_ok);
656  }
657 }
int mark_elem
Definition: nbtree.h:370
Datum * elem_values
Definition: nbtree.h:372
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
int cur_elem
Definition: nbtree.h:369
int numArrayKeys
Definition: nbtree.h:384
ScanKey arrayKeyData
Definition: nbtree.h:383
void _bt_preprocess_keys(IndexScanDesc scan)
Definition: nbtutils.c:745
#define Assert(condition)
Definition: c.h:675
BTArrayKeyInfo * arrayKeys
Definition: nbtree.h:388
int i
Datum sk_argument
Definition: skey.h:72
int scan_key
Definition: nbtree.h:368
static int _bt_sort_array_elements ( IndexScanDesc  scan,
ScanKey  skey,
bool  reverse,
Datum elems,
int  nelems 
)
static

Definition at line 431 of file nbtutils.c.

References _bt_compare_array_elements(), BTORDER_PROC, BTSortArrayContext::collation, compare(), DatumGetInt32, elog, ERROR, BTSortArrayContext::flinfo, fmgr_info(), FunctionCall2Coll(), get_opfamily_proc(), i, IndexScanDescData::indexRelation, InvalidOid, qsort_arg(), RelationData::rd_opcintype, RelationData::rd_opfamily, RegProcedureIsValid, BTSortArrayContext::reverse, ScanKeyData::sk_attno, ScanKeyData::sk_collation, and ScanKeyData::sk_subtype.

Referenced by _bt_preprocess_array_keys().

434 {
435  Relation rel = scan->indexRelation;
436  Oid elemtype;
437  RegProcedure cmp_proc;
438  BTSortArrayContext cxt;
439  int last_non_dup;
440  int i;
441 
442  if (nelems <= 1)
443  return nelems; /* no work to do */
444 
445  /*
446  * Determine the nominal datatype of the array elements. We have to
447  * support the convention that sk_subtype == InvalidOid means the opclass
448  * input type; this is a hack to simplify life for ScanKeyInit().
449  */
450  elemtype = skey->sk_subtype;
451  if (elemtype == InvalidOid)
452  elemtype = rel->rd_opcintype[skey->sk_attno - 1];
453 
454  /*
455  * Look up the appropriate comparison function in the opfamily.
456  *
457  * Note: it's possible that this would fail, if the opfamily is
458  * incomplete, but it seems quite unlikely that an opfamily would omit
459  * non-cross-type support functions for any datatype that it supports at
460  * all.
461  */
462  cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
463  elemtype,
464  elemtype,
465  BTORDER_PROC);
466  if (!RegProcedureIsValid(cmp_proc))
467  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
468  BTORDER_PROC, elemtype, elemtype,
469  rel->rd_opfamily[skey->sk_attno - 1]);
470 
471  /* Sort the array elements */
472  fmgr_info(cmp_proc, &cxt.flinfo);
473  cxt.collation = skey->sk_collation;
474  cxt.reverse = reverse;
475  qsort_arg((void *) elems, nelems, sizeof(Datum),
476  _bt_compare_array_elements, (void *) &cxt);
477 
478  /* Now scan the sorted elements and remove duplicates */
479  last_non_dup = 0;
480  for (i = 1; i < nelems; i++)
481  {
482  int32 compare;
483 
484  compare = DatumGetInt32(FunctionCall2Coll(&cxt.flinfo,
485  cxt.collation,
486  elems[last_non_dup],
487  elems[i]));
488  if (compare != 0)
489  elems[++last_non_dup] = elems[i];
490  }
491 
492  return last_non_dup + 1;
493 }
Oid sk_subtype
Definition: skey.h:69
#define BTORDER_PROC
Definition: nbtree.h:229
#define DatumGetInt32(X)
Definition: postgres.h:478
regproc RegProcedure
Definition: c.h:395
FmgrInfo flinfo
Definition: nbtutils.c:32
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1356
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
Relation indexRelation
Definition: relscan.h:89
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define RegProcedureIsValid(p)
Definition: c.h:540
Oid * rd_opfamily
Definition: rel.h:182
static int _bt_compare_array_elements(const void *a, const void *b, void *arg)
Definition: nbtutils.c:499
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
Oid sk_collation
Definition: skey.h:70
int i
#define elog
Definition: elog.h:219
Oid * rd_opcintype
Definition: rel.h:183
AttrNumber sk_attno
Definition: skey.h:67
void _bt_start_array_keys ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 521 of file nbtutils.c.

References BTScanOpaqueData::arrayKeyData, BTScanOpaqueData::arrayKeys, Assert, BTArrayKeyInfo::cur_elem, BTArrayKeyInfo::elem_values, i, BTArrayKeyInfo::num_elems, BTScanOpaqueData::numArrayKeys, IndexScanDescData::opaque, BTArrayKeyInfo::scan_key, ScanDirectionIsBackward, and ScanKeyData::sk_argument.

Referenced by btgetbitmap(), and btgettuple().

522 {
523  BTScanOpaque so = (BTScanOpaque) scan->opaque;
524  int i;
525 
526  for (i = 0; i < so->numArrayKeys; i++)
527  {
528  BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
529  ScanKey skey = &so->arrayKeyData[curArrayKey->scan_key];
530 
531  Assert(curArrayKey->num_elems > 0);
532  if (ScanDirectionIsBackward(dir))
533  curArrayKey->cur_elem = curArrayKey->num_elems - 1;
534  else
535  curArrayKey->cur_elem = 0;
536  skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
537  }
538 }
Datum * elem_values
Definition: nbtree.h:372
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
BTScanOpaqueData * BTScanOpaque
Definition: nbtree.h:417
int cur_elem
Definition: nbtree.h:369
int numArrayKeys
Definition: nbtree.h:384
ScanKey arrayKeyData
Definition: nbtree.h:383
int num_elems
Definition: nbtree.h:371
#define Assert(condition)
Definition: c.h:675
BTArrayKeyInfo * arrayKeys
Definition: nbtree.h:388
int i
Datum sk_argument
Definition: skey.h:72
int scan_key
Definition: nbtree.h:368
BTCycleId _bt_start_vacuum ( Relation  rel)

Definition at line 1907 of file nbtutils.c.

References BTVacInfo::cycle_ctr, BTOneVacInfo::cycleid, LockRelId::dbId, elog, ERROR, i, LockInfoData::lockRelId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_BT_CYCLE_ID, BTVacInfo::max_vacuums, BTVacInfo::num_vacuums, RelationData::rd_lockInfo, RelationGetRelationName, LockRelId::relId, BTOneVacInfo::relid, result, and BTVacInfo::vacuums.

Referenced by btbulkdelete().

1908 {
1909  BTCycleId result;
1910  int i;
1911  BTOneVacInfo *vac;
1912 
1913  LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
1914 
1915  /*
1916  * Assign the next cycle ID, being careful to avoid zero as well as the
1917  * reserved high values.
1918  */
1919  result = ++(btvacinfo->cycle_ctr);
1920  if (result == 0 || result > MAX_BT_CYCLE_ID)
1921  result = btvacinfo->cycle_ctr = 1;
1922 
1923  /* Let's just make sure there's no entry already for this index */
1924  for (i = 0; i < btvacinfo->num_vacuums; i++)
1925  {
1926  vac = &btvacinfo->vacuums[i];
1927  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1928  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1929  {
1930  /*
1931  * Unlike most places in the backend, we have to explicitly
1932  * release our LWLock before throwing an error. This is because
1933  * we expect _bt_end_vacuum() to be called before transaction
1934  * abort cleanup can run to release LWLocks.
1935  */
1936  LWLockRelease(BtreeVacuumLock);
1937  elog(ERROR, "multiple active vacuums for index \"%s\"",
1939  }
1940  }
1941 
1942  /* OK, add an entry */
1944  {
1945  LWLockRelease(BtreeVacuumLock);
1946  elog(ERROR, "out of btvacinfo slots");
1947  }
1949  vac->relid = rel->rd_lockInfo.lockRelId;
1950  vac->cycleid = result;
1952 
1953  LWLockRelease(BtreeVacuumLock);
1954  return result;
1955 }
LockRelId lockRelId
Definition: rel.h:44
static BTVacInfo * btvacinfo
Definition: nbtutils.c:1860
LockRelId relid
Definition: nbtutils.c:1848
Oid dbId
Definition: rel.h:39
BTCycleId cycle_ctr
Definition: nbtutils.c:1854
BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtutils.c:1857
return result
Definition: formatting.c:1618
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define ERROR
Definition: elog.h:43
uint16 BTCycleId
Definition: nbtree.h:26
LockInfoData rd_lockInfo
Definition: rel.h:117
#define RelationGetRelationName(relation)
Definition: rel.h:437
BTCycleId cycleid
Definition: nbtutils.c:1849
#define MAX_BT_CYCLE_ID
Definition: nbtree.h:86
int max_vacuums
Definition: nbtutils.c:1856
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int num_vacuums
Definition: nbtutils.c:1855
int i
#define elog
Definition: elog.h:219
Oid relId
Definition: rel.h:38
BTCycleId _bt_vacuum_cycleid ( Relation  rel)

Definition at line 1873 of file nbtutils.c.

References BTOneVacInfo::cycleid, LockRelId::dbId, i, LockInfoData::lockRelId, LW_SHARED, LWLockAcquire(), LWLockRelease(), BTVacInfo::num_vacuums, RelationData::rd_lockInfo, LockRelId::relId, BTOneVacInfo::relid, result, and BTVacInfo::vacuums.

Referenced by _bt_split().

1874 {
1875  BTCycleId result = 0;
1876  int i;
1877 
1878  /* Share lock is enough since this is a read-only operation */
1879  LWLockAcquire(BtreeVacuumLock, LW_SHARED);
1880 
1881  for (i = 0; i < btvacinfo->num_vacuums; i++)
1882  {
1883  BTOneVacInfo *vac = &btvacinfo->vacuums[i];
1884 
1885  if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
1886  vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
1887  {
1888  result = vac->cycleid;
1889  break;
1890  }
1891  }
1892 
1893  LWLockRelease(BtreeVacuumLock);
1894  return result;
1895 }
LockRelId lockRelId
Definition: rel.h:44
static BTVacInfo * btvacinfo
Definition: nbtutils.c:1860
LockRelId relid
Definition: nbtutils.c:1848
Oid dbId
Definition: rel.h:39
BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER]
Definition: nbtutils.c:1857
return result
Definition: formatting.c:1618
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
uint16 BTCycleId
Definition: nbtree.h:26
LockInfoData rd_lockInfo
Definition: rel.h:117
BTCycleId cycleid
Definition: nbtutils.c:1849
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int num_vacuums
Definition: nbtutils.c:1855
int i
Oid relId
Definition: rel.h:38
bytea* btoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 2042 of file nbtutils.c.

References default_reloptions(), and RELOPT_KIND_BTREE.

Referenced by bthandler().

2043 {
2044  return default_reloptions(reloptions, validate, RELOPT_KIND_BTREE);
2045 }
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
Definition: reloptions.c:1309
bool btproperty ( Oid  index_oid,
int  attno,
IndexAMProperty  prop,
const char *  propname,
bool res,
bool isnull 
)

Definition at line 2054 of file nbtutils.c.

References AMPROP_RETURNABLE.

Referenced by bthandler().

2057 {
2058  switch (prop)
2059  {
2060  case AMPROP_RETURNABLE:
2061  /* answer only for columns, not AM or whole index */
2062  if (attno == 0)
2063  return false;
2064  /* otherwise, btree can always return data */
2065  *res = true;
2066  return true;
2067 
2068  default:
2069  return false; /* punt to generic code */
2070  }
2071 }
void BTreeShmemInit ( void  )

Definition at line 2014 of file nbtutils.c.

References Assert, BTreeShmemSize(), BTVacInfo::cycle_ctr, IsUnderPostmaster, BTVacInfo::max_vacuums, MaxBackends, NULL, BTVacInfo::num_vacuums, and ShmemInitStruct().

Referenced by CreateSharedMemoryAndSemaphores().

2015 {
2016  bool found;
2017 
2018  btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
2019  BTreeShmemSize(),
2020  &found);
2021 
2022  if (!IsUnderPostmaster)
2023  {
2024  /* Initialize shared memory area */
2025  Assert(!found);
2026 
2027  /*
2028  * It doesn't really matter what the cycle counter starts at, but
2029  * having it always start the same doesn't seem good. Seed with
2030  * low-order bits of time() instead.
2031  */
2032  btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
2033 
2034  btvacinfo->num_vacuums = 0;
2036  }
2037  else
2038  Assert(found);
2039 }
static BTVacInfo * btvacinfo
Definition: nbtutils.c:1860
BTCycleId cycle_ctr
Definition: nbtutils.c:1854
Size BTreeShmemSize(void)
Definition: nbtutils.c:2001
uint16 BTCycleId
Definition: nbtree.h:26
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
int MaxBackends
Definition: globals.c:126
bool IsUnderPostmaster
Definition: globals.c:100
int max_vacuums
Definition: nbtutils.c:1856
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int num_vacuums
Definition: nbtutils.c:1855
Size BTreeShmemSize ( void  )

Definition at line 2001 of file nbtutils.c.

References add_size(), MaxBackends, mul_size(), and offsetof.

Referenced by BTreeShmemInit(), and CreateSharedMemoryAndSemaphores().

2002 {
2003  Size size;
2004 
2005  size = offsetof(BTVacInfo, vacuums);
2006  size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
2007  return size;
2008 }
int MaxBackends
Definition: globals.c:126
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:356
#define offsetof(type, field)
Definition: c.h:555

Variable Documentation

BTVacInfo* btvacinfo
static

Definition at line 1860 of file nbtutils.c.