rel.h 8.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * rel.h
4
 *	  POSTGRES relation descriptor (a/k/a relcache entry) definitions.
5 6
 *
 *
P
 
PostgreSQL Daemon 已提交
7
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.86 2005/10/06 02:29:21 tgl Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#ifndef REL_H
15 16
#define REL_H

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

26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * 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;

45 46 47 48
/*
 * Likewise, this struct really belongs to trigger.h, but for convenience
 * we put it here.
 */
49 50
typedef struct Trigger
{
51 52
	Oid			tgoid;			/* OID of trigger (pg_trigger row) */
	/* Remaining fields are copied from pg_trigger, see pg_trigger.h */
53 54 55
	char	   *tgname;
	Oid			tgfoid;
	int16		tgtype;
56 57
	bool		tgenabled;
	bool		tgisconstraint;
58
	Oid			tgconstrrelid;
59 60
	bool		tgdeferrable;
	bool		tginitdeferred;
61
	int16		tgnargs;
62 63
	int16		tgnattr;
	int16	   *tgattr;
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
 * Same for the statistics collector data in Relation and scan data.
 */
94
typedef struct PgStat_Info
95
{
96
	void	   *tabentry;
97 98
} PgStat_Info;

99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*
 * Cached lookup information for the index access method functions defined
 * by the pg_am row associated with an index relation.
 */
typedef struct RelationAmInfo
{
	FmgrInfo	aminsert;
	FmgrInfo	ambeginscan;
	FmgrInfo	amgettuple;
	FmgrInfo	amgetmulti;
	FmgrInfo	amrescan;
	FmgrInfo	amendscan;
	FmgrInfo	ammarkpos;
	FmgrInfo	amrestrpos;
	FmgrInfo	ambuild;
	FmgrInfo	ambulkdelete;
	FmgrInfo	amvacuumcleanup;
	FmgrInfo	amcostestimate;
} RelationAmInfo;


121 122 123
/*
 * Here are the contents of a relation cache entry.
 */
124

125 126
typedef struct RelationData
{
127 128
	RelFileNode rd_node;		/* relation physical identifier */
	/* use "struct" here to avoid needing to include smgr.h: */
B
Bruce Momjian 已提交
129
	struct SMgrRelationData *rd_smgr;	/* cached file handle, or NULL */
130 131
	BlockNumber rd_targblock;	/* current insertion target block, or
								 * InvalidBlockNumber */
132
	int			rd_refcnt;		/* reference count */
133
	bool		rd_istemp;		/* rel uses the local buffer mgr */
134 135
	bool		rd_isnailed;	/* rel is nailed in cache */
	bool		rd_isvalid;		/* relcache entry is valid */
136 137
	char		rd_indexvalid;	/* state of rd_indexlist: 0 = not valid,
								 * 1 = valid, 2 = temporarily forced */
138
	SubTransactionId rd_createSubid;	/* rel was created in current xact */
B
Bruce Momjian 已提交
139

140
	/*
141
	 * rd_createSubid is the ID of the highest subtransaction the rel has
142
	 * survived into; or zero if the rel was not created in the current
143
	 * top transaction.  This should be relied on only for optimization
B
Bruce Momjian 已提交
144 145
	 * purposes; it is possible for new-ness to be "forgotten" (eg, after
	 * CLUSTER).
146
	 */
147
	Form_pg_class rd_rel;		/* RELATION tuple */
148
	TupleDesc	rd_att;			/* tuple descriptor */
149
	Oid			rd_id;			/* relation's object id */
150
	List	   *rd_indexlist;	/* list of OIDs of indexes on relation */
151
	Oid			rd_oidindex;	/* OID of unique index on OID, if any */
152
	LockInfoData rd_lockInfo;	/* lock mgr's info for locking relation */
153
	RuleLock   *rd_rules;		/* rewrite rules */
154
	MemoryContext rd_rulescxt;	/* private memory cxt for rd_rules, if any */
155
	TriggerDesc *trigdesc;		/* Trigger info, or NULL if rel has none */
156

157 158
	/* These are non-NULL only for an index relation: */
	Form_pg_index rd_index;		/* pg_index tuple describing this index */
B
Bruce Momjian 已提交
159 160
	struct HeapTupleData *rd_indextuple;		/* all of pg_index tuple */
	/* "struct HeapTupleData *" avoids need to include htup.h here	*/
161
	oidvector  *rd_indclass;	/* extracted pointer to rd_index field */
162 163
	Form_pg_am	rd_am;			/* pg_am tuple for index's AM */

164 165 166
	/*
	 * index access support info (used only for an index relation)
	 *
167
	 * Note: only default operators and support procs for each opclass are
B
Bruce Momjian 已提交
168
	 * cached, namely those with subtype zero.	The arrays are indexed by
169 170 171
	 * strategy or support number, which is a sufficient identifier given
	 * that restriction.
	 */
172
	MemoryContext rd_indexcxt;	/* private memory cxt for this stuff */
173
	RelationAmInfo *rd_aminfo;	/* lookup info for funcs found in pg_am */
174
	Oid		   *rd_operator;	/* OIDs of index operators */
175
	RegProcedure *rd_support;	/* OIDs of support procedures */
176
	FmgrInfo   *rd_supportinfo;	/* lookup info for support procedures */
177 178
	List	   *rd_indexprs;	/* index expression trees, if any */
	List	   *rd_indpred;		/* index predicate tree, if any */
179 180

	/* statistics collection area */
181
	PgStat_Info pgstat_info;
182
} RelationData;
183 184

typedef RelationData *Relation;
185

186

187
/* ----------------
188 189 190
 *		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
191 192
 * ----------------
 */
193
typedef Relation *RelationPtr;
194 195 196


/*
B
Bruce Momjian 已提交
197
 * RelationIsValid
198
 *		True iff relation descriptor is valid.
199
 */
200
#define RelationIsValid(relation) PointerIsValid(relation)
201

202 203
#define InvalidRelation ((Relation) NULL)

204
/*
B
Bruce Momjian 已提交
205
 * RelationHasReferenceCountZero
206
 *		True iff relation reference count is zero.
207 208
 *
 * Note:
209
 *		Assumes relation descriptor is valid.
210 211
 */
#define RelationHasReferenceCountZero(relation) \
212
		((bool)((relation)->rd_refcnt == 0))
213 214

/*
B
Bruce Momjian 已提交
215
 * RelationGetForm
216
 *		Returns pg_class tuple for a relation.
217 218
 *
 * Note:
219
 *		Assumes relation descriptor is valid.
220
 */
221
#define RelationGetForm(relation) ((relation)->rd_rel)
222

223
/*
B
Bruce Momjian 已提交
224
 * RelationGetRelid
N
Neil Conway 已提交
225
 *		Returns the OID of the relation
226
 */
227
#define RelationGetRelid(relation) ((relation)->rd_id)
228 229

/*
230
 * RelationGetNumberOfAttributes
N
Neil Conway 已提交
231
 *		Returns the number of attributes in a relation.
232 233 234 235
 */
#define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)

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

241 242
/*
 * RelationGetRelationName
N
Neil Conway 已提交
243
 *		Returns the rel's name.
244
 *
245
 * Note that the name is only unique within the containing namespace.
246 247
 */
#define RelationGetRelationName(relation) \
248
	(NameStr((relation)->rd_rel->relname))
249

250 251
/*
 * RelationGetNamespace
N
Neil Conway 已提交
252
 *		Returns the rel's namespace OID.
253 254 255 256
 */
#define RelationGetNamespace(relation) \
	((relation)->rd_rel->relnamespace)

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
/*
 * RelationOpenSmgr
 *		Open the relation at the smgr level, if not already done.
 */
#define RelationOpenSmgr(relation) \
	do { \
		if ((relation)->rd_smgr == NULL) \
			smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
	} while (0)

/*
 * RelationCloseSmgr
 *		Close the relation at the smgr level, if not already done.
 *
 * Note: smgrclose should unhook from owner pointer, hence the Assert.
 */
#define RelationCloseSmgr(relation) \
	do { \
		if ((relation)->rd_smgr != NULL) \
		{ \
			smgrclose((relation)->rd_smgr); \
			Assert((relation)->rd_smgr == NULL); \
		} \
	} while (0)

282 283 284 285 286 287 288 289
/*
 * RELATION_IS_LOCAL
 *		If a rel is either temp or newly created in the current transaction,
 *		it can be assumed to be visible only to the current backend.
 *
 * Beware of multiple eval of argument
 */
#define RELATION_IS_LOCAL(relation) \
290 291
	((relation)->rd_istemp || \
	 (relation)->rd_createSubid != InvalidSubTransactionId)
292

293 294 295 296
/* routines in utils/cache/relcache.c */
extern void RelationIncrementReferenceCount(Relation rel);
extern void RelationDecrementReferenceCount(Relation rel);

297
#endif   /* REL_H */