PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeIndexscan.h File Reference
#include "access/parallel.h"
#include "nodes/execnodes.h"
Include dependency graph for nodeIndexscan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

IndexScanStateExecInitIndexScan (IndexScan *node, EState *estate, int eflags)
 
void ExecEndIndexScan (IndexScanState *node)
 
void ExecIndexMarkPos (IndexScanState *node)
 
void ExecIndexRestrPos (IndexScanState *node)
 
void ExecReScanIndexScan (IndexScanState *node)
 
void ExecIndexScanEstimate (IndexScanState *node, ParallelContext *pcxt)
 
void ExecIndexScanInitializeDSM (IndexScanState *node, ParallelContext *pcxt)
 
void ExecIndexScanReInitializeDSM (IndexScanState *node, ParallelContext *pcxt)
 
void ExecIndexScanInitializeWorker (IndexScanState *node, shm_toc *toc)
 
void ExecIndexBuildScanKeys (PlanState *planstate, Relation index, List *quals, bool isorderby, ScanKey *scanKeys, int *numScanKeys, IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys, IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
 
void ExecIndexEvalRuntimeKeys (ExprContext *econtext, IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
 
bool ExecIndexEvalArrayKeys (ExprContext *econtext, IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
 
bool ExecIndexAdvanceArrayKeys (IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
 

Function Documentation

void ExecEndIndexScan ( IndexScanState node)

Definition at line 808 of file nodeIndexscan.c.

References ExecClearTuple(), ExecCloseScanRelation(), ExecFreeExprContext(), FreeExprContext(), index_close(), index_endscan(), IndexScanState::iss_RelationDesc, IndexScanState::iss_RuntimeContext, IndexScanState::iss_ScanDesc, NoLock, ScanState::ps, PlanState::ps_ResultTupleSlot, IndexScanState::ss, ScanState::ss_currentRelation, and ScanState::ss_ScanTupleSlot.

Referenced by ExecEndNode().

809 {
810  Relation indexRelationDesc;
811  IndexScanDesc indexScanDesc;
812  Relation relation;
813 
814  /*
815  * extract information from the node
816  */
817  indexRelationDesc = node->iss_RelationDesc;
818  indexScanDesc = node->iss_ScanDesc;
819  relation = node->ss.ss_currentRelation;
820 
821  /*
822  * Free the exprcontext(s) ... now dead code, see ExecFreeExprContext
823  */
824 #ifdef NOT_USED
825  ExecFreeExprContext(&node->ss.ps);
826  if (node->iss_RuntimeContext)
827  FreeExprContext(node->iss_RuntimeContext, true);
828 #endif
829 
830  /*
831  * clear out tuple table slots
832  */
835 
836  /*
837  * close the index relation (no-op if we didn't open it)
838  */
839  if (indexScanDesc)
840  index_endscan(indexScanDesc);
841  if (indexRelationDesc)
842  index_close(indexRelationDesc, NoLock);
843 
844  /*
845  * close the heap relation.
846  */
847  ExecCloseScanRelation(relation);
848 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1104
Relation ss_currentRelation
Definition: execnodes.h:1102
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:521
PlanState ps
Definition: execnodes.h:1101
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
#define NoLock
Definition: lockdefs.h:34
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:340
void FreeExprContext(ExprContext *econtext, bool isCommit)
Definition: execUtils.c:354
ScanState ss
Definition: execnodes.h:1185
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:666
ExprContext * iss_RuntimeContext
Definition: execnodes.h:1195
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
IndexScanDesc iss_ScanDesc
Definition: execnodes.h:1197
Relation iss_RelationDesc
Definition: execnodes.h:1196
bool ExecIndexAdvanceArrayKeys ( IndexArrayKeyInfo arrayKeys,
int  numArrayKeys 
)

Definition at line 763 of file nodeIndexscan.c.

References IndexArrayKeyInfo::elem_nulls, IndexArrayKeyInfo::elem_values, IndexArrayKeyInfo::next_elem, IndexArrayKeyInfo::num_elems, IndexArrayKeyInfo::scan_key, ScanKeyData::sk_argument, ScanKeyData::sk_flags, and SK_ISNULL.

Referenced by MultiExecBitmapIndexScan().

764 {
765  bool found = false;
766  int j;
767 
768  /*
769  * Note we advance the rightmost array key most quickly, since it will
770  * correspond to the lowest-order index column among the available
771  * qualifications. This is hypothesized to result in better locality of
772  * access in the index.
773  */
774  for (j = numArrayKeys - 1; j >= 0; j--)
775  {
776  ScanKey scan_key = arrayKeys[j].scan_key;
777  int next_elem = arrayKeys[j].next_elem;
778  int num_elems = arrayKeys[j].num_elems;
779  Datum *elem_values = arrayKeys[j].elem_values;
780  bool *elem_nulls = arrayKeys[j].elem_nulls;
781 
782  if (next_elem >= num_elems)
783  {
784  next_elem = 0;
785  found = false; /* need to advance next array key */
786  }
787  else
788  found = true;
789  scan_key->sk_argument = elem_values[next_elem];
790  if (elem_nulls[next_elem])
791  scan_key->sk_flags |= SK_ISNULL;
792  else
793  scan_key->sk_flags &= ~SK_ISNULL;
794  arrayKeys[j].next_elem = next_elem + 1;
795  if (found)
796  break;
797  }
798 
799  return found;
800 }
Datum * elem_values
Definition: execnodes.h:1153
#define SK_ISNULL
Definition: skey.h:115
uintptr_t Datum
Definition: postgres.h:372
int sk_flags
Definition: skey.h:66
Datum sk_argument
Definition: skey.h:72
void ExecIndexBuildScanKeys ( PlanState planstate,
Relation  index,
List quals,
bool  isorderby,
ScanKey scanKeys,
int *  numScanKeys,
IndexRuntimeKeyInfo **  runtimeKeys,
int *  numRuntimeKeys,
IndexArrayKeyInfo **  arrayKeys,
int *  numArrayKeys 
)

Definition at line 1142 of file nodeIndexscan.c.

References IndexAmRoutine::amsearcharray, arg, NullTest::arg, ScalarArrayOpExpr::args, IndexArrayKeyInfo::array_expr, Assert, BTORDER_PROC, BTREE_AM_OID, elog, ERROR, ExecInitExpr(), get_leftop(), get_op_opfamily_properties(), get_opfamily_proc(), get_rightop(), INDEX_VAR, ScalarArrayOpExpr::inputcollid, RowCompareExpr::inputcollids, InvalidOid, InvalidStrategy, IS_NOT_NULL, IS_NULL, IsA, IndexRuntimeKeyInfo::key_expr, IndexRuntimeKeyInfo::key_toastable, RowCompareExpr::largs, lfirst, lfirst_oid, linitial, list_head(), list_length(), lnext, lsecond, MemSet, nodeTag, NullTest::nulltesttype, ScalarArrayOpExpr::opfuncid, ScalarArrayOpExpr::opno, RowCompareExpr::opnos, palloc(), palloc0(), pfree(), PointerGetDatum, RowCompareExpr::rargs, RowCompareExpr::rctype, RelationData::rd_amroutine, RelationData::rd_index, RelationData::rd_opfamily, RelationData::rd_rel, RegProcedureIsValid, repalloc(), IndexRuntimeKeyInfo::scan_key, IndexArrayKeyInfo::scan_key, ScanKeyEntryInitialize(), ScanKeyData::sk_argument, ScanKeyData::sk_attno, ScanKeyData::sk_flags, SK_ISNULL, SK_ORDER_BY, SK_ROW_END, SK_ROW_HEADER, SK_ROW_MEMBER, SK_SEARCHARRAY, SK_SEARCHNOTNULL, SK_SEARCHNULL, ScanKeyData::sk_strategy, TypeIsToastable, and ScalarArrayOpExpr::useOr.

Referenced by ExecInitBitmapIndexScan(), ExecInitIndexOnlyScan(), and ExecInitIndexScan().

1147 {
1148  ListCell *qual_cell;
1149  ScanKey scan_keys;
1150  IndexRuntimeKeyInfo *runtime_keys;
1151  IndexArrayKeyInfo *array_keys;
1152  int n_scan_keys;
1153  int n_runtime_keys;
1154  int max_runtime_keys;
1155  int n_array_keys;
1156  int j;
1157 
1158  /* Allocate array for ScanKey structs: one per qual */
1159  n_scan_keys = list_length(quals);
1160  scan_keys = (ScanKey) palloc(n_scan_keys * sizeof(ScanKeyData));
1161 
1162  /*
1163  * runtime_keys array is dynamically resized as needed. We handle it this
1164  * way so that the same runtime keys array can be shared between
1165  * indexquals and indexorderbys, which will be processed in separate calls
1166  * of this function. Caller must be sure to pass in NULL/0 for first
1167  * call.
1168  */
1169  runtime_keys = *runtimeKeys;
1170  n_runtime_keys = max_runtime_keys = *numRuntimeKeys;
1171 
1172  /* Allocate array_keys as large as it could possibly need to be */
1173  array_keys = (IndexArrayKeyInfo *)
1174  palloc0(n_scan_keys * sizeof(IndexArrayKeyInfo));
1175  n_array_keys = 0;
1176 
1177  /*
1178  * for each opclause in the given qual, convert the opclause into a single
1179  * scan key
1180  */
1181  j = 0;
1182  foreach(qual_cell, quals)
1183  {
1184  Expr *clause = (Expr *) lfirst(qual_cell);
1185  ScanKey this_scan_key = &scan_keys[j++];
1186  Oid opno; /* operator's OID */
1187  RegProcedure opfuncid; /* operator proc id used in scan */
1188  Oid opfamily; /* opfamily of index column */
1189  int op_strategy; /* operator's strategy number */
1190  Oid op_lefttype; /* operator's declared input types */
1191  Oid op_righttype;
1192  Expr *leftop; /* expr on lhs of operator */
1193  Expr *rightop; /* expr on rhs ... */
1194  AttrNumber varattno; /* att number used in scan */
1195 
1196  if (IsA(clause, OpExpr))
1197  {
1198  /* indexkey op const or indexkey op expression */
1199  int flags = 0;
1200  Datum scanvalue;
1201 
1202  opno = ((OpExpr *) clause)->opno;
1203  opfuncid = ((OpExpr *) clause)->opfuncid;
1204 
1205  /*
1206  * leftop should be the index key Var, possibly relabeled
1207  */
1208  leftop = (Expr *) get_leftop(clause);
1209 
1210  if (leftop && IsA(leftop, RelabelType))
1211  leftop = ((RelabelType *) leftop)->arg;
1212 
1213  Assert(leftop != NULL);
1214 
1215  if (!(IsA(leftop, Var) &&
1216  ((Var *) leftop)->varno == INDEX_VAR))
1217  elog(ERROR, "indexqual doesn't have key on left side");
1218 
1219  varattno = ((Var *) leftop)->varattno;
1220  if (varattno < 1 || varattno > index->rd_index->indnatts)
1221  elog(ERROR, "bogus index qualification");
1222 
1223  /*
1224  * We have to look up the operator's strategy number. This
1225  * provides a cross-check that the operator does match the index.
1226  */
1227  opfamily = index->rd_opfamily[varattno - 1];
1228 
1229  get_op_opfamily_properties(opno, opfamily, isorderby,
1230  &op_strategy,
1231  &op_lefttype,
1232  &op_righttype);
1233 
1234  if (isorderby)
1235  flags |= SK_ORDER_BY;
1236 
1237  /*
1238  * rightop is the constant or variable comparison value
1239  */
1240  rightop = (Expr *) get_rightop(clause);
1241 
1242  if (rightop && IsA(rightop, RelabelType))
1243  rightop = ((RelabelType *) rightop)->arg;
1244 
1245  Assert(rightop != NULL);
1246 
1247  if (IsA(rightop, Const))
1248  {
1249  /* OK, simple constant comparison value */
1250  scanvalue = ((Const *) rightop)->constvalue;
1251  if (((Const *) rightop)->constisnull)
1252  flags |= SK_ISNULL;
1253  }
1254  else
1255  {
1256  /* Need to treat this one as a runtime key */
1257  if (n_runtime_keys >= max_runtime_keys)
1258  {
1259  if (max_runtime_keys == 0)
1260  {
1261  max_runtime_keys = 8;
1262  runtime_keys = (IndexRuntimeKeyInfo *)
1263  palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1264  }
1265  else
1266  {
1267  max_runtime_keys *= 2;
1268  runtime_keys = (IndexRuntimeKeyInfo *)
1269  repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1270  }
1271  }
1272  runtime_keys[n_runtime_keys].scan_key = this_scan_key;
1273  runtime_keys[n_runtime_keys].key_expr =
1274  ExecInitExpr(rightop, planstate);
1275  runtime_keys[n_runtime_keys].key_toastable =
1276  TypeIsToastable(op_righttype);
1277  n_runtime_keys++;
1278  scanvalue = (Datum) 0;
1279  }
1280 
1281  /*
1282  * initialize the scan key's fields appropriately
1283  */
1284  ScanKeyEntryInitialize(this_scan_key,
1285  flags,
1286  varattno, /* attribute number to scan */
1287  op_strategy, /* op's strategy */
1288  op_righttype, /* strategy subtype */
1289  ((OpExpr *) clause)->inputcollid, /* collation */
1290  opfuncid, /* reg proc to use */
1291  scanvalue); /* constant */
1292  }
1293  else if (IsA(clause, RowCompareExpr))
1294  {
1295  /* (indexkey, indexkey, ...) op (expression, expression, ...) */
1296  RowCompareExpr *rc = (RowCompareExpr *) clause;
1297  ListCell *largs_cell = list_head(rc->largs);
1298  ListCell *rargs_cell = list_head(rc->rargs);
1299  ListCell *opnos_cell = list_head(rc->opnos);
1300  ListCell *collids_cell = list_head(rc->inputcollids);
1301  ScanKey first_sub_key;
1302  int n_sub_key;
1303 
1304  Assert(!isorderby);
1305 
1306  first_sub_key = (ScanKey)
1307  palloc(list_length(rc->opnos) * sizeof(ScanKeyData));
1308  n_sub_key = 0;
1309 
1310  /* Scan RowCompare columns and generate subsidiary ScanKey items */
1311  while (opnos_cell != NULL)
1312  {
1313  ScanKey this_sub_key = &first_sub_key[n_sub_key];
1314  int flags = SK_ROW_MEMBER;
1315  Datum scanvalue;
1316  Oid inputcollation;
1317 
1318  /*
1319  * leftop should be the index key Var, possibly relabeled
1320  */
1321  leftop = (Expr *) lfirst(largs_cell);
1322  largs_cell = lnext(largs_cell);
1323 
1324  if (leftop && IsA(leftop, RelabelType))
1325  leftop = ((RelabelType *) leftop)->arg;
1326 
1327  Assert(leftop != NULL);
1328 
1329  if (!(IsA(leftop, Var) &&
1330  ((Var *) leftop)->varno == INDEX_VAR))
1331  elog(ERROR, "indexqual doesn't have key on left side");
1332 
1333  varattno = ((Var *) leftop)->varattno;
1334 
1335  /*
1336  * We have to look up the operator's associated btree support
1337  * function
1338  */
1339  opno = lfirst_oid(opnos_cell);
1340  opnos_cell = lnext(opnos_cell);
1341 
1342  if (index->rd_rel->relam != BTREE_AM_OID ||
1343  varattno < 1 || varattno > index->rd_index->indnatts)
1344  elog(ERROR, "bogus RowCompare index qualification");
1345  opfamily = index->rd_opfamily[varattno - 1];
1346 
1347  get_op_opfamily_properties(opno, opfamily, isorderby,
1348  &op_strategy,
1349  &op_lefttype,
1350  &op_righttype);
1351 
1352  if (op_strategy != rc->rctype)
1353  elog(ERROR, "RowCompare index qualification contains wrong operator");
1354 
1355  opfuncid = get_opfamily_proc(opfamily,
1356  op_lefttype,
1357  op_righttype,
1358  BTORDER_PROC);
1359  if (!RegProcedureIsValid(opfuncid))
1360  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
1361  BTORDER_PROC, op_lefttype, op_righttype, opfamily);
1362 
1363  inputcollation = lfirst_oid(collids_cell);
1364  collids_cell = lnext(collids_cell);
1365 
1366  /*
1367  * rightop is the constant or variable comparison value
1368  */
1369  rightop = (Expr *) lfirst(rargs_cell);
1370  rargs_cell = lnext(rargs_cell);
1371 
1372  if (rightop && IsA(rightop, RelabelType))
1373  rightop = ((RelabelType *) rightop)->arg;
1374 
1375  Assert(rightop != NULL);
1376 
1377  if (IsA(rightop, Const))
1378  {
1379  /* OK, simple constant comparison value */
1380  scanvalue = ((Const *) rightop)->constvalue;
1381  if (((Const *) rightop)->constisnull)
1382  flags |= SK_ISNULL;
1383  }
1384  else
1385  {
1386  /* Need to treat this one as a runtime key */
1387  if (n_runtime_keys >= max_runtime_keys)
1388  {
1389  if (max_runtime_keys == 0)
1390  {
1391  max_runtime_keys = 8;
1392  runtime_keys = (IndexRuntimeKeyInfo *)
1393  palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1394  }
1395  else
1396  {
1397  max_runtime_keys *= 2;
1398  runtime_keys = (IndexRuntimeKeyInfo *)
1399  repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1400  }
1401  }
1402  runtime_keys[n_runtime_keys].scan_key = this_sub_key;
1403  runtime_keys[n_runtime_keys].key_expr =
1404  ExecInitExpr(rightop, planstate);
1405  runtime_keys[n_runtime_keys].key_toastable =
1406  TypeIsToastable(op_righttype);
1407  n_runtime_keys++;
1408  scanvalue = (Datum) 0;
1409  }
1410 
1411  /*
1412  * initialize the subsidiary scan key's fields appropriately
1413  */
1414  ScanKeyEntryInitialize(this_sub_key,
1415  flags,
1416  varattno, /* attribute number */
1417  op_strategy, /* op's strategy */
1418  op_righttype, /* strategy subtype */
1419  inputcollation, /* collation */
1420  opfuncid, /* reg proc to use */
1421  scanvalue); /* constant */
1422  n_sub_key++;
1423  }
1424 
1425  /* Mark the last subsidiary scankey correctly */
1426  first_sub_key[n_sub_key - 1].sk_flags |= SK_ROW_END;
1427 
1428  /*
1429  * We don't use ScanKeyEntryInitialize for the header because it
1430  * isn't going to contain a valid sk_func pointer.
1431  */
1432  MemSet(this_scan_key, 0, sizeof(ScanKeyData));
1433  this_scan_key->sk_flags = SK_ROW_HEADER;
1434  this_scan_key->sk_attno = first_sub_key->sk_attno;
1435  this_scan_key->sk_strategy = rc->rctype;
1436  /* sk_subtype, sk_collation, sk_func not used in a header */
1437  this_scan_key->sk_argument = PointerGetDatum(first_sub_key);
1438  }
1439  else if (IsA(clause, ScalarArrayOpExpr))
1440  {
1441  /* indexkey op ANY (array-expression) */
1442  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1443  int flags = 0;
1444  Datum scanvalue;
1445 
1446  Assert(!isorderby);
1447 
1448  Assert(saop->useOr);
1449  opno = saop->opno;
1450  opfuncid = saop->opfuncid;
1451 
1452  /*
1453  * leftop should be the index key Var, possibly relabeled
1454  */
1455  leftop = (Expr *) linitial(saop->args);
1456 
1457  if (leftop && IsA(leftop, RelabelType))
1458  leftop = ((RelabelType *) leftop)->arg;
1459 
1460  Assert(leftop != NULL);
1461 
1462  if (!(IsA(leftop, Var) &&
1463  ((Var *) leftop)->varno == INDEX_VAR))
1464  elog(ERROR, "indexqual doesn't have key on left side");
1465 
1466  varattno = ((Var *) leftop)->varattno;
1467  if (varattno < 1 || varattno > index->rd_index->indnatts)
1468  elog(ERROR, "bogus index qualification");
1469 
1470  /*
1471  * We have to look up the operator's strategy number. This
1472  * provides a cross-check that the operator does match the index.
1473  */
1474  opfamily = index->rd_opfamily[varattno - 1];
1475 
1476  get_op_opfamily_properties(opno, opfamily, isorderby,
1477  &op_strategy,
1478  &op_lefttype,
1479  &op_righttype);
1480 
1481  /*
1482  * rightop is the constant or variable array value
1483  */
1484  rightop = (Expr *) lsecond(saop->args);
1485 
1486  if (rightop && IsA(rightop, RelabelType))
1487  rightop = ((RelabelType *) rightop)->arg;
1488 
1489  Assert(rightop != NULL);
1490 
1491  if (index->rd_amroutine->amsearcharray)
1492  {
1493  /* Index AM will handle this like a simple operator */
1494  flags |= SK_SEARCHARRAY;
1495  if (IsA(rightop, Const))
1496  {
1497  /* OK, simple constant comparison value */
1498  scanvalue = ((Const *) rightop)->constvalue;
1499  if (((Const *) rightop)->constisnull)
1500  flags |= SK_ISNULL;
1501  }
1502  else
1503  {
1504  /* Need to treat this one as a runtime key */
1505  if (n_runtime_keys >= max_runtime_keys)
1506  {
1507  if (max_runtime_keys == 0)
1508  {
1509  max_runtime_keys = 8;
1510  runtime_keys = (IndexRuntimeKeyInfo *)
1511  palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1512  }
1513  else
1514  {
1515  max_runtime_keys *= 2;
1516  runtime_keys = (IndexRuntimeKeyInfo *)
1517  repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
1518  }
1519  }
1520  runtime_keys[n_runtime_keys].scan_key = this_scan_key;
1521  runtime_keys[n_runtime_keys].key_expr =
1522  ExecInitExpr(rightop, planstate);
1523 
1524  /*
1525  * Careful here: the runtime expression is not of
1526  * op_righttype, but rather is an array of same; so
1527  * TypeIsToastable() isn't helpful. However, we can
1528  * assume that all array types are toastable.
1529  */
1530  runtime_keys[n_runtime_keys].key_toastable = true;
1531  n_runtime_keys++;
1532  scanvalue = (Datum) 0;
1533  }
1534  }
1535  else
1536  {
1537  /* Executor has to expand the array value */
1538  array_keys[n_array_keys].scan_key = this_scan_key;
1539  array_keys[n_array_keys].array_expr =
1540  ExecInitExpr(rightop, planstate);
1541  /* the remaining fields were zeroed by palloc0 */
1542  n_array_keys++;
1543  scanvalue = (Datum) 0;
1544  }
1545 
1546  /*
1547  * initialize the scan key's fields appropriately
1548  */
1549  ScanKeyEntryInitialize(this_scan_key,
1550  flags,
1551  varattno, /* attribute number to scan */
1552  op_strategy, /* op's strategy */
1553  op_righttype, /* strategy subtype */
1554  saop->inputcollid, /* collation */
1555  opfuncid, /* reg proc to use */
1556  scanvalue); /* constant */
1557  }
1558  else if (IsA(clause, NullTest))
1559  {
1560  /* indexkey IS NULL or indexkey IS NOT NULL */
1561  NullTest *ntest = (NullTest *) clause;
1562  int flags;
1563 
1564  Assert(!isorderby);
1565 
1566  /*
1567  * argument should be the index key Var, possibly relabeled
1568  */
1569  leftop = ntest->arg;
1570 
1571  if (leftop && IsA(leftop, RelabelType))
1572  leftop = ((RelabelType *) leftop)->arg;
1573 
1574  Assert(leftop != NULL);
1575 
1576  if (!(IsA(leftop, Var) &&
1577  ((Var *) leftop)->varno == INDEX_VAR))
1578  elog(ERROR, "NullTest indexqual has wrong key");
1579 
1580  varattno = ((Var *) leftop)->varattno;
1581 
1582  /*
1583  * initialize the scan key's fields appropriately
1584  */
1585  switch (ntest->nulltesttype)
1586  {
1587  case IS_NULL:
1588  flags = SK_ISNULL | SK_SEARCHNULL;
1589  break;
1590  case IS_NOT_NULL:
1591  flags = SK_ISNULL | SK_SEARCHNOTNULL;
1592  break;
1593  default:
1594  elog(ERROR, "unrecognized nulltesttype: %d",
1595  (int) ntest->nulltesttype);
1596  flags = 0; /* keep compiler quiet */
1597  break;
1598  }
1599 
1600  ScanKeyEntryInitialize(this_scan_key,
1601  flags,
1602  varattno, /* attribute number to scan */
1603  InvalidStrategy, /* no strategy */
1604  InvalidOid, /* no strategy subtype */
1605  InvalidOid, /* no collation */
1606  InvalidOid, /* no reg proc for this */
1607  (Datum) 0); /* constant */
1608  }
1609  else
1610  elog(ERROR, "unsupported indexqual type: %d",
1611  (int) nodeTag(clause));
1612  }
1613 
1614  Assert(n_runtime_keys <= max_runtime_keys);
1615 
1616  /* Get rid of any unused arrays */
1617  if (n_array_keys == 0)
1618  {
1619  pfree(array_keys);
1620  array_keys = NULL;
1621  }
1622 
1623  /*
1624  * Return info to our caller.
1625  */
1626  *scanKeys = scan_keys;
1627  *numScanKeys = n_scan_keys;
1628  *runtimeKeys = runtime_keys;
1629  *numRuntimeKeys = n_runtime_keys;
1630  if (arrayKeys)
1631  {
1632  *arrayKeys = array_keys;
1633  *numArrayKeys = n_array_keys;
1634  }
1635  else if (n_array_keys != 0)
1636  elog(ERROR, "ScalarArrayOpExpr index qual found where not allowed");
1637 }
#define InvalidStrategy
Definition: stratnum.h:24
#define SK_ROW_MEMBER
Definition: skey.h:118
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
#define BTORDER_PROC
Definition: nbtree.h:229
#define SK_ORDER_BY
Definition: skey.h:123
#define PointerGetDatum(X)
Definition: postgres.h:562
RowCompareType rctype
Definition: primnodes.h:1031
regproc RegProcedure
Definition: c.h:389
#define BTREE_AM_OID
Definition: pg_am.h:70
#define MemSet(start, val, len)
Definition: c.h:863
struct ScanKeyData ScanKeyData
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
#define SK_SEARCHARRAY
Definition: skey.h:120
struct IndexAmRoutine * rd_amroutine
Definition: rel.h:181
#define lsecond(l)
Definition: pg_list.h:116
#define SK_ROW_END
Definition: skey.h:119
Form_pg_index rd_index
Definition: rel.h:159
void pfree(void *pointer)
Definition: mcxt.c:949
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
StrategyNumber sk_strategy
Definition: skey.h:68
Expr * arg
Definition: primnodes.h:1180
ExprState * key_expr
Definition: execnodes.h:1143
void ScanKeyEntryInitialize(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, RegProcedure procedure, Datum argument)
Definition: scankey.c:32
ScanKeyData * ScanKey
Definition: skey.h:75
#define RegProcedureIsValid(p)
Definition: c.h:534
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define SK_SEARCHNOTNULL
Definition: skey.h:122
Oid * rd_opfamily
Definition: rel.h:182
#define SK_ISNULL
Definition: skey.h:115
#define lnext(lc)
Definition: pg_list.h:105
#define SK_ROW_HEADER
Definition: skey.h:117
void * palloc0(Size size)
Definition: mcxt.c:877
uintptr_t Datum
Definition: postgres.h:372
NullTestType nulltesttype
Definition: primnodes.h:1181
bool amsearcharray
Definition: amapi.h:183
#define InvalidOid
Definition: postgres_ext.h:36
ExprState * array_expr
Definition: execnodes.h:1150
int sk_flags
Definition: skey.h:66
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
static int list_length(const List *l)
Definition: pg_list.h:89
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962
#define nodeTag(nodeptr)
Definition: nodes.h:515
Node * get_rightop(const Expr *clause)
Definition: clauses.c:216
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:133
void * palloc(Size size)
Definition: mcxt.c:848
void * arg
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:113
#define elog
Definition: elog.h:219
List * inputcollids
Definition: primnodes.h:1034
#define INDEX_VAR
Definition: primnodes.h:155
#define TypeIsToastable(typid)
Definition: lsyscache.h:184
Datum sk_argument
Definition: skey.h:72
int16 AttrNumber
Definition: attnum.h:21
#define SK_SEARCHNULL
Definition: skey.h:121
AttrNumber sk_attno
Definition: skey.h:67
#define lfirst_oid(lc)
Definition: pg_list.h:108
bool ExecIndexEvalArrayKeys ( ExprContext econtext,
IndexArrayKeyInfo arrayKeys,
int  numArrayKeys 
)

Definition at line 684 of file nodeIndexscan.c.

References ARR_ELEMTYPE, IndexArrayKeyInfo::array_expr, DatumGetArrayTypeP, deconstruct_array(), ExprContext::ecxt_per_tuple_memory, IndexArrayKeyInfo::elem_nulls, IndexArrayKeyInfo::elem_values, ExecEvalExpr(), get_typlenbyvalalign(), MemoryContextSwitchTo(), IndexArrayKeyInfo::next_elem, IndexArrayKeyInfo::num_elems, IndexArrayKeyInfo::scan_key, ScanKeyData::sk_argument, ScanKeyData::sk_flags, and SK_ISNULL.

Referenced by ExecReScanBitmapIndexScan().

686 {
687  bool result = true;
688  int j;
689  MemoryContext oldContext;
690 
691  /* We want to keep the arrays in per-tuple memory */
692  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
693 
694  for (j = 0; j < numArrayKeys; j++)
695  {
696  ScanKey scan_key = arrayKeys[j].scan_key;
697  ExprState *array_expr = arrayKeys[j].array_expr;
698  Datum arraydatum;
699  bool isNull;
700  ArrayType *arrayval;
701  int16 elmlen;
702  bool elmbyval;
703  char elmalign;
704  int num_elems;
705  Datum *elem_values;
706  bool *elem_nulls;
707 
708  /*
709  * Compute and deconstruct the array expression. (Notes in
710  * ExecIndexEvalRuntimeKeys() apply here too.)
711  */
712  arraydatum = ExecEvalExpr(array_expr,
713  econtext,
714  &isNull);
715  if (isNull)
716  {
717  result = false;
718  break; /* no point in evaluating more */
719  }
720  arrayval = DatumGetArrayTypeP(arraydatum);
721  /* We could cache this data, but not clear it's worth it */
723  &elmlen, &elmbyval, &elmalign);
724  deconstruct_array(arrayval,
725  ARR_ELEMTYPE(arrayval),
726  elmlen, elmbyval, elmalign,
727  &elem_values, &elem_nulls, &num_elems);
728  if (num_elems <= 0)
729  {
730  result = false;
731  break; /* no point in evaluating more */
732  }
733 
734  /*
735  * Note: we expect the previous array data, if any, to be
736  * automatically freed by resetting the per-tuple context; hence no
737  * pfree's here.
738  */
739  arrayKeys[j].elem_values = elem_values;
740  arrayKeys[j].elem_nulls = elem_nulls;
741  arrayKeys[j].num_elems = num_elems;
742  scan_key->sk_argument = elem_values[0];
743  if (elem_nulls[0])
744  scan_key->sk_flags |= SK_ISNULL;
745  else
746  scan_key->sk_flags &= ~SK_ISNULL;
747  arrayKeys[j].next_elem = 1;
748  }
749 
750  MemoryContextSwitchTo(oldContext);
751 
752  return result;
753 }
Datum * elem_values
Definition: execnodes.h:1153
signed short int16
Definition: c.h:245
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2021
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:286
#define SK_ISNULL
Definition: skey.h:115
uintptr_t Datum
Definition: postgres.h:372
ExprState * array_expr
Definition: execnodes.h:1150
int sk_flags
Definition: skey.h:66
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
Datum sk_argument
Definition: skey.h:72
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define DatumGetArrayTypeP(X)
Definition: array.h:246
void ExecIndexEvalRuntimeKeys ( ExprContext econtext,
IndexRuntimeKeyInfo runtimeKeys,
int  numRuntimeKeys 
)

Definition at line 622 of file nodeIndexscan.c.

References ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), IndexRuntimeKeyInfo::key_expr, MemoryContextSwitchTo(), PG_DETOAST_DATUM, PointerGetDatum, IndexRuntimeKeyInfo::scan_key, ScanKeyData::sk_argument, ScanKeyData::sk_flags, and SK_ISNULL.

Referenced by ExecReScanBitmapIndexScan(), ExecReScanIndexOnlyScan(), and ExecReScanIndexScan().

624 {
625  int j;
626  MemoryContext oldContext;
627 
628  /* We want to keep the key values in per-tuple memory */
629  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
630 
631  for (j = 0; j < numRuntimeKeys; j++)
632  {
633  ScanKey scan_key = runtimeKeys[j].scan_key;
634  ExprState *key_expr = runtimeKeys[j].key_expr;
635  Datum scanvalue;
636  bool isNull;
637 
638  /*
639  * For each run-time key, extract the run-time expression and evaluate
640  * it with respect to the current context. We then stick the result
641  * into the proper scan key.
642  *
643  * Note: the result of the eval could be a pass-by-ref value that's
644  * stored in some outer scan's tuple, not in
645  * econtext->ecxt_per_tuple_memory. We assume that the outer tuple
646  * will stay put throughout our scan. If this is wrong, we could copy
647  * the result into our context explicitly, but I think that's not
648  * necessary.
649  *
650  * It's also entirely possible that the result of the eval is a
651  * toasted value. In this case we should forcibly detoast it, to
652  * avoid repeat detoastings each time the value is examined by an
653  * index support function.
654  */
655  scanvalue = ExecEvalExpr(key_expr,
656  econtext,
657  &isNull);
658  if (isNull)
659  {
660  scan_key->sk_argument = scanvalue;
661  scan_key->sk_flags |= SK_ISNULL;
662  }
663  else
664  {
665  if (runtimeKeys[j].key_toastable)
666  scanvalue = PointerGetDatum(PG_DETOAST_DATUM(scanvalue));
667  scan_key->sk_argument = scanvalue;
668  scan_key->sk_flags &= ~SK_ISNULL;
669  }
670  }
671 
672  MemoryContextSwitchTo(oldContext);
673 }
#define PointerGetDatum(X)
Definition: postgres.h:562
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExprState * key_expr
Definition: execnodes.h:1143
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:286
#define SK_ISNULL
Definition: skey.h:115
uintptr_t Datum
Definition: postgres.h:372
int sk_flags
Definition: skey.h:66
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:205
Datum sk_argument
Definition: skey.h:72
void ExecIndexMarkPos ( IndexScanState node)

Definition at line 855 of file nodeIndexscan.c.

References index_markpos(), and IndexScanState::iss_ScanDesc.

Referenced by ExecMarkPos().

856 {
858 }
void index_markpos(IndexScanDesc scan)
Definition: indexam.c:370
IndexScanDesc iss_ScanDesc
Definition: execnodes.h:1197
void ExecIndexRestrPos ( IndexScanState node)

Definition at line 865 of file nodeIndexscan.c.

References index_restrpos(), and IndexScanState::iss_ScanDesc.

Referenced by ExecRestrPos().

866 {
868 }
void index_restrpos(IndexScanDesc scan)
Definition: indexam.c:395
IndexScanDesc iss_ScanDesc
Definition: execnodes.h:1197
void ExecIndexScanEstimate ( IndexScanState node,
ParallelContext pcxt 
)

Definition at line 1651 of file nodeIndexscan.c.

References EState::es_snapshot, ParallelContext::estimator, index_parallelscan_estimate(), IndexScanState::iss_PscanLen, IndexScanState::iss_RelationDesc, ScanState::ps, shm_toc_estimate_chunk, shm_toc_estimate_keys, IndexScanState::ss, and PlanState::state.

Referenced by ExecParallelEstimate().

1653 {
1654  EState *estate = node->ss.ps.state;
1655 
1657  estate->es_snapshot);
1659  shm_toc_estimate_keys(&pcxt->estimator, 1);
1660 }
shm_toc_estimator estimator
Definition: parallel.h:41
Snapshot es_snapshot
Definition: execnodes.h:429
EState * state
Definition: execnodes.h:849
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size index_parallelscan_estimate(Relation indexRelation, Snapshot snapshot)
Definition: indexam.c:417
PlanState ps
Definition: execnodes.h:1101
ScanState ss
Definition: execnodes.h:1185
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Relation iss_RelationDesc
Definition: execnodes.h:1196
void ExecIndexScanInitializeDSM ( IndexScanState node,
ParallelContext pcxt 
)

Definition at line 1669 of file nodeIndexscan.c.

References EState::es_snapshot, index_beginscan_parallel(), index_parallelscan_initialize(), index_rescan(), IndexScanState::iss_NumOrderByKeys, IndexScanState::iss_NumRuntimeKeys, IndexScanState::iss_NumScanKeys, IndexScanState::iss_OrderByKeys, IndexScanState::iss_PscanLen, IndexScanState::iss_RelationDesc, IndexScanState::iss_RuntimeKeysReady, IndexScanState::iss_ScanDesc, IndexScanState::iss_ScanKeys, PlanState::plan, Plan::plan_node_id, ScanState::ps, shm_toc_allocate(), shm_toc_insert(), IndexScanState::ss, ScanState::ss_currentRelation, PlanState::state, and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

1671 {
1672  EState *estate = node->ss.ps.state;
1673  ParallelIndexScanDesc piscan;
1674 
1675  piscan = shm_toc_allocate(pcxt->toc, node->iss_PscanLen);
1677  node->iss_RelationDesc,
1678  estate->es_snapshot,
1679  piscan);
1680  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, piscan);
1681  node->iss_ScanDesc =
1683  node->iss_RelationDesc,
1684  node->iss_NumScanKeys,
1685  node->iss_NumOrderByKeys,
1686  piscan);
1687 
1688  /*
1689  * If no run-time keys to calculate or they are ready, go ahead and pass
1690  * the scankeys to the index AM.
1691  */
1692  if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
1693  index_rescan(node->iss_ScanDesc,
1694  node->iss_ScanKeys, node->iss_NumScanKeys,
1695  node->iss_OrderByKeys, node->iss_NumOrderByKeys);
1696 }
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition: indexam.c:496
int plan_node_id
Definition: plannodes.h:143
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:310
Snapshot es_snapshot
Definition: execnodes.h:429
Relation ss_currentRelation
Definition: execnodes.h:1102
EState * state
Definition: execnodes.h:849
ScanKey iss_ScanKeys
Definition: execnodes.h:1188
PlanState ps
Definition: execnodes.h:1101
int iss_NumRuntimeKeys
Definition: execnodes.h:1193
bool iss_RuntimeKeysReady
Definition: execnodes.h:1194
Plan * plan
Definition: execnodes.h:847
ScanState ss
Definition: execnodes.h:1185
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void index_parallelscan_initialize(Relation heapRelation, Relation indexRelation, Snapshot snapshot, ParallelIndexScanDesc target)
Definition: indexam.c:450
int iss_NumOrderByKeys
Definition: execnodes.h:1191
IndexScanDesc iss_ScanDesc
Definition: execnodes.h:1197
shm_toc * toc
Definition: parallel.h:44
Relation iss_RelationDesc
Definition: execnodes.h:1196
ScanKey iss_OrderByKeys
Definition: execnodes.h:1190
void ExecIndexScanInitializeWorker ( IndexScanState node,
shm_toc toc 
)

Definition at line 1718 of file nodeIndexscan.c.

References index_beginscan_parallel(), index_rescan(), IndexScanState::iss_NumOrderByKeys, IndexScanState::iss_NumRuntimeKeys, IndexScanState::iss_NumScanKeys, IndexScanState::iss_OrderByKeys, IndexScanState::iss_RelationDesc, IndexScanState::iss_RuntimeKeysReady, IndexScanState::iss_ScanDesc, IndexScanState::iss_ScanKeys, PlanState::plan, Plan::plan_node_id, ScanState::ps, shm_toc_lookup(), IndexScanState::ss, and ScanState::ss_currentRelation.

Referenced by ExecParallelInitializeWorker().

1719 {
1720  ParallelIndexScanDesc piscan;
1721 
1722  piscan = shm_toc_lookup(toc, node->ss.ps.plan->plan_node_id, false);
1723  node->iss_ScanDesc =
1725  node->iss_RelationDesc,
1726  node->iss_NumScanKeys,
1727  node->iss_NumOrderByKeys,
1728  piscan);
1729 
1730  /*
1731  * If no run-time keys to calculate or they are ready, go ahead and pass
1732  * the scankeys to the index AM.
1733  */
1734  if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
1735  index_rescan(node->iss_ScanDesc,
1736  node->iss_ScanKeys, node->iss_NumScanKeys,
1737  node->iss_OrderByKeys, node->iss_NumOrderByKeys);
1738 }
IndexScanDesc index_beginscan_parallel(Relation heaprel, Relation indexrel, int nkeys, int norderbys, ParallelIndexScanDesc pscan)
Definition: indexam.c:496
int plan_node_id
Definition: plannodes.h:143
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:310
Relation ss_currentRelation
Definition: execnodes.h:1102
ScanKey iss_ScanKeys
Definition: execnodes.h:1188
PlanState ps
Definition: execnodes.h:1101
int iss_NumRuntimeKeys
Definition: execnodes.h:1193
bool iss_RuntimeKeysReady
Definition: execnodes.h:1194
Plan * plan
Definition: execnodes.h:847
ScanState ss
Definition: execnodes.h:1185
int iss_NumOrderByKeys
Definition: execnodes.h:1191
IndexScanDesc iss_ScanDesc
Definition: execnodes.h:1197
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
Relation iss_RelationDesc
Definition: execnodes.h:1196
ScanKey iss_OrderByKeys
Definition: execnodes.h:1190
void ExecIndexScanReInitializeDSM ( IndexScanState node,
ParallelContext pcxt 
)

Definition at line 1705 of file nodeIndexscan.c.

References index_parallelrescan(), and IndexScanState::iss_ScanDesc.

Referenced by ExecParallelReInitializeDSM().

1707 {
1709 }
void index_parallelrescan(IndexScanDesc scan)
Definition: indexam.c:481
IndexScanDesc iss_ScanDesc
Definition: execnodes.h:1197
IndexScanState* ExecInitIndexScan ( IndexScan node,
EState estate,
int  eflags 
)

Definition at line 882 of file nodeIndexscan.c.

References SortSupportData::abbreviate, AccessShareLock, Assert, CurrentMemoryContext, EXEC_FLAG_EXPLAIN_ONLY, ExecAssignExprContext(), ExecAssignResultTypeFromTL(), ExecAssignScanProjectionInfo(), ExecAssignScanType(), ExecIndexBuildScanKeys(), ExecIndexScan(), ExecInitExprList(), ExecInitQual(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), PlanState::ExecProcNode, ExecRelationIsTargetRelation(), exprCollation(), exprType(), forboth, get_typlenbyval(), i, index_open(), IndexScan::indexid, IndexScan::indexorderby, IndexScan::indexorderbyops, IndexScan::indexorderbyorig, IndexScanState::indexorderbyorig, IndexScan::indexqual, IndexScan::indexqualorig, IndexScanState::indexqualorig, IndexScanState::iss_NumOrderByKeys, IndexScanState::iss_NumRuntimeKeys, IndexScanState::iss_NumScanKeys, IndexScanState::iss_OrderByKeys, IndexScanState::iss_OrderByNulls, IndexScanState::iss_OrderByTypByVals, IndexScanState::iss_OrderByTypLens, IndexScanState::iss_OrderByValues, IndexScanState::iss_RelationDesc, IndexScanState::iss_ReorderQueue, IndexScanState::iss_RuntimeContext, IndexScanState::iss_RuntimeKeys, IndexScanState::iss_RuntimeKeysReady, IndexScanState::iss_ScanKeys, IndexScanState::iss_SortSupport, lfirst, lfirst_oid, list_length(), makeNode, NoLock, pairingheap_allocate(), palloc(), palloc0(), Scan::plan, PlanState::plan, PrepareSortSupportFromOrderingOp(), ScanState::ps, PlanState::ps_ExprContext, Plan::qual, PlanState::qual, RelationGetDescr, reorderqueue_cmp(), IndexScan::scan, Scan::scanrelid, IndexScanState::ss, ScanState::ss_currentRelation, ScanState::ss_currentScanDesc, SortSupportData::ssup_attno, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, and PlanState::state.

Referenced by ExecInitNode().

883 {
884  IndexScanState *indexstate;
885  Relation currentRelation;
886  bool relistarget;
887 
888  /*
889  * create state structure
890  */
891  indexstate = makeNode(IndexScanState);
892  indexstate->ss.ps.plan = (Plan *) node;
893  indexstate->ss.ps.state = estate;
894  indexstate->ss.ps.ExecProcNode = ExecIndexScan;
895 
896  /*
897  * Miscellaneous initialization
898  *
899  * create expression context for node
900  */
901  ExecAssignExprContext(estate, &indexstate->ss.ps);
902 
903  /*
904  * initialize child expressions
905  *
906  * Note: we don't initialize all of the indexqual expression, only the
907  * sub-parts corresponding to runtime keys (see below). Likewise for
908  * indexorderby, if any. But the indexqualorig expression is always
909  * initialized even though it will only be used in some uncommon cases ---
910  * would be nice to improve that. (Problem is that any SubPlans present
911  * in the expression must be found now...)
912  */
913  indexstate->ss.ps.qual =
914  ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
915  indexstate->indexqualorig =
916  ExecInitQual(node->indexqualorig, (PlanState *) indexstate);
917  indexstate->indexorderbyorig =
918  ExecInitExprList(node->indexorderbyorig, (PlanState *) indexstate);
919 
920  /*
921  * tuple table initialization
922  */
923  ExecInitResultTupleSlot(estate, &indexstate->ss.ps);
924  ExecInitScanTupleSlot(estate, &indexstate->ss);
925 
926  /*
927  * open the base relation and acquire appropriate lock on it.
928  */
929  currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
930 
931  indexstate->ss.ss_currentRelation = currentRelation;
932  indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
933 
934  /*
935  * get the scan type from the relation descriptor.
936  */
937  ExecAssignScanType(&indexstate->ss, RelationGetDescr(currentRelation));
938 
939  /*
940  * Initialize result tuple type and projection info.
941  */
942  ExecAssignResultTypeFromTL(&indexstate->ss.ps);
943  ExecAssignScanProjectionInfo(&indexstate->ss);
944 
945  /*
946  * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
947  * here. This allows an index-advisor plugin to EXPLAIN a plan containing
948  * references to nonexistent indexes.
949  */
950  if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
951  return indexstate;
952 
953  /*
954  * Open the index relation.
955  *
956  * If the parent table is one of the target relations of the query, then
957  * InitPlan already opened and write-locked the index, so we can avoid
958  * taking another lock here. Otherwise we need a normal reader's lock.
959  */
960  relistarget = ExecRelationIsTargetRelation(estate, node->scan.scanrelid);
961  indexstate->iss_RelationDesc = index_open(node->indexid,
962  relistarget ? NoLock : AccessShareLock);
963 
964  /*
965  * Initialize index-specific scan state
966  */
967  indexstate->iss_RuntimeKeysReady = false;
968  indexstate->iss_RuntimeKeys = NULL;
969  indexstate->iss_NumRuntimeKeys = 0;
970 
971  /*
972  * build the index scan keys from the index qualification
973  */
974  ExecIndexBuildScanKeys((PlanState *) indexstate,
975  indexstate->iss_RelationDesc,
976  node->indexqual,
977  false,
978  &indexstate->iss_ScanKeys,
979  &indexstate->iss_NumScanKeys,
980  &indexstate->iss_RuntimeKeys,
981  &indexstate->iss_NumRuntimeKeys,
982  NULL, /* no ArrayKeys */
983  NULL);
984 
985  /*
986  * any ORDER BY exprs have to be turned into scankeys in the same way
987  */
988  ExecIndexBuildScanKeys((PlanState *) indexstate,
989  indexstate->iss_RelationDesc,
990  node->indexorderby,
991  true,
992  &indexstate->iss_OrderByKeys,
993  &indexstate->iss_NumOrderByKeys,
994  &indexstate->iss_RuntimeKeys,
995  &indexstate->iss_NumRuntimeKeys,
996  NULL, /* no ArrayKeys */
997  NULL);
998 
999  /* Initialize sort support, if we need to re-check ORDER BY exprs */
1000  if (indexstate->iss_NumOrderByKeys > 0)
1001  {
1002  int numOrderByKeys = indexstate->iss_NumOrderByKeys;
1003  int i;
1004  ListCell *lco;
1005  ListCell *lcx;
1006 
1007  /*
1008  * Prepare sort support, and look up the data type for each ORDER BY
1009  * expression.
1010  */
1011  Assert(numOrderByKeys == list_length(node->indexorderbyops));
1012  Assert(numOrderByKeys == list_length(node->indexorderbyorig));
1013  indexstate->iss_SortSupport = (SortSupportData *)
1014  palloc0(numOrderByKeys * sizeof(SortSupportData));
1015  indexstate->iss_OrderByTypByVals = (bool *)
1016  palloc(numOrderByKeys * sizeof(bool));
1017  indexstate->iss_OrderByTypLens = (int16 *)
1018  palloc(numOrderByKeys * sizeof(int16));
1019  i = 0;
1020  forboth(lco, node->indexorderbyops, lcx, node->indexorderbyorig)
1021  {
1022  Oid orderbyop = lfirst_oid(lco);
1023  Node *orderbyexpr = (Node *) lfirst(lcx);
1024  Oid orderbyType = exprType(orderbyexpr);
1025  Oid orderbyColl = exprCollation(orderbyexpr);
1026  SortSupport orderbysort = &indexstate->iss_SortSupport[i];
1027 
1028  /* Initialize sort support */
1029  orderbysort->ssup_cxt = CurrentMemoryContext;
1030  orderbysort->ssup_collation = orderbyColl;
1031  /* See cmp_orderbyvals() comments on NULLS LAST */
1032  orderbysort->ssup_nulls_first = false;
1033  /* ssup_attno is unused here and elsewhere */
1034  orderbysort->ssup_attno = 0;
1035  /* No abbreviation */
1036  orderbysort->abbreviate = false;
1037  PrepareSortSupportFromOrderingOp(orderbyop, orderbysort);
1038 
1039  get_typlenbyval(orderbyType,
1040  &indexstate->iss_OrderByTypLens[i],
1041  &indexstate->iss_OrderByTypByVals[i]);
1042  i++;
1043  }
1044 
1045  /* allocate arrays to hold the re-calculated distances */
1046  indexstate->iss_OrderByValues = (Datum *)
1047  palloc(numOrderByKeys * sizeof(Datum));
1048  indexstate->iss_OrderByNulls = (bool *)
1049  palloc(numOrderByKeys * sizeof(bool));
1050 
1051  /* and initialize the reorder queue */
1053  indexstate);
1054  }
1055 
1056  /*
1057  * If we have runtime keys, we need an ExprContext to evaluate them. The
1058  * node's standard context won't do because we want to reset that context
1059  * for every tuple. So, build another context just like the other one...
1060  * -tgl 7/11/00
1061  */
1062  if (indexstate->iss_NumRuntimeKeys != 0)
1063  {
1064  ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
1065 
1066  ExecAssignExprContext(estate, &indexstate->ss.ps);
1067  indexstate->iss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
1068  indexstate->ss.ps.ps_ExprContext = stdecontext;
1069  }
1070  else
1071  {
1072  indexstate->iss_RuntimeContext = NULL;
1073  }
1074 
1075  /*
1076  * all done.
1077  */
1078  return indexstate;
1079 }
signed short int16
Definition: c.h:245
bool ssup_nulls_first
Definition: sortsupport.h:75
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
Index scanrelid
Definition: plannodes.h:329
#define RelationGetDescr(relation)
Definition: rel.h:428
IndexRuntimeKeyInfo * iss_RuntimeKeys
Definition: execnodes.h:1192
int16 * iss_OrderByTypLens
Definition: execnodes.h:1206
ExprContext * ps_ExprContext
Definition: execnodes.h:881
SortSupport iss_SortSupport
Definition: execnodes.h:1204
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:133
List * indexqualorig
Definition: plannodes.h:391
#define AccessShareLock
Definition: lockdefs.h:36
static TupleTableSlot * ExecIndexScan(PlanState *pstate)
Definition: nodes.h:510
pairingheap * pairingheap_allocate(pairingheap_comparator compare, void *arg)
Definition: pairingheap.c:42
Relation ss_currentRelation
Definition: execnodes.h:1102
EState * state
Definition: execnodes.h:849
unsigned int Oid
Definition: postgres_ext.h:31
ExprState * indexqualorig
Definition: execnodes.h:1186
ScanKey iss_ScanKeys
Definition: execnodes.h:1188
Oid indexid
Definition: plannodes.h:389
List * indexorderbyorig
Definition: execnodes.h:1187
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:445
bool * iss_OrderByNulls
Definition: execnodes.h:1203
PlanState ps
Definition: execnodes.h:1101
List * indexorderbyorig
Definition: plannodes.h:393
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:237
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:608
MemoryContext ssup_cxt
Definition: sortsupport.h:66
Scan scan
Definition: plannodes.h:388
#define NoLock
Definition: lockdefs.h:34
int iss_NumRuntimeKeys
Definition: execnodes.h:1193
bool * iss_OrderByTypByVals
Definition: execnodes.h:1205
static int reorderqueue_cmp(const pairingheap_node *a, const pairingheap_node *b, void *arg)
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:266
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
bool iss_RuntimeKeysReady
Definition: execnodes.h:1194
List * indexqual
Definition: plannodes.h:390
void * palloc0(Size size)
Definition: mcxt.c:877
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:853
uintptr_t Datum
Definition: postgres.h:372
AttrNumber ssup_attno
Definition: sortsupport.h:81
Plan * plan
Definition: execnodes.h:847
ScanState ss
Definition: execnodes.h:1185
#define makeNode(_type_)
Definition: nodes.h:558
List * indexorderby
Definition: plannodes.h:392
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:423
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
void ExecIndexBuildScanKeys(PlanState *planstate, Relation index, List *quals, bool isorderby, ScanKey *scanKeys, int *numScanKeys, IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys, IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2001
ExprContext * iss_RuntimeContext
Definition: execnodes.h:1195
List * indexorderbyops
Definition: plannodes.h:394
ExprState * qual
Definition: execnodes.h:865
HeapScanDesc ss_currentScanDesc
Definition: execnodes.h:1103
void * palloc(Size size)
Definition: mcxt.c:848
int i
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:545
Datum * iss_OrderByValues
Definition: execnodes.h:1202
int iss_NumOrderByKeys
Definition: execnodes.h:1191
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:58
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid)
Definition: execUtils.c:582
#define lfirst_oid(lc)
Definition: pg_list.h:108
Relation iss_RelationDesc
Definition: execnodes.h:1196
pairingheap * iss_ReorderQueue
Definition: execnodes.h:1200
ScanKey iss_OrderByKeys
Definition: execnodes.h:1190
void ExecReScanIndexScan ( IndexScanState node)

Definition at line 579 of file nodeIndexscan.c.

References ExecIndexEvalRuntimeKeys(), ExecScanReScan(), index_rescan(), IndexScanState::iss_NumOrderByKeys, IndexScanState::iss_NumRuntimeKeys, IndexScanState::iss_NumScanKeys, IndexScanState::iss_OrderByKeys, IndexScanState::iss_ReachedEnd, IndexScanState::iss_ReorderQueue, IndexScanState::iss_RuntimeContext, IndexScanState::iss_RuntimeKeys, IndexScanState::iss_RuntimeKeysReady, IndexScanState::iss_ScanDesc, IndexScanState::iss_ScanKeys, pairingheap_is_empty, reorderqueue_pop(), ResetExprContext, and IndexScanState::ss.

Referenced by ExecReScan().

580 {
581  /*
582  * If we are doing runtime key calculations (ie, any of the index key
583  * values weren't simple Consts), compute the new key values. But first,
584  * reset the context so we don't leak memory as each outer tuple is
585  * scanned. Note this assumes that we will recalculate *all* runtime keys
586  * on each call.
587  */
588  if (node->iss_NumRuntimeKeys != 0)
589  {
590  ExprContext *econtext = node->iss_RuntimeContext;
591 
592  ResetExprContext(econtext);
593  ExecIndexEvalRuntimeKeys(econtext,
594  node->iss_RuntimeKeys,
595  node->iss_NumRuntimeKeys);
596  }
597  node->iss_RuntimeKeysReady = true;
598 
599  /* flush the reorder queue */
600  if (node->iss_ReorderQueue)
601  {
602  while (!pairingheap_is_empty(node->iss_ReorderQueue))
603  reorderqueue_pop(node);
604  }
605 
606  /* reset index scan */
607  if (node->iss_ScanDesc)
609  node->iss_ScanKeys, node->iss_NumScanKeys,
610  node->iss_OrderByKeys, node->iss_NumOrderByKeys);
611  node->iss_ReachedEnd = false;
612 
613  ExecScanReScan(&node->ss);
614 }
IndexRuntimeKeyInfo * iss_RuntimeKeys
Definition: execnodes.h:1192
#define pairingheap_is_empty(h)
Definition: pairingheap.h:96
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:310
ScanKey iss_ScanKeys
Definition: execnodes.h:1188
bool iss_ReachedEnd
Definition: execnodes.h:1201
int iss_NumRuntimeKeys
Definition: execnodes.h:1193
bool iss_RuntimeKeysReady
Definition: execnodes.h:1194
ScanState ss
Definition: execnodes.h:1185
static HeapTuple reorderqueue_pop(IndexScanState *node)
ExprContext * iss_RuntimeContext
Definition: execnodes.h:1195
void ExecIndexEvalRuntimeKeys(ExprContext *econtext, IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
void ExecScanReScan(ScanState *node)
Definition: execScan.c:329
int iss_NumOrderByKeys
Definition: execnodes.h:1191
IndexScanDesc iss_ScanDesc
Definition: execnodes.h:1197
#define ResetExprContext(econtext)
Definition: executor.h:471
pairingheap * iss_ReorderQueue
Definition: execnodes.h:1200
ScanKey iss_OrderByKeys
Definition: execnodes.h:1190