00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "postgres.h"
00021
00022 #include "access/relscan.h"
00023 #include "access/transam.h"
00024 #include "catalog/index.h"
00025 #include "lib/stringinfo.h"
00026 #include "miscadmin.h"
00027 #include "storage/bufmgr.h"
00028 #include "utils/builtins.h"
00029 #include "utils/lsyscache.h"
00030 #include "utils/rel.h"
00031 #include "utils/tqual.h"
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
00067
00068
00069
00070
00071
00072 IndexScanDesc
00073 RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
00074 {
00075 IndexScanDesc scan;
00076
00077 scan = (IndexScanDesc) palloc(sizeof(IndexScanDescData));
00078
00079 scan->heapRelation = NULL;
00080 scan->indexRelation = indexRelation;
00081 scan->xs_snapshot = SnapshotNow;
00082 scan->numberOfKeys = nkeys;
00083 scan->numberOfOrderBys = norderbys;
00084
00085
00086
00087
00088 if (nkeys > 0)
00089 scan->keyData = (ScanKey) palloc(sizeof(ScanKeyData) * nkeys);
00090 else
00091 scan->keyData = NULL;
00092 if (norderbys > 0)
00093 scan->orderByData = (ScanKey) palloc(sizeof(ScanKeyData) * norderbys);
00094 else
00095 scan->orderByData = NULL;
00096
00097 scan->xs_want_itup = false;
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 scan->kill_prior_tuple = false;
00110 scan->xactStartedInRecovery = TransactionStartedDuringRecovery();
00111 scan->ignore_killed_tuples = !scan->xactStartedInRecovery;
00112
00113 scan->opaque = NULL;
00114
00115 scan->xs_itup = NULL;
00116 scan->xs_itupdesc = NULL;
00117
00118 ItemPointerSetInvalid(&scan->xs_ctup.t_self);
00119 scan->xs_ctup.t_data = NULL;
00120 scan->xs_cbuf = InvalidBuffer;
00121 scan->xs_continue_hot = false;
00122
00123 return scan;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 void
00139 IndexScanEnd(IndexScanDesc scan)
00140 {
00141 if (scan->keyData != NULL)
00142 pfree(scan->keyData);
00143 if (scan->orderByData != NULL)
00144 pfree(scan->orderByData);
00145
00146 pfree(scan);
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 char *
00161 BuildIndexValueDescription(Relation indexRelation,
00162 Datum *values, bool *isnull)
00163 {
00164 StringInfoData buf;
00165 int natts = indexRelation->rd_rel->relnatts;
00166 int i;
00167
00168 initStringInfo(&buf);
00169 appendStringInfo(&buf, "(%s)=(",
00170 pg_get_indexdef_columns(RelationGetRelid(indexRelation),
00171 true));
00172
00173 for (i = 0; i < natts; i++)
00174 {
00175 char *val;
00176
00177 if (isnull[i])
00178 val = "null";
00179 else
00180 {
00181 Oid foutoid;
00182 bool typisvarlena;
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 getTypeOutputInfo(indexRelation->rd_opcintype[i],
00195 &foutoid, &typisvarlena);
00196 val = OidOutputFunctionCall(foutoid, values[i]);
00197 }
00198
00199 if (i > 0)
00200 appendStringInfoString(&buf, ", ");
00201 appendStringInfoString(&buf, val);
00202 }
00203
00204 appendStringInfoChar(&buf, ')');
00205
00206 return buf.data;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 SysScanDesc
00248 systable_beginscan(Relation heapRelation,
00249 Oid indexId,
00250 bool indexOK,
00251 Snapshot snapshot,
00252 int nkeys, ScanKey key)
00253 {
00254 SysScanDesc sysscan;
00255 Relation irel;
00256
00257 if (indexOK &&
00258 !IgnoreSystemIndexes &&
00259 !ReindexIsProcessingIndex(indexId))
00260 irel = index_open(indexId, AccessShareLock);
00261 else
00262 irel = NULL;
00263
00264 sysscan = (SysScanDesc) palloc(sizeof(SysScanDescData));
00265
00266 sysscan->heap_rel = heapRelation;
00267 sysscan->irel = irel;
00268
00269 if (irel)
00270 {
00271 int i;
00272
00273
00274 for (i = 0; i < nkeys; i++)
00275 {
00276 int j;
00277
00278 for (j = 0; j < irel->rd_index->indnatts; j++)
00279 {
00280 if (key[i].sk_attno == irel->rd_index->indkey.values[j])
00281 {
00282 key[i].sk_attno = j + 1;
00283 break;
00284 }
00285 }
00286 if (j == irel->rd_index->indnatts)
00287 elog(ERROR, "column is not in index");
00288 }
00289
00290 sysscan->iscan = index_beginscan(heapRelation, irel,
00291 snapshot, nkeys, 0);
00292 index_rescan(sysscan->iscan, key, nkeys, NULL, 0);
00293 sysscan->scan = NULL;
00294 }
00295 else
00296 {
00297
00298
00299
00300
00301
00302
00303
00304 sysscan->scan = heap_beginscan_strat(heapRelation, snapshot,
00305 nkeys, key,
00306 true, false);
00307 sysscan->iscan = NULL;
00308 }
00309
00310 return sysscan;
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 HeapTuple
00323 systable_getnext(SysScanDesc sysscan)
00324 {
00325 HeapTuple htup;
00326
00327 if (sysscan->irel)
00328 {
00329 htup = index_getnext(sysscan->iscan, ForwardScanDirection);
00330
00331
00332
00333
00334
00335
00336
00337
00338 if (htup && sysscan->iscan->xs_recheck)
00339 elog(ERROR, "system catalog scans with lossy index conditions are not implemented");
00340 }
00341 else
00342 htup = heap_getnext(sysscan->scan, ForwardScanDirection);
00343
00344 return htup;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 bool
00357 systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
00358 {
00359 bool result;
00360
00361 if (sysscan->irel)
00362 {
00363 IndexScanDesc scan = sysscan->iscan;
00364
00365 Assert(tup == &scan->xs_ctup);
00366 Assert(BufferIsValid(scan->xs_cbuf));
00367
00368 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_SHARE);
00369 result = HeapTupleSatisfiesVisibility(tup, scan->xs_snapshot,
00370 scan->xs_cbuf);
00371 LockBuffer(scan->xs_cbuf, BUFFER_LOCK_UNLOCK);
00372 }
00373 else
00374 {
00375 HeapScanDesc scan = sysscan->scan;
00376
00377 Assert(tup == &scan->rs_ctup);
00378 Assert(BufferIsValid(scan->rs_cbuf));
00379
00380 LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
00381 result = HeapTupleSatisfiesVisibility(tup, scan->rs_snapshot,
00382 scan->rs_cbuf);
00383 LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
00384 }
00385 return result;
00386 }
00387
00388
00389
00390
00391
00392
00393 void
00394 systable_endscan(SysScanDesc sysscan)
00395 {
00396 if (sysscan->irel)
00397 {
00398 index_endscan(sysscan->iscan);
00399 index_close(sysscan->irel, AccessShareLock);
00400 }
00401 else
00402 heap_endscan(sysscan->scan);
00403
00404 pfree(sysscan);
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 SysScanDesc
00425 systable_beginscan_ordered(Relation heapRelation,
00426 Relation indexRelation,
00427 Snapshot snapshot,
00428 int nkeys, ScanKey key)
00429 {
00430 SysScanDesc sysscan;
00431 int i;
00432
00433
00434 if (ReindexIsProcessingIndex(RelationGetRelid(indexRelation)))
00435 elog(ERROR, "cannot do ordered scan on index \"%s\", because it is being reindexed",
00436 RelationGetRelationName(indexRelation));
00437
00438 if (IgnoreSystemIndexes)
00439 elog(WARNING, "using index \"%s\" despite IgnoreSystemIndexes",
00440 RelationGetRelationName(indexRelation));
00441
00442 sysscan = (SysScanDesc) palloc(sizeof(SysScanDescData));
00443
00444 sysscan->heap_rel = heapRelation;
00445 sysscan->irel = indexRelation;
00446
00447
00448 for (i = 0; i < nkeys; i++)
00449 {
00450 int j;
00451
00452 for (j = 0; j < indexRelation->rd_index->indnatts; j++)
00453 {
00454 if (key[i].sk_attno == indexRelation->rd_index->indkey.values[j])
00455 {
00456 key[i].sk_attno = j + 1;
00457 break;
00458 }
00459 }
00460 if (j == indexRelation->rd_index->indnatts)
00461 elog(ERROR, "column is not in index");
00462 }
00463
00464 sysscan->iscan = index_beginscan(heapRelation, indexRelation,
00465 snapshot, nkeys, 0);
00466 index_rescan(sysscan->iscan, key, nkeys, NULL, 0);
00467 sysscan->scan = NULL;
00468
00469 return sysscan;
00470 }
00471
00472
00473
00474
00475 HeapTuple
00476 systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
00477 {
00478 HeapTuple htup;
00479
00480 Assert(sysscan->irel);
00481 htup = index_getnext(sysscan->iscan, direction);
00482
00483 if (htup && sysscan->iscan->xs_recheck)
00484 elog(ERROR, "system catalog scans with lossy index conditions are not implemented");
00485
00486 return htup;
00487 }
00488
00489
00490
00491
00492 void
00493 systable_endscan_ordered(SysScanDesc sysscan)
00494 {
00495 Assert(sysscan->irel);
00496 index_endscan(sysscan->iscan);
00497 pfree(sysscan);
00498 }