Skip to content

Commit

Permalink
Change the functions to take the relation name as 'regclass', instead of
Browse files Browse the repository at this point in the history
text. Makes it easier to do things like "SELECT pg_check_table(oid) FROM
pg_class", and simplifies the code a bit, too.
  • Loading branch information
hlinnaka committed Nov 11, 2011
1 parent 27a6913 commit 5a6d73f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
36 changes: 15 additions & 21 deletions pg_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Datum pg_check_table_pages(PG_FUNCTION_ARGS);
Datum pg_check_index(PG_FUNCTION_ARGS);
Datum pg_check_index_pages(PG_FUNCTION_ARGS);

static uint32 check_table(text *relname, bool checkIndexes, BlockNumber blockFrom, BlockNumber blockTo);
static uint32 check_table(Oid relid, bool checkIndexes, BlockNumber blockFrom, BlockNumber blockTo);

static uint32 check_index(text *relname, BlockNumber blockFrom, BlockNumber blockTo);
static uint32 check_index(Oid indexOid, BlockNumber blockFrom, BlockNumber blockTo);

static uint32 check_index_oid(Oid indexOid);

Expand All @@ -47,11 +47,11 @@ PG_FUNCTION_INFO_V1(pg_check_table);
Datum
pg_check_table(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_P(0);
Oid relid = PG_GETARG_OID(0);
bool checkIndexes = PG_GETARG_BOOL(1);
uint32 nerrs;

nerrs = check_table(relname, checkIndexes, 0, -1);
nerrs = check_table(relid, checkIndexes, 0, -1);

PG_RETURN_INT32(nerrs);
}
Expand All @@ -66,12 +66,12 @@ PG_FUNCTION_INFO_V1(pg_check_table_pages);
Datum
pg_check_table_pages(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_P(0);
Oid relid = PG_GETARG_OID(0);
uint32 blkfrom = PG_GETARG_UINT32(1);
uint32 blkto = PG_GETARG_UINT32(2);
uint32 nerrs;

nerrs = check_table(relname, false, blkfrom, blkto);
nerrs = check_table(relid, false, blkfrom, blkto);

PG_RETURN_INT32(nerrs);
}
Expand All @@ -86,10 +86,10 @@ PG_FUNCTION_INFO_V1(pg_check_index);
Datum
pg_check_index(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_P(0);
Oid relid = PG_GETARG_OID(0);
uint32 nerrs;

nerrs = check_index(relname, 0, -1);
nerrs = check_index(relid, 0, -1);

PG_RETURN_INT32(nerrs);
}
Expand All @@ -104,12 +104,12 @@ PG_FUNCTION_INFO_V1(pg_check_index_pages);
Datum
pg_check_index_pages(PG_FUNCTION_ARGS)
{
text *relname = PG_GETARG_TEXT_P(0);
Oid relid = PG_GETARG_OID(0);
uint32 blkfrom = PG_GETARG_UINT32(1);
uint32 blkto = PG_GETARG_UINT32(2);
uint32 nerrs;

nerrs = check_index(relname, blkfrom, blkto);
nerrs = check_index(relid, blkfrom, blkto);

PG_RETURN_INT32(nerrs);
}
Expand All @@ -121,9 +121,8 @@ pg_check_index_pages(PG_FUNCTION_ARGS)
*
*/
static uint32
check_table(text *relname, bool checkIndexes, BlockNumber blockFrom, BlockNumber blockTo)
check_table(Oid relid, bool checkIndexes, BlockNumber blockFrom, BlockNumber blockTo)
{
RangeVar *relrv; /* used to search the relation by name */
Relation rel; /* relation for the 'relname' */
char *raw_page; /* raw data of the page */
Buffer buf; /* buffer the page is read into */
Expand All @@ -146,10 +145,8 @@ check_table(text *relname, bool checkIndexes, BlockNumber blockFrom, BlockNumber
ereport(ERROR,
(errmsg("invalid combination of checkIndexes, block range")));

relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));

/* FIXME is this lock mode sufficient? */
rel = relation_openrv(relrv, AccessShareLock);
rel = relation_open(relid, AccessShareLock);

/* Check that this relation has storage */
if (rel->rd_rel->relkind != RELKIND_RELATION)
Expand Down Expand Up @@ -295,9 +292,8 @@ check_index_oid(Oid indexOid)
* check the index, acquires AccessShareLock
*/
static uint32
check_index(text *relname, BlockNumber blockFrom, BlockNumber blockTo)
check_index(Oid indexOid, BlockNumber blockFrom, BlockNumber blockTo)
{
RangeVar *relrv; /* used to search the relation by name */
Relation rel; /* relation for the 'relname' */
char *raw_page; /* raw data of the page */
Buffer buf; /* buffer the page is read into */
Expand All @@ -314,10 +310,8 @@ check_index(text *relname, BlockNumber blockFrom, BlockNumber blockTo)
ereport(ERROR,
(errmsg("invalid starting block number %d", blockFrom)));

relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));

/* FIXME A more strict lock might be more appropriate. */
rel = relation_openrv(relrv, AccessShareLock);
rel = relation_open(indexOid, AccessShareLock);

/* Check that this relation is a b-tree index */
if ((rel->rd_rel->relkind != RELKIND_INDEX) || (rel->rd_rel->relam != BTREE_AM_OID)) {
Expand Down Expand Up @@ -367,4 +361,4 @@ check_index(text *relname, BlockNumber blockFrom, BlockNumber blockTo)
relation_close(rel, AccessShareLock);

return nerrs;
}
}
8 changes: 4 additions & 4 deletions pg_check.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ SET search_path = public;
-- pg_check_table()
--

CREATE OR REPLACE FUNCTION pg_check_table(text, bool)
CREATE OR REPLACE FUNCTION pg_check_table(regclass, bool)
RETURNS int4
AS 'MODULE_PATHNAME', 'pg_check_table'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION pg_check_table(text, int, int)
CREATE OR REPLACE FUNCTION pg_check_table(regclass, int, int)
RETURNS int4
AS 'MODULE_PATHNAME', 'pg_check_table_pages'
LANGUAGE C STRICT;
Expand All @@ -19,12 +19,12 @@ LANGUAGE C STRICT;
-- pg_check_index()
--

CREATE OR REPLACE FUNCTION pg_check_index(text)
CREATE OR REPLACE FUNCTION pg_check_index(regclass)
RETURNS int4
AS 'MODULE_PATHNAME', 'pg_check_index'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION pg_check_index(text, int, int)
CREATE OR REPLACE FUNCTION pg_check_index(regclass, int, int)
RETURNS int4
AS 'MODULE_PATHNAME', 'pg_check_index_pages'
LANGUAGE C STRICT;
8 changes: 4 additions & 4 deletions uninstall_pg_check.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Adjust this setting to control where the objects get dropped.
SET search_path = public;

DROP FUNCTION pg_check_table(text, bool);
DROP FUNCTION pg_check_table(text, int, int);
DROP FUNCTION pg_check_index(text);
DROP FUNCTION pg_check_index(text, int, int);
DROP FUNCTION pg_check_table(regclass, bool);
DROP FUNCTION pg_check_table(regclass, int, int);
DROP FUNCTION pg_check_index(regclass);
DROP FUNCTION pg_check_index(regclass, int, int);

0 comments on commit 5a6d73f

Please sign in to comment.