PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hashfuncs.c File Reference
#include "postgres.h"
#include "pageinspect.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "catalog/pg_am.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "utils/builtins.h"
Include dependency graph for hashfuncs.c:

Go to the source code of this file.

Data Structures

struct  HashPageStat
 
struct  user_args
 

Macros

#define IS_HASH(r)   ((r)->rd_rel->relam == HASH_AM_OID)
 

Typedefs

typedef struct HashPageStat HashPageStat
 

Functions

 PG_FUNCTION_INFO_V1 (hash_page_type)
 
 PG_FUNCTION_INFO_V1 (hash_page_stats)
 
 PG_FUNCTION_INFO_V1 (hash_page_items)
 
 PG_FUNCTION_INFO_V1 (hash_bitmap_info)
 
 PG_FUNCTION_INFO_V1 (hash_metapage_info)
 
static Page verify_hash_page (bytea *raw_page, int flags)
 
static void GetHashPageStatistics (Page page, HashPageStat *stat)
 
Datum hash_page_type (PG_FUNCTION_ARGS)
 
Datum hash_page_stats (PG_FUNCTION_ARGS)
 
Datum hash_page_items (PG_FUNCTION_ARGS)
 
Datum hash_bitmap_info (PG_FUNCTION_ARGS)
 
Datum hash_metapage_info (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define IS_HASH (   r)    ((r)->rd_rel->relam == HASH_AM_OID)

Definition at line 29 of file hashfuncs.c.

Referenced by hash_bitmap_info().

Typedef Documentation

Function Documentation

static void GetHashPageStatistics ( Page  page,
HashPageStat stat 
)
static

Definition at line 146 of file hashfuncs.c.

References HashPageStat::dead_items, FirstOffsetNumber, HashPageStat::free_size, HashPageStat::hasho_bucket, HashPageOpaqueData::hasho_bucket, HashPageStat::hasho_flag, HashPageOpaqueData::hasho_flag, HashPageStat::hasho_nextblkno, HashPageOpaqueData::hasho_nextblkno, HashPageStat::hasho_page_id, HashPageOpaqueData::hasho_page_id, HashPageStat::hasho_prevblkno, HashPageOpaqueData::hasho_prevblkno, ItemIdIsDead, HashPageStat::live_items, HashPageStat::page_size, PageGetFreeSpace(), PageGetItemId, PageGetMaxOffsetNumber, PageGetPageSize, and PageGetSpecialPointer.

Referenced by hash_page_stats().

147 {
150  int off;
151 
152  stat->dead_items = stat->live_items = 0;
153  stat->page_size = PageGetPageSize(page);
154 
155  /* hash page opaque data */
156  stat->hasho_prevblkno = opaque->hasho_prevblkno;
157  stat->hasho_nextblkno = opaque->hasho_nextblkno;
158  stat->hasho_bucket = opaque->hasho_bucket;
159  stat->hasho_flag = opaque->hasho_flag;
160  stat->hasho_page_id = opaque->hasho_page_id;
161 
162  /* count live and dead tuples, and free space */
163  for (off = FirstOffsetNumber; off <= maxoff; off++)
164  {
165  ItemId id = PageGetItemId(page, off);
166 
167  if (!ItemIdIsDead(id))
168  stat->live_items++;
169  else
170  stat->dead_items++;
171  }
173 }
uint16 hasho_page_id
Definition: hash.h:93
int free_size
Definition: hashfuncs.c:40
int page_size
Definition: hashfuncs.c:39
Bucket hasho_bucket
Definition: hashfuncs.c:45
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:582
int dead_items
Definition: hashfuncs.c:38
uint16 OffsetNumber
Definition: off.h:24
BlockNumber hasho_prevblkno
Definition: hash.h:89
BlockNumber hasho_prevblkno
Definition: hashfuncs.c:43
#define FirstOffsetNumber
Definition: off.h:27
#define PageGetPageSize(page)
Definition: bufpage.h:264
int live_items
Definition: hashfuncs.c:37
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
Page page
Definition: btreefuncs.c:245
uint16 hasho_page_id
Definition: hashfuncs.c:47
Bucket hasho_bucket
Definition: hash.h:91
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:96
uint16 hasho_flag
Definition: hash.h:92
BlockNumber hasho_nextblkno
Definition: hash.h:90
uint16 hasho_flag
Definition: hashfuncs.c:46
BlockNumber hasho_nextblkno
Definition: hashfuncs.c:44
Datum hash_bitmap_info ( PG_FUNCTION_ARGS  )

Definition at line 388 of file hashfuncs.c.

References _hash_getbuf(), _hash_ovflblkno_to_bitno(), _hash_relbuf(), AccessShareLock, bit(), BlessTupleDesc(), BMPG_MASK, BMPG_SHIFT, BoolGetDatum, BufferGetPage, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), HASH_METAPAGE, HASH_READ, HashMetaPageData::hashm_mapp, HashMetaPageData::hashm_nmaps, HashPageGetBitmap, HashPageGetMeta, heap_form_tuple(), HeapTupleGetDatum, i, index_close(), index_open(), Int32GetDatum, Int64GetDatum(), IS_HASH, ISSET, LH_BITMAP_PAGE, LH_META_PAGE, MemSet, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_DATUM, RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, superuser(), TYPEFUNC_COMPOSITE, UINT64_FORMAT, and values.

389 {
390  Oid indexRelid = PG_GETARG_OID(0);
391  uint64 ovflblkno = PG_GETARG_INT64(1);
392  HashMetaPage metap;
393  Buffer metabuf,
394  mapbuf;
395  BlockNumber bitmapblkno;
396  Page mappage;
397  bool bit = false;
399  Relation indexRel;
400  uint32 ovflbitno;
401  int32 bitmappage,
402  bitmapbit;
403  HeapTuple tuple;
404  int i,
405  j;
406  Datum values[3];
407  bool nulls[3];
408  uint32 *freep;
409 
410  if (!superuser())
411  ereport(ERROR,
412  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
413  (errmsg("must be superuser to use raw page functions"))));
414 
415  indexRel = index_open(indexRelid, AccessShareLock);
416 
417  if (!IS_HASH(indexRel))
418  elog(ERROR, "relation \"%s\" is not a hash index",
419  RelationGetRelationName(indexRel));
420 
421  if (RELATION_IS_OTHER_TEMP(indexRel))
422  ereport(ERROR,
423  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
424  errmsg("cannot access temporary tables of other sessions")));
425 
426  if (ovflblkno >= RelationGetNumberOfBlocks(indexRel))
427  ereport(ERROR,
428  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
429  errmsg("block number " UINT64_FORMAT " is out of range for relation \"%s\"",
430  ovflblkno, RelationGetRelationName(indexRel))));
431 
432  /* Read the metapage so we can determine which bitmap page to use */
433  metabuf = _hash_getbuf(indexRel, HASH_METAPAGE, HASH_READ, LH_META_PAGE);
434  metap = HashPageGetMeta(BufferGetPage(metabuf));
435 
436  /*
437  * Reject attempt to read the bit for a metapage or bitmap page; this is
438  * only meaningful for overflow pages.
439  */
440  if (ovflblkno == 0)
441  ereport(ERROR,
442  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
443  errmsg("invalid overflow block number %u",
444  (BlockNumber) ovflblkno)));
445  for (i = 0; i < metap->hashm_nmaps; i++)
446  if (metap->hashm_mapp[i] == ovflblkno)
447  ereport(ERROR,
448  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
449  errmsg("invalid overflow block number %u",
450  (BlockNumber) ovflblkno)));
451 
452  /*
453  * Identify overflow bit number. This will error out for primary bucket
454  * pages, and we've already rejected the metapage and bitmap pages above.
455  */
456  ovflbitno = _hash_ovflblkno_to_bitno(metap, (BlockNumber) ovflblkno);
457 
458  bitmappage = ovflbitno >> BMPG_SHIFT(metap);
459  bitmapbit = ovflbitno & BMPG_MASK(metap);
460 
461  if (bitmappage >= metap->hashm_nmaps)
462  ereport(ERROR,
463  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
464  errmsg("invalid overflow block number %u",
465  (BlockNumber) ovflblkno)));
466 
467  bitmapblkno = metap->hashm_mapp[bitmappage];
468 
469  _hash_relbuf(indexRel, metabuf);
470 
471  /* Check the status of bitmap bit for overflow page */
472  mapbuf = _hash_getbuf(indexRel, bitmapblkno, HASH_READ, LH_BITMAP_PAGE);
473  mappage = BufferGetPage(mapbuf);
474  freep = HashPageGetBitmap(mappage);
475 
476  bit = ISSET(freep, bitmapbit) != 0;
477 
478  _hash_relbuf(indexRel, mapbuf);
479  index_close(indexRel, AccessShareLock);
480 
481  /* Build a tuple descriptor for our result type */
482  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
483  elog(ERROR, "return type must be a row type");
484  tupleDesc = BlessTupleDesc(tupleDesc);
485 
486  MemSet(nulls, 0, sizeof(nulls));
487 
488  j = 0;
489  values[j++] = Int64GetDatum((int64) bitmapblkno);
490  values[j++] = Int32GetDatum(bitmapbit);
491  values[j++] = BoolGetDatum(bit);
492 
493  tuple = heap_form_tuple(tupleDesc, values, nulls);
494 
496 }
#define HashPageGetBitmap(page)
Definition: hash.h:269
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define LH_BITMAP_PAGE
Definition: hash.h:66
#define LH_META_PAGE
Definition: hash.h:67
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:846
uint32 BlockNumber
Definition: block.h:31
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:78
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:246
#define HASH_READ
Definition: hash.h:292
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
uint32 hashm_nmaps
Definition: hash.h:228
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define BMPG_MASK(metap)
Definition: hash.h:267
#define RelationGetRelationName(relation)
Definition: rel.h:436
unsigned int uint32
Definition: c.h:258
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
#define BMPG_SHIFT(metap)
Definition: hash.h:266
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
#define ISSET(A, N)
Definition: hash.h:287
#define HASH_METAPAGE
Definition: hash.h:169
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define IS_HASH(r)
Definition: hashfuncs.c:29
#define BoolGetDatum(X)
Definition: postgres.h:408
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:274
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:361
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:533
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
uint32 _hash_ovflblkno_to_bitno(HashMetaPage metap, BlockNumber ovflblkno)
Definition: hashovfl.c:61
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define Int32GetDatum(X)
Definition: postgres.h:485
#define HashPageGetMeta(page)
Definition: hash.h:276
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define elog
Definition: elog.h:219
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
BlockNumber hashm_mapp[HASH_MAX_BITMAPS]
Definition: hash.h:232
int Buffer
Definition: buf.h:23
#define UINT64_FORMAT
Definition: c.h:301
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
Pointer Page
Definition: bufpage.h:74
Datum hash_metapage_info ( PG_FUNCTION_ARGS  )

Definition at line 507 of file hashfuncs.c.

References BlessTupleDesc(), construct_array(), elog, ereport, errcode(), errmsg(), ERROR, Float8GetDatum(), get_call_result_type(), HASH_MAX_BITMAPS, HASH_MAX_SPLITPOINTS, HashMetaPageData::hashm_bmshift, HashMetaPageData::hashm_bmsize, HashMetaPageData::hashm_bsize, HashMetaPageData::hashm_ffactor, HashMetaPageData::hashm_firstfree, HashMetaPageData::hashm_highmask, HashMetaPageData::hashm_lowmask, HashMetaPageData::hashm_magic, HashMetaPageData::hashm_mapp, HashMetaPageData::hashm_maxbucket, HashMetaPageData::hashm_nmaps, HashMetaPageData::hashm_ntuples, HashMetaPageData::hashm_ovflpoint, HashMetaPageData::hashm_procid, HashMetaPageData::hashm_spares, HashMetaPageData::hashm_version, HashPageGetMeta, heap_form_tuple(), HeapTupleGetDatum, i, Int32GetDatum, Int64GetDatum(), INT8OID, LH_META_PAGE, MemSet, ObjectIdGetDatum, user_args::page, PG_GETARG_BYTEA_P, PG_RETURN_DATUM, PointerGetDatum, superuser(), TYPEFUNC_COMPOSITE, values, and verify_hash_page().

508 {
509  bytea *raw_page = PG_GETARG_BYTEA_P(0);
510  Page page;
511  HashMetaPageData *metad;
513  HeapTuple tuple;
514  int i,
515  j;
516  Datum values[16];
517  bool nulls[16];
518  Datum spares[HASH_MAX_SPLITPOINTS];
519  Datum mapp[HASH_MAX_BITMAPS];
520 
521  if (!superuser())
522  ereport(ERROR,
523  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
524  (errmsg("must be superuser to use raw page functions"))));
525 
526  page = verify_hash_page(raw_page, LH_META_PAGE);
527 
528  /* Build a tuple descriptor for our result type */
529  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
530  elog(ERROR, "return type must be a row type");
531  tupleDesc = BlessTupleDesc(tupleDesc);
532 
533  metad = HashPageGetMeta(page);
534 
535  MemSet(nulls, 0, sizeof(nulls));
536 
537  j = 0;
538  values[j++] = Int64GetDatum((int64) metad->hashm_magic);
539  values[j++] = Int64GetDatum((int64) metad->hashm_version);
540  values[j++] = Float8GetDatum(metad->hashm_ntuples);
541  values[j++] = Int32GetDatum((int32) metad->hashm_ffactor);
542  values[j++] = Int32GetDatum((int32) metad->hashm_bsize);
543  values[j++] = Int32GetDatum((int32) metad->hashm_bmsize);
544  values[j++] = Int32GetDatum((int32) metad->hashm_bmshift);
545  values[j++] = Int64GetDatum((int64) metad->hashm_maxbucket);
546  values[j++] = Int64GetDatum((int64) metad->hashm_highmask);
547  values[j++] = Int64GetDatum((int64) metad->hashm_lowmask);
548  values[j++] = Int64GetDatum((int64) metad->hashm_ovflpoint);
549  values[j++] = Int64GetDatum((int64) metad->hashm_firstfree);
550  values[j++] = Int64GetDatum((int64) metad->hashm_nmaps);
551  values[j++] = ObjectIdGetDatum((Oid) metad->hashm_procid);
552 
553  for (i = 0; i < HASH_MAX_SPLITPOINTS; i++)
554  spares[i] = Int64GetDatum((int64) metad->hashm_spares[i]);
555  values[j++] = PointerGetDatum(construct_array(spares,
556  HASH_MAX_SPLITPOINTS,
557  INT8OID,
558  8, FLOAT8PASSBYVAL, 'd'));
559 
560  for (i = 0; i < HASH_MAX_BITMAPS; i++)
561  mapp[i] = Int64GetDatum((int64) metad->hashm_mapp[i]);
562  values[j++] = PointerGetDatum(construct_array(mapp,
563  HASH_MAX_BITMAPS,
564  INT8OID,
565  8, FLOAT8PASSBYVAL, 'd'));
566 
567  tuple = heap_form_tuple(tupleDesc, values, nulls);
568 
570 }
uint16 hashm_bmshift
Definition: hash.h:221
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
RegProcedure hashm_procid
Definition: hash.h:229
#define LH_META_PAGE
Definition: hash.h:67
#define PointerGetDatum(X)
Definition: postgres.h:562
uint32 hashm_magic
Definition: hash.h:214
uint16 hashm_ffactor
Definition: hash.h:217
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3307
uint32 hashm_highmask
Definition: hash.h:223
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:846
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
unsigned int Oid
Definition: postgres_ext.h:31
uint32 hashm_lowmask
Definition: hash.h:224
signed int int32
Definition: c.h:246
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1815
#define HASH_MAX_SPLITPOINTS
Definition: hash.h:207
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
uint32 hashm_version
Definition: hash.h:215
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
uint32 hashm_nmaps
Definition: hash.h:228
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
#define ereport(elevel, rest)
Definition: elog.h:122
#define HASH_MAX_BITMAPS
Definition: hash.h:198
uint32 hashm_ovflpoint
Definition: hash.h:225
uint16 hashm_bsize
Definition: hash.h:218
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
double hashm_ntuples
Definition: hash.h:216
uint32 hashm_firstfree
Definition: hash.h:227
uint32 hashm_spares[HASH_MAX_SPLITPOINTS]
Definition: hash.h:230
Page page
Definition: btreefuncs.c:245
#define INT8OID
Definition: pg_type.h:304
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
uint32 hashm_maxbucket
Definition: hash.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define Int32GetDatum(X)
Definition: postgres.h:485
#define HashPageGetMeta(page)
Definition: hash.h:276
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
uint16 hashm_bmsize
Definition: hash.h:219
Definition: c.h:433
#define elog
Definition: elog.h:219
static Page verify_hash_page(bytea *raw_page, int flags)
Definition: hashfuncs.c:56
BlockNumber hashm_mapp[HASH_MAX_BITMAPS]
Definition: hash.h:232
Pointer Page
Definition: bufpage.h:74
Datum hash_page_items ( PG_FUNCTION_ARGS  )

Definition at line 292 of file hashfuncs.c.

References _hash_get_indextuple_hashkey(), FuncCallContext::attinmeta, BlessTupleDesc(), FuncCallContext::call_cntr, elog, ereport, errcode(), errmsg(), ERROR, FirstOffsetNumber, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum, Int32GetDatum, Int64GetDatum(), ItemIdIsValid, LH_BUCKET_PAGE, LH_OVERFLOW_PAGE, FuncCallContext::max_calls, MemoryContextSwitchTo(), MemSet, FuncCallContext::multi_call_memory_ctx, user_args::offset, user_args::page, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, palloc(), pfree(), PG_GETARG_BYTEA_P, PointerGetDatum, result, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, superuser(), IndexTupleData::t_tid, AttInMetadata::tupdesc, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, values, and verify_hash_page().

293 {
294  bytea *raw_page = PG_GETARG_BYTEA_P(0);
295  Page page;
296  Datum result;
297  Datum values[3];
298  bool nulls[3];
299  uint32 hashkey;
300  HeapTuple tuple;
301  FuncCallContext *fctx;
302  MemoryContext mctx;
303  struct user_args *uargs;
304 
305  if (!superuser())
306  ereport(ERROR,
307  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
308  (errmsg("must be superuser to use raw page functions"))));
309 
310  if (SRF_IS_FIRSTCALL())
311  {
313 
314  fctx = SRF_FIRSTCALL_INIT();
315 
317 
319 
320  uargs = palloc(sizeof(struct user_args));
321 
322  uargs->page = page;
323 
324  uargs->offset = FirstOffsetNumber;
325 
326  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
327 
328  /* Build a tuple descriptor for our result type */
329  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
330  elog(ERROR, "return type must be a row type");
331  tupleDesc = BlessTupleDesc(tupleDesc);
332 
333  fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
334 
335  fctx->user_fctx = uargs;
336 
337  MemoryContextSwitchTo(mctx);
338  }
339 
340  fctx = SRF_PERCALL_SETUP();
341  uargs = fctx->user_fctx;
342 
343  if (fctx->call_cntr < fctx->max_calls)
344  {
345  ItemId id;
346  IndexTuple itup;
347  int j;
348 
349  id = PageGetItemId(uargs->page, uargs->offset);
350 
351  if (!ItemIdIsValid(id))
352  elog(ERROR, "invalid ItemId");
353 
354  itup = (IndexTuple) PageGetItem(uargs->page, id);
355 
356  MemSet(nulls, 0, sizeof(nulls));
357 
358  j = 0;
359  values[j++] = Int32GetDatum((int32) uargs->offset);
360  values[j++] = PointerGetDatum(&itup->t_tid);
361 
362  hashkey = _hash_get_indextuple_hashkey(itup);
363  values[j] = Int64GetDatum((int64) hashkey);
364 
365  tuple = heap_form_tuple(fctx->attinmeta->tupdesc, values, nulls);
366  result = HeapTupleGetDatum(tuple);
367 
368  uargs->offset = uargs->offset + 1;
369 
370  SRF_RETURN_NEXT(fctx, result);
371  }
372  else
373  {
374  pfree(uargs);
375  SRF_RETURN_DONE(fctx);
376  }
377 }
uint64 call_cntr
Definition: funcapi.h:65
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:562
ItemPointerData t_tid
Definition: itup.h:37
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:846
return result
Definition: formatting.c:1633
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
signed int int32
Definition: c.h:246
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:246
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:299
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
#define FirstOffsetNumber
Definition: off.h:27
AttInMetadata * attinmeta
Definition: funcapi.h:99
IndexTupleData * IndexTuple
Definition: itup.h:53
TupleDesc tupdesc
Definition: funcapi.h:38
unsigned int uint32
Definition: c.h:258
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
#define ereport(elevel, rest)
Definition: elog.h:122
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
uintptr_t Datum
Definition: postgres.h:372
#define LH_OVERFLOW_PAGE
Definition: hash.h:64
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
Page page
Definition: btreefuncs.c:245
#define ItemIdIsValid(itemId)
Definition: itemid.h:85
#define LH_BUCKET_PAGE
Definition: hash.h:65
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define Int32GetDatum(X)
Definition: postgres.h:485
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
#define elog
Definition: elog.h:219
static Page verify_hash_page(bytea *raw_page, int flags)
Definition: hashfuncs.c:56
uint64 max_calls
Definition: funcapi.h:74
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum hash_page_stats ( PG_FUNCTION_ARGS  )

Definition at line 227 of file hashfuncs.c.

References BlessTupleDesc(), HashPageStat::dead_items, elog, ereport, errcode(), errmsg(), ERROR, HashPageStat::free_size, get_call_result_type(), GetHashPageStatistics(), HashPageStat::hasho_bucket, HashPageStat::hasho_flag, HashPageStat::hasho_nextblkno, HashPageStat::hasho_page_id, HashPageStat::hasho_prevblkno, heap_form_tuple(), HeapTupleGetDatum, Int32GetDatum, Int64GetDatum(), InvalidBlockNumber, LH_BUCKET_PAGE, LH_OVERFLOW_PAGE, HashPageStat::live_items, MemSet, HashPageStat::page_size, PG_GETARG_BYTEA_P, PG_RETURN_DATUM, superuser(), TYPEFUNC_COMPOSITE, values, and verify_hash_page().

228 {
229  bytea *raw_page = PG_GETARG_BYTEA_P(0);
230  Page page;
231  int j;
232  Datum values[9];
233  bool nulls[9];
234  HashPageStat stat;
235  HeapTuple tuple;
237 
238  if (!superuser())
239  ereport(ERROR,
240  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
241  (errmsg("must be superuser to use raw page functions"))));
242 
244 
245  /* keep compiler quiet */
247  stat.hasho_flag = stat.hasho_page_id = stat.free_size = 0;
248 
249  GetHashPageStatistics(page, &stat);
250 
251  /* Build a tuple descriptor for our result type */
252  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
253  elog(ERROR, "return type must be a row type");
254  tupleDesc = BlessTupleDesc(tupleDesc);
255 
256  MemSet(nulls, 0, sizeof(nulls));
257 
258  j = 0;
259  values[j++] = Int32GetDatum(stat.live_items);
260  values[j++] = Int32GetDatum(stat.dead_items);
261  values[j++] = Int32GetDatum(stat.page_size);
262  values[j++] = Int32GetDatum(stat.free_size);
263  values[j++] = Int64GetDatum((int64) stat.hasho_prevblkno);
264  values[j++] = Int64GetDatum((int64) stat.hasho_nextblkno);
265  values[j++] = Int64GetDatum((int64) stat.hasho_bucket);
266  values[j++] = Int32GetDatum((int32) stat.hasho_flag);
267  values[j++] = Int32GetDatum((int32) stat.hasho_page_id);
268 
269  tuple = heap_form_tuple(tupleDesc, values, nulls);
270 
272 }
static void GetHashPageStatistics(Page page, HashPageStat *stat)
Definition: hashfuncs.c:146
int free_size
Definition: hashfuncs.c:40
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
int page_size
Definition: hashfuncs.c:39
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:846
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
Bucket hasho_bucket
Definition: hashfuncs.c:45
int dead_items
Definition: hashfuncs.c:38
signed int int32
Definition: c.h:246
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
BlockNumber hasho_prevblkno
Definition: hashfuncs.c:43
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
#define ereport(elevel, rest)
Definition: elog.h:122
int live_items
Definition: hashfuncs.c:37
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define LH_OVERFLOW_PAGE
Definition: hash.h:64
Page page
Definition: btreefuncs.c:245
uint16 hasho_page_id
Definition: hashfuncs.c:47
#define LH_BUCKET_PAGE
Definition: hash.h:65
#define InvalidBlockNumber
Definition: block.h:33
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
uint16 hasho_flag
Definition: hashfuncs.c:46
#define elog
Definition: elog.h:219
static Page verify_hash_page(bytea *raw_page, int flags)
Definition: hashfuncs.c:56
BlockNumber hasho_nextblkno
Definition: hashfuncs.c:44
Pointer Page
Definition: bufpage.h:74
Datum hash_page_type ( PG_FUNCTION_ARGS  )

Definition at line 182 of file hashfuncs.c.

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, HashPageOpaqueData::hasho_flag, LH_BITMAP_PAGE, LH_BUCKET_PAGE, LH_META_PAGE, LH_OVERFLOW_PAGE, LH_PAGE_TYPE, PageGetSpecialPointer, PageIsNew, PG_GETARG_BYTEA_P, PG_RETURN_TEXT_P, superuser(), and verify_hash_page().

183 {
184  bytea *raw_page = PG_GETARG_BYTEA_P(0);
185  Page page;
186  HashPageOpaque opaque;
187  int pagetype;
188  const char *type;
189 
190  if (!superuser())
191  ereport(ERROR,
192  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
193  (errmsg("must be superuser to use raw page functions"))));
194 
195  page = verify_hash_page(raw_page, 0);
196 
197  if (PageIsNew(page))
198  type = "unused";
199  else
200  {
201  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
202 
203  /* page type (flags) */
204  pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
205  if (pagetype == LH_META_PAGE)
206  type = "metapage";
207  else if (pagetype == LH_OVERFLOW_PAGE)
208  type = "overflow";
209  else if (pagetype == LH_BUCKET_PAGE)
210  type = "bucket";
211  else if (pagetype == LH_BITMAP_PAGE)
212  type = "bitmap";
213  else
214  type = "unused";
215  }
216 
218 }
#define LH_BITMAP_PAGE
Definition: hash.h:66
#define LH_META_PAGE
Definition: hash.h:67
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define LH_PAGE_TYPE
Definition: hash.h:73
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define LH_OVERFLOW_PAGE
Definition: hash.h:64
Page page
Definition: btreefuncs.c:245
#define LH_BUCKET_PAGE
Definition: hash.h:65
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:96
uint16 hasho_flag
Definition: hash.h:92
#define PageIsNew(page)
Definition: bufpage.h:225
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
static Page verify_hash_page(bytea *raw_page, int flags)
Definition: hashfuncs.c:56
Pointer Page
Definition: bufpage.h:74
PG_FUNCTION_INFO_V1 ( hash_page_type  )
PG_FUNCTION_INFO_V1 ( hash_page_stats  )
PG_FUNCTION_INFO_V1 ( hash_page_items  )
PG_FUNCTION_INFO_V1 ( hash_bitmap_info  )
PG_FUNCTION_INFO_V1 ( hash_metapage_info  )
static Page verify_hash_page ( bytea raw_page,
int  flags 
)
static

Definition at line 56 of file hashfuncs.c.

References elog, ereport, errcode(), errdetail(), errmsg(), ERROR, get_page_from_raw(), HASH_MAGIC, HASH_VERSION, HashMetaPageData::hashm_magic, HashMetaPageData::hashm_version, HashPageOpaqueData::hasho_flag, HashPageOpaqueData::hasho_page_id, HASHO_PAGE_ID, HashPageGetMeta, LH_BITMAP_PAGE, LH_BUCKET_PAGE, LH_META_PAGE, LH_OVERFLOW_PAGE, LH_PAGE_TYPE, LH_UNUSED_PAGE, MAXALIGN, PageGetSpecialPointer, PageGetSpecialSize, and PageIsNew.

Referenced by hash_metapage_info(), hash_page_items(), hash_page_stats(), and hash_page_type().

57 {
58  Page page = get_page_from_raw(raw_page);
59  int pagetype = LH_UNUSED_PAGE;
60 
61  /* Treat new pages as unused. */
62  if (!PageIsNew(page))
63  {
64  HashPageOpaque pageopaque;
65 
66  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(HashPageOpaqueData)))
67  ereport(ERROR,
68  (errcode(ERRCODE_INDEX_CORRUPTED),
69  errmsg("index table contains corrupted page")));
70 
71  pageopaque = (HashPageOpaque) PageGetSpecialPointer(page);
72  if (pageopaque->hasho_page_id != HASHO_PAGE_ID)
73  ereport(ERROR,
74  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
75  errmsg("page is not a hash page"),
76  errdetail("Expected %08x, got %08x.",
77  HASHO_PAGE_ID, pageopaque->hasho_page_id)));
78 
79  pagetype = pageopaque->hasho_flag & LH_PAGE_TYPE;
80  }
81 
82  /* Check that page type is sane. */
83  if (pagetype != LH_OVERFLOW_PAGE && pagetype != LH_BUCKET_PAGE &&
84  pagetype != LH_BITMAP_PAGE && pagetype != LH_META_PAGE &&
85  pagetype != LH_UNUSED_PAGE)
86  ereport(ERROR,
87  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
88  errmsg("invalid hash page type %08x", pagetype)));
89 
90  /* If requested, verify page type. */
91  if (flags != 0 && (pagetype & flags) == 0)
92  {
93  switch (flags)
94  {
95  case LH_META_PAGE:
96  ereport(ERROR,
97  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
98  errmsg("page is not a hash meta page")));
100  ereport(ERROR,
101  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
102  errmsg("page is not a hash bucket or overflow page")));
103  case LH_OVERFLOW_PAGE:
104  ereport(ERROR,
105  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
106  errmsg("page is not a hash overflow page")));
107  default:
108  elog(ERROR,
109  "hash page of type %08x not in mask %08x",
110  pagetype, flags);
111  }
112  }
113 
114  /*
115  * If it is the metapage, also verify magic number and version.
116  */
117  if (pagetype == LH_META_PAGE)
118  {
119  HashMetaPage metap = HashPageGetMeta(page);
120 
121  if (metap->hashm_magic != HASH_MAGIC)
122  ereport(ERROR,
123  (errcode(ERRCODE_INDEX_CORRUPTED),
124  errmsg("invalid magic number for metadata"),
125  errdetail("Expected 0x%08x, got 0x%08x.",
126  HASH_MAGIC, metap->hashm_magic)));
127 
128  if (metap->hashm_version != HASH_VERSION)
129  ereport(ERROR,
130  (errcode(ERRCODE_INDEX_CORRUPTED),
131  errmsg("invalid version for metadata"),
132  errdetail("Expected %d, got %d",
133  HASH_VERSION, metap->hashm_version)));
134  }
135 
136  return page;
137 }
uint16 hasho_page_id
Definition: hash.h:93
#define LH_BITMAP_PAGE
Definition: hash.h:66
#define LH_META_PAGE
Definition: hash.h:67
uint32 hashm_magic
Definition: hash.h:214
int errcode(int sqlerrcode)
Definition: elog.c:575
#define HASH_VERSION
Definition: hash.h:172
#define LH_UNUSED_PAGE
Definition: hash.h:63
#define HASH_MAGIC
Definition: hash.h:171
#define LH_PAGE_TYPE
Definition: hash.h:73
#define ERROR
Definition: elog.h:43
uint32 hashm_version
Definition: hash.h:215
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
#define LH_OVERFLOW_PAGE
Definition: hash.h:64
Page page
Definition: btreefuncs.c:245
#define LH_BUCKET_PAGE
Definition: hash.h:65
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:183
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:96
#define HASHO_PAGE_ID
Definition: hash.h:109
#define MAXALIGN(LEN)
Definition: c.h:576
#define PageGetSpecialSize(page)
Definition: bufpage.h:296
uint16 hasho_flag
Definition: hash.h:92
#define PageIsNew(page)
Definition: bufpage.h:225
#define HashPageGetMeta(page)
Definition: hash.h:276
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Pointer Page
Definition: bufpage.h:74