rel.h 7.0 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * rel.h
4
 *	  POSTGRES relation descriptor (a/k/a relcache entry) definitions.
5 6
 *
 *
B
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $Id: rel.h,v 1.64 2002/11/23 03:59:09 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#ifndef REL_H
15 16
#define REL_H

17 18
#include "access/strat.h"
#include "access/tupdesc.h"
B
Bruce Momjian 已提交
19 20
#include "catalog/pg_am.h"
#include "catalog/pg_class.h"
21
#include "catalog/pg_index.h"
22
#include "rewrite/prs2lock.h"
23
#include "storage/block.h"
24
#include "storage/fd.h"
25
#include "storage/relfilenode.h"
26

27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
 * to declare them here so we can have a LockInfoData field in a Relation.
 */

typedef struct LockRelId
{
	Oid			relId;			/* a relation identifier */
	Oid			dbId;			/* a database identifier */
} LockRelId;

typedef struct LockInfoData
{
	LockRelId	lockRelId;
} LockInfoData;

typedef LockInfoData *LockInfo;

46 47 48 49
/*
 * Likewise, this struct really belongs to trigger.h, but for convenience
 * we put it here.
 */
50 51
typedef struct Trigger
{
52 53
	Oid			tgoid;			/* OID of trigger (pg_trigger row) */
	/* Remaining fields are copied from pg_trigger, see pg_trigger.h */
54 55 56
	char	   *tgname;
	Oid			tgfoid;
	int16		tgtype;
57 58
	bool		tgenabled;
	bool		tgisconstraint;
59
	Oid			tgconstrrelid;
60 61
	bool		tgdeferrable;
	bool		tginitdeferred;
62
	int16		tgnargs;
63
	int16		tgattr[FUNC_MAX_ARGS];
64
	char	  **tgargs;
65
} Trigger;
66 67 68

typedef struct TriggerDesc
{
69
	/*
70 71 72
	 * Index data to identify which triggers are which.  Since each
	 * trigger can appear in more than one class, for each class we
	 * provide a list of integer indexes into the triggers array.
73
	 */
74
#define TRIGGER_NUM_EVENT_CLASSES  3
75 76 77 78 79 80 81 82 83 84 85

	uint16		n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
	uint16		n_before_row[TRIGGER_NUM_EVENT_CLASSES];
	uint16		n_after_row[TRIGGER_NUM_EVENT_CLASSES];
	uint16		n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
	int		   *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
	int		   *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
	int		   *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
	int		   *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];

	/* The actual array of triggers is here */
86
	Trigger    *triggers;
87
	int			numtriggers;
88
} TriggerDesc;
89

90 91 92 93 94

/* ----------
 * Same for the statistics collector data in Relation and scan data.
 * ----------
 */
95
typedef struct PgStat_Info
96
{
97 98 99 100
	void	   *tabentry;
	bool		no_stats;
	bool		heap_scan_counted;
	bool		index_scan_counted;
101 102
} PgStat_Info;

103 104 105
/*
 * Here are the contents of a relation cache entry.
 */
106

107 108
typedef struct RelationData
{
109
	File		rd_fd;			/* open file descriptor, or -1 if none */
110
	RelFileNode rd_node;		/* file node (physical identifier) */
111 112 113
	BlockNumber rd_nblocks;		/* number of blocks in rel */
	BlockNumber rd_targblock;	/* current insertion target block, or
								 * InvalidBlockNumber */
114
	int			rd_refcnt;		/* reference count */
115
	bool		rd_isnew;		/* rel was created in current xact */
B
Bruce Momjian 已提交
116

117 118 119 120
	/*
	 * NOTE: rd_isnew should be relied on only for optimization purposes;
	 * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
	 */
121
	bool		rd_istemp;		/* rel uses the local buffer mgr */
122
	bool		rd_isnailed;	/* rel is nailed in cache */
123
	bool		rd_indexfound;	/* true if rd_indexlist is valid */
124
	Form_pg_class rd_rel;		/* RELATION tuple */
125
	TupleDesc	rd_att;			/* tuple descriptor */
126
	Oid			rd_id;			/* relation's object id */
127 128
	List	   *rd_indexlist;	/* list of OIDs of indexes on relation */
	LockInfoData rd_lockInfo;	/* lock mgr's info for locking relation */
129
	RuleLock   *rd_rules;		/* rewrite rules */
130
	MemoryContext rd_rulescxt;	/* private memory cxt for rd_rules, if any */
131
	TriggerDesc *trigdesc;		/* Trigger info, or NULL if rel has none */
132

133 134 135 136
	/* These are non-NULL only for an index relation: */
	Form_pg_index rd_index;		/* pg_index tuple describing this index */
	Form_pg_am	rd_am;			/* pg_am tuple for index's AM */

137 138 139
	/* index access support info (used only for an index relation) */
	MemoryContext rd_indexcxt;	/* private memory cxt for this stuff */
	IndexStrategy rd_istrat;	/* operator strategy map */
140
	Oid		   *rd_operator;	/* OIDs of index operators */
141
	RegProcedure *rd_support;	/* OIDs of support procedures */
142 143
	struct FmgrInfo *rd_supportinfo;	/* lookup info for support
										 * procedures */
144 145 146
	/* "struct FmgrInfo" avoids need to include fmgr.h here */

	/* statistics collection area */
147
	PgStat_Info pgstat_info;
148
} RelationData;
149 150

typedef RelationData *Relation;
151

152

153
/* ----------------
154 155 156
 *		RelationPtr is used in the executor to support index scans
 *		where we have to keep track of several index relations in an
 *		array.	-cim 9/10/89
157 158
 * ----------------
 */
159
typedef Relation *RelationPtr;
160 161 162


/*
B
Bruce Momjian 已提交
163
 * RelationIsValid
164
 *		True iff relation descriptor is valid.
165
 */
166
#define RelationIsValid(relation) PointerIsValid(relation)
167

168 169
#define InvalidRelation ((Relation) NULL)

170
/*
B
Bruce Momjian 已提交
171
 * RelationHasReferenceCountZero
172
 *		True iff relation reference count is zero.
173 174
 *
 * Note:
175
 *		Assumes relation descriptor is valid.
176 177
 */
#define RelationHasReferenceCountZero(relation) \
178
		((bool)((relation)->rd_refcnt == 0))
179 180

/*
B
Bruce Momjian 已提交
181
 * RelationSetReferenceCount
182
 *		Sets relation reference count.
183
 */
184 185
#define RelationSetReferenceCount(relation,count) \
	((relation)->rd_refcnt = (count))
186 187

/*
B
Bruce Momjian 已提交
188
 * RelationIncrementReferenceCount
189
 *		Increments relation reference count.
190
 */
191 192
#define RelationIncrementReferenceCount(relation) \
	((relation)->rd_refcnt += 1)
193 194

/*
B
Bruce Momjian 已提交
195
 * RelationDecrementReferenceCount
196
 *		Decrements relation reference count.
197
 */
198 199 200
#define RelationDecrementReferenceCount(relation) \
	(AssertMacro((relation)->rd_refcnt > 0), \
	 (relation)->rd_refcnt -= 1)
201 202

/*
B
Bruce Momjian 已提交
203
 * RelationGetForm
204
 *		Returns pg_class tuple for a relation.
205 206
 *
 * Note:
207
 *		Assumes relation descriptor is valid.
208
 */
209
#define RelationGetForm(relation) ((relation)->rd_rel)
210

211
/*
B
Bruce Momjian 已提交
212
 * RelationGetRelid
213
 *
214
 *	returns the OID of the relation
215
 */
216
#define RelationGetRelid(relation) ((relation)->rd_id)
217 218

/*
B
Bruce Momjian 已提交
219
 * RelationGetFile
220
 *
221
 *	  Returns the open file descriptor for the rel
222 223 224 225
 */
#define RelationGetFile(relation) ((relation)->rd_fd)

/*
226
 * RelationGetNumberOfAttributes
227
 *
228
 *	  Returns the number of attributes.
229 230 231 232
 */
#define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)

/*
B
Bruce Momjian 已提交
233
 * RelationGetDescr
234
 *		Returns tuple descriptor for a relation.
235
 */
236
#define RelationGetDescr(relation) ((relation)->rd_att)
237

238 239 240 241 242 243 244 245 246
/*
 * RelationGetIndexStrategy
 *		Returns index strategy for a relation.
 *
 * Note:
 *		Assumes relation descriptor is valid.
 *		Assumes relation descriptor is for an index relation.
 */
#define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
247

248 249 250
/*
 * RelationGetRelationName
 *
251
 *	  Returns the rel's name.
252
 *
253
 * Note that the name is only unique within the containing namespace.
254 255
 */
#define RelationGetRelationName(relation) \
256
	(NameStr((relation)->rd_rel->relname))
257

258 259 260 261 262 263 264 265
/*
 * RelationGetNamespace
 *
 *	  Returns the rel's namespace OID.
 */
#define RelationGetNamespace(relation) \
	((relation)->rd_rel->relnamespace)

266
#endif   /* REL_H */