⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plancat.c

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------- * * plancat.c *	   routines for accessing the system catalogs * * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.114.2.1 2005/11/22 18:23:12 momjian Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include <math.h>#include "access/genam.h"#include "access/heapam.h"#include "catalog/pg_amop.h"#include "catalog/pg_inherits.h"#include "catalog/pg_index.h"#include "nodes/makefuncs.h"#include "optimizer/clauses.h"#include "optimizer/plancat.h"#include "optimizer/prep.h"#include "optimizer/tlist.h"#include "parser/parsetree.h"#include "parser/parse_expr.h"#include "parser/parse_relation.h"#include "rewrite/rewriteManip.h"#include "utils/builtins.h"#include "utils/fmgroids.h"#include "utils/lsyscache.h"#include "utils/relcache.h"#include "utils/syscache.h"#include "catalog/catalog.h"#include "miscadmin.h"static void estimate_rel_size(Relation rel, int32 *attr_widths,				  BlockNumber *pages, double *tuples);/* * get_relation_info - *	  Retrieves catalog information for a given relation. * * Given the Oid of the relation, return the following info into fields * of the RelOptInfo struct: * *	min_attr	lowest valid AttrNumber *	max_attr	highest valid AttrNumber *	indexlist	list of IndexOptInfos for relation's indexes *	pages		number of pages *	tuples		number of tuples * * Also, initialize the attr_needed[] and attr_widths[] arrays.  In most * cases these are left as zeroes, but sometimes we need to compute attr * widths here, and we may as well cache the results for costsize.c. */voidget_relation_info(Oid relationObjectId, RelOptInfo *rel){	Index		varno = rel->relid;	Relation	relation;	bool		hasindex;	List	   *indexinfos = NIL;	/*	 * Normally, we can assume the rewriter already acquired at least	 * AccessShareLock on each relation used in the query.	However this will	 * not be the case for relations added to the query because they are	 * inheritance children of some relation mentioned explicitly. For them,	 * this is the first access during the parse/rewrite/plan pipeline, and so	 * we need to obtain and keep a suitable lock.	 *	 * XXX really, a suitable lock is RowShareLock if the relation is an	 * UPDATE/DELETE target, and AccessShareLock otherwise.  However we cannot	 * easily tell here which to get, so for the moment just get	 * AccessShareLock always.	The executor will get the right lock when it	 * runs, which means there is a very small chance of deadlock trying to	 * upgrade our lock.	 */	if (rel->reloptkind == RELOPT_BASEREL)		relation = heap_open(relationObjectId, NoLock);	else		relation = heap_open(relationObjectId, AccessShareLock);	rel->min_attr = FirstLowInvalidHeapAttributeNumber + 1;	rel->max_attr = RelationGetNumberOfAttributes(relation);	Assert(rel->max_attr >= rel->min_attr);	rel->attr_needed = (Relids *)		palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));	rel->attr_widths = (int32 *)		palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));	/*	 * Estimate relation size.	 */	estimate_rel_size(relation, rel->attr_widths - rel->min_attr,					  &rel->pages, &rel->tuples);	/*	 * Make list of indexes.  Ignore indexes on system catalogs if told to.	 */	if (IsIgnoringSystemIndexes() && IsSystemClass(relation->rd_rel))		hasindex = false;	else		hasindex = relation->rd_rel->relhasindex;	if (hasindex)	{		List	   *indexoidlist;		ListCell   *l;		indexoidlist = RelationGetIndexList(relation);		foreach(l, indexoidlist)		{			Oid			indexoid = lfirst_oid(l);			Relation	indexRelation;			Form_pg_index index;			IndexOptInfo *info;			int			ncolumns;			int			i;			int16		amorderstrategy;			/*			 * Extract info from the relation descriptor for the index.			 *			 * Note that we take no lock on the index; we assume our lock on			 * the parent table will protect the index's schema information.			 * When and if the executor actually uses the index, it will take			 * a lock as needed to protect the access to the index contents.			 */			indexRelation = index_open(indexoid);			index = indexRelation->rd_index;			info = makeNode(IndexOptInfo);			info->indexoid = index->indexrelid;			info->rel = rel;			info->ncolumns = ncolumns = index->indnatts;			/*			 * Need to make classlist and ordering arrays large enough to put			 * a terminating 0 at the end of each one.			 */			info->indexkeys = (int *) palloc(sizeof(int) * ncolumns);			info->classlist = (Oid *) palloc0(sizeof(Oid) * (ncolumns + 1));			info->ordering = (Oid *) palloc0(sizeof(Oid) * (ncolumns + 1));			for (i = 0; i < ncolumns; i++)			{				info->classlist[i] = indexRelation->rd_indclass->values[i];				info->indexkeys[i] = index->indkey.values[i];			}			info->relam = indexRelation->rd_rel->relam;			info->amcostestimate = indexRelation->rd_am->amcostestimate;			info->amoptionalkey = indexRelation->rd_am->amoptionalkey;			/*			 * Fetch the ordering operators associated with the index, if any.			 */			amorderstrategy = indexRelation->rd_am->amorderstrategy;			if (amorderstrategy != 0)			{				int			oprindex = amorderstrategy - 1;				for (i = 0; i < ncolumns; i++)				{					info->ordering[i] = indexRelation->rd_operator[oprindex];					oprindex += indexRelation->rd_am->amstrategies;				}			}			/*			 * Fetch the index expressions and predicate, if any.  We must			 * modify the copies we obtain from the relcache to have the			 * correct varno for the parent relation, so that they match up			 * correctly against qual clauses.			 */			info->indexprs = RelationGetIndexExpressions(indexRelation);			info->indpred = RelationGetIndexPredicate(indexRelation);			if (info->indexprs && varno != 1)				ChangeVarNodes((Node *) info->indexprs, 1, varno, 0);			if (info->indpred && varno != 1)				ChangeVarNodes((Node *) info->indpred, 1, varno, 0);			info->predOK = false;		/* set later in indxpath.c */			info->unique = index->indisunique;			/*			 * Estimate the index size.  If it's not a partial index, we lock			 * the number-of-tuples estimate to equal the parent table; if it			 * is partial then we have to use the same methods as we would for			 * a table, except we can be sure that the index is not larger			 * than the table.			 */			if (info->indpred == NIL)			{				info->pages = RelationGetNumberOfBlocks(indexRelation);				info->tuples = rel->tuples;			}			else			{				estimate_rel_size(indexRelation, NULL,								  &info->pages, &info->tuples);				if (info->tuples > rel->tuples)					info->tuples = rel->tuples;			}			index_close(indexRelation);			indexinfos = lcons(info, indexinfos);		}		list_free(indexoidlist);	}	rel->indexlist = indexinfos;	/* close rel, but keep lock if any */	heap_close(relation, NoLock);}/* * estimate_rel_size - estimate # pages and # tuples in a table or index * * If attr_widths isn't NULL, it points to the zero-index entry of the * relation's attr_width[] cache; we fill this in if we have need to compute * the attribute widths for estimation purposes. */static voidestimate_rel_size(Relation rel, int32 *attr_widths,				  BlockNumber *pages, double *tuples){	BlockNumber curpages;	BlockNumber relpages;	double		reltuples;	double		density;	switch (rel->rd_rel->relkind)	{		case RELKIND_RELATION:		case RELKIND_INDEX:		case RELKIND_TOASTVALUE:			/* it has storage, ok to call the smgr */			curpages = RelationGetNumberOfBlocks(rel);			/*			 * HACK: if the relation has never yet been vacuumed, use a			 * minimum estimate of 10 pages.  This emulates a desirable aspect			 * of pre-8.0 behavior, which is that we wouldn't assume a newly			 * created relation is really small, which saves us from making			 * really bad plans during initial data loading.  (The plans are			 * not wrong when they are made, but if they are cached and used			 * again after the table has grown a lot, they are bad.) It would			 * be better to force replanning if the table size has changed a			 * lot since the plan was made ... but we don't currently have any			 * infrastructure for redoing cached plans at all, so we have to			 * kluge things here instead.			 *			 * We approximate "never vacuumed" by "has relpages = 0", which			 * means this will also fire on genuinely empty relations.	Not			 * great, but fortunately that's a seldom-seen case in the real			 * world, and it shouldn't degrade the quality of the plan too			 * much anyway to err in this direction.			 */			if (curpages < 10 && rel->rd_rel->relpages == 0)				curpages = 10;			/* report estimated # pages */			*pages = curpages;			/* quick exit if rel is clearly empty */			if (curpages == 0)			{				*tuples = 0;				break;			}			/* coerce values in pg_class to more desirable types */			relpages = (BlockNumber) rel->rd_rel->relpages;			reltuples = (double) rel->rd_rel->reltuples;			/*			 * If it's an index, discount the metapage.  This is a kluge			 * because it assumes more than it ought to about index contents;			 * it's reasonably OK for btrees but a bit suspect otherwise.			 */			if (rel->rd_rel->relkind == RELKIND_INDEX &&				relpages > 0)			{				curpages--;				relpages--;			}			/* estimate number of tuples from previous tuple density */			if (relpages > 0)				density = reltuples / (double) relpages;			else			{				/*				 * When we have no data because the relation was truncated,				 * estimate tuple width from attribute datatypes.  We assume				 * here that the pages are completely full, which is OK for				 * tables (since they've presumably not been VACUUMed yet) but				 * is probably an overestimate for indexes.  Fortunately				 * get_relation_info() can clamp the overestimate to the				 * parent table's size.				 *				 * Note: this code intentionally disregards alignment				 * considerations, because (a) that would be gilding the lily				 * considering how crude the estimate is, and (b) it creates				 * platform dependencies in the default plans which are kind				 * of a headache for regression testing.				 */				int32		tuple_width = 0;				int			i;				for (i = 1; i <= RelationGetNumberOfAttributes(rel); i++)				{					Form_pg_attribute att = rel->rd_att->attrs[i - 1];					int32		item_width;					if (att->attisdropped)						continue;					/* This should match set_rel_width() in costsize.c */					item_width = get_attavgwidth(RelationGetRelid(rel), i);					if (item_width <= 0)					{						item_width = get_typavgwidth(att->atttypid,													 att->atttypmod);						Assert(item_width > 0);					}					if (attr_widths != NULL)						attr_widths[i] = item_width;					tuple_width += item_width;				}				tuple_width += sizeof(HeapTupleHeaderData);				tuple_width += sizeof(ItemPointerData);				/* note: integer division is intentional here */				density = (BLCKSZ - sizeof(PageHeaderData)) / tuple_width;			}			*tuples = rint(density * (double) curpages);			break;		case RELKIND_SEQUENCE:			/* Sequences always have a known size */			*pages = 1;			*tuples = 1;			break;		default:			/* else it has no disk storage; probably shouldn't get here? */			*pages = 0;			*tuples = 0;			break;	}}/* * get_relation_constraints * * Retrieve the CHECK constraint expressions of the given relation. * * Returns a List (possibly empty) of constraint expressions.  Each one * has been canonicalized, and its Vars are changed to have the varno * indicated by rel->relid.  This allows the expressions to be easily * compared to expressions taken from WHERE. *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -