|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pg_prewarm.c |
| 4 | + * prewarming utilities |
| 5 | + * |
| 6 | + * Copyright (c) 2010-2013, 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 | + |
| 29 | +PG_MODULE_MAGIC; |
| 30 | + |
| 31 | +externDatumpg_prewarm(PG_FUNCTION_ARGS); |
| 32 | + |
| 33 | +PG_FUNCTION_INFO_V1(pg_prewarm); |
| 34 | + |
| 35 | +typedefenum |
| 36 | +{ |
| 37 | +PREWARM_PREFETCH, |
| 38 | +PREWARM_READ, |
| 39 | +PREWARM_BUFFER |
| 40 | +}PrewarmType; |
| 41 | + |
| 42 | +staticcharblockbuffer[BLCKSZ]; |
| 43 | + |
| 44 | +/* |
| 45 | + * pg_prewarm(regclass, mode text, fork text, |
| 46 | + * first_block int8, last_block int8) |
| 47 | + * |
| 48 | + * The first argument is the relation to be prewarmed; the second controls |
| 49 | + * how prewarming is done; legal options are 'prefetch', 'read', and 'buffer'. |
| 50 | + * The third is the name of the relation fork to be prewarmed.The fourth |
| 51 | + * and fifth arguments specify the first and last block to be prewarmed. |
| 52 | + * If the fourth argument is NULL, it will be taken as 0; if the fifth argument |
| 53 | + * is NULL, it will be taken as the number of blocks in the relation. The |
| 54 | + * return value is the number of blocks successfully prewarmed. |
| 55 | + */ |
| 56 | +Datum |
| 57 | +pg_prewarm(PG_FUNCTION_ARGS) |
| 58 | +{ |
| 59 | +OidrelOid; |
| 60 | +text*forkName; |
| 61 | +text*type; |
| 62 | +int64first_block; |
| 63 | +int64last_block; |
| 64 | +int64nblocks; |
| 65 | +int64blocks_done=0; |
| 66 | +int64block; |
| 67 | +Relationrel; |
| 68 | +ForkNumberforkNumber; |
| 69 | +char*forkString; |
| 70 | +char*ttype; |
| 71 | +PrewarmTypeptype; |
| 72 | +AclResultaclresult; |
| 73 | + |
| 74 | +/* Basic sanity checking. */ |
| 75 | +if (PG_ARGISNULL(0)) |
| 76 | +ereport(ERROR, |
| 77 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 78 | +errmsg("relation cannot be null"))); |
| 79 | +relOid=PG_GETARG_OID(0); |
| 80 | +if (PG_ARGISNULL(1)) |
| 81 | +ereport(ERROR, |
| 82 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 83 | + (errmsg("prewarm type cannot be null")))); |
| 84 | +type=PG_GETARG_TEXT_P(1); |
| 85 | +ttype=text_to_cstring(type); |
| 86 | +if (strcmp(ttype,"prefetch")==0) |
| 87 | +ptype=PREWARM_PREFETCH; |
| 88 | +elseif (strcmp(ttype,"read")==0) |
| 89 | +ptype=PREWARM_READ; |
| 90 | +elseif (strcmp(ttype,"buffer")==0) |
| 91 | +ptype=PREWARM_BUFFER; |
| 92 | +else |
| 93 | +{ |
| 94 | +ereport(ERROR, |
| 95 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 96 | +errmsg("invalid prewarm type"), |
| 97 | +errhint("Valid prewarm types are \"prefetch\", \"read\", and \"buffer\"."))); |
| 98 | +PG_RETURN_INT64(0);/* Placate compiler. */ |
| 99 | +} |
| 100 | +if (PG_ARGISNULL(2)) |
| 101 | +ereport(ERROR, |
| 102 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 103 | + (errmsg("relation fork cannot be null")))); |
| 104 | +forkName=PG_GETARG_TEXT_P(2); |
| 105 | +forkString=text_to_cstring(forkName); |
| 106 | +forkNumber=forkname_to_number(forkString); |
| 107 | + |
| 108 | +/* Open relation and check privileges. */ |
| 109 | +rel=relation_open(relOid,AccessShareLock); |
| 110 | +aclresult=pg_class_aclcheck(relOid,GetUserId(),ACL_SELECT); |
| 111 | +if (aclresult!=ACLCHECK_OK) |
| 112 | +aclcheck_error(aclresult,ACL_KIND_CLASS,get_rel_name(relOid)); |
| 113 | + |
| 114 | +/* Check that the fork exists. */ |
| 115 | +RelationOpenSmgr(rel); |
| 116 | +if (!smgrexists(rel->rd_smgr,forkNumber)) |
| 117 | +ereport(ERROR, |
| 118 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 119 | +errmsg("fork \"%s\" does not exist for this relation", |
| 120 | +forkString))); |
| 121 | + |
| 122 | +/* Validate block numbers, or handle nulls. */ |
| 123 | +nblocks=RelationGetNumberOfBlocksInFork(rel,forkNumber); |
| 124 | +if (PG_ARGISNULL(3)) |
| 125 | +first_block=0; |
| 126 | +else |
| 127 | +{ |
| 128 | +first_block=PG_GETARG_INT64(3); |
| 129 | +if (first_block<0||first_block >=nblocks) |
| 130 | +ereport(ERROR, |
| 131 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 132 | +errmsg("starting block number must be between 0 and "INT64_FORMAT, |
| 133 | +nblocks-1))); |
| 134 | +} |
| 135 | +if (PG_ARGISNULL(4)) |
| 136 | +last_block=nblocks-1; |
| 137 | +else |
| 138 | +{ |
| 139 | +last_block=PG_GETARG_INT64(4); |
| 140 | +if (last_block<0||last_block >=nblocks) |
| 141 | +ereport(ERROR, |
| 142 | +(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 143 | +errmsg("ending block number must be between 0 and "INT64_FORMAT, |
| 144 | +nblocks-1))); |
| 145 | +} |
| 146 | + |
| 147 | +/* Now we're ready to do the real work. */ |
| 148 | +if (ptype==PREWARM_PREFETCH) |
| 149 | +{ |
| 150 | +#ifdefUSE_PREFETCH |
| 151 | + |
| 152 | +/* |
| 153 | + * In prefetch mode, we just hint the OS to read the blocks, but we |
| 154 | + * don't know whether it really does it, and we don't wait for it to |
| 155 | + * finish. |
| 156 | + * |
| 157 | + * It would probably be better to pass our prefetch requests in chunks |
| 158 | + * of a megabyte or maybe even a whole segment at a time, but there's |
| 159 | + * no practical way to do that at present without a gross modularity |
| 160 | + * violation, so we just do this. |
| 161 | + */ |
| 162 | +for (block=first_block;block <=last_block;++block) |
| 163 | +{ |
| 164 | +PrefetchBuffer(rel,forkNumber,block); |
| 165 | +++blocks_done; |
| 166 | +} |
| 167 | +#else |
| 168 | +ereport(ERROR, |
| 169 | +(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 170 | +errmsg("prefetch is not supported by this build"))); |
| 171 | +#endif |
| 172 | +} |
| 173 | +elseif (ptype==PREWARM_READ) |
| 174 | +{ |
| 175 | +/* |
| 176 | + * In read mode, we actually read the blocks, but not into shared |
| 177 | + * buffers. This is more portable than prefetch mode (it works |
| 178 | + * everywhere) and is synchronous. |
| 179 | + */ |
| 180 | +for (block=first_block;block <=last_block;++block) |
| 181 | +{ |
| 182 | +smgrread(rel->rd_smgr,forkNumber,block,blockbuffer); |
| 183 | +++blocks_done; |
| 184 | +} |
| 185 | +} |
| 186 | +elseif (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 | +Bufferbuf; |
| 194 | + |
| 195 | +buf=ReadBufferExtended(rel,forkNumber,block,RBM_NORMAL,NULL); |
| 196 | +ReleaseBuffer(buf); |
| 197 | +++blocks_done; |
| 198 | +} |
| 199 | +} |
| 200 | + |
| 201 | +/* Close relation, release lock. */ |
| 202 | +relation_close(rel,AccessShareLock); |
| 203 | + |
| 204 | +PG_RETURN_INT64(blocks_done); |
| 205 | +} |