PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_prewarm.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_prewarm.c
4  * prewarming utilities
5  *
6  * Copyright (c) 2010-2017, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * contrib/pg_prewarm/pg_prewarm.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include <sys/stat.h>
16 #include <unistd.h>
17 
18 #include "access/heapam.h"
19 #include "catalog/catalog.h"
20 #include "fmgr.h"
21 #include "miscadmin.h"
22 #include "storage/bufmgr.h"
23 #include "storage/smgr.h"
24 #include "utils/acl.h"
25 #include "utils/builtins.h"
26 #include "utils/lsyscache.h"
27 #include "utils/rel.h"
28 
30 
32 
33 typedef enum
34 {
38 } PrewarmType;
39 
40 static char blockbuffer[BLCKSZ];
41 
42 /*
43  * pg_prewarm(regclass, mode text, fork text,
44  * first_block int8, last_block int8)
45  *
46  * The first argument is the relation to be prewarmed; the second controls
47  * how prewarming is done; legal options are 'prefetch', 'read', and 'buffer'.
48  * The third is the name of the relation fork to be prewarmed. The fourth
49  * and fifth arguments specify the first and last block to be prewarmed.
50  * If the fourth argument is NULL, it will be taken as 0; if the fifth argument
51  * is NULL, it will be taken as the number of blocks in the relation. The
52  * return value is the number of blocks successfully prewarmed.
53  */
54 Datum
56 {
57  Oid relOid;
58  text *forkName;
59  text *type;
60  int64 first_block;
61  int64 last_block;
62  int64 nblocks;
63  int64 blocks_done = 0;
64  int64 block;
65  Relation rel;
66  ForkNumber forkNumber;
67  char *forkString;
68  char *ttype;
69  PrewarmType ptype;
70  AclResult aclresult;
71 
72  /* Basic sanity checking. */
73  if (PG_ARGISNULL(0))
74  ereport(ERROR,
75  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
76  errmsg("relation cannot be null")));
77  relOid = PG_GETARG_OID(0);
78  if (PG_ARGISNULL(1))
79  ereport(ERROR,
80  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
81  (errmsg("prewarm type cannot be null"))));
82  type = PG_GETARG_TEXT_PP(1);
83  ttype = text_to_cstring(type);
84  if (strcmp(ttype, "prefetch") == 0)
85  ptype = PREWARM_PREFETCH;
86  else if (strcmp(ttype, "read") == 0)
87  ptype = PREWARM_READ;
88  else if (strcmp(ttype, "buffer") == 0)
89  ptype = PREWARM_BUFFER;
90  else
91  {
92  ereport(ERROR,
93  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
94  errmsg("invalid prewarm type"),
95  errhint("Valid prewarm types are \"prefetch\", \"read\", and \"buffer\".")));
96  PG_RETURN_INT64(0); /* Placate compiler. */
97  }
98  if (PG_ARGISNULL(2))
99  ereport(ERROR,
100  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
101  (errmsg("relation fork cannot be null"))));
102  forkName = PG_GETARG_TEXT_PP(2);
103  forkString = text_to_cstring(forkName);
104  forkNumber = forkname_to_number(forkString);
105 
106  /* Open relation and check privileges. */
107  rel = relation_open(relOid, AccessShareLock);
108  aclresult = pg_class_aclcheck(relOid, GetUserId(), ACL_SELECT);
109  if (aclresult != ACLCHECK_OK)
110  aclcheck_error(aclresult, ACL_KIND_CLASS, get_rel_name(relOid));
111 
112  /* Check that the fork exists. */
113  RelationOpenSmgr(rel);
114  if (!smgrexists(rel->rd_smgr, forkNumber))
115  ereport(ERROR,
116  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
117  errmsg("fork \"%s\" does not exist for this relation",
118  forkString)));
119 
120  /* Validate block numbers, or handle nulls. */
121  nblocks = RelationGetNumberOfBlocksInFork(rel, forkNumber);
122  if (PG_ARGISNULL(3))
123  first_block = 0;
124  else
125  {
126  first_block = PG_GETARG_INT64(3);
127  if (first_block < 0 || first_block >= nblocks)
128  ereport(ERROR,
129  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
130  errmsg("starting block number must be between 0 and " INT64_FORMAT,
131  nblocks - 1)));
132  }
133  if (PG_ARGISNULL(4))
134  last_block = nblocks - 1;
135  else
136  {
137  last_block = PG_GETARG_INT64(4);
138  if (last_block < 0 || last_block >= nblocks)
139  ereport(ERROR,
140  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
141  errmsg("ending block number must be between 0 and " INT64_FORMAT,
142  nblocks - 1)));
143  }
144 
145  /* Now we're ready to do the real work. */
146  if (ptype == PREWARM_PREFETCH)
147  {
148 #ifdef USE_PREFETCH
149 
150  /*
151  * In prefetch mode, we just hint the OS to read the blocks, but we
152  * don't know whether it really does it, and we don't wait for it to
153  * finish.
154  *
155  * It would probably be better to pass our prefetch requests in chunks
156  * of a megabyte or maybe even a whole segment at a time, but there's
157  * no practical way to do that at present without a gross modularity
158  * violation, so we just do this.
159  */
160  for (block = first_block; block <= last_block; ++block)
161  {
163  PrefetchBuffer(rel, forkNumber, block);
164  ++blocks_done;
165  }
166 #else
167  ereport(ERROR,
168  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
169  errmsg("prefetch is not supported by this build")));
170 #endif
171  }
172  else if (ptype == PREWARM_READ)
173  {
174  /*
175  * In read mode, we actually read the blocks, but not into shared
176  * buffers. This is more portable than prefetch mode (it works
177  * everywhere) and is synchronous.
178  */
179  for (block = first_block; block <= last_block; ++block)
180  {
182  smgrread(rel->rd_smgr, forkNumber, block, blockbuffer);
183  ++blocks_done;
184  }
185  }
186  else if (ptype == PREWARM_BUFFER)
187  {
188  /*
189  * In buffer mode, we actually pull the data into shared_buffers.
190  */
191  for (block = first_block; block <= last_block; ++block)
192  {
193  Buffer buf;
194 
196  buf = ReadBufferExtended(rel, forkNumber, block, RBM_NORMAL, NULL);
197  ReleaseBuffer(buf);
198  ++blocks_done;
199  }
200  }
201 
202  /* Close relation, release lock. */
204 
205  PG_RETURN_INT64(blocks_done);
206 }
PrewarmType
Definition: pg_prewarm.c:33
int errhint(const char *fmt,...)
Definition: elog.c:987
Oid GetUserId(void)
Definition: miscinit.c:284
PG_MODULE_MAGIC
Definition: pg_prewarm.c:29
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
struct SMgrRelationData * rd_smgr
Definition: rel.h:87
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define AccessShareLock
Definition: lockdefs.h:36
Datum pg_prewarm(PG_FUNCTION_ARGS)
Definition: pg_prewarm.c:55
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:287
unsigned int Oid
Definition: postgres_ext.h:31
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:48
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: smgr.c:625
#define RelationOpenSmgr(relation)
Definition: rel.h:460
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:66
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
static char blockbuffer[BLCKSZ]
Definition: pg_prewarm.c:40
#define ereport(elevel, rest)
Definition: elog.h:122
ForkNumber
Definition: relpath.h:24
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
#define ACL_SELECT
Definition: parsenodes.h:73
void PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:529
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2788
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
PG_FUNCTION_INFO_V1(pg_prewarm)
#define INT64_FORMAT
Definition: c.h:315
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4422
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1114
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
int Buffer
Definition: buf.h:23