1996-07-09 02:22:35 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
1999-02-13 18:22:53 -05:00
|
|
|
* lsyscache.c
|
1999-08-08 23:13:31 -04:00
|
|
|
* Convenience routines for common queries in the system catalog cache.
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2017-01-03 13:48:53 -05:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2000-01-26 00:58:53 -05:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/backend/utils/cache/lsyscache.c
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
* NOTES
|
1997-09-07 01:04:48 -04:00
|
|
|
* Eventually, the index information should go through here, too.
|
1996-07-09 02:22:35 -04:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
2003-06-22 18:04:55 -04:00
|
|
|
#include "access/hash.h"
|
2012-08-30 16:15:44 -04:00
|
|
|
#include "access/htup_details.h"
|
2007-01-08 21:14:16 -05:00
|
|
|
#include "access/nbtree.h"
|
2006-08-15 18:36:17 -04:00
|
|
|
#include "bootstrap/bootstrap.h"
|
2015-04-06 10:40:55 -04:00
|
|
|
#include "catalog/namespace.h"
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-17 19:36:59 -05:00
|
|
|
#include "catalog/pg_am.h"
|
2001-08-21 12:36:06 -04:00
|
|
|
#include "catalog/pg_amop.h"
|
2003-06-22 18:04:55 -04:00
|
|
|
#include "catalog/pg_amproc.h"
|
2011-02-08 16:04:18 -05:00
|
|
|
#include "catalog/pg_collation.h"
|
2007-02-13 20:58:58 -05:00
|
|
|
#include "catalog/pg_constraint.h"
|
2015-04-26 10:33:14 -04:00
|
|
|
#include "catalog/pg_language.h"
|
2002-04-01 20:03:07 -05:00
|
|
|
#include "catalog/pg_namespace.h"
|
2001-08-21 12:36:06 -04:00
|
|
|
#include "catalog/pg_opclass.h"
|
1996-10-31 00:58:01 -05:00
|
|
|
#include "catalog/pg_operator.h"
|
1999-08-15 22:06:25 -04:00
|
|
|
#include "catalog/pg_proc.h"
|
2011-11-03 07:16:28 -04:00
|
|
|
#include "catalog/pg_range.h"
|
2001-05-06 20:43:27 -04:00
|
|
|
#include "catalog/pg_statistic.h"
|
2015-04-26 10:33:14 -04:00
|
|
|
#include "catalog/pg_transform.h"
|
1996-07-09 02:22:35 -04:00
|
|
|
#include "catalog/pg_type.h"
|
2006-08-15 18:36:17 -04:00
|
|
|
#include "miscadmin.h"
|
2002-03-20 14:45:13 -05:00
|
|
|
#include "nodes/makefuncs.h"
|
2001-05-06 20:43:27 -04:00
|
|
|
#include "utils/array.h"
|
|
|
|
|
#include "utils/builtins.h"
|
2012-08-28 18:26:24 -04:00
|
|
|
#include "utils/catcache.h"
|
2003-03-23 00:14:37 -05:00
|
|
|
#include "utils/datum.h"
|
2011-06-03 15:38:12 -04:00
|
|
|
#include "utils/fmgroids.h"
|
1999-07-16 01:23:30 -04:00
|
|
|
#include "utils/lsyscache.h"
|
2011-02-23 12:18:09 -05:00
|
|
|
#include "utils/rel.h"
|
1999-07-16 01:23:30 -04:00
|
|
|
#include "utils/syscache.h"
|
2010-10-30 21:55:20 -04:00
|
|
|
#include "utils/typcache.h"
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2008-09-28 15:51:40 -04:00
|
|
|
/* Hook for plugins to get control in get_attavgwidth() */
|
|
|
|
|
get_attavgwidth_hook_type get_attavgwidth_hook = NULL;
|
|
|
|
|
|
2001-06-13 21:09:22 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/* ---------- AMOP CACHES ---------- */
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2006-12-22 19:43:13 -05:00
|
|
|
* op_in_opfamily
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
2006-12-22 19:43:13 -05:00
|
|
|
* Return t iff operator 'opno' is in operator family 'opfamily'.
|
2010-11-24 14:20:39 -05:00
|
|
|
*
|
|
|
|
|
* This function only considers search operators, not ordering operators.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
bool
|
2006-12-22 19:43:13 -05:00
|
|
|
op_in_opfamily(Oid opno, Oid opfamily)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
2010-11-24 14:20:39 -05:00
|
|
|
return SearchSysCacheExists3(AMOPOPID,
|
2010-02-14 13:42:19 -05:00
|
|
|
ObjectIdGetDatum(opno),
|
2010-11-24 14:20:39 -05:00
|
|
|
CharGetDatum(AMOP_SEARCH),
|
2010-02-14 13:42:19 -05:00
|
|
|
ObjectIdGetDatum(opfamily));
|
2001-08-21 12:36:06 -04:00
|
|
|
}
|
|
|
|
|
|
2005-04-11 19:06:57 -04:00
|
|
|
/*
|
2006-12-22 19:43:13 -05:00
|
|
|
* get_op_opfamily_strategy
|
2005-04-11 19:06:57 -04:00
|
|
|
*
|
2006-12-22 19:43:13 -05:00
|
|
|
* Get the operator's strategy number within the specified opfamily,
|
|
|
|
|
* or 0 if it's not a member of the opfamily.
|
2010-11-24 14:20:39 -05:00
|
|
|
*
|
|
|
|
|
* This function only considers search operators, not ordering operators.
|
2005-04-11 19:06:57 -04:00
|
|
|
*/
|
|
|
|
|
int
|
2006-12-22 19:43:13 -05:00
|
|
|
get_op_opfamily_strategy(Oid opno, Oid opfamily)
|
2005-04-11 19:06:57 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_amop amop_tup;
|
|
|
|
|
int result;
|
|
|
|
|
|
2010-11-24 14:20:39 -05:00
|
|
|
tp = SearchSysCache3(AMOPOPID,
|
2010-02-14 13:42:19 -05:00
|
|
|
ObjectIdGetDatum(opno),
|
2010-11-24 14:20:39 -05:00
|
|
|
CharGetDatum(AMOP_SEARCH),
|
2010-02-14 13:42:19 -05:00
|
|
|
ObjectIdGetDatum(opfamily));
|
2005-04-11 19:06:57 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
return 0;
|
|
|
|
|
amop_tup = (Form_pg_amop) GETSTRUCT(tp);
|
|
|
|
|
result = amop_tup->amopstrategy;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-02 20:50:48 -05:00
|
|
|
/*
|
|
|
|
|
* get_op_opfamily_sortfamily
|
|
|
|
|
*
|
|
|
|
|
* If the operator is an ordering operator within the specified opfamily,
|
|
|
|
|
* return its amopsortfamily OID; else return InvalidOid.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_op_opfamily_sortfamily(Oid opno, Oid opfamily)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_amop amop_tup;
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache3(AMOPOPID,
|
|
|
|
|
ObjectIdGetDatum(opno),
|
|
|
|
|
CharGetDatum(AMOP_ORDER),
|
|
|
|
|
ObjectIdGetDatum(opfamily));
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
amop_tup = (Form_pg_amop) GETSTRUCT(tp);
|
|
|
|
|
result = amop_tup->amopsortfamily;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2001-08-21 12:36:06 -04:00
|
|
|
/*
|
2006-12-22 19:43:13 -05:00
|
|
|
* get_op_opfamily_properties
|
2001-08-21 12:36:06 -04:00
|
|
|
*
|
2008-04-13 16:51:21 -04:00
|
|
|
* Get the operator's strategy number and declared input data types
|
|
|
|
|
* within the specified opfamily.
|
2001-08-21 12:36:06 -04:00
|
|
|
*
|
2006-12-22 19:43:13 -05:00
|
|
|
* Caller should already have verified that opno is a member of opfamily,
|
2001-08-21 12:36:06 -04:00
|
|
|
* therefore we raise an error if the tuple is not found.
|
|
|
|
|
*/
|
2003-11-09 16:30:38 -05:00
|
|
|
void
|
2010-12-02 20:50:48 -05:00
|
|
|
get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op,
|
2006-12-22 19:43:13 -05:00
|
|
|
int *strategy,
|
|
|
|
|
Oid *lefttype,
|
2008-04-13 16:51:21 -04:00
|
|
|
Oid *righttype)
|
2001-08-21 12:36:06 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_amop amop_tup;
|
|
|
|
|
|
2010-11-24 14:20:39 -05:00
|
|
|
tp = SearchSysCache3(AMOPOPID,
|
2010-02-14 13:42:19 -05:00
|
|
|
ObjectIdGetDatum(opno),
|
2010-12-02 20:50:48 -05:00
|
|
|
CharGetDatum(ordering_op ? AMOP_ORDER : AMOP_SEARCH),
|
2010-02-14 13:42:19 -05:00
|
|
|
ObjectIdGetDatum(opfamily));
|
2001-08-21 12:36:06 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
2006-12-22 19:43:13 -05:00
|
|
|
elog(ERROR, "operator %u is not a member of opfamily %u",
|
|
|
|
|
opno, opfamily);
|
2001-08-21 12:36:06 -04:00
|
|
|
amop_tup = (Form_pg_amop) GETSTRUCT(tp);
|
2003-11-09 16:30:38 -05:00
|
|
|
*strategy = amop_tup->amopstrategy;
|
2006-12-22 19:43:13 -05:00
|
|
|
*lefttype = amop_tup->amoplefttype;
|
|
|
|
|
*righttype = amop_tup->amoprighttype;
|
2001-08-21 12:36:06 -04:00
|
|
|
ReleaseSysCache(tp);
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
2003-05-25 20:11:29 -04:00
|
|
|
/*
|
2006-12-22 19:43:13 -05:00
|
|
|
* get_opfamily_member
|
2003-05-25 20:11:29 -04:00
|
|
|
* Get the OID of the operator that implements the specified strategy
|
2006-12-22 19:43:13 -05:00
|
|
|
* with the specified datatypes for the specified opfamily.
|
2003-05-25 20:11:29 -04:00
|
|
|
*
|
|
|
|
|
* Returns InvalidOid if there is no pg_amop entry for the given keys.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
2006-12-22 19:43:13 -05:00
|
|
|
get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype,
|
|
|
|
|
int16 strategy)
|
2003-05-25 20:11:29 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_amop amop_tup;
|
|
|
|
|
Oid result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache4(AMOPSTRATEGY,
|
|
|
|
|
ObjectIdGetDatum(opfamily),
|
|
|
|
|
ObjectIdGetDatum(lefttype),
|
|
|
|
|
ObjectIdGetDatum(righttype),
|
|
|
|
|
Int16GetDatum(strategy));
|
2003-05-25 20:11:29 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
amop_tup = (Form_pg_amop) GETSTRUCT(tp);
|
|
|
|
|
result = amop_tup->amopopr;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-08 21:14:16 -05:00
|
|
|
/*
|
2007-01-20 19:57:15 -05:00
|
|
|
* get_ordering_op_properties
|
|
|
|
|
* Given the OID of an ordering operator (a btree "<" or ">" operator),
|
|
|
|
|
* determine its opfamily, its declared input datatype, and its
|
|
|
|
|
* strategy number (BTLessStrategyNumber or BTGreaterStrategyNumber).
|
2007-01-08 21:14:16 -05:00
|
|
|
*
|
2007-01-20 19:57:15 -05:00
|
|
|
* Returns TRUE if successful, FALSE if no matching pg_amop entry exists.
|
2007-01-08 21:14:16 -05:00
|
|
|
* (This indicates that the operator is not a valid ordering operator.)
|
2007-01-20 19:57:15 -05:00
|
|
|
*
|
|
|
|
|
* Note: the operator could be registered in multiple families, for example
|
2014-05-06 12:12:18 -04:00
|
|
|
* if someone were to build a "reverse sort" opfamily. This would result in
|
2007-01-20 19:57:15 -05:00
|
|
|
* uncertainty as to whether "ORDER BY USING op" would default to NULLS FIRST
|
|
|
|
|
* or NULLS LAST, as well as inefficient planning due to failure to match up
|
|
|
|
|
* pathkeys that should be the same. So we want a determinate result here.
|
|
|
|
|
* Because of the way the syscache search works, we'll use the interpretation
|
|
|
|
|
* associated with the opfamily with smallest OID, which is probably
|
2014-05-06 12:12:18 -04:00
|
|
|
* determinate enough. Since there is no longer any particularly good reason
|
2007-01-20 19:57:15 -05:00
|
|
|
* to build reverse-sort opfamilies, it doesn't seem worth expending any
|
|
|
|
|
* additional effort on ensuring consistency.
|
2007-01-08 21:14:16 -05:00
|
|
|
*/
|
|
|
|
|
bool
|
2007-01-20 19:57:15 -05:00
|
|
|
get_ordering_op_properties(Oid opno,
|
|
|
|
|
Oid *opfamily, Oid *opcintype, int16 *strategy)
|
2007-01-08 21:14:16 -05:00
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
CatCList *catlist;
|
|
|
|
|
int i;
|
|
|
|
|
|
2007-01-20 19:57:15 -05:00
|
|
|
/* ensure outputs are initialized on failure */
|
|
|
|
|
*opfamily = InvalidOid;
|
|
|
|
|
*opcintype = InvalidOid;
|
|
|
|
|
*strategy = 0;
|
2007-01-08 21:14:16 -05:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Search pg_amop to see if the target operator is registered as the "<"
|
2007-01-20 19:57:15 -05:00
|
|
|
* or ">" operator of any btree opfamily.
|
2007-01-08 21:14:16 -05:00
|
|
|
*/
|
2010-02-14 13:42:19 -05:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
|
2007-01-08 21:14:16 -05:00
|
|
|
|
|
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
|
|
|
|
|
|
|
|
|
|
/* must be btree */
|
|
|
|
|
if (aform->amopmethod != BTREE_AM_OID)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (aform->amopstrategy == BTLessStrategyNumber ||
|
|
|
|
|
aform->amopstrategy == BTGreaterStrategyNumber)
|
|
|
|
|
{
|
2007-01-20 19:57:15 -05:00
|
|
|
/* Found it ... should have consistent input types */
|
|
|
|
|
if (aform->amoplefttype == aform->amoprighttype)
|
|
|
|
|
{
|
|
|
|
|
/* Found a suitable opfamily, return info */
|
|
|
|
|
*opfamily = aform->amopfamily;
|
|
|
|
|
*opcintype = aform->amoplefttype;
|
|
|
|
|
*strategy = aform->amopstrategy;
|
|
|
|
|
result = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-01-08 21:14:16 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-10 13:06:05 -05:00
|
|
|
/*
|
|
|
|
|
* get_equality_op_for_ordering_op
|
|
|
|
|
* Get the OID of the datatype-specific btree equality operator
|
|
|
|
|
* associated with an ordering operator (a "<" or ">" operator).
|
|
|
|
|
*
|
2008-08-02 17:32:01 -04:00
|
|
|
* If "reverse" isn't NULL, also set *reverse to FALSE if the operator is "<",
|
|
|
|
|
* TRUE if it's ">"
|
|
|
|
|
*
|
2007-01-10 13:06:05 -05:00
|
|
|
* Returns InvalidOid if no matching equality operator can be found.
|
|
|
|
|
* (This indicates that the operator is not a valid ordering operator.)
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
2008-08-02 17:32:01 -04:00
|
|
|
get_equality_op_for_ordering_op(Oid opno, bool *reverse)
|
2007-01-10 13:06:05 -05:00
|
|
|
{
|
|
|
|
|
Oid result = InvalidOid;
|
2007-01-20 19:57:15 -05:00
|
|
|
Oid opfamily;
|
|
|
|
|
Oid opcintype;
|
|
|
|
|
int16 strategy;
|
2007-01-10 13:06:05 -05:00
|
|
|
|
2007-01-20 19:57:15 -05:00
|
|
|
/* Find the operator in pg_amop */
|
|
|
|
|
if (get_ordering_op_properties(opno,
|
|
|
|
|
&opfamily, &opcintype, &strategy))
|
2007-01-10 13:06:05 -05:00
|
|
|
{
|
2007-01-20 19:57:15 -05:00
|
|
|
/* Found a suitable opfamily, get matching equality operator */
|
|
|
|
|
result = get_opfamily_member(opfamily,
|
|
|
|
|
opcintype,
|
|
|
|
|
opcintype,
|
|
|
|
|
BTEqualStrategyNumber);
|
2008-08-02 17:32:01 -04:00
|
|
|
if (reverse)
|
|
|
|
|
*reverse = (strategy == BTGreaterStrategyNumber);
|
2007-01-10 13:06:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get_ordering_op_for_equality_op
|
|
|
|
|
* Get the OID of a datatype-specific btree ordering operator
|
|
|
|
|
* associated with an equality operator. (If there are multiple
|
|
|
|
|
* possibilities, assume any one will do.)
|
|
|
|
|
*
|
|
|
|
|
* This function is used when we have to sort data before unique-ifying,
|
|
|
|
|
* and don't much care which sorting op is used as long as it's compatible
|
|
|
|
|
* with the intended equality operator. Since we need a sorting operator,
|
|
|
|
|
* it should be single-data-type even if the given operator is cross-type.
|
|
|
|
|
* The caller specifies whether to find an op for the LHS or RHS data type.
|
|
|
|
|
*
|
|
|
|
|
* Returns InvalidOid if no matching ordering operator can be found.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_ordering_op_for_equality_op(Oid opno, bool use_lhs_type)
|
|
|
|
|
{
|
|
|
|
|
Oid result = InvalidOid;
|
|
|
|
|
CatCList *catlist;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Search pg_amop to see if the target operator is registered as the "="
|
|
|
|
|
* operator of any btree opfamily.
|
|
|
|
|
*/
|
2010-02-14 13:42:19 -05:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
|
2007-01-10 13:06:05 -05:00
|
|
|
|
|
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
|
|
|
|
|
|
|
|
|
|
/* must be btree */
|
|
|
|
|
if (aform->amopmethod != BTREE_AM_OID)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (aform->amopstrategy == BTEqualStrategyNumber)
|
|
|
|
|
{
|
|
|
|
|
/* Found a suitable opfamily, get matching ordering operator */
|
2007-11-15 16:14:46 -05:00
|
|
|
Oid typid;
|
2007-01-10 13:06:05 -05:00
|
|
|
|
|
|
|
|
typid = use_lhs_type ? aform->amoplefttype : aform->amoprighttype;
|
|
|
|
|
result = get_opfamily_member(aform->amopfamily,
|
|
|
|
|
typid, typid,
|
|
|
|
|
BTLessStrategyNumber);
|
|
|
|
|
if (OidIsValid(result))
|
|
|
|
|
break;
|
|
|
|
|
/* failure probably shouldn't happen, but keep looking if so */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-20 15:45:41 -05:00
|
|
|
/*
|
|
|
|
|
* get_mergejoin_opfamilies
|
|
|
|
|
* Given a putatively mergejoinable operator, return a list of the OIDs
|
|
|
|
|
* of the btree opfamilies in which it represents equality.
|
|
|
|
|
*
|
|
|
|
|
* It is possible (though at present unusual) for an operator to be equality
|
|
|
|
|
* in more than one opfamily, hence the result is a list. This also lets us
|
|
|
|
|
* return NIL if the operator is not found in any opfamilies.
|
|
|
|
|
*
|
|
|
|
|
* The planner currently uses simple equal() tests to compare the lists
|
|
|
|
|
* returned by this function, which makes the list order relevant, though
|
2014-05-06 12:12:18 -04:00
|
|
|
* strictly speaking it should not be. Because of the way syscache list
|
2007-01-20 15:45:41 -05:00
|
|
|
* searches are handled, in normal operation the result will be sorted by OID
|
|
|
|
|
* so everything works fine. If running with system index usage disabled,
|
|
|
|
|
* the result ordering is unspecified and hence the planner might fail to
|
|
|
|
|
* recognize optimization opportunities ... but that's hardly a scenario in
|
|
|
|
|
* which performance is good anyway, so there's no point in expending code
|
|
|
|
|
* or cycles here to guarantee the ordering in that case.
|
|
|
|
|
*/
|
|
|
|
|
List *
|
|
|
|
|
get_mergejoin_opfamilies(Oid opno)
|
|
|
|
|
{
|
|
|
|
|
List *result = NIL;
|
|
|
|
|
CatCList *catlist;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Search pg_amop to see if the target operator is registered as the "="
|
|
|
|
|
* operator of any btree opfamily.
|
|
|
|
|
*/
|
2010-02-14 13:42:19 -05:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
|
2007-01-20 15:45:41 -05:00
|
|
|
|
|
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
|
|
|
|
|
|
|
|
|
|
/* must be btree equality */
|
|
|
|
|
if (aform->amopmethod == BTREE_AM_OID &&
|
|
|
|
|
aform->amopstrategy == BTEqualStrategyNumber)
|
|
|
|
|
result = lappend_oid(result, aform->amopfamily);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-10 13:06:05 -05:00
|
|
|
/*
|
2007-01-29 20:33:36 -05:00
|
|
|
* get_compatible_hash_operators
|
|
|
|
|
* Get the OID(s) of hash equality operator(s) compatible with the given
|
|
|
|
|
* operator, but operating on its LHS and/or RHS datatype.
|
2007-01-10 13:06:05 -05:00
|
|
|
*
|
2007-01-29 20:33:36 -05:00
|
|
|
* An operator for the LHS type is sought and returned into *lhs_opno if
|
|
|
|
|
* lhs_opno isn't NULL. Similarly, an operator for the RHS type is sought
|
|
|
|
|
* and returned into *rhs_opno if rhs_opno isn't NULL.
|
2007-01-10 13:06:05 -05:00
|
|
|
*
|
2007-01-29 20:33:36 -05:00
|
|
|
* If the given operator is not cross-type, the results should be the same
|
|
|
|
|
* operator, but in cross-type situations they will be different.
|
|
|
|
|
*
|
|
|
|
|
* Returns true if able to find the requested operator(s), false if not.
|
|
|
|
|
* (This indicates that the operator should not have been marked oprcanhash.)
|
2007-01-10 13:06:05 -05:00
|
|
|
*/
|
2007-01-29 20:33:36 -05:00
|
|
|
bool
|
|
|
|
|
get_compatible_hash_operators(Oid opno,
|
|
|
|
|
Oid *lhs_opno, Oid *rhs_opno)
|
2007-01-10 13:06:05 -05:00
|
|
|
{
|
2007-01-29 20:33:36 -05:00
|
|
|
bool result = false;
|
2007-01-10 13:06:05 -05:00
|
|
|
CatCList *catlist;
|
|
|
|
|
int i;
|
|
|
|
|
|
2007-01-29 20:33:36 -05:00
|
|
|
/* Ensure output args are initialized on failure */
|
|
|
|
|
if (lhs_opno)
|
|
|
|
|
*lhs_opno = InvalidOid;
|
|
|
|
|
if (rhs_opno)
|
|
|
|
|
*rhs_opno = InvalidOid;
|
|
|
|
|
|
2007-01-10 13:06:05 -05:00
|
|
|
/*
|
|
|
|
|
* Search pg_amop to see if the target operator is registered as the "="
|
|
|
|
|
* operator of any hash opfamily. If the operator is registered in
|
|
|
|
|
* multiple opfamilies, assume we can use any one.
|
|
|
|
|
*/
|
2010-02-14 13:42:19 -05:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
|
2007-01-10 13:06:05 -05:00
|
|
|
|
|
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
|
|
|
|
|
|
|
|
|
|
if (aform->amopmethod == HASH_AM_OID &&
|
|
|
|
|
aform->amopstrategy == HTEqualStrategyNumber)
|
|
|
|
|
{
|
|
|
|
|
/* No extra lookup needed if given operator is single-type */
|
|
|
|
|
if (aform->amoplefttype == aform->amoprighttype)
|
|
|
|
|
{
|
2007-01-29 20:33:36 -05:00
|
|
|
if (lhs_opno)
|
|
|
|
|
*lhs_opno = opno;
|
|
|
|
|
if (rhs_opno)
|
|
|
|
|
*rhs_opno = opno;
|
|
|
|
|
result = true;
|
2007-01-10 13:06:05 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2007-11-15 16:14:46 -05:00
|
|
|
|
2007-01-29 20:33:36 -05:00
|
|
|
/*
|
|
|
|
|
* Get the matching single-type operator(s). Failure probably
|
|
|
|
|
* shouldn't happen --- it implies a bogus opfamily --- but
|
|
|
|
|
* continue looking if so.
|
|
|
|
|
*/
|
|
|
|
|
if (lhs_opno)
|
|
|
|
|
{
|
|
|
|
|
*lhs_opno = get_opfamily_member(aform->amopfamily,
|
|
|
|
|
aform->amoplefttype,
|
|
|
|
|
aform->amoplefttype,
|
|
|
|
|
HTEqualStrategyNumber);
|
|
|
|
|
if (!OidIsValid(*lhs_opno))
|
|
|
|
|
continue;
|
|
|
|
|
/* Matching LHS found, done if caller doesn't want RHS */
|
|
|
|
|
if (!rhs_opno)
|
|
|
|
|
{
|
|
|
|
|
result = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (rhs_opno)
|
|
|
|
|
{
|
|
|
|
|
*rhs_opno = get_opfamily_member(aform->amopfamily,
|
|
|
|
|
aform->amoprighttype,
|
|
|
|
|
aform->amoprighttype,
|
|
|
|
|
HTEqualStrategyNumber);
|
|
|
|
|
if (!OidIsValid(*rhs_opno))
|
|
|
|
|
{
|
|
|
|
|
/* Forget any LHS operator from this opfamily */
|
|
|
|
|
if (lhs_opno)
|
|
|
|
|
*lhs_opno = InvalidOid;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* Matching RHS found, so done */
|
|
|
|
|
result = true;
|
2007-01-10 13:06:05 -05:00
|
|
|
break;
|
2007-01-29 20:33:36 -05:00
|
|
|
}
|
2007-01-10 13:06:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-22 18:04:55 -04:00
|
|
|
/*
|
2007-01-29 20:33:36 -05:00
|
|
|
* get_op_hash_functions
|
2017-08-31 22:21:21 -04:00
|
|
|
* Get the OID(s) of the standard hash support function(s) compatible with
|
|
|
|
|
* the given operator, operating on its LHS and/or RHS datatype as required.
|
2007-01-29 20:33:36 -05:00
|
|
|
*
|
|
|
|
|
* A function for the LHS type is sought and returned into *lhs_procno if
|
|
|
|
|
* lhs_procno isn't NULL. Similarly, a function for the RHS type is sought
|
|
|
|
|
* and returned into *rhs_procno if rhs_procno isn't NULL.
|
2003-06-22 18:04:55 -04:00
|
|
|
*
|
2007-01-29 20:33:36 -05:00
|
|
|
* If the given operator is not cross-type, the results should be the same
|
|
|
|
|
* function, but in cross-type situations they will be different.
|
2006-12-22 19:43:13 -05:00
|
|
|
*
|
2007-01-29 20:33:36 -05:00
|
|
|
* Returns true if able to find the requested function(s), false if not.
|
|
|
|
|
* (This indicates that the operator should not have been marked oprcanhash.)
|
2003-06-22 18:04:55 -04:00
|
|
|
*/
|
2007-01-29 20:33:36 -05:00
|
|
|
bool
|
|
|
|
|
get_op_hash_functions(Oid opno,
|
|
|
|
|
RegProcedure *lhs_procno, RegProcedure *rhs_procno)
|
2003-06-22 18:04:55 -04:00
|
|
|
{
|
2007-01-29 20:33:36 -05:00
|
|
|
bool result = false;
|
2003-06-22 18:04:55 -04:00
|
|
|
CatCList *catlist;
|
|
|
|
|
int i;
|
|
|
|
|
|
2007-01-29 20:33:36 -05:00
|
|
|
/* Ensure output args are initialized on failure */
|
|
|
|
|
if (lhs_procno)
|
|
|
|
|
*lhs_procno = InvalidOid;
|
|
|
|
|
if (rhs_procno)
|
|
|
|
|
*rhs_procno = InvalidOid;
|
|
|
|
|
|
2003-06-22 18:04:55 -04:00
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* Search pg_amop to see if the target operator is registered as the "="
|
2006-12-22 19:43:13 -05:00
|
|
|
* operator of any hash opfamily. If the operator is registered in
|
2007-01-29 20:33:36 -05:00
|
|
|
* multiple opfamilies, assume we can use any one.
|
2003-06-22 18:04:55 -04:00
|
|
|
*/
|
2010-02-14 13:42:19 -05:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
|
2003-06-22 18:04:55 -04:00
|
|
|
|
|
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
2003-08-17 15:58:06 -04:00
|
|
|
HeapTuple tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
|
2003-06-22 18:04:55 -04:00
|
|
|
|
2006-12-22 19:43:13 -05:00
|
|
|
if (aform->amopmethod == HASH_AM_OID &&
|
|
|
|
|
aform->amopstrategy == HTEqualStrategyNumber)
|
2003-06-22 18:04:55 -04:00
|
|
|
{
|
2007-01-29 20:33:36 -05:00
|
|
|
/*
|
|
|
|
|
* Get the matching support function(s). Failure probably
|
|
|
|
|
* shouldn't happen --- it implies a bogus opfamily --- but
|
|
|
|
|
* continue looking if so.
|
|
|
|
|
*/
|
|
|
|
|
if (lhs_procno)
|
|
|
|
|
{
|
|
|
|
|
*lhs_procno = get_opfamily_proc(aform->amopfamily,
|
|
|
|
|
aform->amoplefttype,
|
|
|
|
|
aform->amoplefttype,
|
2017-08-31 22:21:21 -04:00
|
|
|
HASHSTANDARD_PROC);
|
2007-01-29 20:33:36 -05:00
|
|
|
if (!OidIsValid(*lhs_procno))
|
|
|
|
|
continue;
|
|
|
|
|
/* Matching LHS found, done if caller doesn't want RHS */
|
|
|
|
|
if (!rhs_procno)
|
|
|
|
|
{
|
|
|
|
|
result = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Only one lookup needed if given operator is single-type */
|
|
|
|
|
if (aform->amoplefttype == aform->amoprighttype)
|
|
|
|
|
{
|
|
|
|
|
*rhs_procno = *lhs_procno;
|
|
|
|
|
result = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (rhs_procno)
|
|
|
|
|
{
|
|
|
|
|
*rhs_procno = get_opfamily_proc(aform->amopfamily,
|
|
|
|
|
aform->amoprighttype,
|
|
|
|
|
aform->amoprighttype,
|
2017-08-31 22:21:21 -04:00
|
|
|
HASHSTANDARD_PROC);
|
2007-01-29 20:33:36 -05:00
|
|
|
if (!OidIsValid(*rhs_procno))
|
|
|
|
|
{
|
|
|
|
|
/* Forget any LHS function from this opfamily */
|
|
|
|
|
if (lhs_procno)
|
|
|
|
|
*lhs_procno = InvalidOid;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* Matching RHS found, so done */
|
|
|
|
|
result = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-06-22 18:04:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
|
2006-12-22 19:43:13 -05:00
|
|
|
return result;
|
2003-06-22 18:04:55 -04:00
|
|
|
}
|
|
|
|
|
|
2005-12-27 20:30:02 -05:00
|
|
|
/*
|
|
|
|
|
* get_op_btree_interpretation
|
2006-12-22 19:43:13 -05:00
|
|
|
* Given an operator's OID, find out which btree opfamilies it belongs to,
|
2011-07-06 14:53:16 -04:00
|
|
|
* and what properties it has within each one. The results are returned
|
|
|
|
|
* as a palloc'd list of OpBtreeInterpretation structs.
|
2005-12-27 20:30:02 -05:00
|
|
|
*
|
|
|
|
|
* In addition to the normal btree operators, we consider a <> operator to be
|
2006-12-22 19:43:13 -05:00
|
|
|
* a "member" of an opfamily if its negator is an equality operator of the
|
|
|
|
|
* opfamily. ROWCOMPARE_NE is returned as the strategy number for this case.
|
2005-12-27 20:30:02 -05:00
|
|
|
*/
|
2011-07-06 14:53:16 -04:00
|
|
|
List *
|
|
|
|
|
get_op_btree_interpretation(Oid opno)
|
2005-12-27 20:30:02 -05:00
|
|
|
{
|
2011-07-06 14:53:16 -04:00
|
|
|
List *result = NIL;
|
|
|
|
|
OpBtreeInterpretation *thisresult;
|
2005-12-27 20:30:02 -05:00
|
|
|
CatCList *catlist;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Find all the pg_amop entries containing the operator.
|
|
|
|
|
*/
|
2010-02-14 13:42:19 -05:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
|
2006-10-03 20:30:14 -04:00
|
|
|
|
2005-12-27 20:30:02 -05:00
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple op_tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
|
|
|
|
|
StrategyNumber op_strategy;
|
|
|
|
|
|
|
|
|
|
/* must be btree */
|
2006-12-22 19:43:13 -05:00
|
|
|
if (op_form->amopmethod != BTREE_AM_OID)
|
2005-12-27 20:30:02 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* Get the operator's btree strategy number */
|
|
|
|
|
op_strategy = (StrategyNumber) op_form->amopstrategy;
|
|
|
|
|
Assert(op_strategy >= 1 && op_strategy <= 5);
|
|
|
|
|
|
2011-07-06 14:53:16 -04:00
|
|
|
thisresult = (OpBtreeInterpretation *)
|
|
|
|
|
palloc(sizeof(OpBtreeInterpretation));
|
|
|
|
|
thisresult->opfamily_id = op_form->amopfamily;
|
|
|
|
|
thisresult->strategy = op_strategy;
|
|
|
|
|
thisresult->oplefttype = op_form->amoplefttype;
|
|
|
|
|
thisresult->oprighttype = op_form->amoprighttype;
|
|
|
|
|
result = lappend(result, thisresult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If we didn't find any btree opfamily containing the operator, perhaps
|
|
|
|
|
* it is a <> operator. See if it has a negator that is in an opfamily.
|
|
|
|
|
*/
|
|
|
|
|
if (result == NIL)
|
|
|
|
|
{
|
|
|
|
|
Oid op_negator = get_negator(opno);
|
|
|
|
|
|
|
|
|
|
if (OidIsValid(op_negator))
|
2005-12-27 20:30:02 -05:00
|
|
|
{
|
2011-07-06 14:53:16 -04:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID,
|
|
|
|
|
ObjectIdGetDatum(op_negator));
|
2005-12-27 20:30:02 -05:00
|
|
|
|
2011-07-06 14:53:16 -04:00
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple op_tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
|
|
|
|
|
StrategyNumber op_strategy;
|
|
|
|
|
|
|
|
|
|
/* must be btree */
|
|
|
|
|
if (op_form->amopmethod != BTREE_AM_OID)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* Get the operator's btree strategy number */
|
|
|
|
|
op_strategy = (StrategyNumber) op_form->amopstrategy;
|
|
|
|
|
Assert(op_strategy >= 1 && op_strategy <= 5);
|
|
|
|
|
|
|
|
|
|
/* Only consider negators that are = */
|
|
|
|
|
if (op_strategy != BTEqualStrategyNumber)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* OK, report it with "strategy" ROWCOMPARE_NE */
|
|
|
|
|
thisresult = (OpBtreeInterpretation *)
|
|
|
|
|
palloc(sizeof(OpBtreeInterpretation));
|
|
|
|
|
thisresult->opfamily_id = op_form->amopfamily;
|
|
|
|
|
thisresult->strategy = ROWCOMPARE_NE;
|
|
|
|
|
thisresult->oplefttype = op_form->amoplefttype;
|
|
|
|
|
thisresult->oprighttype = op_form->amoprighttype;
|
|
|
|
|
result = lappend(result, thisresult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
}
|
2005-12-27 20:30:02 -05:00
|
|
|
}
|
|
|
|
|
|
2011-07-06 14:53:16 -04:00
|
|
|
return result;
|
2005-12-27 20:30:02 -05:00
|
|
|
}
|
|
|
|
|
|
2007-01-10 13:06:05 -05:00
|
|
|
/*
|
2008-08-02 17:32:01 -04:00
|
|
|
* equality_ops_are_compatible
|
|
|
|
|
* Return TRUE if the two given equality operators have compatible
|
|
|
|
|
* semantics.
|
|
|
|
|
*
|
|
|
|
|
* This is trivially true if they are the same operator. Otherwise,
|
|
|
|
|
* we look to see if they can be found in the same btree or hash opfamily.
|
|
|
|
|
* Either finding allows us to assume that they have compatible notions
|
|
|
|
|
* of equality. (The reason we need to do these pushups is that one might
|
|
|
|
|
* be a cross-type operator; for instance int24eq vs int4eq.)
|
2007-01-10 13:06:05 -05:00
|
|
|
*/
|
|
|
|
|
bool
|
2008-08-02 17:32:01 -04:00
|
|
|
equality_ops_are_compatible(Oid opno1, Oid opno2)
|
2007-01-10 13:06:05 -05:00
|
|
|
{
|
2008-08-02 17:32:01 -04:00
|
|
|
bool result;
|
2007-01-10 13:06:05 -05:00
|
|
|
CatCList *catlist;
|
|
|
|
|
int i;
|
|
|
|
|
|
2008-08-02 17:32:01 -04:00
|
|
|
/* Easy if they're the same operator */
|
|
|
|
|
if (opno1 == opno2)
|
|
|
|
|
return true;
|
|
|
|
|
|
2007-01-10 13:06:05 -05:00
|
|
|
/*
|
|
|
|
|
* We search through all the pg_amop entries for opno1.
|
|
|
|
|
*/
|
2010-02-14 13:42:19 -05:00
|
|
|
catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno1));
|
2008-08-02 17:32:01 -04:00
|
|
|
|
|
|
|
|
result = false;
|
2007-01-10 13:06:05 -05:00
|
|
|
for (i = 0; i < catlist->n_members; i++)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple op_tuple = &catlist->members[i]->tuple;
|
|
|
|
|
Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
|
|
|
|
|
|
2008-08-02 17:32:01 -04:00
|
|
|
/* must be btree or hash */
|
|
|
|
|
if (op_form->amopmethod == BTREE_AM_OID ||
|
|
|
|
|
op_form->amopmethod == HASH_AM_OID)
|
2007-01-10 13:06:05 -05:00
|
|
|
{
|
2008-08-02 17:32:01 -04:00
|
|
|
if (op_in_opfamily(opno2, op_form->amopfamily))
|
|
|
|
|
{
|
|
|
|
|
result = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-01-10 13:06:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReleaseSysCacheList(catlist);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-25 20:11:29 -04:00
|
|
|
|
2003-08-17 15:58:06 -04:00
|
|
|
/* ---------- AMPROC CACHES ---------- */
|
|
|
|
|
|
|
|
|
|
/*
|
2006-12-22 19:43:13 -05:00
|
|
|
* get_opfamily_proc
|
2003-08-17 15:58:06 -04:00
|
|
|
* Get the OID of the specified support function
|
2006-12-22 19:43:13 -05:00
|
|
|
* for the specified opfamily and datatypes.
|
2003-08-17 15:58:06 -04:00
|
|
|
*
|
|
|
|
|
* Returns InvalidOid if there is no pg_amproc entry for the given keys.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
2006-12-22 19:43:13 -05:00
|
|
|
get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
|
2003-08-17 15:58:06 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_amproc amproc_tup;
|
|
|
|
|
RegProcedure result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache4(AMPROCNUM,
|
|
|
|
|
ObjectIdGetDatum(opfamily),
|
|
|
|
|
ObjectIdGetDatum(lefttype),
|
|
|
|
|
ObjectIdGetDatum(righttype),
|
|
|
|
|
Int16GetDatum(procnum));
|
2003-08-17 15:58:06 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
amproc_tup = (Form_pg_amproc) GETSTRUCT(tp);
|
|
|
|
|
result = amproc_tup->amproc;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/* ---------- ATTRIBUTE CACHES ---------- */
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_attname
|
1997-09-07 01:04:48 -04:00
|
|
|
* Given the relation id and the attribute number,
|
|
|
|
|
* return the "attname" field from the attribute relation.
|
2000-11-16 17:30:52 -05:00
|
|
|
*
|
2003-08-11 19:04:50 -04:00
|
|
|
* Note: returns a palloc'd copy of the string, or NULL if no such attribute.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
1998-02-25 23:46:47 -05:00
|
|
|
char *
|
1996-07-09 02:22:35 -04:00
|
|
|
get_attname(Oid relid, AttrNumber attnum)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache2(ATTNUM,
|
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
|
Int16GetDatum(attnum));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
char *result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = pstrdup(NameStr(att_tup->attname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1998-01-20 00:05:08 -05:00
|
|
|
return NULL;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
2003-08-11 19:04:50 -04:00
|
|
|
/*
|
|
|
|
|
* get_relid_attribute_name
|
|
|
|
|
*
|
|
|
|
|
* Same as above routine get_attname(), except that error
|
|
|
|
|
* is handled by elog() instead of returning NULL.
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
get_relid_attribute_name(Oid relid, AttrNumber attnum)
|
|
|
|
|
{
|
|
|
|
|
char *attname;
|
|
|
|
|
|
|
|
|
|
attname = get_attname(relid, attnum);
|
|
|
|
|
if (attname == NULL)
|
|
|
|
|
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
|
|
|
|
|
attnum, relid);
|
|
|
|
|
return attname;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_attnum
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Given the relation id and the attribute name,
|
|
|
|
|
* return the "attnum" field from the attribute relation.
|
2002-08-02 14:15:10 -04:00
|
|
|
*
|
|
|
|
|
* Returns InvalidAttrNumber if the attr doesn't exist (or is dropped).
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
AttrNumber
|
2002-08-02 14:15:10 -04:00
|
|
|
get_attnum(Oid relid, const char *attname)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2002-08-02 14:15:10 -04:00
|
|
|
tp = SearchSysCacheAttName(relid, attname);
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
|
2000-11-16 17:30:52 -05:00
|
|
|
AttrNumber result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = att_tup->attnum;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1998-01-20 00:05:08 -05:00
|
|
|
return InvalidAttrNumber;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-06 08:33:16 -04:00
|
|
|
/*
|
|
|
|
|
* get_attidentity
|
|
|
|
|
*
|
|
|
|
|
* Given the relation id and the attribute name,
|
|
|
|
|
* return the "attidentity" field from the attribute relation.
|
|
|
|
|
*
|
|
|
|
|
* Returns '\0' if not found.
|
|
|
|
|
*
|
|
|
|
|
* Since no identity is represented by '\0', this can also be used as a
|
|
|
|
|
* Boolean test.
|
|
|
|
|
*/
|
|
|
|
|
char
|
|
|
|
|
get_attidentity(Oid relid, AttrNumber attnum)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache2(ATTNUM,
|
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
|
Int16GetDatum(attnum));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
|
2017-05-17 16:31:56 -04:00
|
|
|
char result;
|
2017-04-06 08:33:16 -04:00
|
|
|
|
|
|
|
|
result = att_tup->attidentity;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return '\0';
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_atttype
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Given the relation OID and the attribute number with the relation,
|
|
|
|
|
* return the attribute type OID.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_atttype(Oid relid, AttrNumber attnum)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache2(ATTNUM,
|
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
|
Int16GetDatum(attnum));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
Oid result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = att_tup->atttypid;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1999-05-28 21:45:21 -04:00
|
|
|
return InvalidOid;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
1998-01-20 00:05:08 -05:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_atttypmod
|
1998-01-20 00:05:08 -05:00
|
|
|
*
|
|
|
|
|
* Given the relation id and the attribute number,
|
|
|
|
|
* return the "atttypmod" field from the attribute relation.
|
|
|
|
|
*/
|
1998-07-12 17:29:40 -04:00
|
|
|
int32
|
1998-01-20 00:05:08 -05:00
|
|
|
get_atttypmod(Oid relid, AttrNumber attnum)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1998-01-20 00:05:08 -05:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache2(ATTNUM,
|
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
|
Int16GetDatum(attnum));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
int32 result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = att_tup->atttypmod;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1998-01-20 00:05:08 -05:00
|
|
|
else
|
1998-01-28 22:24:36 -05:00
|
|
|
return -1;
|
1998-01-20 00:05:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-02-08 16:04:18 -05:00
|
|
|
/*
|
2011-03-26 14:25:48 -04:00
|
|
|
* get_atttypetypmodcoll
|
2011-02-08 16:04:18 -05:00
|
|
|
*
|
2011-03-26 14:25:48 -04:00
|
|
|
* A three-fer: given the relation id and the attribute number,
|
|
|
|
|
* fetch atttypid, atttypmod, and attcollation in a single cache lookup.
|
2001-05-09 19:13:37 -04:00
|
|
|
*
|
|
|
|
|
* Unlike the otherwise-similar get_atttype/get_atttypmod, this routine
|
|
|
|
|
* raises an error if it can't obtain the information.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2011-03-26 14:25:48 -04:00
|
|
|
get_atttypetypmodcoll(Oid relid, AttrNumber attnum,
|
|
|
|
|
Oid *typid, int32 *typmod, Oid *collid)
|
2001-05-09 19:13:37 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_attribute att_tup;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache2(ATTNUM,
|
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
|
Int16GetDatum(attnum));
|
2001-05-09 19:13:37 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-27 20:09:16 -04:00
|
|
|
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
|
|
|
|
|
attnum, relid);
|
2001-05-09 19:13:37 -04:00
|
|
|
att_tup = (Form_pg_attribute) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
|
|
*typid = att_tup->atttypid;
|
|
|
|
|
*typmod = att_tup->atttypmod;
|
2011-03-26 14:25:48 -04:00
|
|
|
*collid = att_tup->attcollation;
|
2001-05-09 19:13:37 -04:00
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-08 16:04:18 -05:00
|
|
|
/* ---------- COLLATION CACHE ---------- */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get_collation_name
|
|
|
|
|
* Returns the name of a given pg_collation entry.
|
|
|
|
|
*
|
|
|
|
|
* Returns a palloc'd copy of the string, or NULL if no such constraint.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: since collation name is not unique, be wary of code that uses this
|
|
|
|
|
* for anything except preparing error messages.
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
get_collation_name(Oid colloid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(colloid));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_collation colltup = (Form_pg_collation) GETSTRUCT(tp);
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
result = pstrdup(NameStr(colltup->collname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-13 20:58:58 -05:00
|
|
|
/* ---------- CONSTRAINT CACHE ---------- */
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2007-02-13 20:58:58 -05:00
|
|
|
/*
|
|
|
|
|
* get_constraint_name
|
|
|
|
|
* Returns the name of a given pg_constraint entry.
|
|
|
|
|
*
|
|
|
|
|
* Returns a palloc'd copy of the string, or NULL if no such constraint.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: since constraint name is not unique, be wary of code that uses this
|
|
|
|
|
* for anything except preparing error messages.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
2007-02-13 20:58:58 -05:00
|
|
|
char *
|
|
|
|
|
get_constraint_name(Oid conoid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(CONSTROID, ObjectIdGetDatum(conoid));
|
2007-02-13 20:58:58 -05:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_constraint contup = (Form_pg_constraint) GETSTRUCT(tp);
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
result = pstrdup(NameStr(contup->conname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2015-04-26 10:33:14 -04:00
|
|
|
/* ---------- LANGUAGE CACHE ---------- */
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
get_language_name(Oid langoid, bool missing_ok)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(LANGOID, ObjectIdGetDatum(langoid));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_language lantup = (Form_pg_language) GETSTRUCT(tp);
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
result = pstrdup(NameStr(lantup->lanname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!missing_ok)
|
|
|
|
|
elog(ERROR, "cache lookup failed for language %u",
|
|
|
|
|
langoid);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2001-08-21 12:36:06 -04:00
|
|
|
/* ---------- OPCLASS CACHE ---------- */
|
|
|
|
|
|
|
|
|
|
/*
|
2006-12-22 19:43:13 -05:00
|
|
|
* get_opclass_family
|
2001-08-21 12:36:06 -04:00
|
|
|
*
|
2006-12-22 19:43:13 -05:00
|
|
|
* Returns the OID of the operator family the opclass belongs to.
|
2001-08-21 12:36:06 -04:00
|
|
|
*/
|
2006-12-22 19:43:13 -05:00
|
|
|
Oid
|
|
|
|
|
get_opclass_family(Oid opclass)
|
2005-12-27 20:30:02 -05:00
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_opclass cla_tup;
|
2006-12-22 19:43:13 -05:00
|
|
|
Oid result;
|
2005-12-27 20:30:02 -05:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
|
2005-12-27 20:30:02 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for opclass %u", opclass);
|
|
|
|
|
cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
|
|
|
|
|
|
2006-12-22 19:43:13 -05:00
|
|
|
result = cla_tup->opcfamily;
|
2005-12-27 20:30:02 -05:00
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get_opclass_input_type
|
|
|
|
|
*
|
|
|
|
|
* Returns the OID of the datatype the opclass indexes.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_opclass_input_type(Oid opclass)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_opclass cla_tup;
|
|
|
|
|
Oid result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
|
2005-12-27 20:30:02 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for opclass %u", opclass);
|
|
|
|
|
cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
|
|
result = cla_tup->opcintype;
|
|
|
|
|
ReleaseSysCache(tp);
|
2003-06-22 18:04:55 -04:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/* ---------- OPERATOR CACHE ---------- */
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_opcode
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Returns the regproc id of the routine used to implement an
|
1999-05-28 21:45:21 -04:00
|
|
|
* operator given the operator oid.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
RegProcedure
|
|
|
|
|
get_opcode(Oid opno)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
RegProcedure result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = optup->oprcode;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
2000-08-12 22:50:35 -04:00
|
|
|
return (RegProcedure) InvalidOid;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_opname
|
1997-09-07 01:04:48 -04:00
|
|
|
* returns the name of the operator with the given opno
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
1999-05-28 21:45:21 -04:00
|
|
|
* Note: returns a palloc'd copy of the string, or NULL if no such operator.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
1998-02-25 23:46:47 -05:00
|
|
|
char *
|
1996-07-09 02:22:35 -04:00
|
|
|
get_opname(Oid opno)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
1997-09-07 01:04:48 -04:00
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
char *result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = pstrdup(NameStr(optup->oprname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1997-09-07 01:04:48 -04:00
|
|
|
}
|
1999-05-28 21:45:21 -04:00
|
|
|
else
|
|
|
|
|
return NULL;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-21 19:47:15 -05:00
|
|
|
/*
|
|
|
|
|
* get_op_rettype
|
|
|
|
|
* Given operator oid, return the operator's result type.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_op_rettype(Oid opno)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
result = optup->oprresult;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-03 12:45:10 -05:00
|
|
|
/*
|
|
|
|
|
* op_input_types
|
|
|
|
|
*
|
|
|
|
|
* Returns the left and right input datatypes for an operator
|
|
|
|
|
* (InvalidOid if not relevant).
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_operator optup;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
2003-12-03 12:45:10 -05:00
|
|
|
if (!HeapTupleIsValid(tp)) /* shouldn't happen */
|
|
|
|
|
elog(ERROR, "cache lookup failed for operator %u", opno);
|
|
|
|
|
optup = (Form_pg_operator) GETSTRUCT(tp);
|
|
|
|
|
*lefttype = optup->oprleft;
|
|
|
|
|
*righttype = optup->oprright;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* op_mergejoinable
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
2006-12-22 19:43:13 -05:00
|
|
|
* Returns true if the operator is potentially mergejoinable. (The planner
|
|
|
|
|
* will fail to find any mergejoin plans unless there are suitable btree
|
|
|
|
|
* opfamily entries for this operator and associated sortops. The pg_operator
|
|
|
|
|
* flag is just a hint to tell the planner whether to bother looking.)
|
2010-10-30 21:55:20 -04:00
|
|
|
*
|
2011-06-03 15:38:12 -04:00
|
|
|
* In some cases (currently only array_eq and record_eq), mergejoinability
|
|
|
|
|
* depends on the specific input data type the operator is invoked for, so
|
|
|
|
|
* that must be passed as well. We currently assume that only one input's type
|
|
|
|
|
* is needed to check this --- by convention, pass the left input's data type.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
bool
|
2010-10-30 21:55:20 -04:00
|
|
|
op_mergejoinable(Oid opno, Oid inputtype)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
2000-11-16 17:30:52 -05:00
|
|
|
bool result = false;
|
2011-06-03 15:38:12 -04:00
|
|
|
HeapTuple tp;
|
|
|
|
|
TypeCacheEntry *typentry;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2011-06-03 15:38:12 -04:00
|
|
|
/*
|
|
|
|
|
* For array_eq or record_eq, we can sort if the element or field types
|
|
|
|
|
* are all sortable. We could implement all the checks for that here, but
|
|
|
|
|
* the typcache already does that and caches the results too, so let's
|
|
|
|
|
* rely on the typcache.
|
|
|
|
|
*/
|
2010-10-30 21:55:20 -04:00
|
|
|
if (opno == ARRAY_EQ_OP)
|
1997-09-07 01:04:48 -04:00
|
|
|
{
|
2011-06-03 15:38:12 -04:00
|
|
|
typentry = lookup_type_cache(inputtype, TYPECACHE_CMP_PROC);
|
|
|
|
|
if (typentry->cmp_proc == F_BTARRAYCMP)
|
|
|
|
|
result = true;
|
|
|
|
|
}
|
|
|
|
|
else if (opno == RECORD_EQ_OP)
|
|
|
|
|
{
|
|
|
|
|
typentry = lookup_type_cache(inputtype, TYPECACHE_CMP_PROC);
|
|
|
|
|
if (typentry->cmp_proc == F_BTRECORDCMP)
|
|
|
|
|
result = true;
|
2010-10-30 21:55:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* For all other operators, rely on pg_operator.oprcanmerge */
|
|
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
|
|
result = optup->oprcanmerge;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
}
|
2000-11-16 17:30:52 -05:00
|
|
|
return result;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
1999-02-13 18:22:53 -05:00
|
|
|
* op_hashjoinable
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
2006-12-22 19:43:13 -05:00
|
|
|
* Returns true if the operator is hashjoinable. (There must be a suitable
|
|
|
|
|
* hash opfamily entry for this operator if it is so marked.)
|
2010-10-30 21:55:20 -04:00
|
|
|
*
|
|
|
|
|
* In some cases (currently only array_eq), hashjoinability depends on the
|
|
|
|
|
* specific input data type the operator is invoked for, so that must be
|
2014-05-06 12:12:18 -04:00
|
|
|
* passed as well. We currently assume that only one input's type is needed
|
2010-10-30 21:55:20 -04:00
|
|
|
* to check this --- by convention, pass the left input's data type.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
2003-01-15 14:35:48 -05:00
|
|
|
bool
|
2010-10-30 21:55:20 -04:00
|
|
|
op_hashjoinable(Oid opno, Oid inputtype)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
2003-01-15 14:35:48 -05:00
|
|
|
bool result = false;
|
2011-06-03 15:38:12 -04:00
|
|
|
HeapTuple tp;
|
|
|
|
|
TypeCacheEntry *typentry;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2011-06-03 15:38:12 -04:00
|
|
|
/* As in op_mergejoinable, let the typcache handle the hard cases */
|
|
|
|
|
/* Eventually we'll need a similar case for record_eq ... */
|
2010-10-30 21:55:20 -04:00
|
|
|
if (opno == ARRAY_EQ_OP)
|
1999-05-28 21:45:21 -04:00
|
|
|
{
|
2011-06-03 15:38:12 -04:00
|
|
|
typentry = lookup_type_cache(inputtype, TYPECACHE_HASH_PROC);
|
|
|
|
|
if (typentry->hash_proc == F_HASH_ARRAY)
|
|
|
|
|
result = true;
|
2010-10-30 21:55:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* For all other operators, rely on pg_operator.oprcanhash */
|
|
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
|
|
result = optup->oprcanhash;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
2000-11-16 17:30:52 -05:00
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
|
|
|
|
|
2002-12-01 16:05:14 -05:00
|
|
|
/*
|
|
|
|
|
* op_strict
|
|
|
|
|
*
|
|
|
|
|
* Get the proisstrict flag for the operator's underlying function.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
op_strict(Oid opno)
|
|
|
|
|
{
|
|
|
|
|
RegProcedure funcid = get_opcode(opno);
|
|
|
|
|
|
|
|
|
|
if (funcid == (RegProcedure) InvalidOid)
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "operator %u does not exist", opno);
|
2002-12-01 16:05:14 -05:00
|
|
|
|
|
|
|
|
return func_strict((Oid) funcid);
|
|
|
|
|
}
|
|
|
|
|
|
2000-08-12 22:50:35 -04:00
|
|
|
/*
|
2002-04-04 19:31:36 -05:00
|
|
|
* op_volatile
|
2000-08-12 22:50:35 -04:00
|
|
|
*
|
2002-04-04 19:31:36 -05:00
|
|
|
* Get the provolatile flag for the operator's underlying function.
|
2000-08-12 22:50:35 -04:00
|
|
|
*/
|
2002-04-04 19:31:36 -05:00
|
|
|
char
|
|
|
|
|
op_volatile(Oid opno)
|
2000-08-12 22:50:35 -04:00
|
|
|
{
|
2001-03-21 23:01:46 -05:00
|
|
|
RegProcedure funcid = get_opcode(opno);
|
2000-08-12 22:50:35 -04:00
|
|
|
|
|
|
|
|
if (funcid == (RegProcedure) InvalidOid)
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "operator %u does not exist", opno);
|
2000-08-12 22:50:35 -04:00
|
|
|
|
2002-04-04 19:31:36 -05:00
|
|
|
return func_volatile((Oid) funcid);
|
2000-08-12 22:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_commutator
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Returns the corresponding commutator of an operator.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_commutator(Oid opno)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
Oid result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = optup->oprcom;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1999-05-28 21:45:21 -04:00
|
|
|
return InvalidOid;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_negator
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Returns the corresponding negator of an operator.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_negator(Oid opno)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
Oid result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = optup->oprnegate;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1999-05-28 21:45:21 -04:00
|
|
|
return InvalidOid;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_oprrest
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Returns procedure id for computing selectivity of an operator.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
RegProcedure
|
|
|
|
|
get_oprrest(Oid opno)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
RegProcedure result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = optup->oprrest;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
2000-08-12 22:50:35 -04:00
|
|
|
return (RegProcedure) InvalidOid;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_oprjoin
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Returns procedure id for computing selectivity of a join.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
RegProcedure
|
|
|
|
|
get_oprjoin(Oid opno)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
RegProcedure result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = optup->oprjoin;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
2000-08-12 22:50:35 -04:00
|
|
|
return (RegProcedure) InvalidOid;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
1999-08-15 22:06:25 -04:00
|
|
|
/* ---------- FUNCTION CACHE ---------- */
|
|
|
|
|
|
2002-04-26 23:45:03 -04:00
|
|
|
/*
|
|
|
|
|
* get_func_name
|
|
|
|
|
* returns the name of the function with the given funcid
|
|
|
|
|
*
|
|
|
|
|
* Note: returns a palloc'd copy of the string, or NULL if no such function.
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
get_func_name(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2002-04-26 23:45:03 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
result = pstrdup(NameStr(functup->proname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-10 01:46:50 -04:00
|
|
|
/*
|
|
|
|
|
* get_func_namespace
|
|
|
|
|
*
|
|
|
|
|
* Returns the pg_namespace OID associated with a given function.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_func_namespace(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2009-08-10 01:46:50 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
result = functup->pronamespace;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
1999-08-15 22:06:25 -04:00
|
|
|
/*
|
|
|
|
|
* get_func_rettype
|
|
|
|
|
* Given procedure id, return the function's result type.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_func_rettype(Oid funcid)
|
|
|
|
|
{
|
2000-11-16 17:30:52 -05:00
|
|
|
HeapTuple tp;
|
|
|
|
|
Oid result;
|
1999-08-15 22:06:25 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2000-11-16 17:30:52 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
1999-08-15 22:06:25 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->prorettype;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-08-15 22:06:25 -04:00
|
|
|
}
|
|
|
|
|
|
2005-03-28 19:17:27 -05:00
|
|
|
/*
|
|
|
|
|
* get_func_nargs
|
|
|
|
|
* Given procedure id, return the number of arguments.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
get_func_nargs(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
int result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2005-03-28 19:17:27 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
|
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->pronargs;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-01 15:10:53 -04:00
|
|
|
/*
|
|
|
|
|
* get_func_signature
|
|
|
|
|
* Given procedure id, return the function's argument and result types.
|
|
|
|
|
* (The return value is the result type.)
|
|
|
|
|
*
|
2005-03-28 19:17:27 -05:00
|
|
|
* The arguments are returned as a palloc'd array.
|
2003-07-01 15:10:53 -04:00
|
|
|
*/
|
|
|
|
|
Oid
|
2005-03-28 19:17:27 -05:00
|
|
|
get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
|
2003-07-01 15:10:53 -04:00
|
|
|
{
|
2003-08-03 20:43:34 -04:00
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_proc procstruct;
|
2003-07-01 15:10:53 -04:00
|
|
|
Oid result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2003-07-01 15:10:53 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
2003-07-01 15:10:53 -04:00
|
|
|
|
|
|
|
|
procstruct = (Form_pg_proc) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
|
|
result = procstruct->prorettype;
|
|
|
|
|
*nargs = (int) procstruct->pronargs;
|
2005-03-28 19:17:27 -05:00
|
|
|
Assert(*nargs == procstruct->proargtypes.dim1);
|
|
|
|
|
*argtypes = (Oid *) palloc(*nargs * sizeof(Oid));
|
|
|
|
|
memcpy(*argtypes, procstruct->proargtypes.values, *nargs * sizeof(Oid));
|
2003-07-01 15:10:53 -04:00
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 16:11:35 -05:00
|
|
|
/*
|
|
|
|
|
* get_func_variadictype
|
|
|
|
|
* Given procedure id, return the function's provariadic field.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_func_variadictype(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
|
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->provariadic;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-12 19:43:04 -04:00
|
|
|
/*
|
|
|
|
|
* get_func_retset
|
|
|
|
|
* Given procedure id, return the function's proretset flag.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
get_func_retset(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
bool result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2002-05-12 19:43:04 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
2002-05-12 19:43:04 -04:00
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->proretset;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-01 16:05:14 -05:00
|
|
|
/*
|
|
|
|
|
* func_strict
|
|
|
|
|
* Given procedure id, return the function's proisstrict flag.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
func_strict(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
bool result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2002-12-01 16:05:14 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
2002-12-01 16:05:14 -05:00
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->proisstrict;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2000-08-12 22:50:35 -04:00
|
|
|
/*
|
2002-04-04 19:31:36 -05:00
|
|
|
* func_volatile
|
|
|
|
|
* Given procedure id, return the function's provolatile flag.
|
2000-08-12 22:50:35 -04:00
|
|
|
*/
|
2002-04-04 19:31:36 -05:00
|
|
|
char
|
|
|
|
|
func_volatile(Oid funcid)
|
2000-08-12 22:50:35 -04:00
|
|
|
{
|
2000-11-16 17:30:52 -05:00
|
|
|
HeapTuple tp;
|
2002-04-04 19:31:36 -05:00
|
|
|
char result;
|
2000-08-12 22:50:35 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2000-11-16 17:30:52 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
2000-08-12 22:50:35 -04:00
|
|
|
|
2002-04-04 19:31:36 -05:00
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->provolatile;
|
2000-11-16 17:30:52 -05:00
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
2000-08-12 22:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 15:38:47 -04:00
|
|
|
/*
|
|
|
|
|
* func_parallel
|
|
|
|
|
* Given procedure id, return the function's proparallel flag.
|
|
|
|
|
*/
|
|
|
|
|
char
|
|
|
|
|
func_parallel(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
char result;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
|
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->proparallel;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-13 22:20:27 -05:00
|
|
|
/*
|
|
|
|
|
* get_func_leakproof
|
2012-06-10 15:20:04 -04:00
|
|
|
* Given procedure id, return the function's leakproof field.
|
2012-02-13 22:20:27 -05:00
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
get_func_leakproof(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
|
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->proleakproof;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-21 20:35:23 -05:00
|
|
|
/*
|
|
|
|
|
* get_func_cost
|
|
|
|
|
* Given procedure id, return the function's procost field.
|
|
|
|
|
*/
|
|
|
|
|
float4
|
|
|
|
|
get_func_cost(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
float4 result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2007-01-21 20:35:23 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
|
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->procost;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get_func_rows
|
|
|
|
|
* Given procedure id, return the function's prorows field.
|
|
|
|
|
*/
|
|
|
|
|
float4
|
|
|
|
|
get_func_rows(Oid funcid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
float4 result;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
|
2007-01-21 20:35:23 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for function %u", funcid);
|
|
|
|
|
|
|
|
|
|
result = ((Form_pg_proc) GETSTRUCT(tp))->prorows;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/* ---------- RELATION CACHE ---------- */
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2002-03-26 14:17:02 -05:00
|
|
|
/*
|
|
|
|
|
* get_relname_relid
|
|
|
|
|
* Given name and namespace of a relation, look up the OID.
|
|
|
|
|
*
|
|
|
|
|
* Returns InvalidOid if there is no such relation.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_relname_relid(const char *relname, Oid relnamespace)
|
|
|
|
|
{
|
2010-02-14 13:42:19 -05:00
|
|
|
return GetSysCacheOid2(RELNAMENSP,
|
|
|
|
|
PointerGetDatum(relname),
|
|
|
|
|
ObjectIdGetDatum(relnamespace));
|
2002-03-26 14:17:02 -05:00
|
|
|
}
|
|
|
|
|
|
2000-06-08 18:38:00 -04:00
|
|
|
#ifdef NOT_USED
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_relnatts
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Returns the number of attributes for a given relation.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
get_relnatts(Oid relid)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
int result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = reltup->relnatts;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1998-01-20 00:05:08 -05:00
|
|
|
return InvalidAttrNumber;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
2000-06-08 18:38:00 -04:00
|
|
|
#endif
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_rel_name
|
1997-09-07 01:04:48 -04:00
|
|
|
* Returns the name of a given relation.
|
2000-11-16 17:30:52 -05:00
|
|
|
*
|
2002-03-26 14:17:02 -05:00
|
|
|
* Returns a palloc'd copy of the string, or NULL if no such relation.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: since relation name is not unique, be wary of code that uses this
|
|
|
|
|
* for anything except preparing error messages.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
1998-02-25 23:46:47 -05:00
|
|
|
char *
|
1996-07-09 02:22:35 -04:00
|
|
|
get_rel_name(Oid relid)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
char *result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = pstrdup(NameStr(reltup->relname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1998-01-20 00:05:08 -05:00
|
|
|
return NULL;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
2002-04-29 21:26:26 -04:00
|
|
|
/*
|
|
|
|
|
* get_rel_namespace
|
|
|
|
|
*
|
|
|
|
|
* Returns the pg_namespace OID associated with a given relation.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_rel_namespace(Oid relid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
2002-04-29 21:26:26 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
|
2002-09-04 16:31:48 -04:00
|
|
|
Oid result;
|
2002-04-29 21:26:26 -04:00
|
|
|
|
|
|
|
|
result = reltup->relnamespace;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-21 21:56:37 -05:00
|
|
|
/*
|
|
|
|
|
* get_rel_type_id
|
|
|
|
|
*
|
|
|
|
|
* Returns the pg_type OID associated with a given relation.
|
|
|
|
|
*
|
|
|
|
|
* Note: not all pg_class entries have associated pg_type OIDs; so be
|
|
|
|
|
* careful to check for InvalidOid result.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_rel_type_id(Oid relid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
2002-03-21 21:56:37 -05:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
|
2002-09-04 16:31:48 -04:00
|
|
|
Oid result;
|
2002-03-21 21:56:37 -05:00
|
|
|
|
|
|
|
|
result = reltup->reltype;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-19 19:40:56 -04:00
|
|
|
/*
|
|
|
|
|
* get_rel_relkind
|
|
|
|
|
*
|
|
|
|
|
* Returns the relkind associated with a given relation.
|
|
|
|
|
*/
|
|
|
|
|
char
|
|
|
|
|
get_rel_relkind(Oid relid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
2002-09-19 19:40:56 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
|
|
|
|
|
char result;
|
|
|
|
|
|
|
|
|
|
result = reltup->relkind;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-13 11:55:40 -04:00
|
|
|
/*
|
|
|
|
|
* get_rel_tablespace
|
|
|
|
|
*
|
|
|
|
|
* Returns the pg_tablespace OID associated with a given relation.
|
|
|
|
|
*
|
|
|
|
|
* Note: InvalidOid might mean either that we couldn't find the relation,
|
|
|
|
|
* or that it is in the database's default tablespace.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_rel_tablespace(Oid relid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
2007-10-13 11:55:40 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
result = reltup->reltablespace;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
Generate parallel sequential scan plans in simple cases.
Add a new flag, consider_parallel, to each RelOptInfo, indicating
whether a plan for that relation could conceivably be run inside of
a parallel worker. Right now, we're pretty conservative: for example,
it might be possible to defer applying a parallel-restricted qual
in a worker, and later do it in the leader, but right now we just
don't try to parallelize access to that relation. That's probably
the right decision in most cases, anyway.
Using the new flag, generate parallel sequential scan plans for plain
baserels, meaning that we now have parallel sequential scan in
PostgreSQL. The logic here is pretty unsophisticated right now: the
costing model probably isn't right in detail, and we can't push joins
beneath Gather nodes, so the number of plans that can actually benefit
from this is pretty limited right now. Lots more work is needed.
Nevertheless, it seems time to enable this functionality so that all
this code can actually be tested easily by users and developers.
Note that, if you wish to test this functionality, it will be
necessary to set max_parallel_degree to a value greater than the
default of 0. Once a few more loose ends have been tidied up here, we
might want to consider changing the default value of this GUC, but
I'm leaving it alone for now.
Along the way, fix a bug in cost_gather: the previous coding thought
that a Gather node's transfer overhead should be costed on the basis of
the relation size rather than the number of tuples that actually need
to be passed off to the leader.
Patch by me, reviewed in earlier versions by Amit Kapila.
2015-11-11 09:02:52 -05:00
|
|
|
/*
|
|
|
|
|
* get_rel_persistence
|
|
|
|
|
*
|
|
|
|
|
* Returns the relpersistence associated with a given relation.
|
|
|
|
|
*/
|
|
|
|
|
char
|
|
|
|
|
get_rel_persistence(Oid relid)
|
|
|
|
|
{
|
2016-06-09 18:02:36 -04:00
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_class reltup;
|
|
|
|
|
char result;
|
Generate parallel sequential scan plans in simple cases.
Add a new flag, consider_parallel, to each RelOptInfo, indicating
whether a plan for that relation could conceivably be run inside of
a parallel worker. Right now, we're pretty conservative: for example,
it might be possible to defer applying a parallel-restricted qual
in a worker, and later do it in the leader, but right now we just
don't try to parallelize access to that relation. That's probably
the right decision in most cases, anyway.
Using the new flag, generate parallel sequential scan plans for plain
baserels, meaning that we now have parallel sequential scan in
PostgreSQL. The logic here is pretty unsophisticated right now: the
costing model probably isn't right in detail, and we can't push joins
beneath Gather nodes, so the number of plans that can actually benefit
from this is pretty limited right now. Lots more work is needed.
Nevertheless, it seems time to enable this functionality so that all
this code can actually be tested easily by users and developers.
Note that, if you wish to test this functionality, it will be
necessary to set max_parallel_degree to a value greater than the
default of 0. Once a few more loose ends have been tidied up here, we
might want to consider changing the default value of this GUC, but
I'm leaving it alone for now.
Along the way, fix a bug in cost_gather: the previous coding thought
that a Gather node's transfer overhead should be costed on the basis of
the relation size rather than the number of tuples that actually need
to be passed off to the leader.
Patch by me, reviewed in earlier versions by Amit Kapila.
2015-11-11 09:02:52 -05:00
|
|
|
|
|
|
|
|
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relid);
|
|
|
|
|
reltup = (Form_pg_class) GETSTRUCT(tp);
|
|
|
|
|
result = reltup->relpersistence;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-19 19:40:56 -04:00
|
|
|
|
2015-04-26 10:33:14 -04:00
|
|
|
/* ---------- TRANSFORM CACHE ---------- */
|
|
|
|
|
|
|
|
|
|
Oid
|
|
|
|
|
get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
|
|
if (!list_member_oid(trftypes, typid))
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
|
|
|
|
|
tup = SearchSysCache2(TRFTYPELANG, typid, langid);
|
|
|
|
|
if (HeapTupleIsValid(tup))
|
|
|
|
|
{
|
|
|
|
|
Oid funcid;
|
|
|
|
|
|
|
|
|
|
funcid = ((Form_pg_transform) GETSTRUCT(tup))->trffromsql;
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
return funcid;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Oid
|
|
|
|
|
get_transform_tosql(Oid typid, Oid langid, List *trftypes)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
|
|
if (!list_member_oid(trftypes, typid))
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
|
|
|
|
|
tup = SearchSysCache2(TRFTYPELANG, typid, langid);
|
|
|
|
|
if (HeapTupleIsValid(tup))
|
|
|
|
|
{
|
|
|
|
|
Oid funcid;
|
|
|
|
|
|
|
|
|
|
funcid = ((Form_pg_transform) GETSTRUCT(tup))->trftosql;
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
return funcid;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/* ---------- TYPE CACHE ---------- */
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2002-03-29 14:06:29 -05:00
|
|
|
/*
|
|
|
|
|
* get_typisdefined
|
|
|
|
|
*
|
|
|
|
|
* Given the type OID, determine whether the type is defined
|
|
|
|
|
* (if not, it's only a shell).
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
get_typisdefined(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2002-03-29 14:06:29 -05:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
|
|
result = typtup->typisdefined;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_typlen
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Given the type OID, return the length of the type.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
int16
|
|
|
|
|
get_typlen(Oid typid)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
int16 result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = typtup->typlen;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1999-05-28 21:45:21 -04:00
|
|
|
return 0;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_typbyval
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Given the type OID, determine whether the type is returned by value or
|
2000-07-22 23:50:26 -04:00
|
|
|
* not. Returns true if by value, false if by reference.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
get_typbyval(Oid typid)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
bool result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = typtup->typbyval;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1998-01-20 00:05:08 -05:00
|
|
|
return false;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
/*
|
|
|
|
|
* get_typlenbyval
|
|
|
|
|
*
|
|
|
|
|
* A two-fer: given the type OID, return both typlen and typbyval.
|
|
|
|
|
*
|
|
|
|
|
* Since both pieces of info are needed to know how to copy a Datum,
|
2014-05-06 12:12:18 -04:00
|
|
|
* many places need both. Might as well get them with one cache lookup
|
2000-11-16 17:30:52 -05:00
|
|
|
* instead of two. Also, this routine raises an error instead of
|
|
|
|
|
* returning a bogus value when given a bad type OID.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_type typtup;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2000-11-16 17:30:52 -05:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for type %u", typid);
|
|
|
|
|
typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
*typlen = typtup->typlen;
|
|
|
|
|
*typbyval = typtup->typbyval;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-26 13:54:02 -04:00
|
|
|
/*
|
|
|
|
|
* get_typlenbyvalalign
|
|
|
|
|
*
|
|
|
|
|
* A three-fer: given the type OID, return typlen, typbyval, typalign.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval,
|
|
|
|
|
char *typalign)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_type typtup;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2002-08-26 13:54:02 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for type %u", typid);
|
|
|
|
|
typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
*typlen = typtup->typlen;
|
|
|
|
|
*typbyval = typtup->typbyval;
|
|
|
|
|
*typalign = typtup->typalign;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-05 20:41:28 -04:00
|
|
|
/*
|
|
|
|
|
* getTypeIOParam
|
|
|
|
|
* Given a pg_type row, select the type OID to pass to I/O functions
|
|
|
|
|
*
|
|
|
|
|
* Formerly, all I/O functions were passed pg_type.typelem as their second
|
|
|
|
|
* parameter, but we now have a more complex rule about what to pass.
|
|
|
|
|
* This knowledge is intended to be centralized here --- direct references
|
|
|
|
|
* to typelem elsewhere in the code are wrong, if they are associated with
|
|
|
|
|
* I/O calls and not with actual subscripting operations! (But see
|
2006-08-15 18:36:17 -04:00
|
|
|
* bootstrap.c's boot_get_type_io_data() if you need to change this.)
|
2005-05-01 14:56:19 -04:00
|
|
|
*
|
|
|
|
|
* As of PostgreSQL 8.1, output functions receive only the value itself
|
|
|
|
|
* and not any auxiliary parameters, so the name of this routine is now
|
|
|
|
|
* a bit of a misnomer ... it should be getTypeInputParam.
|
2004-06-05 20:41:28 -04:00
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
getTypeIOParam(HeapTuple typeTuple)
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
|
|
|
|
|
|
|
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* Array types get their typelem as parameter; everybody else gets their
|
2011-12-01 12:44:16 -05:00
|
|
|
* own type OID as parameter.
|
2004-06-05 20:41:28 -04:00
|
|
|
*/
|
2011-12-01 12:44:16 -05:00
|
|
|
if (OidIsValid(typeStruct->typelem))
|
2004-06-05 20:41:28 -04:00
|
|
|
return typeStruct->typelem;
|
2005-08-12 17:49:47 -04:00
|
|
|
else
|
|
|
|
|
return HeapTupleGetOid(typeTuple);
|
2004-06-05 20:41:28 -04:00
|
|
|
}
|
|
|
|
|
|
2003-06-26 20:33:26 -04:00
|
|
|
/*
|
|
|
|
|
* get_type_io_data
|
|
|
|
|
*
|
|
|
|
|
* A six-fer: given the type OID, return typlen, typbyval, typalign,
|
2004-06-05 20:41:28 -04:00
|
|
|
* typdelim, typioparam, and IO function OID. The IO function
|
2003-06-26 20:33:26 -04:00
|
|
|
* returned is controlled by IOFuncSelector
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
get_type_io_data(Oid typid,
|
|
|
|
|
IOFuncSelector which_func,
|
|
|
|
|
int16 *typlen,
|
|
|
|
|
bool *typbyval,
|
|
|
|
|
char *typalign,
|
|
|
|
|
char *typdelim,
|
2004-06-05 20:41:28 -04:00
|
|
|
Oid *typioparam,
|
2003-06-26 20:33:26 -04:00
|
|
|
Oid *func)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
|
Form_pg_type typeStruct;
|
|
|
|
|
|
2006-08-15 18:36:17 -04:00
|
|
|
/*
|
2006-10-03 20:30:14 -04:00
|
|
|
* In bootstrap mode, pass it off to bootstrap.c. This hack allows us to
|
|
|
|
|
* use array_in and array_out during bootstrap.
|
2006-08-15 18:36:17 -04:00
|
|
|
*/
|
|
|
|
|
if (IsBootstrapProcessingMode())
|
|
|
|
|
{
|
2006-10-03 20:30:14 -04:00
|
|
|
Oid typinput;
|
|
|
|
|
Oid typoutput;
|
2006-08-15 18:36:17 -04:00
|
|
|
|
|
|
|
|
boot_get_type_io_data(typid,
|
|
|
|
|
typlen,
|
|
|
|
|
typbyval,
|
|
|
|
|
typalign,
|
|
|
|
|
typdelim,
|
|
|
|
|
typioparam,
|
|
|
|
|
&typinput,
|
|
|
|
|
&typoutput);
|
|
|
|
|
switch (which_func)
|
|
|
|
|
{
|
|
|
|
|
case IOFunc_input:
|
|
|
|
|
*func = typinput;
|
|
|
|
|
break;
|
|
|
|
|
case IOFunc_output:
|
|
|
|
|
*func = typoutput;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
elog(ERROR, "binary I/O not supported during bootstrap");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2003-06-26 20:33:26 -04:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
|
|
|
|
elog(ERROR, "cache lookup failed for type %u", typid);
|
|
|
|
|
typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
|
|
|
|
|
|
|
|
|
|
*typlen = typeStruct->typlen;
|
|
|
|
|
*typbyval = typeStruct->typbyval;
|
|
|
|
|
*typalign = typeStruct->typalign;
|
|
|
|
|
*typdelim = typeStruct->typdelim;
|
2004-06-05 20:41:28 -04:00
|
|
|
*typioparam = getTypeIOParam(typeTuple);
|
2003-06-26 20:33:26 -04:00
|
|
|
switch (which_func)
|
|
|
|
|
{
|
|
|
|
|
case IOFunc_input:
|
|
|
|
|
*func = typeStruct->typinput;
|
|
|
|
|
break;
|
|
|
|
|
case IOFunc_output:
|
|
|
|
|
*func = typeStruct->typoutput;
|
|
|
|
|
break;
|
|
|
|
|
case IOFunc_receive:
|
|
|
|
|
*func = typeStruct->typreceive;
|
|
|
|
|
break;
|
|
|
|
|
case IOFunc_send:
|
|
|
|
|
*func = typeStruct->typsend;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
ReleaseSysCache(typeTuple);
|
|
|
|
|
}
|
|
|
|
|
|
1997-08-19 17:40:56 -04:00
|
|
|
#ifdef NOT_USED
|
1996-07-09 02:22:35 -04:00
|
|
|
char
|
|
|
|
|
get_typalign(Oid typid)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
char result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = typtup->typalign;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1998-01-20 00:05:08 -05:00
|
|
|
return 'i';
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
1997-08-19 17:40:56 -04:00
|
|
|
#endif
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2000-11-20 15:36:57 -05:00
|
|
|
char
|
|
|
|
|
get_typstorage(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2000-11-20 15:36:57 -05:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
char result;
|
2000-11-20 15:36:57 -05:00
|
|
|
|
|
|
|
|
result = typtup->typstorage;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return 'p';
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_typdefault
|
2001-09-05 22:07:42 -04:00
|
|
|
* Given a type OID, return the type's default value, if any.
|
2002-03-20 14:45:13 -05:00
|
|
|
*
|
|
|
|
|
* The result is a palloc'd expression node tree, or NULL if there
|
|
|
|
|
* is no defined default for the datatype.
|
|
|
|
|
*
|
|
|
|
|
* NB: caller should be prepared to coerce result to correct datatype;
|
|
|
|
|
* the returned expression tree might produce something of the wrong type.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
2002-03-18 21:18:25 -05:00
|
|
|
Node *
|
2002-03-20 14:45:13 -05:00
|
|
|
get_typdefault(Oid typid)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
1999-08-08 23:13:31 -04:00
|
|
|
HeapTuple typeTuple;
|
|
|
|
|
Form_pg_type type;
|
2002-03-18 21:18:25 -05:00
|
|
|
Datum datum;
|
2000-01-22 22:43:24 -05:00
|
|
|
bool isNull;
|
2002-03-18 21:18:25 -05:00
|
|
|
Node *expr;
|
1999-08-08 23:13:31 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
1999-08-08 23:13:31 -04:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", typid);
|
1999-08-08 23:13:31 -04:00
|
|
|
type = (Form_pg_type) GETSTRUCT(typeTuple);
|
2000-01-22 22:43:24 -05:00
|
|
|
|
|
|
|
|
/*
|
2002-03-20 14:45:13 -05:00
|
|
|
* typdefault and typdefaultbin are potentially null, so don't try to
|
|
|
|
|
* access 'em as struct fields. Must do it the hard way with
|
|
|
|
|
* SysCacheGetAttr.
|
2000-01-22 22:43:24 -05:00
|
|
|
*/
|
2002-03-18 21:18:25 -05:00
|
|
|
datum = SysCacheGetAttr(TYPEOID,
|
|
|
|
|
typeTuple,
|
|
|
|
|
Anum_pg_type_typdefaultbin,
|
|
|
|
|
&isNull);
|
2000-01-22 22:43:24 -05:00
|
|
|
|
2002-03-20 14:45:13 -05:00
|
|
|
if (!isNull)
|
|
|
|
|
{
|
|
|
|
|
/* We have an expression default */
|
2008-03-25 18:42:46 -04:00
|
|
|
expr = stringToNode(TextDatumGetCString(datum));
|
2002-03-20 14:45:13 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Perhaps we have a plain literal default */
|
|
|
|
|
datum = SysCacheGetAttr(TYPEOID,
|
|
|
|
|
typeTuple,
|
|
|
|
|
Anum_pg_type_typdefault,
|
|
|
|
|
&isNull);
|
|
|
|
|
|
|
|
|
|
if (!isNull)
|
|
|
|
|
{
|
|
|
|
|
char *strDefaultVal;
|
|
|
|
|
|
|
|
|
|
/* Convert text datum to C string */
|
2008-03-25 18:42:46 -04:00
|
|
|
strDefaultVal = TextDatumGetCString(datum);
|
2002-03-20 14:45:13 -05:00
|
|
|
/* Convert C string to a value of the given type */
|
2006-04-04 15:35:37 -04:00
|
|
|
datum = OidInputFunctionCall(type->typinput, strDefaultVal,
|
|
|
|
|
getTypeIOParam(typeTuple), -1);
|
2002-03-20 14:45:13 -05:00
|
|
|
/* Build a Const node containing the value */
|
|
|
|
|
expr = (Node *) makeConst(typid,
|
2007-03-16 20:11:05 -04:00
|
|
|
-1,
|
2011-03-25 20:10:42 -04:00
|
|
|
type->typcollation,
|
2002-03-20 14:45:13 -05:00
|
|
|
type->typlen,
|
|
|
|
|
datum,
|
|
|
|
|
false,
|
2002-11-25 16:29:42 -05:00
|
|
|
type->typbyval);
|
2002-03-20 14:45:13 -05:00
|
|
|
pfree(strDefaultVal);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* No default */
|
|
|
|
|
expr = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-08-08 23:13:31 -04:00
|
|
|
|
2002-03-20 14:45:13 -05:00
|
|
|
ReleaseSysCache(typeTuple);
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2002-03-20 14:45:13 -05:00
|
|
|
return expr;
|
|
|
|
|
}
|
2002-03-06 15:35:02 -05:00
|
|
|
|
2002-03-20 14:45:13 -05:00
|
|
|
/*
|
|
|
|
|
* getBaseType
|
|
|
|
|
* If the given type is a domain, return its base type;
|
|
|
|
|
* otherwise return the type's own OID.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
getBaseType(Oid typid)
|
2006-04-05 18:11:58 -04:00
|
|
|
{
|
|
|
|
|
int32 typmod = -1;
|
|
|
|
|
|
|
|
|
|
return getBaseTypeAndTypmod(typid, &typmod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* getBaseTypeAndTypmod
|
|
|
|
|
* If the given type is a domain, return its base type and typmod;
|
|
|
|
|
* otherwise return the type's own OID, and leave *typmod unchanged.
|
|
|
|
|
*
|
|
|
|
|
* Note that the "applied typmod" should be -1 for every domain level
|
|
|
|
|
* above the bottommost; therefore, if the passed-in typid is indeed
|
|
|
|
|
* a domain, *typmod should be -1.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
getBaseTypeAndTypmod(Oid typid, int32 *typmod)
|
2002-03-20 14:45:13 -05:00
|
|
|
{
|
2002-03-18 21:18:25 -05:00
|
|
|
/*
|
2002-03-20 14:45:13 -05:00
|
|
|
* We loop to find the bottom base type in a stack of domains.
|
2002-03-18 21:18:25 -05:00
|
|
|
*/
|
2002-03-20 14:45:13 -05:00
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tup;
|
|
|
|
|
Form_pg_type typTup;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2002-03-20 14:45:13 -05:00
|
|
|
if (!HeapTupleIsValid(tup))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", typid);
|
2002-03-20 14:45:13 -05:00
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
2007-04-01 23:49:42 -04:00
|
|
|
if (typTup->typtype != TYPTYPE_DOMAIN)
|
2002-03-20 14:45:13 -05:00
|
|
|
{
|
|
|
|
|
/* Not a domain, so done */
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-03-18 21:18:25 -05:00
|
|
|
|
2006-04-05 18:11:58 -04:00
|
|
|
Assert(*typmod == -1);
|
2002-03-20 14:45:13 -05:00
|
|
|
typid = typTup->typbasetype;
|
2006-04-05 18:11:58 -04:00
|
|
|
*typmod = typTup->typtypmod;
|
|
|
|
|
|
2002-03-20 14:45:13 -05:00
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
}
|
2000-11-16 17:30:52 -05:00
|
|
|
|
2002-03-20 14:45:13 -05:00
|
|
|
return typid;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
2001-05-08 20:35:09 -04:00
|
|
|
/*
|
|
|
|
|
* get_typavgwidth
|
|
|
|
|
*
|
|
|
|
|
* Given a type OID and a typmod value (pass -1 if typmod is unknown),
|
|
|
|
|
* estimate the average width of values of the type. This is used by
|
|
|
|
|
* the planner, which doesn't require absolutely correct results;
|
|
|
|
|
* it's OK (and expected) to guess if we don't know for sure.
|
|
|
|
|
*/
|
|
|
|
|
int32
|
|
|
|
|
get_typavgwidth(Oid typid, int32 typmod)
|
|
|
|
|
{
|
|
|
|
|
int typlen = get_typlen(typid);
|
|
|
|
|
int32 maxwidth;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Easy if it's a fixed-width type
|
|
|
|
|
*/
|
|
|
|
|
if (typlen > 0)
|
|
|
|
|
return typlen;
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2001-05-08 20:35:09 -04:00
|
|
|
/*
|
|
|
|
|
* type_maximum_size knows the encoding of typmod for some datatypes;
|
|
|
|
|
* don't duplicate that knowledge here.
|
|
|
|
|
*/
|
|
|
|
|
maxwidth = type_maximum_size(typid, typmod);
|
|
|
|
|
if (maxwidth > 0)
|
|
|
|
|
{
|
|
|
|
|
/*
|
2001-10-25 01:50:21 -04:00
|
|
|
* For BPCHAR, the max width is also the only width. Otherwise we
|
2005-10-14 22:49:52 -04:00
|
|
|
* need to guess about the typical data width given the max. A sliding
|
|
|
|
|
* scale for percentage of max width seems reasonable.
|
2001-05-08 20:35:09 -04:00
|
|
|
*/
|
|
|
|
|
if (typid == BPCHAROID)
|
|
|
|
|
return maxwidth;
|
|
|
|
|
if (maxwidth <= 32)
|
|
|
|
|
return maxwidth; /* assume full width */
|
|
|
|
|
if (maxwidth < 1000)
|
2001-10-25 01:50:21 -04:00
|
|
|
return 32 + (maxwidth - 32) / 2; /* assume 50% */
|
|
|
|
|
|
2001-05-08 20:35:09 -04:00
|
|
|
/*
|
|
|
|
|
* Beyond 1000, assume we're looking at something like
|
2005-10-14 22:49:52 -04:00
|
|
|
* "varchar(10000)" where the limit isn't actually reached often, and
|
|
|
|
|
* use a fixed estimate.
|
2001-05-08 20:35:09 -04:00
|
|
|
*/
|
|
|
|
|
return 32 + (1000 - 32) / 2;
|
|
|
|
|
}
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2001-05-08 20:35:09 -04:00
|
|
|
/*
|
2017-03-14 11:38:30 -04:00
|
|
|
* Oops, we have no idea ... wild guess time.
|
2001-05-08 20:35:09 -04:00
|
|
|
*/
|
|
|
|
|
return 32;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
2000-07-22 23:50:26 -04:00
|
|
|
* get_typtype
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
2002-08-04 22:30:50 -04:00
|
|
|
* Given the type OID, find if it is a basic type, a complex type, etc.
|
1997-09-07 01:04:48 -04:00
|
|
|
* It returns the null char if the cache lookup fails...
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
char
|
|
|
|
|
get_typtype(Oid typid)
|
|
|
|
|
{
|
1999-05-28 21:45:21 -04:00
|
|
|
HeapTuple tp;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
1999-05-28 21:45:21 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
2001-03-21 23:01:46 -05:00
|
|
|
char result;
|
2000-04-12 13:17:23 -04:00
|
|
|
|
2000-11-16 17:30:52 -05:00
|
|
|
result = typtup->typtype;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
1999-05-28 21:45:21 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
1998-01-20 00:05:08 -05:00
|
|
|
return '\0';
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
2002-08-04 22:30:50 -04:00
|
|
|
|
2006-09-28 16:51:43 -04:00
|
|
|
/*
|
|
|
|
|
* type_is_rowtype
|
|
|
|
|
*
|
|
|
|
|
* Convenience function to determine whether a type OID represents
|
|
|
|
|
* a "rowtype" type --- either RECORD or a named composite type.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
type_is_rowtype(Oid typid)
|
|
|
|
|
{
|
2007-04-01 23:49:42 -04:00
|
|
|
return (typid == RECORDOID || get_typtype(typid) == TYPTYPE_COMPOSITE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* type_is_enum
|
2007-11-15 16:14:46 -05:00
|
|
|
* Returns true if the given type is an enum type.
|
2007-04-01 23:49:42 -04:00
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
type_is_enum(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
return (get_typtype(typid) == TYPTYPE_ENUM);
|
2006-09-28 16:51:43 -04:00
|
|
|
}
|
|
|
|
|
|
2011-11-03 07:16:28 -04:00
|
|
|
/*
|
|
|
|
|
* type_is_range
|
2011-11-20 23:50:27 -05:00
|
|
|
* Returns true if the given type is a range type.
|
2011-11-03 07:16:28 -04:00
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
type_is_range(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
return (get_typtype(typid) == TYPTYPE_RANGE);
|
|
|
|
|
}
|
|
|
|
|
|
Replace the hard-wired type knowledge in TypeCategory() and IsPreferredType()
with system catalog lookups, as was foreseen to be necessary almost since
their creation. Instead put the information into two new pg_type columns,
typcategory and typispreferred. Add support for setting these when
creating a user-defined base type.
The category column is just a "char" (i.e. a poor man's enum), allowing
a crude form of user extensibility of the category list: just use an
otherwise-unused character. This seems sufficient for foreseen uses,
but we could upgrade to having an actual category catalog someday, if
there proves to be a huge demand for custom type categories.
In this patch I have attempted to hew exactly to the behavior of the
previous hardwired logic, except for introducing new type categories for
arrays, composites, and enums. In particular the default preferred state
for user-defined types remains TRUE. That seems worth revisiting, but it
should be done as a separate patch from introducing the infrastructure.
Likewise, any adjustment of the standard set of categories should be done
separately.
2008-07-30 13:05:05 -04:00
|
|
|
/*
|
|
|
|
|
* get_type_category_preferred
|
|
|
|
|
*
|
|
|
|
|
* Given the type OID, fetch its category and preferred-type status.
|
|
|
|
|
* Throws error on failure.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
Form_pg_type typtup;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
Replace the hard-wired type knowledge in TypeCategory() and IsPreferredType()
with system catalog lookups, as was foreseen to be necessary almost since
their creation. Instead put the information into two new pg_type columns,
typcategory and typispreferred. Add support for setting these when
creating a user-defined base type.
The category column is just a "char" (i.e. a poor man's enum), allowing
a crude form of user extensibility of the category list: just use an
otherwise-unused character. This seems sufficient for foreseen uses,
but we could upgrade to having an actual category catalog someday, if
there proves to be a huge demand for custom type categories.
In this patch I have attempted to hew exactly to the behavior of the
previous hardwired logic, except for introducing new type categories for
arrays, composites, and enums. In particular the default preferred state
for user-defined types remains TRUE. That seems worth revisiting, but it
should be done as a separate patch from introducing the infrastructure.
Likewise, any adjustment of the standard set of categories should be done
separately.
2008-07-30 13:05:05 -04:00
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
|
elog(ERROR, "cache lookup failed for type %u", typid);
|
|
|
|
|
typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
*typcategory = typtup->typcategory;
|
|
|
|
|
*typispreferred = typtup->typispreferred;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-19 19:40:56 -04:00
|
|
|
/*
|
|
|
|
|
* get_typ_typrelid
|
|
|
|
|
*
|
|
|
|
|
* Given the type OID, get the typrelid (InvalidOid if not a complex
|
|
|
|
|
* type).
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_typ_typrelid(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2002-09-19 19:40:56 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
result = typtup->typrelid;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-08 19:20:04 -04:00
|
|
|
/*
|
|
|
|
|
* get_element_type
|
|
|
|
|
*
|
|
|
|
|
* Given the type OID, get the typelem (InvalidOid if not an array type).
|
|
|
|
|
*
|
|
|
|
|
* NB: this only considers varlena arrays to be true arrays; InvalidOid is
|
|
|
|
|
* returned if the input is a fixed-length array type.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_element_type(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2003-04-08 19:20:04 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
if (typtup->typlen == -1)
|
|
|
|
|
result = typtup->typelem;
|
|
|
|
|
else
|
|
|
|
|
result = InvalidOid;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get_array_type
|
|
|
|
|
*
|
Support arrays of composite types, including the rowtypes of regular tables
and views (but not system catalogs, nor sequences or toast tables). Get rid
of the hardwired convention that a type's array type is named exactly "_type",
instead using a new column pg_type.typarray to provide the linkage. (It still
will be named "_type", though, except in odd corner cases such as
maximum-length type names.)
Along the way, make tracking of owner and schema dependencies for types more
uniform: a type directly created by the user has these dependencies, while a
table rowtype or auto-generated array type does not have them, but depends on
its parent object instead.
David Fetter, Andrew Dunstan, Tom Lane
2007-05-11 13:57:14 -04:00
|
|
|
* Given the type OID, get the corresponding "true" array type.
|
2003-04-08 19:20:04 -04:00
|
|
|
* Returns InvalidOid if no array type can be found.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_array_type(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
2007-11-15 16:14:46 -05:00
|
|
|
Oid result = InvalidOid;
|
2003-04-08 19:20:04 -04:00
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2003-04-08 19:20:04 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
Support arrays of composite types, including the rowtypes of regular tables
and views (but not system catalogs, nor sequences or toast tables). Get rid
of the hardwired convention that a type's array type is named exactly "_type",
instead using a new column pg_type.typarray to provide the linkage. (It still
will be named "_type", though, except in odd corner cases such as
maximum-length type names.)
Along the way, make tracking of owner and schema dependencies for types more
uniform: a type directly created by the user has these dependencies, while a
table rowtype or auto-generated array type does not have them, but depends on
its parent object instead.
David Fetter, Andrew Dunstan, Tom Lane
2007-05-11 13:57:14 -04:00
|
|
|
result = ((Form_pg_type) GETSTRUCT(tp))->typarray;
|
2003-04-08 19:20:04 -04:00
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
}
|
Support arrays of composite types, including the rowtypes of regular tables
and views (but not system catalogs, nor sequences or toast tables). Get rid
of the hardwired convention that a type's array type is named exactly "_type",
instead using a new column pg_type.typarray to provide the linkage. (It still
will be named "_type", though, except in odd corner cases such as
maximum-length type names.)
Along the way, make tracking of owner and schema dependencies for types more
uniform: a type directly created by the user has these dependencies, while a
table rowtype or auto-generated array type does not have them, but depends on
its parent object instead.
David Fetter, Andrew Dunstan, Tom Lane
2007-05-11 13:57:14 -04:00
|
|
|
return result;
|
2003-04-08 19:20:04 -04:00
|
|
|
}
|
|
|
|
|
|
2014-11-25 12:21:22 -05:00
|
|
|
/*
|
|
|
|
|
* get_promoted_array_type
|
|
|
|
|
*
|
|
|
|
|
* The "promoted" type is what you'd get from an ARRAY(SELECT ...)
|
|
|
|
|
* construct, that is, either the corresponding "true" array type
|
|
|
|
|
* if the input is a scalar type that has such an array type,
|
|
|
|
|
* or the same type if the input is already a "true" array type.
|
|
|
|
|
* Returns InvalidOid if neither rule is satisfied.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_promoted_array_type(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
Oid array_type = get_array_type(typid);
|
|
|
|
|
|
|
|
|
|
if (OidIsValid(array_type))
|
|
|
|
|
return array_type;
|
|
|
|
|
if (OidIsValid(get_element_type(typid)))
|
|
|
|
|
return typid;
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
Improve handling of domains over arrays.
This patch eliminates various bizarre behaviors caused by sloppy thinking
about the difference between a domain type and its underlying array type.
In particular, the operation of updating one element of such an array
has to be considered as yielding a value of the underlying array type,
*not* a value of the domain, because there's no assurance that the
domain's CHECK constraints are still satisfied. If we're intending to
store the result back into a domain column, we have to re-cast to the
domain type so that constraints are re-checked.
For similar reasons, such a domain can't be blindly matched to an ANYARRAY
polymorphic parameter, because the polymorphic function is likely to apply
array-ish operations that could invalidate the domain constraints. For the
moment, we just forbid such matching. We might later wish to insert an
automatic downcast to the underlying array type, but such a change should
also change matching of domains to ANYELEMENT for consistency.
To ensure that all such logic is rechecked, this patch removes the original
hack of setting a domain's pg_type.typelem field to match its base type;
the typelem will always be zero instead. In those places where it's really
okay to look through the domain type with no other logic changes, use the
newly added get_base_element_type function in place of get_element_type.
catversion bumped due to change in pg_type contents.
Per bug #5717 from Richard Huxton and subsequent discussion.
2010-10-21 16:07:17 -04:00
|
|
|
/*
|
|
|
|
|
* get_base_element_type
|
|
|
|
|
* Given the type OID, get the typelem, looking "through" any domain
|
|
|
|
|
* to its underlying array type.
|
|
|
|
|
*
|
|
|
|
|
* This is equivalent to get_element_type(getBaseType(typid)), but avoids
|
|
|
|
|
* an extra cache lookup. Note that it fails to provide any information
|
|
|
|
|
* about the typmod of the array.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_base_element_type(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* We loop to find the bottom base type in a stack of domains.
|
|
|
|
|
*/
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tup;
|
|
|
|
|
Form_pg_type typTup;
|
|
|
|
|
|
|
|
|
|
tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
|
break;
|
|
|
|
|
typTup = (Form_pg_type) GETSTRUCT(tup);
|
|
|
|
|
if (typTup->typtype != TYPTYPE_DOMAIN)
|
|
|
|
|
{
|
|
|
|
|
/* Not a domain, so stop descending */
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
/* This test must match get_element_type */
|
|
|
|
|
if (typTup->typlen == -1)
|
|
|
|
|
result = typTup->typelem;
|
|
|
|
|
else
|
|
|
|
|
result = InvalidOid;
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typid = typTup->typbasetype;
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Like get_element_type, silently return InvalidOid for bogus input */
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-28 20:17:06 -04:00
|
|
|
/*
|
|
|
|
|
* getTypeInputInfo
|
|
|
|
|
*
|
|
|
|
|
* Get info needed for converting values of a type to internal form
|
|
|
|
|
*/
|
|
|
|
|
void
|
2004-06-05 20:41:28 -04:00
|
|
|
getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
|
2002-08-28 20:17:06 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
|
Form_pg_type pt;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
|
2002-08-28 20:17:06 -04:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", type);
|
2002-08-28 20:17:06 -04:00
|
|
|
pt = (Form_pg_type) GETSTRUCT(typeTuple);
|
|
|
|
|
|
|
|
|
|
if (!pt->typisdefined)
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("type %s is only a shell",
|
|
|
|
|
format_type_be(type))));
|
2003-05-09 14:08:48 -04:00
|
|
|
if (!OidIsValid(pt->typinput))
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
|
errmsg("no input function available for type %s",
|
|
|
|
|
format_type_be(type))));
|
2002-08-28 20:17:06 -04:00
|
|
|
|
|
|
|
|
*typInput = pt->typinput;
|
2004-06-05 20:41:28 -04:00
|
|
|
*typIOParam = getTypeIOParam(typeTuple);
|
2002-08-28 20:17:06 -04:00
|
|
|
|
|
|
|
|
ReleaseSysCache(typeTuple);
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-21 20:01:51 -04:00
|
|
|
/*
|
|
|
|
|
* getTypeOutputInfo
|
|
|
|
|
*
|
|
|
|
|
* Get info needed for printing values of a type
|
|
|
|
|
*/
|
2003-05-09 14:08:48 -04:00
|
|
|
void
|
2005-05-01 14:56:19 -04:00
|
|
|
getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
|
2002-08-21 20:01:51 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
|
Form_pg_type pt;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
|
2002-08-21 20:01:51 -04:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", type);
|
2002-08-21 20:01:51 -04:00
|
|
|
pt = (Form_pg_type) GETSTRUCT(typeTuple);
|
|
|
|
|
|
2003-05-09 14:08:48 -04:00
|
|
|
if (!pt->typisdefined)
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("type %s is only a shell",
|
|
|
|
|
format_type_be(type))));
|
2003-05-09 14:08:48 -04:00
|
|
|
if (!OidIsValid(pt->typoutput))
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
|
errmsg("no output function available for type %s",
|
|
|
|
|
format_type_be(type))));
|
2003-05-09 14:08:48 -04:00
|
|
|
|
2002-08-21 20:01:51 -04:00
|
|
|
*typOutput = pt->typoutput;
|
|
|
|
|
*typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
|
2003-05-09 14:08:48 -04:00
|
|
|
|
|
|
|
|
ReleaseSysCache(typeTuple);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* getTypeBinaryInputInfo
|
|
|
|
|
*
|
|
|
|
|
* Get info needed for binary input of values of a type
|
|
|
|
|
*/
|
|
|
|
|
void
|
2004-06-05 20:41:28 -04:00
|
|
|
getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
|
2003-05-09 14:08:48 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
|
Form_pg_type pt;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
|
2003-05-09 14:08:48 -04:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", type);
|
2003-05-09 14:08:48 -04:00
|
|
|
pt = (Form_pg_type) GETSTRUCT(typeTuple);
|
|
|
|
|
|
|
|
|
|
if (!pt->typisdefined)
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("type %s is only a shell",
|
|
|
|
|
format_type_be(type))));
|
2003-05-09 14:08:48 -04:00
|
|
|
if (!OidIsValid(pt->typreceive))
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
|
errmsg("no binary input function available for type %s",
|
|
|
|
|
format_type_be(type))));
|
2003-05-09 14:08:48 -04:00
|
|
|
|
|
|
|
|
*typReceive = pt->typreceive;
|
2004-06-05 20:41:28 -04:00
|
|
|
*typIOParam = getTypeIOParam(typeTuple);
|
2003-05-09 14:08:48 -04:00
|
|
|
|
|
|
|
|
ReleaseSysCache(typeTuple);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* getTypeBinaryOutputInfo
|
|
|
|
|
*
|
|
|
|
|
* Get info needed for binary output of values of a type
|
|
|
|
|
*/
|
|
|
|
|
void
|
2005-05-01 14:56:19 -04:00
|
|
|
getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
|
2003-05-09 14:08:48 -04:00
|
|
|
{
|
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
|
Form_pg_type pt;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
|
2003-05-09 14:08:48 -04:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", type);
|
2003-05-09 14:08:48 -04:00
|
|
|
pt = (Form_pg_type) GETSTRUCT(typeTuple);
|
|
|
|
|
|
|
|
|
|
if (!pt->typisdefined)
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("type %s is only a shell",
|
|
|
|
|
format_type_be(type))));
|
2003-05-09 14:08:48 -04:00
|
|
|
if (!OidIsValid(pt->typsend))
|
2003-07-25 16:18:01 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
|
errmsg("no binary output function available for type %s",
|
|
|
|
|
format_type_be(type))));
|
2003-05-09 14:08:48 -04:00
|
|
|
|
|
|
|
|
*typSend = pt->typsend;
|
|
|
|
|
*typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
|
|
|
|
|
|
2002-08-21 20:01:51 -04:00
|
|
|
ReleaseSysCache(typeTuple);
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-30 16:21:56 -05:00
|
|
|
/*
|
|
|
|
|
* get_typmodin
|
|
|
|
|
*
|
|
|
|
|
* Given the type OID, return the type's typmodin procedure, if any.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_typmodin(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2006-12-30 16:21:56 -05:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
result = typtup->typmodin;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef NOT_USED
|
|
|
|
|
/*
|
|
|
|
|
* get_typmodout
|
|
|
|
|
*
|
|
|
|
|
* Given the type OID, return the type's typmodout procedure, if any.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_typmodout(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
2006-12-30 16:21:56 -05:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
result = typtup->typmodout;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 15:18:54 -04:00
|
|
|
#endif /* NOT_USED */
|
2006-12-30 16:21:56 -05:00
|
|
|
|
2011-02-08 16:04:18 -05:00
|
|
|
/*
|
|
|
|
|
* get_typcollation
|
|
|
|
|
*
|
|
|
|
|
* Given the type OID, return the type's typcollation attribute.
|
|
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
get_typcollation(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
|
|
|
|
|
|
|
|
|
result = typtup->typcollation;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* type_is_collatable
|
|
|
|
|
*
|
|
|
|
|
* Return whether the type cares about collations
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
type_is_collatable(Oid typid)
|
|
|
|
|
{
|
|
|
|
|
return OidIsValid(get_typcollation(typid));
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-06 20:43:27 -04:00
|
|
|
|
|
|
|
|
/* ---------- STATISTICS CACHE ---------- */
|
|
|
|
|
|
2001-05-08 20:35:09 -04:00
|
|
|
/*
|
|
|
|
|
* get_attavgwidth
|
|
|
|
|
*
|
|
|
|
|
* Given the table and attribute number of a column, get the average
|
|
|
|
|
* width of entries in the column. Return zero if no data available.
|
2008-09-28 15:51:40 -04:00
|
|
|
*
|
2009-12-29 15:11:45 -05:00
|
|
|
* Currently this is only consulted for individual tables, not for inheritance
|
|
|
|
|
* trees, so we don't need an "inh" parameter.
|
|
|
|
|
*
|
2008-09-28 15:51:40 -04:00
|
|
|
* Calling a hook at this point looks somewhat strange, but is required
|
|
|
|
|
* because the optimizer calls this function without any other way for
|
|
|
|
|
* plug-ins to control the result.
|
2001-05-08 20:35:09 -04:00
|
|
|
*/
|
|
|
|
|
int32
|
|
|
|
|
get_attavgwidth(Oid relid, AttrNumber attnum)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
2008-09-28 15:51:40 -04:00
|
|
|
int32 stawidth;
|
2001-05-08 20:35:09 -04:00
|
|
|
|
2008-09-28 15:51:40 -04:00
|
|
|
if (get_attavgwidth_hook)
|
|
|
|
|
{
|
|
|
|
|
stawidth = (*get_attavgwidth_hook) (relid, attnum);
|
|
|
|
|
if (stawidth > 0)
|
|
|
|
|
return stawidth;
|
|
|
|
|
}
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache3(STATRELATTINH,
|
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
|
Int16GetDatum(attnum),
|
|
|
|
|
BoolGetDatum(false));
|
2001-05-08 20:35:09 -04:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
2008-09-28 15:51:40 -04:00
|
|
|
stawidth = ((Form_pg_statistic) GETSTRUCT(tp))->stawidth;
|
2001-05-08 20:35:09 -04:00
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
if (stawidth > 0)
|
|
|
|
|
return stawidth;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-06 20:43:27 -04:00
|
|
|
/*
|
|
|
|
|
* get_attstatsslot
|
|
|
|
|
*
|
|
|
|
|
* Extract the contents of a "slot" of a pg_statistic tuple.
|
|
|
|
|
* Returns TRUE if requested slot type was found, else FALSE.
|
|
|
|
|
*
|
|
|
|
|
* Unlike other routines in this file, this takes a pointer to an
|
|
|
|
|
* already-looked-up tuple in the pg_statistic cache. We do this since
|
|
|
|
|
* most callers will want to extract more than one value from the cache
|
|
|
|
|
* entry, and we don't want to repeat the cache lookup unnecessarily.
|
2008-09-28 15:51:40 -04:00
|
|
|
* Also, this API allows this routine to be used with statistics tuples
|
|
|
|
|
* that have been provided by a stats hook and didn't really come from
|
|
|
|
|
* pg_statistic.
|
2001-05-06 20:43:27 -04:00
|
|
|
*
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
* sslot: pointer to output area (typically, a local variable in the caller).
|
2017-01-25 14:35:31 -05:00
|
|
|
* statstuple: pg_statistic tuple to be examined.
|
2001-05-06 20:43:27 -04:00
|
|
|
* reqkind: STAKIND code for desired statistics slot kind.
|
|
|
|
|
* reqop: STAOP value wanted, or InvalidOid if don't care.
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
* flags: bitmask of ATTSTATSSLOT_VALUES and/or ATTSTATSSLOT_NUMBERS.
|
2001-05-06 20:43:27 -04:00
|
|
|
*
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
* If a matching slot is found, TRUE is returned, and *sslot is filled thus:
|
|
|
|
|
* staop: receives the actual STAOP value.
|
|
|
|
|
* valuetype: receives actual datatype of the elements of stavalues.
|
|
|
|
|
* values: receives pointer to an array of the slot's stavalues.
|
|
|
|
|
* nvalues: receives number of stavalues.
|
|
|
|
|
* numbers: receives pointer to an array of the slot's stanumbers (as float4).
|
|
|
|
|
* nnumbers: receives number of stanumbers.
|
2010-07-09 18:57:39 -04:00
|
|
|
*
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
* valuetype/values/nvalues are InvalidOid/NULL/0 if ATTSTATSSLOT_VALUES
|
|
|
|
|
* wasn't specified. Likewise, numbers/nnumbers are NULL/0 if
|
|
|
|
|
* ATTSTATSSLOT_NUMBERS wasn't specified.
|
|
|
|
|
*
|
|
|
|
|
* If no matching slot is found, FALSE is returned, and *sslot is zeroed.
|
|
|
|
|
*
|
|
|
|
|
* The data referred to by the fields of sslot is locally palloc'd and
|
|
|
|
|
* is independent of the original pg_statistic tuple. When the caller
|
|
|
|
|
* is done with it, call free_attstatsslot to release the palloc'd data.
|
|
|
|
|
*
|
|
|
|
|
* If it's desirable to call free_attstatsslot when get_attstatsslot might
|
|
|
|
|
* not have been called, memset'ing sslot to zeroes will allow that.
|
2001-05-06 20:43:27 -04:00
|
|
|
*/
|
|
|
|
|
bool
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
|
|
|
|
|
int reqkind, Oid reqop, int flags)
|
2001-05-06 20:43:27 -04:00
|
|
|
{
|
|
|
|
|
Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple);
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
int i;
|
2001-05-06 20:43:27 -04:00
|
|
|
Datum val;
|
|
|
|
|
bool isnull;
|
|
|
|
|
ArrayType *statarray;
|
2010-07-09 18:57:39 -04:00
|
|
|
Oid arrayelemtype;
|
2001-05-06 20:43:27 -04:00
|
|
|
int narrayelem;
|
|
|
|
|
HeapTuple typeTuple;
|
2003-03-23 00:14:37 -05:00
|
|
|
Form_pg_type typeForm;
|
2001-05-06 20:43:27 -04:00
|
|
|
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
/* initialize *sslot properly */
|
|
|
|
|
memset(sslot, 0, sizeof(AttStatsSlot));
|
|
|
|
|
|
2001-05-06 20:43:27 -04:00
|
|
|
for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
|
|
|
|
|
{
|
|
|
|
|
if ((&stats->stakind1)[i] == reqkind &&
|
|
|
|
|
(reqop == InvalidOid || (&stats->staop1)[i] == reqop))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (i >= STATISTIC_NUM_SLOTS)
|
|
|
|
|
return false; /* not there */
|
|
|
|
|
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
sslot->staop = (&stats->staop1)[i];
|
2010-01-03 21:44:40 -05:00
|
|
|
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
if (flags & ATTSTATSSLOT_VALUES)
|
2001-05-06 20:43:27 -04:00
|
|
|
{
|
2009-12-29 15:11:45 -05:00
|
|
|
val = SysCacheGetAttr(STATRELATTINH, statstuple,
|
2001-05-06 20:43:27 -04:00
|
|
|
Anum_pg_statistic_stavalues1 + i,
|
|
|
|
|
&isnull);
|
|
|
|
|
if (isnull)
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "stavalues is null");
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2010-07-09 18:57:39 -04:00
|
|
|
/*
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
* Detoast the array if needed, and in any case make a copy that's
|
|
|
|
|
* under control of this AttStatsSlot.
|
2010-07-09 18:57:39 -04:00
|
|
|
*/
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
statarray = DatumGetArrayTypePCopy(val);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Extract the actual array element type, and pass it back in case the
|
|
|
|
|
* caller needs it.
|
|
|
|
|
*/
|
|
|
|
|
sslot->valuetype = arrayelemtype = ARR_ELEMTYPE(statarray);
|
|
|
|
|
|
|
|
|
|
/* Need info about element type */
|
2010-07-09 18:57:39 -04:00
|
|
|
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(arrayelemtype));
|
2001-05-06 20:43:27 -04:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
2010-07-09 18:57:39 -04:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", arrayelemtype);
|
2003-03-23 00:14:37 -05:00
|
|
|
typeForm = (Form_pg_type) GETSTRUCT(typeTuple);
|
|
|
|
|
|
2005-11-17 17:14:56 -05:00
|
|
|
/* Deconstruct array into Datum elements; NULLs not expected */
|
2003-03-23 00:14:37 -05:00
|
|
|
deconstruct_array(statarray,
|
2010-07-09 18:57:39 -04:00
|
|
|
arrayelemtype,
|
2003-03-23 00:14:37 -05:00
|
|
|
typeForm->typlen,
|
|
|
|
|
typeForm->typbyval,
|
|
|
|
|
typeForm->typalign,
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
&sslot->values, NULL, &sslot->nvalues);
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2001-05-06 20:43:27 -04:00
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* If the element type is pass-by-reference, we now have a bunch of
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
* Datums that are pointers into the statarray, so we need to keep
|
|
|
|
|
* that until free_attstatsslot. Otherwise, all the useful info is in
|
|
|
|
|
* sslot->values[], so we can free the array object immediately.
|
2001-05-06 20:43:27 -04:00
|
|
|
*/
|
2003-03-23 00:14:37 -05:00
|
|
|
if (!typeForm->typbyval)
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
sslot->values_arr = statarray;
|
|
|
|
|
else
|
|
|
|
|
pfree(statarray);
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2003-03-23 00:14:37 -05:00
|
|
|
ReleaseSysCache(typeTuple);
|
2001-05-06 20:43:27 -04:00
|
|
|
}
|
|
|
|
|
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
if (flags & ATTSTATSSLOT_NUMBERS)
|
2001-05-06 20:43:27 -04:00
|
|
|
{
|
2009-12-29 15:11:45 -05:00
|
|
|
val = SysCacheGetAttr(STATRELATTINH, statstuple,
|
2001-05-06 20:43:27 -04:00
|
|
|
Anum_pg_statistic_stanumbers1 + i,
|
|
|
|
|
&isnull);
|
|
|
|
|
if (isnull)
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "stanumbers is null");
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Detoast the array if needed, and in any case make a copy that's
|
|
|
|
|
* under control of this AttStatsSlot.
|
|
|
|
|
*/
|
|
|
|
|
statarray = DatumGetArrayTypePCopy(val);
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2001-05-06 20:43:27 -04:00
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* We expect the array to be a 1-D float4 array; verify that. We don't
|
|
|
|
|
* need to use deconstruct_array() since the array data is just going
|
|
|
|
|
* to look like a C array of float4 values.
|
2001-05-06 20:43:27 -04:00
|
|
|
*/
|
|
|
|
|
narrayelem = ARR_DIMS(statarray)[0];
|
|
|
|
|
if (ARR_NDIM(statarray) != 1 || narrayelem <= 0 ||
|
2005-11-17 17:14:56 -05:00
|
|
|
ARR_HASNULL(statarray) ||
|
2002-08-26 13:54:02 -04:00
|
|
|
ARR_ELEMTYPE(statarray) != FLOAT4OID)
|
2003-07-25 16:18:01 -04:00
|
|
|
elog(ERROR, "stanumbers is not a 1-D float4 array");
|
2001-10-25 01:50:21 -04:00
|
|
|
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
/* Give caller a pointer directly into the statarray */
|
|
|
|
|
sslot->numbers = (float4 *) ARR_DATA_PTR(statarray);
|
|
|
|
|
sslot->nnumbers = narrayelem;
|
|
|
|
|
|
|
|
|
|
/* We'll free the statarray in free_attstatsslot */
|
|
|
|
|
sslot->numbers_arr = statarray;
|
2001-05-06 20:43:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-11 13:27:14 -04:00
|
|
|
/*
|
|
|
|
|
* free_attstatsslot
|
|
|
|
|
* Free data allocated by get_attstatsslot
|
|
|
|
|
*/
|
2001-05-06 20:43:27 -04:00
|
|
|
void
|
Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed.
The mess cleaned up in commit da0759600 is clear evidence that it's a
bug hazard to expect the caller of get_attstatsslot()/free_attstatsslot()
to provide the correct type OID for the array elements in the slot.
Moreover, we weren't even getting any performance benefit from that,
since get_attstatsslot() was extracting the real type OID from the array
anyway. So we ought to get rid of that requirement; indeed, it would
make more sense for get_attstatsslot() to pass back the type OID it found,
in case the caller isn't sure what to expect, which is likely in binary-
compatible-operator cases.
Another problem with the current implementation is that if the stats array
element type is pass-by-reference, we incur a palloc/memcpy/pfree cycle
for each element. That seemed acceptable when the code was written because
we were targeting O(10) array sizes --- but these days, stats arrays are
almost always bigger than that, sometimes much bigger. We can save a
significant number of cycles by doing one palloc/memcpy/pfree of the whole
array. Indeed, in the now-probably-common case where the array is toasted,
that happens anyway so this method is basically free. (Note: although the
catcache code will inline any out-of-line toasted values, it doesn't
decompress them. At the other end of the size range, it doesn't expand
short-header datums either. In either case, DatumGetArrayTypeP would have
to make a copy. We do end up using an extra array copy step if the element
type is pass-by-value and the array length is neither small enough for a
short header nor large enough to have suffered compression. But that
seems like a very acceptable price for winning in pass-by-ref cases.)
Hence, redesign to take these insights into account. While at it,
convert to an API in which we fill a struct rather than passing a bunch
of pointers to individual output arguments. That will make it less
painful if we ever want further expansion of what get_attstatsslot can
pass back.
It's certainly arguable that this is new development and not something to
push post-feature-freeze. However, I view it as primarily bug-proofing
and therefore something that's better to have sooner not later. Since
we aren't quite at beta phase yet, let's put it in.
Discussion: https://postgr.es/m/16364.1494520862@sss.pgh.pa.us
2017-05-13 15:14:39 -04:00
|
|
|
free_attstatsslot(AttStatsSlot *sslot)
|
|
|
|
|
{
|
|
|
|
|
/* The values[] array was separately palloc'd by deconstruct_array */
|
|
|
|
|
if (sslot->values)
|
|
|
|
|
pfree(sslot->values);
|
|
|
|
|
/* The numbers[] array points into numbers_arr, do not pfree it */
|
|
|
|
|
/* Free the detoasted array objects, if any */
|
|
|
|
|
if (sslot->values_arr)
|
|
|
|
|
pfree(sslot->values_arr);
|
|
|
|
|
if (sslot->numbers_arr)
|
|
|
|
|
pfree(sslot->numbers_arr);
|
2001-05-06 20:43:27 -04:00
|
|
|
}
|
2001-06-13 21:09:22 -04:00
|
|
|
|
2002-04-01 20:03:07 -05:00
|
|
|
/* ---------- PG_NAMESPACE CACHE ---------- */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get_namespace_name
|
|
|
|
|
* Returns the name of a given namespace
|
|
|
|
|
*
|
|
|
|
|
* Returns a palloc'd copy of the string, or NULL if no such namespace.
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
get_namespace_name(Oid nspid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
2010-02-14 13:42:19 -05:00
|
|
|
tp = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(nspid));
|
2002-04-01 20:03:07 -05:00
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
|
|
|
|
Form_pg_namespace nsptup = (Form_pg_namespace) GETSTRUCT(tp);
|
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
|
|
result = pstrdup(NameStr(nsptup->nspname));
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2011-11-03 07:16:28 -04:00
|
|
|
|
2015-04-06 10:40:55 -04:00
|
|
|
/*
|
|
|
|
|
* get_namespace_name_or_temp
|
|
|
|
|
* As above, but if it is this backend's temporary namespace, return
|
|
|
|
|
* "pg_temp" instead.
|
|
|
|
|
*/
|
|
|
|
|
char *
|
|
|
|
|
get_namespace_name_or_temp(Oid nspid)
|
|
|
|
|
{
|
|
|
|
|
if (isTempNamespace(nspid))
|
|
|
|
|
return "pg_temp";
|
|
|
|
|
else
|
|
|
|
|
return get_namespace_name(nspid);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-20 23:50:27 -05:00
|
|
|
/* ---------- PG_RANGE CACHE ---------- */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get_range_subtype
|
|
|
|
|
* Returns the subtype of a given range type
|
|
|
|
|
*
|
|
|
|
|
* Returns InvalidOid if the type is not a range type.
|
|
|
|
|
*/
|
2011-11-03 07:16:28 -04:00
|
|
|
Oid
|
|
|
|
|
get_range_subtype(Oid rangeOid)
|
|
|
|
|
{
|
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
|
|
|
|
tp = SearchSysCache1(RANGETYPE, ObjectIdGetDatum(rangeOid));
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
|
{
|
2012-06-10 15:20:04 -04:00
|
|
|
Form_pg_range rngtup = (Form_pg_range) GETSTRUCT(tp);
|
|
|
|
|
Oid result;
|
2011-11-03 07:16:28 -04:00
|
|
|
|
|
|
|
|
result = rngtup->rngsubtype;
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|