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

📄 tupdesc.c

📁 postgresql8.3.4源码,开源数据库
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------- * * tupdesc.c *	  POSTGRES tuple descriptor support code * * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.122 2008/01/01 19:45:46 momjian Exp $ * * NOTES *	  some of the executor utility code such as "ExecTypeFromTL" should be *	  moved here. * *------------------------------------------------------------------------- */#include "postgres.h"#include "catalog/pg_type.h"#include "parser/parse_type.h"#include "utils/builtins.h"#include "utils/resowner.h"#include "utils/syscache.h"/* * CreateTemplateTupleDesc *		This function allocates an empty tuple descriptor structure. * * Tuple type ID information is initially set for an anonymous record type; * caller can overwrite this if needed. */TupleDescCreateTemplateTupleDesc(int natts, bool hasoid){	TupleDesc	desc;	char	   *stg;	int			attroffset;	/*	 * sanity checks	 */	AssertArg(natts >= 0);	/*	 * Allocate enough memory for the tuple descriptor, including the	 * attribute rows, and set up the attribute row pointers.	 *	 * Note: we assume that sizeof(struct tupleDesc) is a multiple of the	 * struct pointer alignment requirement, and hence we don't need to insert	 * alignment padding between the struct and the array of attribute row	 * pointers.	 */	attroffset = sizeof(struct tupleDesc) + natts * sizeof(Form_pg_attribute);	attroffset = MAXALIGN(attroffset);	stg = palloc(attroffset + natts * MAXALIGN(ATTRIBUTE_TUPLE_SIZE));	desc = (TupleDesc) stg;	if (natts > 0)	{		Form_pg_attribute *attrs;		int			i;		attrs = (Form_pg_attribute *) (stg + sizeof(struct tupleDesc));		desc->attrs = attrs;		stg += attroffset;		for (i = 0; i < natts; i++)		{			attrs[i] = (Form_pg_attribute) stg;			stg += MAXALIGN(ATTRIBUTE_TUPLE_SIZE);		}	}	else		desc->attrs = NULL;	/*	 * Initialize other fields of the tupdesc.	 */	desc->natts = natts;	desc->constr = NULL;	desc->tdtypeid = RECORDOID;	desc->tdtypmod = -1;	desc->tdhasoid = hasoid;	desc->tdrefcount = -1;		/* assume not reference-counted */	return desc;}/* * CreateTupleDesc *		This function allocates a new TupleDesc pointing to a given *		Form_pg_attribute array. * * Note: if the TupleDesc is ever freed, the Form_pg_attribute array * will not be freed thereby. * * Tuple type ID information is initially set for an anonymous record type; * caller can overwrite this if needed. */TupleDescCreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs){	TupleDesc	desc;	/*	 * sanity checks	 */	AssertArg(natts >= 0);	desc = (TupleDesc) palloc(sizeof(struct tupleDesc));	desc->attrs = attrs;	desc->natts = natts;	desc->constr = NULL;	desc->tdtypeid = RECORDOID;	desc->tdtypmod = -1;	desc->tdhasoid = hasoid;	desc->tdrefcount = -1;		/* assume not reference-counted */	return desc;}/* * CreateTupleDescCopy *		This function creates a new TupleDesc by copying from an existing *		TupleDesc. * * !!! Constraints and defaults are not copied !!! */TupleDescCreateTupleDescCopy(TupleDesc tupdesc){	TupleDesc	desc;	int			i;	desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);	for (i = 0; i < desc->natts; i++)	{		memcpy(desc->attrs[i], tupdesc->attrs[i], ATTRIBUTE_TUPLE_SIZE);		desc->attrs[i]->attnotnull = false;		desc->attrs[i]->atthasdef = false;	}	desc->tdtypeid = tupdesc->tdtypeid;	desc->tdtypmod = tupdesc->tdtypmod;	return desc;}/* * CreateTupleDescCopyConstr *		This function creates a new TupleDesc by copying from an existing *		TupleDesc (including its constraints and defaults). */TupleDescCreateTupleDescCopyConstr(TupleDesc tupdesc){	TupleDesc	desc;	TupleConstr *constr = tupdesc->constr;	int			i;	desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);	for (i = 0; i < desc->natts; i++)	{		memcpy(desc->attrs[i], tupdesc->attrs[i], ATTRIBUTE_TUPLE_SIZE);	}	if (constr)	{		TupleConstr *cpy = (TupleConstr *) palloc0(sizeof(TupleConstr));		cpy->has_not_null = constr->has_not_null;		if ((cpy->num_defval = constr->num_defval) > 0)		{			cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));			memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));			for (i = cpy->num_defval - 1; i >= 0; i--)			{				if (constr->defval[i].adbin)					cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);			}		}		if ((cpy->num_check = constr->num_check) > 0)		{			cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));			memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));			for (i = cpy->num_check - 1; i >= 0; i--)			{				if (constr->check[i].ccname)					cpy->check[i].ccname = pstrdup(constr->check[i].ccname);				if (constr->check[i].ccbin)					cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);			}		}		desc->constr = cpy;	}	desc->tdtypeid = tupdesc->tdtypeid;	desc->tdtypmod = tupdesc->tdtypmod;	return desc;}/* * Free a TupleDesc including all substructure */voidFreeTupleDesc(TupleDesc tupdesc){	int			i;	/*	 * Possibly this should assert tdrefcount == 0, to disallow explicit	 * freeing of un-refcounted tupdescs?	 */	Assert(tupdesc->tdrefcount <= 0);	if (tupdesc->constr)	{		if (tupdesc->constr->num_defval > 0)		{			AttrDefault *attrdef = tupdesc->constr->defval;			for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)			{				if (attrdef[i].adbin)					pfree(attrdef[i].adbin);			}			pfree(attrdef);		}		if (tupdesc->constr->num_check > 0)		{			ConstrCheck *check = tupdesc->constr->check;			for (i = tupdesc->constr->num_check - 1; i >= 0; i--)			{				if (check[i].ccname)					pfree(check[i].ccname);				if (check[i].ccbin)					pfree(check[i].ccbin);			}			pfree(check);		}		pfree(tupdesc->constr);	}	pfree(tupdesc);}/* * Increment the reference count of a tupdesc, and log the reference in * CurrentResourceOwner. * * Do not apply this to tupdescs that are not being refcounted.  (Use the * macro PinTupleDesc for tupdescs of uncertain status.) */voidIncrTupleDescRefCount(TupleDesc tupdesc){	Assert(tupdesc->tdrefcount >= 0);	ResourceOwnerEnlargeTupleDescs(CurrentResourceOwner);	tupdesc->tdrefcount++;	ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);}/* * Decrement the reference count of a tupdesc, remove the corresponding * reference from CurrentResourceOwner, and free the tupdesc if no more * references remain. * * Do not apply this to tupdescs that are not being refcounted.  (Use the * macro ReleaseTupleDesc for tupdescs of uncertain status.) */voidDecrTupleDescRefCount(TupleDesc tupdesc){	Assert(tupdesc->tdrefcount > 0);	ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);	if (--tupdesc->tdrefcount == 0)		FreeTupleDesc(tupdesc);}/* * Compare two TupleDesc structures for logical equality * * Note: we deliberately do not check the attrelid and tdtypmod fields. * This allows typcache.c to use this routine to see if a cached record type * matches a requested type, and is harmless for relcache.c's uses. * We don't compare tdrefcount, either. */boolequalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2){	int			i,				j,				n;	if (tupdesc1->natts != tupdesc2->natts)		return false;	if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)		return false;	if (tupdesc1->tdhasoid != tupdesc2->tdhasoid)		return false;	for (i = 0; i < tupdesc1->natts; i++)	{		Form_pg_attribute attr1 = tupdesc1->attrs[i];		Form_pg_attribute attr2 = tupdesc2->attrs[i];		/*		 * We do not need to check every single field here: we can disregard		 * attrelid and attnum (which were used to place the row in the attrs		 * array in the first place).  It might look like we could dispense		 * with checking attlen/attbyval/attalign, since these are derived		 * from atttypid; but in the case of dropped columns we must check		 * them (since atttypid will be zero for all dropped columns) and in		 * general it seems safer to check them always.

⌨️ 快捷键说明

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