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 144 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().

145 {
148  int off;
149 
150  stat->dead_items = stat->live_items = 0;
151  stat->page_size = PageGetPageSize(page);
152 
153  /* hash page opaque data */
154  stat->hasho_prevblkno = opaque->hasho_prevblkno;
155  stat->hasho_nextblkno = opaque->hasho_nextblkno;
156  stat->hasho_bucket = opaque->hasho_bucket;
157  stat->hasho_flag = opaque->hasho_flag;
158  stat->hasho_page_id = opaque->hasho_page_id;
159 
160  /* count live and dead tuples, and free space */
161  for (off = FirstOffsetNumber; off <= maxoff; off++)
162  {
163  ItemId id = PageGetItemId(page, off);
164 
165  if (!ItemIdIsDead(id))
166  stat->live_items++;
167  else
168  stat->dead_items++;
169  }
171 }
uint16 hasho_page_id
Definition: hash.h:82
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:354
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:78
BlockNumber hasho_prevblkno
Definition: hashfuncs.c:43
#define FirstOffsetNumber
Definition: off.h:27
#define PageGetPageSize(page)
Definition: bufpage.h:265
int live_items
Definition: hashfuncs.c:37
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
Page page
Definition: btreefuncs.c:252
uint16 hasho_page_id
Definition: hashfuncs.c:47
Bucket hasho_bucket
Definition: hash.h:80
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:85
uint16 hasho_flag
Definition: hash.h:81
BlockNumber hasho_nextblkno
Definition: hash.h:79
uint16 hasho_flag
Definition: hashfuncs.c:46
BlockNumber hasho_nextblkno
Definition: hashfuncs.c:44
Datum hash_bitmap_info ( PG_FUNCTION_ARGS  )

Definition at line 378 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, NULL, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_DATUM, RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, superuser(), TYPEFUNC_COMPOSITE, UINT64_FORMAT, and values.

379 {
380  Oid indexRelid = PG_GETARG_OID(0);
381  uint64 ovflblkno = PG_GETARG_INT64(1);
382  HashMetaPage metap;
383  Buffer metabuf,
384  mapbuf;
385  BlockNumber bitmapblkno;
386  Page mappage;
387  bool bit = false;
389  Relation indexRel;
390  uint32 ovflbitno;
391  int32 bitmappage,
392  bitmapbit;
393  HeapTuple tuple;
394  int i,
395  j;
396  Datum values[3];
397  bool nulls[3];
398  uint32 *freep;
399 
400  if (!superuser())
401  ereport(ERROR,
402  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
403  (errmsg("must be superuser to use raw page functions"))));
404 
405  indexRel = index_open(indexRelid, AccessShareLock);
406 
407  if (!IS_HASH(indexRel))
408  elog(ERROR, "relation \"%s\" is not a hash index",
409  RelationGetRelationName(indexRel));
410 
411  if (RELATION_IS_OTHER_TEMP(indexRel))
412  ereport(ERROR,
413  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
414  errmsg("cannot access temporary tables of other sessions")));
415 
416  if (ovflblkno >= RelationGetNumberOfBlocks(indexRel))
417  ereport(ERROR,
418  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
419  errmsg("block number " UINT64_FORMAT " is out of range for relation \"%s\"",
420  ovflblkno, RelationGetRelationName(indexRel))));
421 
422  /* Read the metapage so we can determine which bitmap page to use */
423  metabuf = _hash_getbuf(indexRel, HASH_METAPAGE, HASH_READ, LH_META_PAGE);
424  metap = HashPageGetMeta(BufferGetPage(metabuf));
425 
426  /*
427  * Reject attempt to read the bit for a metapage or bitmap page; this is
428  * only meaningful for overflow pages.
429  */
430  if (ovflblkno == 0)
431  ereport(ERROR,
432  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
433  errmsg("invalid overflow block number %u",
434  (BlockNumber) ovflblkno)));
435  for (i = 0; i < metap->hashm_nmaps; i++)
436  if (metap->hashm_mapp[i] == ovflblkno)
437  ereport(ERROR,
438  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
439  errmsg("invalid overflow block number %u",
440  (BlockNumber) ovflblkno)));
441 
442  /*
443  * Identify overflow bit number. This will error out for primary bucket
444  * pages, and we've already rejected the metapage and bitmap pages above.
445  */
446  ovflbitno = _hash_ovflblkno_to_bitno(metap, (BlockNumber) ovflblkno);
447 
448  bitmappage = ovflbitno >> BMPG_SHIFT(metap);
449  bitmapbit = ovflbitno & BMPG_MASK(metap);
450 
451  if (bitmappage >= metap->hashm_nmaps)
452  ereport(ERROR,
453  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
454  errmsg("invalid overflow block number %u",
455  (BlockNumber) ovflblkno)));
456 
457  bitmapblkno = metap->hashm_mapp[bitmappage];
458 
459  _hash_relbuf(indexRel, metabuf);
460 
461  /* Check the status of bitmap bit for overflow page */
462  mapbuf = _hash_getbuf(indexRel, bitmapblkno, HASH_READ, LH_BITMAP_PAGE);
463  mappage = BufferGetPage(mapbuf);
464  freep = HashPageGetBitmap(mappage);
465 
466  bit = ISSET(freep, bitmapbit) != 0;
467 
468  _hash_relbuf(indexRel, mapbuf);
469  index_close(indexRel, AccessShareLock);
470 
471  /* Build a tuple descriptor for our result type */
472  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
473  elog(ERROR, "return type must be a row type");
474  tupleDesc = BlessTupleDesc(tupleDesc);
475 
476  MemSet(nulls, 0, sizeof(nulls));
477 
478  j = 0;
479  values[j++] = Int64GetDatum((int64) bitmapblkno);
480  values[j++] = Int32GetDatum(bitmapbit);
481  values[j++] = BoolGetDatum(bit);
482 
483  tuple = heap_form_tuple(tupleDesc, values, nulls);
484 
486 }
#define HashPageGetBitmap(page)
Definition: hash.h:243
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define LH_BITMAP_PAGE
Definition: hash.h:55
#define LH_META_PAGE
Definition: hash.h:56
#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:857
uint32 BlockNumber
Definition: block.h:31
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
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:256
#define HASH_READ
Definition: hash.h:266
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
uint32 hashm_nmaps
Definition: hash.h:202
#define PG_GETARG_OID(n)
Definition: fmgr.h:232
#define BMPG_MASK(metap)
Definition: hash.h:241
#define RelationGetRelationName(relation)
Definition: rel.h:433
unsigned int uint32
Definition: c.h:268
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2152
#define BMPG_SHIFT(metap)
Definition: hash.h:240
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
#define ISSET(A, N)
Definition: hash.h:261
#define HASH_METAPAGE
Definition: hash.h:158
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:305
#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
#define NULL
Definition: c.h:229
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:361
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:530
#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:162
#define Int32GetDatum(X)
Definition: postgres.h:485
#define HashPageGetMeta(page)
Definition: hash.h:250
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:239
BlockNumber hashm_mapp[HASH_MAX_BITMAPS]
Definition: hash.h:206
int Buffer
Definition: buf.h:23
#define UINT64_FORMAT
Definition: c.h:316
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 497 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, NULL, ObjectIdGetDatum, user_args::page, PG_GETARG_BYTEA_P, PG_RETURN_DATUM, PointerGetDatum, superuser(), TYPEFUNC_COMPOSITE, values, and verify_hash_page().

498 {
499  bytea *raw_page = PG_GETARG_BYTEA_P(0);
500  Page page;
501  HashMetaPageData *metad;
503  HeapTuple tuple;
504  int i,
505  j;
506  Datum values[16];
507  bool nulls[16];
508  Datum spares[HASH_MAX_SPLITPOINTS];
509  Datum mapp[HASH_MAX_BITMAPS];
510 
511  if (!superuser())
512  ereport(ERROR,
513  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
514  (errmsg("must be superuser to use raw page functions"))));
515 
516  page = verify_hash_page(raw_page, LH_META_PAGE);
517 
518  /* Build a tuple descriptor for our result type */
519  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
520  elog(ERROR, "return type must be a row type");
521  tupleDesc = BlessTupleDesc(tupleDesc);
522 
523  metad = HashPageGetMeta(page);
524 
525  MemSet(nulls, 0, sizeof(nulls));
526 
527  j = 0;
528  values[j++] = Int64GetDatum((int64) metad->hashm_magic);
529  values[j++] = Int64GetDatum((int64) metad->hashm_version);
530  values[j++] = Float8GetDatum(metad->hashm_ntuples);
531  values[j++] = Int32GetDatum((int32) metad->hashm_ffactor);
532  values[j++] = Int32GetDatum((int32) metad->hashm_bsize);
533  values[j++] = Int32GetDatum((int32) metad->hashm_bmsize);
534  values[j++] = Int32GetDatum((int32) metad->hashm_bmshift);
535  values[j++] = Int64GetDatum((int64) metad->hashm_maxbucket);
536  values[j++] = Int64GetDatum((int64) metad->hashm_highmask);
537  values[j++] = Int64GetDatum((int64) metad->hashm_lowmask);
538  values[j++] = Int64GetDatum((int64) metad->hashm_ovflpoint);
539  values[j++] = Int64GetDatum((int64) metad->hashm_firstfree);
540  values[j++] = Int64GetDatum((int64) metad->hashm_nmaps);
541  values[j++] = ObjectIdGetDatum((Oid) metad->hashm_procid);
542 
543  for (i = 0; i < HASH_MAX_SPLITPOINTS; i++)
544  spares[i] = Int64GetDatum((int64) metad->hashm_spares[i]);
545  values[j++] = PointerGetDatum(construct_array(spares,
546  HASH_MAX_SPLITPOINTS,
547  INT8OID,
548  8, FLOAT8PASSBYVAL, 'd'));
549 
550  for (i = 0; i < HASH_MAX_BITMAPS; i++)
551  mapp[i] = Int64GetDatum((int64) metad->hashm_mapp[i]);
552  values[j++] = PointerGetDatum(construct_array(mapp,
553  HASH_MAX_BITMAPS,
554  INT8OID,
555  8, FLOAT8PASSBYVAL, 'd'));
556 
557  tuple = heap_form_tuple(tupleDesc, values, nulls);
558 
560 }
uint16 hashm_bmshift
Definition: hash.h:195
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
RegProcedure hashm_procid
Definition: hash.h:203
#define LH_META_PAGE
Definition: hash.h:56
#define PointerGetDatum(X)
Definition: postgres.h:562
uint32 hashm_magic
Definition: hash.h:188
uint16 hashm_ffactor
Definition: hash.h:191
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
uint32 hashm_highmask
Definition: hash.h:197
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:857
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
unsigned int Oid
Definition: postgres_ext.h:31
uint32 hashm_lowmask
Definition: hash.h:198
signed int int32
Definition: c.h:256
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:2176
#define HASH_MAX_SPLITPOINTS
Definition: hash.h:183
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:291
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
uint32 hashm_version
Definition: hash.h:189
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
uint32 hashm_nmaps
Definition: hash.h:202
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2152
#define ereport(elevel, rest)
Definition: elog.h:122
#define HASH_MAX_BITMAPS
Definition: hash.h:184
uint32 hashm_ovflpoint
Definition: hash.h:199
uint16 hashm_bsize
Definition: hash.h:192
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:305
double hashm_ntuples
Definition: hash.h:190
uint32 hashm_firstfree
Definition: hash.h:201
uint32 hashm_spares[HASH_MAX_SPLITPOINTS]
Definition: hash.h:204
Page page
Definition: btreefuncs.c:252
#define INT8OID
Definition: pg_type.h:304
#define NULL
Definition: c.h:229
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
uint32 hashm_maxbucket
Definition: hash.h:196
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define Int32GetDatum(X)
Definition: postgres.h:485
#define HashPageGetMeta(page)
Definition: hash.h:250
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
uint16 hashm_bmsize
Definition: hash.h:193
Definition: c.h:439
#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:206
Pointer Page
Definition: bufpage.h:74
Datum hash_page_items ( PG_FUNCTION_ARGS  )

Definition at line 282 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, NULL, user_args::offset, user_args::page, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, palloc(), pfree(), PG_GETARG_BYTEA_P, PointerGetDatum, 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().

283 {
284  bytea *raw_page = PG_GETARG_BYTEA_P(0);
285  Page page;
286  Datum result;
287  Datum values[3];
288  bool nulls[3];
289  uint32 hashkey;
290  HeapTuple tuple;
291  FuncCallContext *fctx;
292  MemoryContext mctx;
293  struct user_args *uargs;
294 
295  if (!superuser())
296  ereport(ERROR,
297  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
298  (errmsg("must be superuser to use raw page functions"))));
299 
300  if (SRF_IS_FIRSTCALL())
301  {
303 
304  fctx = SRF_FIRSTCALL_INIT();
305 
307 
309 
310  uargs = palloc(sizeof(struct user_args));
311 
312  uargs->page = page;
313 
314  uargs->offset = FirstOffsetNumber;
315 
316  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
317 
318  /* Build a tuple descriptor for our result type */
319  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
320  elog(ERROR, "return type must be a row type");
321  tupleDesc = BlessTupleDesc(tupleDesc);
322 
323  fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
324 
325  fctx->user_fctx = uargs;
326 
327  MemoryContextSwitchTo(mctx);
328  }
329 
330  fctx = SRF_PERCALL_SETUP();
331  uargs = fctx->user_fctx;
332 
333  if (fctx->call_cntr < fctx->max_calls)
334  {
335  ItemId id;
336  IndexTuple itup;
337  int j;
338 
339  id = PageGetItemId(uargs->page, uargs->offset);
340 
341  if (!ItemIdIsValid(id))
342  elog(ERROR, "invalid ItemId");
343 
344  itup = (IndexTuple) PageGetItem(uargs->page, id);
345 
346  MemSet(nulls, 0, sizeof(nulls));
347 
348  j = 0;
349  values[j++] = Int32GetDatum((int32) uargs->offset);
350  values[j++] = PointerGetDatum(&itup->t_tid);
351 
352  hashkey = _hash_get_indextuple_hashkey(itup);
353  values[j] = Int64GetDatum((int64) hashkey);
354 
355  tuple = heap_form_tuple(fctx->attinmeta->tupdesc, values, nulls);
356  result = HeapTupleGetDatum(tuple);
357 
358  uargs->offset = uargs->offset + 1;
359 
360  SRF_RETURN_NEXT(fctx, result);
361  }
362  else
363  {
364  pfree(uargs);
365  SRF_RETURN_DONE(fctx);
366  }
367 }
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:857
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
signed int int32
Definition: c.h:256
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:291
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:253
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:233
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
#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:268
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2152
#define ereport(elevel, rest)
Definition: elog.h:122
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
uintptr_t Datum
Definition: postgres.h:372
#define LH_OVERFLOW_PAGE
Definition: hash.h:53
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
Page page
Definition: btreefuncs.c:252
#define ItemIdIsValid(itemId)
Definition: itemid.h:85
#define LH_BUCKET_PAGE
Definition: hash.h:54
#define NULL
Definition: c.h:229
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define Int32GetDatum(X)
Definition: postgres.h:485
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#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:337
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 217 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, NULL, HashPageStat::page_size, PG_GETARG_BYTEA_P, PG_RETURN_DATUM, superuser(), TYPEFUNC_COMPOSITE, values, and verify_hash_page().

218 {
219  bytea *raw_page = PG_GETARG_BYTEA_P(0);
220  Page page;
221  int j;
222  Datum values[9];
223  bool nulls[9];
224  HashPageStat stat;
225  HeapTuple tuple;
227 
228  if (!superuser())
229  ereport(ERROR,
230  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
231  (errmsg("must be superuser to use raw page functions"))));
232 
234 
235  /* keep compiler quiet */
237  stat.hasho_flag = stat.hasho_page_id = stat.free_size = 0;
238 
239  GetHashPageStatistics(page, &stat);
240 
241  /* Build a tuple descriptor for our result type */
242  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
243  elog(ERROR, "return type must be a row type");
244  tupleDesc = BlessTupleDesc(tupleDesc);
245 
246  MemSet(nulls, 0, sizeof(nulls));
247 
248  j = 0;
249  values[j++] = Int32GetDatum(stat.live_items);
250  values[j++] = Int32GetDatum(stat.dead_items);
251  values[j++] = Int32GetDatum(stat.page_size);
252  values[j++] = Int32GetDatum(stat.free_size);
253  values[j++] = Int64GetDatum((int64) stat.hasho_prevblkno);
254  values[j++] = Int64GetDatum((int64) stat.hasho_nextblkno);
255  values[j++] = Int64GetDatum((int64) stat.hasho_bucket);
256  values[j++] = Int32GetDatum((int32) stat.hasho_flag);
257  values[j++] = Int32GetDatum((int32) stat.hasho_page_id);
258 
259  tuple = heap_form_tuple(tupleDesc, values, nulls);
260 
262 }
static void GetHashPageStatistics(Page page, HashPageStat *stat)
Definition: hashfuncs.c:144
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:857
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
Bucket hasho_bucket
Definition: hashfuncs.c:45
int dead_items
Definition: hashfuncs.c:38
signed int int32
Definition: c.h:256
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:291
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
BlockNumber hasho_prevblkno
Definition: hashfuncs.c:43
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2152
#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:305
#define LH_OVERFLOW_PAGE
Definition: hash.h:53
Page page
Definition: btreefuncs.c:252
uint16 hasho_page_id
Definition: hashfuncs.c:47
#define LH_BUCKET_PAGE
Definition: hash.h:54
#define NULL
Definition: c.h:229
#define InvalidBlockNumber
Definition: block.h:33
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
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 180 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, PageGetSpecialPointer, PG_GETARG_BYTEA_P, PG_RETURN_TEXT_P, superuser(), and verify_hash_page().

181 {
182  bytea *raw_page = PG_GETARG_BYTEA_P(0);
183  Page page;
184  HashPageOpaque opaque;
185  char *type;
186 
187  if (!superuser())
188  ereport(ERROR,
189  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
190  (errmsg("must be superuser to use raw page functions"))));
191 
192  page = verify_hash_page(raw_page, 0);
193  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
194 
195  /* page type (flags) */
196  if (opaque->hasho_flag & LH_META_PAGE)
197  type = "metapage";
198  else if (opaque->hasho_flag & LH_OVERFLOW_PAGE)
199  type = "overflow";
200  else if (opaque->hasho_flag & LH_BUCKET_PAGE)
201  type = "bucket";
202  else if (opaque->hasho_flag & LH_BITMAP_PAGE)
203  type = "bitmap";
204  else
205  type = "unused";
206 
208 }
#define LH_BITMAP_PAGE
Definition: hash.h:55
#define LH_META_PAGE
Definition: hash.h:56
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:291
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define LH_OVERFLOW_PAGE
Definition: hash.h:53
Page page
Definition: btreefuncs.c:252
#define LH_BUCKET_PAGE
Definition: hash.h:54
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:322
text * cstring_to_text(const char *s)
Definition: varlena.c:151
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:85
uint16 hasho_flag
Definition: hash.h:81
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
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, 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;
60  HashPageOpaque pageopaque;
61 
62  if (PageIsNew(page))
63  ereport(ERROR,
64  (errcode(ERRCODE_INDEX_CORRUPTED),
65  errmsg("index table contains zero page")));
66 
67  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(HashPageOpaqueData)))
68  ereport(ERROR,
69  (errcode(ERRCODE_INDEX_CORRUPTED),
70  errmsg("index table contains corrupted page")));
71 
72  pageopaque = (HashPageOpaque) PageGetSpecialPointer(page);
73  if (pageopaque->hasho_page_id != HASHO_PAGE_ID)
74  ereport(ERROR,
75  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
76  errmsg("page is not a hash page"),
77  errdetail("Expected %08x, got %08x.",
78  HASHO_PAGE_ID, pageopaque->hasho_page_id)));
79 
80  /* Check that page type is sane. */
81  pagetype = pageopaque->hasho_flag & LH_PAGE_TYPE;
82  if (pagetype != LH_OVERFLOW_PAGE && pagetype != LH_BUCKET_PAGE &&
83  pagetype != LH_BITMAP_PAGE && pagetype != LH_META_PAGE)
84  ereport(ERROR,
85  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
86  errmsg("invalid hash page type %08x", pagetype)));
87 
88  /* If requested, verify page type. */
89  if (flags != 0 && (pagetype & flags) == 0)
90  {
91  switch (flags)
92  {
93  case LH_META_PAGE:
94  ereport(ERROR,
95  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
96  errmsg("page is not a hash meta page")));
98  ereport(ERROR,
99  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
100  errmsg("page is not a hash bucket or overflow page")));
101  case LH_OVERFLOW_PAGE:
102  ereport(ERROR,
103  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
104  errmsg("page is not a hash overflow page")));
105  default:
106  elog(ERROR,
107  "hash page of type %08x not in mask %08x",
108  pagetype, flags);
109  }
110  }
111 
112  /*
113  * If it is the metapage, also verify magic number and version.
114  */
115  if (pagetype == LH_META_PAGE)
116  {
117  HashMetaPage metap = HashPageGetMeta(page);
118 
119  if (metap->hashm_magic != HASH_MAGIC)
120  ereport(ERROR,
121  (errcode(ERRCODE_INDEX_CORRUPTED),
122  errmsg("invalid magic number for metadata"),
123  errdetail("Expected 0x%08x, got 0x%08x.",
124  HASH_MAGIC, metap->hashm_magic)));
125 
126  if (metap->hashm_version != HASH_VERSION)
127  ereport(ERROR,
128  (errcode(ERRCODE_INDEX_CORRUPTED),
129  errmsg("invalid version for metadata"),
130  errdetail("Expected %d, got %d",
131  HASH_VERSION, metap->hashm_version)));
132  }
133 
134  return page;
135 }
uint16 hasho_page_id
Definition: hash.h:82
#define LH_BITMAP_PAGE
Definition: hash.h:55
#define LH_META_PAGE
Definition: hash.h:56
uint32 hashm_magic
Definition: hash.h:188
int errcode(int sqlerrcode)
Definition: elog.c:575
#define HASH_VERSION
Definition: hash.h:161
#define HASH_MAGIC
Definition: hash.h:160
#define LH_PAGE_TYPE
Definition: hash.h:62
#define ERROR
Definition: elog.h:43
uint32 hashm_version
Definition: hash.h:189
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
#define LH_OVERFLOW_PAGE
Definition: hash.h:53
Page page
Definition: btreefuncs.c:252
#define LH_BUCKET_PAGE
Definition: hash.h:54
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:183
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:85
#define HASHO_PAGE_ID
Definition: hash.h:98
#define MAXALIGN(LEN)
Definition: c.h:588
#define PageGetSpecialSize(page)
Definition: bufpage.h:297
uint16 hasho_flag
Definition: hash.h:81
#define PageIsNew(page)
Definition: bufpage.h:226
#define HashPageGetMeta(page)
Definition: hash.h:250
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Pointer Page
Definition: bufpage.h:74