1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-18 17:42:25 +03:00

Restore foreign-key-aware estimation of join relation sizes.

This patch provides a new implementation of the logic added by commit
137805f89 and later removed by 77ba61080.  It differs from the original
primarily in expending much less effort per joinrel in large queries,
which it accomplishes by doing most of the matching work once per query not
once per joinrel.  Hopefully, it's also less buggy and better commented.
The never-documented enable_fkey_estimates GUC remains gone.

There remains work to be done to make the selectivity estimates account
for nulls in FK referencing columns; but that was true of the original
patch as well.  We may be able to address this point later in beta.
In the meantime, any error should be in the direction of overestimating
rather than underestimating joinrel sizes, which seems like the direction
we want to err in.

Tomas Vondra and Tom Lane

Discussion: <31041.1465069446@sss.pgh.pa.us>
This commit is contained in:
Tom Lane
2016-06-18 15:22:34 -04:00
parent 598aa194af
commit 100340e2dc
17 changed files with 921 additions and 18 deletions

View File

@ -30,6 +30,7 @@
#include "nodes/plannodes.h"
#include "nodes/relation.h"
#include "utils/datum.h"
#include "utils/rel.h"
/*
@ -2050,6 +2051,7 @@ _outPlannerInfo(StringInfo str, const PlannerInfo *node)
WRITE_NODE_FIELD(append_rel_list);
WRITE_NODE_FIELD(rowMarks);
WRITE_NODE_FIELD(placeholder_list);
WRITE_NODE_FIELD(fkey_list);
WRITE_NODE_FIELD(query_pathkeys);
WRITE_NODE_FIELD(group_pathkeys);
WRITE_NODE_FIELD(window_pathkeys);
@ -2139,6 +2141,37 @@ _outIndexOptInfo(StringInfo str, const IndexOptInfo *node)
/* we don't bother with fields copied from the index AM's API struct */
}
static void
_outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
{
int i;
WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
WRITE_UINT_FIELD(con_relid);
WRITE_UINT_FIELD(ref_relid);
WRITE_INT_FIELD(nkeys);
appendStringInfoString(str, " :conkey");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", node->conkey[i]);
appendStringInfoString(str, " :confkey");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", node->confkey[i]);
appendStringInfoString(str, " :conpfeqop");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %u", node->conpfeqop[i]);
WRITE_INT_FIELD(nmatched_ec);
WRITE_INT_FIELD(nmatched_rcols);
WRITE_INT_FIELD(nmatched_ri);
/* for compactness, just print the number of matches per column: */
appendStringInfoString(str, " :eclass");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", (node->eclass[i] != NULL));
appendStringInfoString(str, " :rinfos");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
}
static void
_outEquivalenceClass(StringInfo str, const EquivalenceClass *node)
{
@ -3209,6 +3242,27 @@ _outConstraint(StringInfo str, const Constraint *node)
}
}
static void
_outForeignKeyCacheInfo(StringInfo str, const ForeignKeyCacheInfo *node)
{
int i;
WRITE_NODE_TYPE("FOREIGNKEYCACHEINFO");
WRITE_OID_FIELD(conrelid);
WRITE_OID_FIELD(confrelid);
WRITE_INT_FIELD(nkeys);
appendStringInfoString(str, " :conkey");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", node->conkey[i]);
appendStringInfoString(str, " :confkey");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %d", node->confkey[i]);
appendStringInfoString(str, " :conpfeqop");
for (i = 0; i < node->nkeys; i++)
appendStringInfo(str, " %u", node->conpfeqop[i]);
}
/*
* outNode -
@ -3609,6 +3663,9 @@ outNode(StringInfo str, const void *obj)
case T_IndexOptInfo:
_outIndexOptInfo(str, obj);
break;
case T_ForeignKeyOptInfo:
_outForeignKeyOptInfo(str, obj);
break;
case T_EquivalenceClass:
_outEquivalenceClass(str, obj);
break;
@ -3785,6 +3842,9 @@ outNode(StringInfo str, const void *obj)
case T_XmlSerialize:
_outXmlSerialize(str, obj);
break;
case T_ForeignKeyCacheInfo:
_outForeignKeyCacheInfo(str, obj);
break;
default: