|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* partcache.c
|
|
|
|
* Support routines for manipulating partition information cached in
|
|
|
|
* relcache
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/utils/cache/partcache.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/hash.h"
|
|
|
|
#include "access/htup_details.h"
|
|
|
|
#include "access/nbtree.h"
|
|
|
|
#include "access/relation.h"
|
|
|
|
#include "catalog/partition.h"
|
|
|
|
#include "catalog/pg_inherits.h"
|
|
|
|
#include "catalog/pg_opclass.h"
|
|
|
|
#include "catalog/pg_partitioned_table.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "nodes/makefuncs.h"
|
|
|
|
#include "nodes/nodeFuncs.h"
|
|
|
|
#include "optimizer/optimizer.h"
|
|
|
|
#include "partitioning/partbounds.h"
|
|
|
|
#include "rewrite/rewriteHandler.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/datum.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/memutils.h"
|
|
|
|
#include "utils/partcache.h"
|
|
|
|
#include "utils/rel.h"
|
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
|
|
|
|
Load relcache entries' partitioning data on-demand, not immediately.
Formerly the rd_partkey and rd_partdesc data structures were always
populated immediately when a relcache entry was built or rebuilt.
This patch changes things so that they are populated only when they
are first requested. (Hence, callers *must* now always use
RelationGetPartitionKey or RelationGetPartitionDesc; just fetching
the pointer directly is no longer acceptable.)
This seems to have some performance benefits, but the main reason to do
it is that it eliminates a recursive-reload failure that occurs if the
partkey or partdesc expressions contain any references to the relation's
rowtype (as discovered by Amit Langote). In retrospect, since loading
these data structures might result in execution of nearly-arbitrary code
via eval_const_expressions, it was a dumb idea to require that to happen
during relcache entry rebuild.
Also, fix things so that old copies of a relcache partition descriptor
will be dropped when the cache entry's refcount goes to zero. In the
previous coding it was possible for such copies to survive for the
lifetime of the session, as I'd complained of in a previous discussion.
(This management technique still isn't perfect, but it's better than
before.) Improve the commentary explaining how that works and why
it's safe to hand out direct pointers to these relcache substructures.
In passing, improve RelationBuildPartitionDesc by using the same
memory-context-parent-swap approach used by RelationBuildPartitionKey,
thereby making it less dependent on strong assumptions about what
partition_bounds_copy does. Avoid doing get_rel_relkind in the
critical section, too.
Patch by Amit Langote and Tom Lane; Robert Haas deserves some credit
for prior work in the area, too. Although this is a pre-existing
problem, no back-patch: the patch seems too invasive to be safe to
back-patch, and the bug it fixes is a corner case that seems
relatively unlikely to cause problems in the field.
Discussion: https://postgr.es/m/CA+HiwqFUzjfj9HEsJtYWcr1SgQ_=iCAvQ=O2Sx6aQxoDu4OiHw@mail.gmail.com
Discussion: https://postgr.es/m/CA+TgmoY3bRmGB6-DUnoVy5fJoreiBJ43rwMrQRCdPXuKt4Ykaw@mail.gmail.com
6 years ago
|
|
|
static void RelationBuildPartitionKey(Relation relation);
|
|
|
|
static List *generate_partition_qual(Relation rel);
|
|
|
|
|
Load relcache entries' partitioning data on-demand, not immediately.
Formerly the rd_partkey and rd_partdesc data structures were always
populated immediately when a relcache entry was built or rebuilt.
This patch changes things so that they are populated only when they
are first requested. (Hence, callers *must* now always use
RelationGetPartitionKey or RelationGetPartitionDesc; just fetching
the pointer directly is no longer acceptable.)
This seems to have some performance benefits, but the main reason to do
it is that it eliminates a recursive-reload failure that occurs if the
partkey or partdesc expressions contain any references to the relation's
rowtype (as discovered by Amit Langote). In retrospect, since loading
these data structures might result in execution of nearly-arbitrary code
via eval_const_expressions, it was a dumb idea to require that to happen
during relcache entry rebuild.
Also, fix things so that old copies of a relcache partition descriptor
will be dropped when the cache entry's refcount goes to zero. In the
previous coding it was possible for such copies to survive for the
lifetime of the session, as I'd complained of in a previous discussion.
(This management technique still isn't perfect, but it's better than
before.) Improve the commentary explaining how that works and why
it's safe to hand out direct pointers to these relcache substructures.
In passing, improve RelationBuildPartitionDesc by using the same
memory-context-parent-swap approach used by RelationBuildPartitionKey,
thereby making it less dependent on strong assumptions about what
partition_bounds_copy does. Avoid doing get_rel_relkind in the
critical section, too.
Patch by Amit Langote and Tom Lane; Robert Haas deserves some credit
for prior work in the area, too. Although this is a pre-existing
problem, no back-patch: the patch seems too invasive to be safe to
back-patch, and the bug it fixes is a corner case that seems
relatively unlikely to cause problems in the field.
Discussion: https://postgr.es/m/CA+HiwqFUzjfj9HEsJtYWcr1SgQ_=iCAvQ=O2Sx6aQxoDu4OiHw@mail.gmail.com
Discussion: https://postgr.es/m/CA+TgmoY3bRmGB6-DUnoVy5fJoreiBJ43rwMrQRCdPXuKt4Ykaw@mail.gmail.com
6 years ago
|
|
|
/*
|
|
|
|
* RelationGetPartitionKey -- get partition key, if relation is partitioned
|
|
|
|
*
|
|
|
|
* Note: partition keys are not allowed to change after the partitioned rel
|
|
|
|
* is created. RelationClearRelation knows this and preserves rd_partkey
|
|
|
|
* across relcache rebuilds, as long as the relation is open. Therefore,
|
|
|
|
* even though we hand back a direct pointer into the relcache entry, it's
|
|
|
|
* safe for callers to continue to use that pointer as long as they hold
|
|
|
|
* the relation open.
|
|
|
|
*/
|
|
|
|
PartitionKey
|
|
|
|
RelationGetPartitionKey(Relation rel)
|
|
|
|
{
|
|
|
|
if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (unlikely(rel->rd_partkey == NULL))
|
|
|
|
RelationBuildPartitionKey(rel);
|
|
|
|
|
|
|
|
return rel->rd_partkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelationBuildPartitionKey
|
|
|
|
* Build partition key data of relation, and attach to relcache
|
|
|
|
*
|
|
|
|
* Partitioning key data is a complex structure; to avoid complicated logic to
|
|
|
|
* free individual elements whenever the relcache entry is flushed, we give it
|
|
|
|
* its own memory context, a child of CacheMemoryContext, which can easily be
|
|
|
|
* deleted on its own. To avoid leaking memory in that context in case of an
|
|
|
|
* error partway through this function, the context is initially created as a
|
|
|
|
* child of CurTransactionContext and only re-parented to CacheMemoryContext
|
|
|
|
* at the end, when no further errors are possible. Also, we don't make this
|
|
|
|
* context the current context except in very brief code sections, out of fear
|
|
|
|
* that some of our callees allocate memory on their own which would be leaked
|
|
|
|
* permanently.
|
|
|
|
*/
|
Load relcache entries' partitioning data on-demand, not immediately.
Formerly the rd_partkey and rd_partdesc data structures were always
populated immediately when a relcache entry was built or rebuilt.
This patch changes things so that they are populated only when they
are first requested. (Hence, callers *must* now always use
RelationGetPartitionKey or RelationGetPartitionDesc; just fetching
the pointer directly is no longer acceptable.)
This seems to have some performance benefits, but the main reason to do
it is that it eliminates a recursive-reload failure that occurs if the
partkey or partdesc expressions contain any references to the relation's
rowtype (as discovered by Amit Langote). In retrospect, since loading
these data structures might result in execution of nearly-arbitrary code
via eval_const_expressions, it was a dumb idea to require that to happen
during relcache entry rebuild.
Also, fix things so that old copies of a relcache partition descriptor
will be dropped when the cache entry's refcount goes to zero. In the
previous coding it was possible for such copies to survive for the
lifetime of the session, as I'd complained of in a previous discussion.
(This management technique still isn't perfect, but it's better than
before.) Improve the commentary explaining how that works and why
it's safe to hand out direct pointers to these relcache substructures.
In passing, improve RelationBuildPartitionDesc by using the same
memory-context-parent-swap approach used by RelationBuildPartitionKey,
thereby making it less dependent on strong assumptions about what
partition_bounds_copy does. Avoid doing get_rel_relkind in the
critical section, too.
Patch by Amit Langote and Tom Lane; Robert Haas deserves some credit
for prior work in the area, too. Although this is a pre-existing
problem, no back-patch: the patch seems too invasive to be safe to
back-patch, and the bug it fixes is a corner case that seems
relatively unlikely to cause problems in the field.
Discussion: https://postgr.es/m/CA+HiwqFUzjfj9HEsJtYWcr1SgQ_=iCAvQ=O2Sx6aQxoDu4OiHw@mail.gmail.com
Discussion: https://postgr.es/m/CA+TgmoY3bRmGB6-DUnoVy5fJoreiBJ43rwMrQRCdPXuKt4Ykaw@mail.gmail.com
6 years ago
|
|
|
static void
|
|
|
|
RelationBuildPartitionKey(Relation relation)
|
|
|
|
{
|
|
|
|
Form_pg_partitioned_table form;
|
|
|
|
HeapTuple tuple;
|
|
|
|
bool isnull;
|
|
|
|
int i;
|
|
|
|
PartitionKey key;
|
|
|
|
AttrNumber *attrs;
|
|
|
|
oidvector *opclass;
|
|
|
|
oidvector *collation;
|
|
|
|
ListCell *partexprs_item;
|
|
|
|
Datum datum;
|
|
|
|
MemoryContext partkeycxt,
|
|
|
|
oldcxt;
|
|
|
|
int16 procnum;
|
|
|
|
|
|
|
|
tuple = SearchSysCache1(PARTRELID,
|
|
|
|
ObjectIdGetDatum(RelationGetRelid(relation)));
|
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
Load relcache entries' partitioning data on-demand, not immediately.
Formerly the rd_partkey and rd_partdesc data structures were always
populated immediately when a relcache entry was built or rebuilt.
This patch changes things so that they are populated only when they
are first requested. (Hence, callers *must* now always use
RelationGetPartitionKey or RelationGetPartitionDesc; just fetching
the pointer directly is no longer acceptable.)
This seems to have some performance benefits, but the main reason to do
it is that it eliminates a recursive-reload failure that occurs if the
partkey or partdesc expressions contain any references to the relation's
rowtype (as discovered by Amit Langote). In retrospect, since loading
these data structures might result in execution of nearly-arbitrary code
via eval_const_expressions, it was a dumb idea to require that to happen
during relcache entry rebuild.
Also, fix things so that old copies of a relcache partition descriptor
will be dropped when the cache entry's refcount goes to zero. In the
previous coding it was possible for such copies to survive for the
lifetime of the session, as I'd complained of in a previous discussion.
(This management technique still isn't perfect, but it's better than
before.) Improve the commentary explaining how that works and why
it's safe to hand out direct pointers to these relcache substructures.
In passing, improve RelationBuildPartitionDesc by using the same
memory-context-parent-swap approach used by RelationBuildPartitionKey,
thereby making it less dependent on strong assumptions about what
partition_bounds_copy does. Avoid doing get_rel_relkind in the
critical section, too.
Patch by Amit Langote and Tom Lane; Robert Haas deserves some credit
for prior work in the area, too. Although this is a pre-existing
problem, no back-patch: the patch seems too invasive to be safe to
back-patch, and the bug it fixes is a corner case that seems
relatively unlikely to cause problems in the field.
Discussion: https://postgr.es/m/CA+HiwqFUzjfj9HEsJtYWcr1SgQ_=iCAvQ=O2Sx6aQxoDu4OiHw@mail.gmail.com
Discussion: https://postgr.es/m/CA+TgmoY3bRmGB6-DUnoVy5fJoreiBJ43rwMrQRCdPXuKt4Ykaw@mail.gmail.com
6 years ago
|
|
|
elog(ERROR, "cache lookup failed for partition key of relation %u",
|
|
|
|
RelationGetRelid(relation));
|
|
|
|
|
|
|
|
partkeycxt = AllocSetContextCreate(CurTransactionContext,
|
|
|
|
"partition key",
|
|
|
|
ALLOCSET_SMALL_SIZES);
|
|
|
|
MemoryContextCopyAndSetIdentifier(partkeycxt,
|
|
|
|
RelationGetRelationName(relation));
|
|
|
|
|
|
|
|
key = (PartitionKey) MemoryContextAllocZero(partkeycxt,
|
|
|
|
sizeof(PartitionKeyData));
|
|
|
|
|
|
|
|
/* Fixed-length attributes */
|
|
|
|
form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
|
|
|
|
key->strategy = form->partstrat;
|
|
|
|
key->partnatts = form->partnatts;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can rely on the first variable-length attribute being mapped to the
|
|
|
|
* relevant field of the catalog's C struct, because all previous
|
|
|
|
* attributes are non-nullable and fixed-length.
|
|
|
|
*/
|
|
|
|
attrs = form->partattrs.values;
|
|
|
|
|
|
|
|
/* But use the hard way to retrieve further variable-length attributes */
|
|
|
|
/* Operator class */
|
|
|
|
datum = SysCacheGetAttr(PARTRELID, tuple,
|
|
|
|
Anum_pg_partitioned_table_partclass, &isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
opclass = (oidvector *) DatumGetPointer(datum);
|
|
|
|
|
|
|
|
/* Collation */
|
|
|
|
datum = SysCacheGetAttr(PARTRELID, tuple,
|
|
|
|
Anum_pg_partitioned_table_partcollation, &isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
collation = (oidvector *) DatumGetPointer(datum);
|
|
|
|
|
|
|
|
/* Expressions */
|
|
|
|
datum = SysCacheGetAttr(PARTRELID, tuple,
|
|
|
|
Anum_pg_partitioned_table_partexprs, &isnull);
|
|
|
|
if (!isnull)
|
|
|
|
{
|
|
|
|
char *exprString;
|
|
|
|
Node *expr;
|
|
|
|
|
|
|
|
exprString = TextDatumGetCString(datum);
|
|
|
|
expr = stringToNode(exprString);
|
|
|
|
pfree(exprString);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the expressions through const-simplification since the planner
|
|
|
|
* will be comparing them to similarly-processed qual clause operands,
|
|
|
|
* and may fail to detect valid matches without this step; fix
|
|
|
|
* opfuncids while at it. We don't need to bother with
|
|
|
|
* canonicalize_qual() though, because partition expressions should be
|
|
|
|
* in canonical form already (ie, no need for OR-merging or constant
|
|
|
|
* elimination).
|
|
|
|
*/
|
|
|
|
expr = eval_const_expressions(NULL, expr);
|
|
|
|
fix_opfuncids(expr);
|
|
|
|
|
|
|
|
oldcxt = MemoryContextSwitchTo(partkeycxt);
|
|
|
|
key->partexprs = (List *) copyObject(expr);
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate assorted arrays in the partkeycxt, which we'll fill below */
|
|
|
|
oldcxt = MemoryContextSwitchTo(partkeycxt);
|
|
|
|
key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
|
|
|
|
key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
|
|
|
|
key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
|
|
|
|
key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
|
|
|
|
|
|
|
|
key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
|
|
|
|
key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
|
|
|
|
key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
|
|
|
|
key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
|
|
|
|
key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
|
|
|
|
key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
|
|
|
|
key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
|
|
|
|
/* determine support function number to search for */
|
|
|
|
procnum = (key->strategy == PARTITION_STRATEGY_HASH) ?
|
|
|
|
HASHEXTENDED_PROC : BTORDER_PROC;
|
|
|
|
|
|
|
|
/* Copy partattrs and fill other per-attribute info */
|
|
|
|
memcpy(key->partattrs, attrs, key->partnatts * sizeof(int16));
|
|
|
|
partexprs_item = list_head(key->partexprs);
|
|
|
|
for (i = 0; i < key->partnatts; i++)
|
|
|
|
{
|
|
|
|
AttrNumber attno = key->partattrs[i];
|
|
|
|
HeapTuple opclasstup;
|
|
|
|
Form_pg_opclass opclassform;
|
|
|
|
Oid funcid;
|
|
|
|
|
|
|
|
/* Collect opfamily information */
|
|
|
|
opclasstup = SearchSysCache1(CLAOID,
|
|
|
|
ObjectIdGetDatum(opclass->values[i]));
|
|
|
|
if (!HeapTupleIsValid(opclasstup))
|
|
|
|
elog(ERROR, "cache lookup failed for opclass %u", opclass->values[i]);
|
|
|
|
|
|
|
|
opclassform = (Form_pg_opclass) GETSTRUCT(opclasstup);
|
|
|
|
key->partopfamily[i] = opclassform->opcfamily;
|
|
|
|
key->partopcintype[i] = opclassform->opcintype;
|
|
|
|
|
|
|
|
/* Get a support function for the specified opfamily and datatypes */
|
|
|
|
funcid = get_opfamily_proc(opclassform->opcfamily,
|
|
|
|
opclassform->opcintype,
|
|
|
|
opclassform->opcintype,
|
|
|
|
procnum);
|
|
|
|
if (!OidIsValid(funcid))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
|
|
|
errmsg("operator class \"%s\" of access method %s is missing support function %d for type %s",
|
|
|
|
NameStr(opclassform->opcname),
|
|
|
|
(key->strategy == PARTITION_STRATEGY_HASH) ?
|
|
|
|
"hash" : "btree",
|
|
|
|
procnum,
|
|
|
|
format_type_be(opclassform->opcintype))));
|
|
|
|
|
|
|
|
fmgr_info_cxt(funcid, &key->partsupfunc[i], partkeycxt);
|
|
|
|
|
|
|
|
/* Collation */
|
|
|
|
key->partcollation[i] = collation->values[i];
|
|
|
|
|
|
|
|
/* Collect type information */
|
|
|
|
if (attno != 0)
|
|
|
|
{
|
|
|
|
Form_pg_attribute att = TupleDescAttr(relation->rd_att, attno - 1);
|
|
|
|
|
|
|
|
key->parttypid[i] = att->atttypid;
|
|
|
|
key->parttypmod[i] = att->atttypmod;
|
|
|
|
key->parttypcoll[i] = att->attcollation;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (partexprs_item == NULL)
|
|
|
|
elog(ERROR, "wrong number of partition key expressions");
|
|
|
|
|
|
|
|
key->parttypid[i] = exprType(lfirst(partexprs_item));
|
|
|
|
key->parttypmod[i] = exprTypmod(lfirst(partexprs_item));
|
|
|
|
key->parttypcoll[i] = exprCollation(lfirst(partexprs_item));
|
|
|
|
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
6 years ago
|
|
|
partexprs_item = lnext(key->partexprs, partexprs_item);
|
|
|
|
}
|
|
|
|
get_typlenbyvalalign(key->parttypid[i],
|
|
|
|
&key->parttyplen[i],
|
|
|
|
&key->parttypbyval[i],
|
|
|
|
&key->parttypalign[i]);
|
|
|
|
|
|
|
|
ReleaseSysCache(opclasstup);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
/* Assert that we're not leaking any old data during assignments below */
|
|
|
|
Assert(relation->rd_partkeycxt == NULL);
|
|
|
|
Assert(relation->rd_partkey == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Success --- reparent our context and make the relcache point to the
|
|
|
|
* newly constructed key
|
|
|
|
*/
|
|
|
|
MemoryContextSetParent(partkeycxt, CacheMemoryContext);
|
|
|
|
relation->rd_partkeycxt = partkeycxt;
|
|
|
|
relation->rd_partkey = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelationGetPartitionQual
|
|
|
|
*
|
|
|
|
* Returns a list of partition quals
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
RelationGetPartitionQual(Relation rel)
|
|
|
|
{
|
|
|
|
/* Quick exit */
|
|
|
|
if (!rel->rd_rel->relispartition)
|
|
|
|
return NIL;
|
|
|
|
|
|
|
|
return generate_partition_qual(rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_partition_qual_relid
|
|
|
|
*
|
|
|
|
* Returns an expression tree describing the passed-in relation's partition
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
* constraint.
|
|
|
|
*
|
|
|
|
* If the relation is not found, or is not a partition, or there is no
|
|
|
|
* partition constraint, return NULL. We must guard against the first two
|
|
|
|
* cases because this supports a SQL function that could be passed any OID.
|
|
|
|
* The last case can happen even if relispartition is true, when a default
|
|
|
|
* partition is the only partition.
|
|
|
|
*/
|
|
|
|
Expr *
|
|
|
|
get_partition_qual_relid(Oid relid)
|
|
|
|
{
|
|
|
|
Expr *result = NULL;
|
|
|
|
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
/* Do the work only if this relation exists and is a partition. */
|
|
|
|
if (get_rel_relispartition(relid))
|
|
|
|
{
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
Relation rel = relation_open(relid, AccessShareLock);
|
|
|
|
List *and_args;
|
|
|
|
|
|
|
|
and_args = generate_partition_qual(rel);
|
|
|
|
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
/* Convert implicit-AND list format to boolean expression */
|
|
|
|
if (and_args == NIL)
|
|
|
|
result = NULL;
|
|
|
|
else if (list_length(and_args) > 1)
|
|
|
|
result = makeBoolExpr(AND_EXPR, and_args, -1);
|
|
|
|
else
|
|
|
|
result = linitial(and_args);
|
|
|
|
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
/* Keep the lock, to allow safe deparsing against the rel by caller. */
|
|
|
|
relation_close(rel, NoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate_partition_qual
|
|
|
|
*
|
|
|
|
* Generate partition predicate from rel's partition bound expression. The
|
|
|
|
* function returns a NIL list if there is no predicate.
|
|
|
|
*
|
|
|
|
* We cache a copy of the result in the relcache entry, after constructing
|
|
|
|
* it using the caller's context. This approach avoids leaking any data
|
|
|
|
* into long-lived cache contexts, especially if we fail partway through.
|
|
|
|
*/
|
|
|
|
static List *
|
|
|
|
generate_partition_qual(Relation rel)
|
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
MemoryContext oldcxt;
|
|
|
|
Datum boundDatum;
|
|
|
|
bool isnull;
|
|
|
|
List *my_qual = NIL,
|
|
|
|
*result = NIL;
|
|
|
|
Relation parent;
|
|
|
|
|
|
|
|
/* Guard against stack overflow due to overly deep partition tree */
|
|
|
|
check_stack_depth();
|
|
|
|
|
|
|
|
/* If we already cached the result, just return a copy */
|
|
|
|
if (rel->rd_partcheckvalid)
|
|
|
|
return copyObject(rel->rd_partcheck);
|
|
|
|
|
|
|
|
/* Grab at least an AccessShareLock on the parent table */
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
parent = relation_open(get_partition_parent(RelationGetRelid(rel)),
|
|
|
|
AccessShareLock);
|
|
|
|
|
|
|
|
/* Get pg_class.relpartbound */
|
|
|
|
tuple = SearchSysCache1(RELOID, RelationGetRelid(rel));
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
elog(ERROR, "cache lookup failed for relation %u",
|
|
|
|
RelationGetRelid(rel));
|
|
|
|
|
|
|
|
boundDatum = SysCacheGetAttr(RELOID, tuple,
|
|
|
|
Anum_pg_class_relpartbound,
|
|
|
|
&isnull);
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
if (!isnull)
|
|
|
|
{
|
|
|
|
PartitionBoundSpec *bound;
|
|
|
|
|
|
|
|
bound = castNode(PartitionBoundSpec,
|
|
|
|
stringToNode(TextDatumGetCString(boundDatum)));
|
|
|
|
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
my_qual = get_qual_from_partbound(rel, parent, bound);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
|
|
|
/* Add the parent's quals to the list (if any) */
|
|
|
|
if (parent->rd_rel->relispartition)
|
|
|
|
result = list_concat(generate_partition_qual(parent), my_qual);
|
|
|
|
else
|
|
|
|
result = my_qual;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change Vars to have partition's attnos instead of the parent's. We do
|
|
|
|
* this after we concatenate the parent's quals, because we want every Var
|
|
|
|
* in it to bear this relation's attnos. It's safe to assume varno = 1
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
result = map_partition_varattnos(result, 1, rel, parent);
|
|
|
|
|
|
|
|
/* Assert that we're not leaking any old data during assignments below */
|
|
|
|
Assert(rel->rd_partcheckcxt == NULL);
|
|
|
|
Assert(rel->rd_partcheck == NIL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save a copy in the relcache. The order of these operations is fairly
|
|
|
|
* critical to avoid memory leaks and ensure that we don't leave a corrupt
|
|
|
|
* relcache entry if we fail partway through copyObject.
|
|
|
|
*
|
|
|
|
* If, as is definitely possible, the partcheck list is NIL, then we do
|
|
|
|
* not need to make a context to hold it.
|
|
|
|
*/
|
|
|
|
if (result != NIL)
|
|
|
|
{
|
|
|
|
rel->rd_partcheckcxt = AllocSetContextCreate(CacheMemoryContext,
|
|
|
|
"partition constraint",
|
|
|
|
ALLOCSET_SMALL_SIZES);
|
|
|
|
MemoryContextCopyAndSetIdentifier(rel->rd_partcheckcxt,
|
|
|
|
RelationGetRelationName(rel));
|
|
|
|
oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
|
|
|
|
rel->rd_partcheck = copyObject(result);
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rel->rd_partcheck = NIL;
|
|
|
|
rel->rd_partcheckvalid = true;
|
|
|
|
|
|
|
|
/* Keep the parent locked until commit */
|
Fix assorted bugs in pg_get_partition_constraintdef().
It failed if passed a nonexistent relation OID, or one that was a non-heap
relation, because of blindly applying heap_open to a user-supplied OID.
This is not OK behavior for a SQL-exposed function; we have a project
policy that we should return NULL in such cases. Moreover, since
pg_get_partition_constraintdef ought now to work on indexes, restricting
it to heaps is flat wrong anyway.
The underlying function generate_partition_qual() wasn't on board with
indexes having partition quals either, nor for that matter with rels
having relispartition set but yet null relpartbound. (One wonders
whether the person who wrote the function comment blocks claiming that
these functions allow a missing relpartbound had ever tested it.)
Fix by testing relispartition before opening the rel, and by using
relation_open not heap_open. (If any other relkinds ever grow the
ability to have relispartition set, the code will work with them
automatically.) Also, don't reject null relpartbound in
generate_partition_qual.
Back-patch to v11, and all but the null-relpartbound change to v10.
(It's not really necessary to change generate_partition_qual at all
in v10, but I thought s/heap_open/relation_open/ would be a good
idea anyway just to keep the code in sync with later branches.)
Per report from Justin Pryzby.
Discussion: https://postgr.es/m/20180927200020.GJ776@telsasoft.com
7 years ago
|
|
|
relation_close(parent, NoLock);
|
|
|
|
|
|
|
|
/* Return the working copy to the caller */
|
|
|
|
return result;
|
|
|
|
}
|