00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #include "postgres.h"
00067
00068 #include "access/relscan.h"
00069 #include "access/transam.h"
00070 #include "catalog/index.h"
00071 #include "pgstat.h"
00072 #include "storage/bufmgr.h"
00073 #include "storage/lmgr.h"
00074 #include "storage/predicate.h"
00075 #include "utils/snapmgr.h"
00076 #include "utils/tqual.h"
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 #define RELATION_CHECKS \
00092 ( \
00093 AssertMacro(RelationIsValid(indexRelation)), \
00094 AssertMacro(PointerIsValid(indexRelation->rd_am)), \
00095 AssertMacro(!ReindexIsProcessingIndex(RelationGetRelid(indexRelation))) \
00096 )
00097
00098 #define SCAN_CHECKS \
00099 ( \
00100 AssertMacro(IndexScanIsValid(scan)), \
00101 AssertMacro(RelationIsValid(scan->indexRelation)), \
00102 AssertMacro(PointerIsValid(scan->indexRelation->rd_am)) \
00103 )
00104
00105 #define GET_REL_PROCEDURE(pname) \
00106 do { \
00107 procedure = &indexRelation->rd_aminfo->pname; \
00108 if (!OidIsValid(procedure->fn_oid)) \
00109 { \
00110 RegProcedure procOid = indexRelation->rd_am->pname; \
00111 if (!RegProcedureIsValid(procOid)) \
00112 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
00113 fmgr_info_cxt(procOid, procedure, indexRelation->rd_indexcxt); \
00114 } \
00115 } while(0)
00116
00117 #define GET_UNCACHED_REL_PROCEDURE(pname) \
00118 do { \
00119 if (!RegProcedureIsValid(indexRelation->rd_am->pname)) \
00120 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
00121 fmgr_info(indexRelation->rd_am->pname, &procedure); \
00122 } while(0)
00123
00124 #define GET_SCAN_PROCEDURE(pname) \
00125 do { \
00126 procedure = &scan->indexRelation->rd_aminfo->pname; \
00127 if (!OidIsValid(procedure->fn_oid)) \
00128 { \
00129 RegProcedure procOid = scan->indexRelation->rd_am->pname; \
00130 if (!RegProcedureIsValid(procOid)) \
00131 elog(ERROR, "invalid %s regproc", CppAsString(pname)); \
00132 fmgr_info_cxt(procOid, procedure, scan->indexRelation->rd_indexcxt); \
00133 } \
00134 } while(0)
00135
00136 static IndexScanDesc index_beginscan_internal(Relation indexRelation,
00137 int nkeys, int norderbys, Snapshot snapshot);
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 Relation
00160 index_open(Oid relationId, LOCKMODE lockmode)
00161 {
00162 Relation r;
00163
00164 r = relation_open(relationId, lockmode);
00165
00166 if (r->rd_rel->relkind != RELKIND_INDEX)
00167 ereport(ERROR,
00168 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00169 errmsg("\"%s\" is not an index",
00170 RelationGetRelationName(r))));
00171
00172 return r;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 void
00185 index_close(Relation relation, LOCKMODE lockmode)
00186 {
00187 LockRelId relid = relation->rd_lockInfo.lockRelId;
00188
00189 Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
00190
00191
00192 RelationClose(relation);
00193
00194 if (lockmode != NoLock)
00195 UnlockRelationId(&relid, lockmode);
00196 }
00197
00198
00199
00200
00201
00202 bool
00203 index_insert(Relation indexRelation,
00204 Datum *values,
00205 bool *isnull,
00206 ItemPointer heap_t_ctid,
00207 Relation heapRelation,
00208 IndexUniqueCheck checkUnique)
00209 {
00210 FmgrInfo *procedure;
00211
00212 RELATION_CHECKS;
00213 GET_REL_PROCEDURE(aminsert);
00214
00215 if (!(indexRelation->rd_am->ampredlocks))
00216 CheckForSerializableConflictIn(indexRelation,
00217 (HeapTuple) NULL,
00218 InvalidBuffer);
00219
00220
00221
00222
00223 return DatumGetBool(FunctionCall6(procedure,
00224 PointerGetDatum(indexRelation),
00225 PointerGetDatum(values),
00226 PointerGetDatum(isnull),
00227 PointerGetDatum(heap_t_ctid),
00228 PointerGetDatum(heapRelation),
00229 Int32GetDatum((int32) checkUnique)));
00230 }
00231
00232
00233
00234
00235
00236
00237 IndexScanDesc
00238 index_beginscan(Relation heapRelation,
00239 Relation indexRelation,
00240 Snapshot snapshot,
00241 int nkeys, int norderbys)
00242 {
00243 IndexScanDesc scan;
00244
00245 scan = index_beginscan_internal(indexRelation, nkeys, norderbys, snapshot);
00246
00247
00248
00249
00250
00251 scan->heapRelation = heapRelation;
00252 scan->xs_snapshot = snapshot;
00253
00254 return scan;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263 IndexScanDesc
00264 index_beginscan_bitmap(Relation indexRelation,
00265 Snapshot snapshot,
00266 int nkeys)
00267 {
00268 IndexScanDesc scan;
00269
00270 scan = index_beginscan_internal(indexRelation, nkeys, 0, snapshot);
00271
00272
00273
00274
00275
00276 scan->xs_snapshot = snapshot;
00277
00278 return scan;
00279 }
00280
00281
00282
00283
00284 static IndexScanDesc
00285 index_beginscan_internal(Relation indexRelation,
00286 int nkeys, int norderbys, Snapshot snapshot)
00287 {
00288 IndexScanDesc scan;
00289 FmgrInfo *procedure;
00290
00291 RELATION_CHECKS;
00292 GET_REL_PROCEDURE(ambeginscan);
00293
00294 if (!(indexRelation->rd_am->ampredlocks))
00295 PredicateLockRelation(indexRelation, snapshot);
00296
00297
00298
00299
00300 RelationIncrementReferenceCount(indexRelation);
00301
00302
00303
00304
00305 scan = (IndexScanDesc)
00306 DatumGetPointer(FunctionCall3(procedure,
00307 PointerGetDatum(indexRelation),
00308 Int32GetDatum(nkeys),
00309 Int32GetDatum(norderbys)));
00310
00311 return scan;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 void
00327 index_rescan(IndexScanDesc scan,
00328 ScanKey keys, int nkeys,
00329 ScanKey orderbys, int norderbys)
00330 {
00331 FmgrInfo *procedure;
00332
00333 SCAN_CHECKS;
00334 GET_SCAN_PROCEDURE(amrescan);
00335
00336 Assert(nkeys == scan->numberOfKeys);
00337 Assert(norderbys == scan->numberOfOrderBys);
00338
00339
00340 if (BufferIsValid(scan->xs_cbuf))
00341 {
00342 ReleaseBuffer(scan->xs_cbuf);
00343 scan->xs_cbuf = InvalidBuffer;
00344 }
00345
00346 scan->xs_continue_hot = false;
00347
00348 scan->kill_prior_tuple = false;
00349
00350 FunctionCall5(procedure,
00351 PointerGetDatum(scan),
00352 PointerGetDatum(keys),
00353 Int32GetDatum(nkeys),
00354 PointerGetDatum(orderbys),
00355 Int32GetDatum(norderbys));
00356 }
00357
00358
00359
00360
00361
00362 void
00363 index_endscan(IndexScanDesc scan)
00364 {
00365 FmgrInfo *procedure;
00366
00367 SCAN_CHECKS;
00368 GET_SCAN_PROCEDURE(amendscan);
00369
00370
00371 if (BufferIsValid(scan->xs_cbuf))
00372 {
00373 ReleaseBuffer(scan->xs_cbuf);
00374 scan->xs_cbuf = InvalidBuffer;
00375 }
00376
00377
00378 FunctionCall1(procedure, PointerGetDatum(scan));
00379
00380
00381 RelationDecrementReferenceCount(scan->indexRelation);
00382
00383
00384 IndexScanEnd(scan);
00385 }
00386
00387
00388
00389
00390
00391 void
00392 index_markpos(IndexScanDesc scan)
00393 {
00394 FmgrInfo *procedure;
00395
00396 SCAN_CHECKS;
00397 GET_SCAN_PROCEDURE(ammarkpos);
00398
00399 FunctionCall1(procedure, PointerGetDatum(scan));
00400 }
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 void
00419 index_restrpos(IndexScanDesc scan)
00420 {
00421 FmgrInfo *procedure;
00422
00423 Assert(IsMVCCSnapshot(scan->xs_snapshot));
00424
00425 SCAN_CHECKS;
00426 GET_SCAN_PROCEDURE(amrestrpos);
00427
00428 scan->xs_continue_hot = false;
00429
00430 scan->kill_prior_tuple = false;
00431
00432 FunctionCall1(procedure, PointerGetDatum(scan));
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442 ItemPointer
00443 index_getnext_tid(IndexScanDesc scan, ScanDirection direction)
00444 {
00445 FmgrInfo *procedure;
00446 bool found;
00447
00448 SCAN_CHECKS;
00449 GET_SCAN_PROCEDURE(amgettuple);
00450
00451 Assert(TransactionIdIsValid(RecentGlobalXmin));
00452
00453
00454
00455
00456
00457
00458
00459 found = DatumGetBool(FunctionCall2(procedure,
00460 PointerGetDatum(scan),
00461 Int32GetDatum(direction)));
00462
00463
00464 scan->kill_prior_tuple = false;
00465
00466
00467 if (!found)
00468 {
00469
00470 if (BufferIsValid(scan->xs_cbuf))
00471 {
00472 ReleaseBuffer(scan->xs_cbuf);
00473 scan->xs_cbuf = InvalidBuffer;
00474 }
00475 return NULL;
00476 }
00477
00478 pgstat_count_index_tuples(scan->indexRelation, 1);
00479
00480
00481 return &scan->xs_ctup.t_self;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 HeapTuple
00503 index_fetch_heap(IndexScanDesc scan)
00504 {
00505 ItemPointer tid = &scan->xs_ctup.t_self;
00506 bool all_dead = false;
00507 bool got_heap_tuple;
00508
00509
00510 if (!scan->xs_continue_hot)
00511 {
00512
00513 Buffer prev_buf = scan->xs_cbuf;
00514
00515 scan->xs_cbuf = ReleaseAndReadBuffer(scan->xs_cbuf,
00516 scan->heapRelation,
00517 ItemPointerGetBlockNumber(tid));
00518
00519
00520
00521
00522 if (prev_buf != scan->xs_cbuf)
00523 heap_page_prune_opt(scan->heapRelation, scan->xs_cbuf,
00524 RecentGlobalXmin);
00525 }
00526
00527
00528 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
00529 got_heap_tuple = heap_hot_search_buffer(tid, scan->heapRelation,
00530 scan->xs_cbuf,
00531 scan->xs_snapshot,
00532 &scan->xs_ctup,
00533 &all_dead,
00534 !scan->xs_continue_hot);
00535 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
00536
00537 if (got_heap_tuple)
00538 {
00539
00540
00541
00542
00543 scan->xs_continue_hot = !IsMVCCSnapshot(scan->xs_snapshot);
00544 pgstat_count_heap_fetch(scan->indexRelation);
00545 return &scan->xs_ctup;
00546 }
00547
00548
00549 scan->xs_continue_hot = false;
00550
00551
00552
00553
00554
00555
00556
00557
00558 if (!scan->xactStartedInRecovery)
00559 scan->kill_prior_tuple = all_dead;
00560
00561 return NULL;
00562 }
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579 HeapTuple
00580 index_getnext(IndexScanDesc scan, ScanDirection direction)
00581 {
00582 HeapTuple heapTuple;
00583 ItemPointer tid;
00584
00585 for (;;)
00586 {
00587 if (scan->xs_continue_hot)
00588 {
00589
00590
00591
00592
00593 Assert(BufferIsValid(scan->xs_cbuf));
00594 Assert(ItemPointerGetBlockNumber(&scan->xs_ctup.t_self) ==
00595 BufferGetBlockNumber(scan->xs_cbuf));
00596 }
00597 else
00598 {
00599
00600 tid = index_getnext_tid(scan, direction);
00601
00602
00603 if (tid == NULL)
00604 break;
00605 }
00606
00607
00608
00609
00610
00611
00612 heapTuple = index_fetch_heap(scan);
00613 if (heapTuple != NULL)
00614 return heapTuple;
00615 }
00616
00617 return NULL;
00618 }
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633 int64
00634 index_getbitmap(IndexScanDesc scan, TIDBitmap *bitmap)
00635 {
00636 FmgrInfo *procedure;
00637 int64 ntids;
00638 Datum d;
00639
00640 SCAN_CHECKS;
00641 GET_SCAN_PROCEDURE(amgetbitmap);
00642
00643
00644 scan->kill_prior_tuple = false;
00645
00646
00647
00648
00649 d = FunctionCall2(procedure,
00650 PointerGetDatum(scan),
00651 PointerGetDatum(bitmap));
00652
00653 ntids = DatumGetInt64(d);
00654
00655
00656 #ifndef USE_FLOAT8_BYVAL
00657 pfree(DatumGetPointer(d));
00658 #endif
00659
00660 pgstat_count_index_tuples(scan->indexRelation, ntids);
00661
00662 return ntids;
00663 }
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674 IndexBulkDeleteResult *
00675 index_bulk_delete(IndexVacuumInfo *info,
00676 IndexBulkDeleteResult *stats,
00677 IndexBulkDeleteCallback callback,
00678 void *callback_state)
00679 {
00680 Relation indexRelation = info->index;
00681 FmgrInfo procedure;
00682 IndexBulkDeleteResult *result;
00683
00684 RELATION_CHECKS;
00685 GET_UNCACHED_REL_PROCEDURE(ambulkdelete);
00686
00687 result = (IndexBulkDeleteResult *)
00688 DatumGetPointer(FunctionCall4(&procedure,
00689 PointerGetDatum(info),
00690 PointerGetDatum(stats),
00691 PointerGetDatum((Pointer) callback),
00692 PointerGetDatum(callback_state)));
00693
00694 return result;
00695 }
00696
00697
00698
00699
00700
00701
00702
00703 IndexBulkDeleteResult *
00704 index_vacuum_cleanup(IndexVacuumInfo *info,
00705 IndexBulkDeleteResult *stats)
00706 {
00707 Relation indexRelation = info->index;
00708 FmgrInfo procedure;
00709 IndexBulkDeleteResult *result;
00710
00711 RELATION_CHECKS;
00712 GET_UNCACHED_REL_PROCEDURE(amvacuumcleanup);
00713
00714 result = (IndexBulkDeleteResult *)
00715 DatumGetPointer(FunctionCall2(&procedure,
00716 PointerGetDatum(info),
00717 PointerGetDatum(stats)));
00718
00719 return result;
00720 }
00721
00722
00723
00724
00725
00726 bool
00727 index_can_return(Relation indexRelation)
00728 {
00729 FmgrInfo *procedure;
00730
00731 RELATION_CHECKS;
00732
00733
00734 if (!RegProcedureIsValid(indexRelation->rd_am->amcanreturn))
00735 return false;
00736
00737 GET_REL_PROCEDURE(amcanreturn);
00738
00739 return DatumGetBool(FunctionCall1(procedure,
00740 PointerGetDatum(indexRelation)));
00741 }
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769 RegProcedure
00770 index_getprocid(Relation irel,
00771 AttrNumber attnum,
00772 uint16 procnum)
00773 {
00774 RegProcedure *loc;
00775 int nproc;
00776 int procindex;
00777
00778 nproc = irel->rd_am->amsupport;
00779
00780 Assert(procnum > 0 && procnum <= (uint16) nproc);
00781
00782 procindex = (nproc * (attnum - 1)) + (procnum - 1);
00783
00784 loc = irel->rd_support;
00785
00786 Assert(loc != NULL);
00787
00788 return loc[procindex];
00789 }
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803 FmgrInfo *
00804 index_getprocinfo(Relation irel,
00805 AttrNumber attnum,
00806 uint16 procnum)
00807 {
00808 FmgrInfo *locinfo;
00809 int nproc;
00810 int procindex;
00811
00812 nproc = irel->rd_am->amsupport;
00813
00814 Assert(procnum > 0 && procnum <= (uint16) nproc);
00815
00816 procindex = (nproc * (attnum - 1)) + (procnum - 1);
00817
00818 locinfo = irel->rd_supportinfo;
00819
00820 Assert(locinfo != NULL);
00821
00822 locinfo += procindex;
00823
00824
00825 if (locinfo->fn_oid == InvalidOid)
00826 {
00827 RegProcedure *loc = irel->rd_support;
00828 RegProcedure procId;
00829
00830 Assert(loc != NULL);
00831
00832 procId = loc[procindex];
00833
00834
00835
00836
00837
00838
00839
00840 if (!RegProcedureIsValid(procId))
00841 elog(ERROR, "missing support function %d for attribute %d of index \"%s\"",
00842 procnum, attnum, RelationGetRelationName(irel));
00843
00844 fmgr_info_cxt(procId, locinfo, irel->rd_indexcxt);
00845 }
00846
00847 return locinfo;
00848 }