1 /*-------------------------------------------------------------------------
4 * general index access method routines
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
14 * many of the old access method routines have been turned into
15 * macros and moved to genam.h -cim 4/30/91
17 *-------------------------------------------------------------------------
22 #include "access/relscan.h"
23 #include "access/transam.h"
24 #include "miscadmin.h"
26 #include "storage/bufmgr.h"
27 #include "utils/rel.h"
28 #include "utils/tqual.h"
31 /* ----------------------------------------------------------------
32 * general access method routines
34 * All indexed access methods use an identical scan structure.
35 * We don't know how the various AMs do locking, however, so we don't
36 * do anything about that here.
38 * The intent is that an AM implementor will define a beginscan routine
39 * that calls RelationGetIndexScan, to fill in the scan, and then does
40 * whatever kind of locking he wants.
42 * At the end of a scan, the AM's endscan routine undoes the locking,
43 * but does *not* call IndexScanEnd --- the higher-level index_endscan
44 * routine does that. (We can't do it in the AM because index_endscan
45 * still needs to touch the IndexScanDesc after calling the AM.)
47 * Because of this, the AM does not have a choice whether to call
48 * RelationGetIndexScan or not; its beginscan routine must return an
49 * object made by RelationGetIndexScan. This is kinda ugly but not
50 * worth cleaning up now.
51 * ----------------------------------------------------------------
55 * RelationGetIndexScan -- Create and fill an IndexScanDesc.
57 * This routine creates an index scan structure and sets its contents
58 * up correctly. This routine calls AMrescan to set up the scan with
62 * indexRelation -- index relation for scan.
63 * nkeys -- count of scan keys.
64 * key -- array of scan keys to restrict the index scan.
67 * An initialized IndexScanDesc.
71 RelationGetIndexScan(Relation indexRelation
,
72 int nkeys
, ScanKey key
)
76 scan
= (IndexScanDesc
) palloc(sizeof(IndexScanDescData
));
78 scan
->heapRelation
= NULL
; /* may be set later */
79 scan
->indexRelation
= indexRelation
;
80 scan
->xs_snapshot
= SnapshotNow
; /* may be set later */
81 scan
->numberOfKeys
= nkeys
;
84 * We allocate the key space here, but the AM is responsible for actually
85 * filling it from the passed key array.
88 scan
->keyData
= (ScanKey
) palloc(sizeof(ScanKeyData
) * nkeys
);
92 scan
->kill_prior_tuple
= false;
93 scan
->ignore_killed_tuples
= true; /* default setting */
97 ItemPointerSetInvalid(&scan
->xs_ctup
.t_self
);
98 scan
->xs_ctup
.t_data
= NULL
;
99 scan
->xs_cbuf
= InvalidBuffer
;
100 scan
->xs_hot_dead
= false;
101 scan
->xs_next_hot
= InvalidOffsetNumber
;
102 scan
->xs_prev_xmax
= InvalidTransactionId
;
105 * Let the AM fill in the key and any opaque data it wants.
107 index_rescan(scan
, key
);
113 * IndexScanEnd -- End an index scan.
115 * This routine just releases the storage acquired by
116 * RelationGetIndexScan(). Any AM-level resources are
117 * assumed to already have been released by the AM's
125 IndexScanEnd(IndexScanDesc scan
)
127 if (scan
->keyData
!= NULL
)
128 pfree(scan
->keyData
);
134 /* ----------------------------------------------------------------
135 * heap-or-index-scan access to system catalogs
137 * These functions support system catalog accesses that normally use
138 * an index but need to be capable of being switched to heap scans
139 * if the system indexes are unavailable.
141 * The specified scan keys must be compatible with the named index.
142 * Generally this means that they must constrain either all columns
143 * of the index, or the first K columns of an N-column index.
145 * These routines could work with non-system tables, actually,
146 * but they're only useful when there is a known index to use with
147 * the given scan keys; so in practice they're only good for
148 * predetermined types of scans of system catalogs.
149 * ----------------------------------------------------------------
153 * systable_beginscan --- set up for heap-or-index scan
155 * rel: catalog to scan, already opened and suitably locked
156 * indexId: OID of index to conditionally use
157 * indexOK: if false, forces a heap scan (see notes below)
158 * snapshot: time qual to use (usually should be SnapshotNow)
159 * nkeys, key: scan keys
161 * The attribute numbers in the scan key should be set for the heap case.
162 * If we choose to index, we reset them to 1..n to reference the index
163 * columns. Note this means there must be one scankey qualification per
164 * index column! This is checked by the Asserts in the normal, index-using
165 * case, but won't be checked if the heapscan path is taken.
167 * The routine checks the normal cases for whether an indexscan is safe,
168 * but caller can make additional checks and pass indexOK=false if needed.
169 * In standard case indexOK can simply be constant TRUE.
172 systable_beginscan(Relation heapRelation
,
176 int nkeys
, ScanKey key
)
182 !IgnoreSystemIndexes
&&
183 !ReindexIsProcessingIndex(indexId
))
184 irel
= index_open(indexId
, AccessShareLock
);
188 sysscan
= (SysScanDesc
) palloc(sizeof(SysScanDescData
));
190 sysscan
->heap_rel
= heapRelation
;
191 sysscan
->irel
= irel
;
197 /* Change attribute numbers to be index column numbers. */
198 for (i
= 0; i
< nkeys
; i
++)
202 for (j
= 0; j
< irel
->rd_index
->indnatts
; j
++)
204 if (key
[i
].sk_attno
== irel
->rd_index
->indkey
.values
[j
])
206 key
[i
].sk_attno
= j
+ 1;
210 if (j
== irel
->rd_index
->indnatts
)
211 elog(ERROR
, "column is not in index");
214 sysscan
->iscan
= index_beginscan(heapRelation
, irel
,
215 snapshot
, nkeys
, key
);
216 sysscan
->scan
= NULL
;
220 sysscan
->scan
= heap_beginscan(heapRelation
, snapshot
, nkeys
, key
);
221 sysscan
->iscan
= NULL
;
228 * systable_getnext --- get next tuple in a heap-or-index scan
230 * Returns NULL if no more tuples available.
232 * Note that returned tuple is a reference to data in a disk buffer;
233 * it must not be modified, and should be presumed inaccessible after
234 * next getnext() or endscan() call.
237 systable_getnext(SysScanDesc sysscan
)
243 htup
= index_getnext(sysscan
->iscan
, ForwardScanDirection
);
246 * We currently don't need to support lossy index operators for any
247 * system catalog scan. It could be done here, using the scan keys to
248 * drive the operator calls, if we arranged to save the heap attnums
249 * during systable_beginscan(); this is practical because we still
250 * wouldn't need to support indexes on expressions.
252 if (htup
&& sysscan
->iscan
->xs_recheck
)
253 elog(ERROR
, "system catalog scans with lossy index conditions are not implemented");
256 htup
= heap_getnext(sysscan
->scan
, ForwardScanDirection
);
262 * systable_recheck_tuple --- recheck visibility of most-recently-fetched tuple
264 * This is useful to test whether an object was deleted while we waited to
265 * acquire lock on it.
267 * Note: we don't actually *need* the tuple to be passed in, but it's a
268 * good crosscheck that the caller is interested in the right tuple.
271 systable_recheck_tuple(SysScanDesc sysscan
, HeapTuple tup
)
277 IndexScanDesc scan
= sysscan
->iscan
;
279 Assert(tup
== &scan
->xs_ctup
);
280 Assert(BufferIsValid(scan
->xs_cbuf
));
281 /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
282 LockBuffer(scan
->xs_cbuf
, BUFFER_LOCK_SHARE
);
283 result
= HeapTupleSatisfiesVisibility(tup
, scan
->xs_snapshot
,
285 LockBuffer(scan
->xs_cbuf
, BUFFER_LOCK_UNLOCK
);
289 HeapScanDesc scan
= sysscan
->scan
;
291 Assert(tup
== &scan
->rs_ctup
);
292 Assert(BufferIsValid(scan
->rs_cbuf
));
293 /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
294 LockBuffer(scan
->rs_cbuf
, BUFFER_LOCK_SHARE
);
295 result
= HeapTupleSatisfiesVisibility(tup
, scan
->rs_snapshot
,
297 LockBuffer(scan
->rs_cbuf
, BUFFER_LOCK_UNLOCK
);
303 * systable_endscan --- close scan, release resources
305 * Note that it's still up to the caller to close the heap relation.
308 systable_endscan(SysScanDesc sysscan
)
312 index_endscan(sysscan
->iscan
);
313 index_close(sysscan
->irel
, AccessShareLock
);
316 heap_endscan(sysscan
->scan
);
323 * systable_beginscan_ordered --- set up for ordered catalog scan
325 * These routines have essentially the same API as systable_beginscan etc,
326 * except that they guarantee to return multiple matching tuples in
327 * index order. Also, for largely historical reasons, the index to use
328 * is opened and locked by the caller, not here.
330 * Currently we do not support non-index-based scans here. (In principle
331 * we could do a heapscan and sort, but the uses are in places that
332 * probably don't need to still work with corrupted catalog indexes.)
333 * For the moment, therefore, these functions are merely the thinnest of
334 * wrappers around index_beginscan/index_getnext. The main reason for their
335 * existence is to centralize possible future support of lossy operators
339 systable_beginscan_ordered(Relation heapRelation
,
340 Relation indexRelation
,
342 int nkeys
, ScanKey key
)
347 /* REINDEX can probably be a hard error here ... */
348 if (ReindexIsProcessingIndex(RelationGetRelid(indexRelation
)))
349 elog(ERROR
, "cannot do ordered scan on index \"%s\", because it is the current REINDEX target",
350 RelationGetRelationName(indexRelation
));
351 /* ... but we only throw a warning about violating IgnoreSystemIndexes */
352 if (IgnoreSystemIndexes
)
353 elog(WARNING
, "using index \"%s\" despite IgnoreSystemIndexes",
354 RelationGetRelationName(indexRelation
));
356 sysscan
= (SysScanDesc
) palloc(sizeof(SysScanDescData
));
358 sysscan
->heap_rel
= heapRelation
;
359 sysscan
->irel
= indexRelation
;
361 /* Change attribute numbers to be index column numbers. */
362 for (i
= 0; i
< nkeys
; i
++)
366 for (j
= 0; j
< indexRelation
->rd_index
->indnatts
; j
++)
368 if (key
[i
].sk_attno
== indexRelation
->rd_index
->indkey
.values
[j
])
370 key
[i
].sk_attno
= j
+ 1;
374 if (j
== indexRelation
->rd_index
->indnatts
)
375 elog(ERROR
, "column is not in index");
378 sysscan
->iscan
= index_beginscan(heapRelation
, indexRelation
,
379 snapshot
, nkeys
, key
);
380 sysscan
->scan
= NULL
;
386 * systable_getnext_ordered --- get next tuple in an ordered catalog scan
389 systable_getnext_ordered(SysScanDesc sysscan
, ScanDirection direction
)
393 Assert(sysscan
->irel
);
394 htup
= index_getnext(sysscan
->iscan
, direction
);
395 /* See notes in systable_getnext */
396 if (htup
&& sysscan
->iscan
->xs_recheck
)
397 elog(ERROR
, "system catalog scans with lossy index conditions are not implemented");
403 * systable_endscan_ordered --- close scan, release resources
406 systable_endscan_ordered(SysScanDesc sysscan
)
408 Assert(sysscan
->irel
);
409 index_endscan(sysscan
->iscan
);