Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 100340e

Browse files
committed
Restore foreign-key-aware estimation of join relation sizes.
This patch provides a new implementation of the logic added by commit 137805f and later removed by 77ba610. 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>
1 parent 598aa19 commit 100340e

File tree

17 files changed

+921
-18
lines changed

17 files changed

+921
-18
lines changed

src/backend/nodes/copyfuncs.c

+26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "nodes/plannodes.h"
2828
#include "nodes/relation.h"
2929
#include "utils/datum.h"
30+
#include "utils/rel.h"
3031

3132

3233
/*
@@ -4254,6 +4255,24 @@ _copyValue(const Value *from)
42544255
return newnode;
42554256
}
42564257

4258+
4259+
static ForeignKeyCacheInfo *
4260+
_copyForeignKeyCacheInfo(const ForeignKeyCacheInfo *from)
4261+
{
4262+
ForeignKeyCacheInfo *newnode = makeNode(ForeignKeyCacheInfo);
4263+
4264+
COPY_SCALAR_FIELD(conrelid);
4265+
COPY_SCALAR_FIELD(confrelid);
4266+
COPY_SCALAR_FIELD(nkeys);
4267+
/* COPY_SCALAR_FIELD might work for these, but let's not assume that */
4268+
memcpy(newnode->conkey, from->conkey, sizeof(newnode->conkey));
4269+
memcpy(newnode->confkey, from->confkey, sizeof(newnode->confkey));
4270+
memcpy(newnode->conpfeqop, from->conpfeqop, sizeof(newnode->conpfeqop));
4271+
4272+
return newnode;
4273+
}
4274+
4275+
42574276
/*
42584277
* copyObject
42594278
*
@@ -5052,6 +5071,13 @@ copyObject(const void *from)
50525071
retval = _copyRoleSpec(from);
50535072
break;
50545073

5074+
/*
5075+
* MISCELLANEOUS NODES
5076+
*/
5077+
case T_ForeignKeyCacheInfo:
5078+
retval = _copyForeignKeyCacheInfo(from);
5079+
break;
5080+
50555081
default:
50565082
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(from));
50575083
retval = 0; /* keep compiler quiet */

src/backend/nodes/outfuncs.c

+60
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "nodes/plannodes.h"
3131
#include "nodes/relation.h"
3232
#include "utils/datum.h"
33+
#include "utils/rel.h"
3334

3435

3536
/*
@@ -2050,6 +2051,7 @@ _outPlannerInfo(StringInfo str, const PlannerInfo *node)
20502051
WRITE_NODE_FIELD(append_rel_list);
20512052
WRITE_NODE_FIELD(rowMarks);
20522053
WRITE_NODE_FIELD(placeholder_list);
2054+
WRITE_NODE_FIELD(fkey_list);
20532055
WRITE_NODE_FIELD(query_pathkeys);
20542056
WRITE_NODE_FIELD(group_pathkeys);
20552057
WRITE_NODE_FIELD(window_pathkeys);
@@ -2139,6 +2141,37 @@ _outIndexOptInfo(StringInfo str, const IndexOptInfo *node)
21392141
/* we don't bother with fields copied from the index AM's API struct */
21402142
}
21412143

2144+
static void
2145+
_outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
2146+
{
2147+
int i;
2148+
2149+
WRITE_NODE_TYPE("FOREIGNKEYOPTINFO");
2150+
2151+
WRITE_UINT_FIELD(con_relid);
2152+
WRITE_UINT_FIELD(ref_relid);
2153+
WRITE_INT_FIELD(nkeys);
2154+
appendStringInfoString(str, " :conkey");
2155+
for (i = 0; i < node->nkeys; i++)
2156+
appendStringInfo(str, " %d", node->conkey[i]);
2157+
appendStringInfoString(str, " :confkey");
2158+
for (i = 0; i < node->nkeys; i++)
2159+
appendStringInfo(str, " %d", node->confkey[i]);
2160+
appendStringInfoString(str, " :conpfeqop");
2161+
for (i = 0; i < node->nkeys; i++)
2162+
appendStringInfo(str, " %u", node->conpfeqop[i]);
2163+
WRITE_INT_FIELD(nmatched_ec);
2164+
WRITE_INT_FIELD(nmatched_rcols);
2165+
WRITE_INT_FIELD(nmatched_ri);
2166+
/* for compactness, just print the number of matches per column: */
2167+
appendStringInfoString(str, " :eclass");
2168+
for (i = 0; i < node->nkeys; i++)
2169+
appendStringInfo(str, " %d", (node->eclass[i] != NULL));
2170+
appendStringInfoString(str, " :rinfos");
2171+
for (i = 0; i < node->nkeys; i++)
2172+
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
2173+
}
2174+
21422175
static void
21432176
_outEquivalenceClass(StringInfo str, const EquivalenceClass *node)
21442177
{
@@ -3209,6 +3242,27 @@ _outConstraint(StringInfo str, const Constraint *node)
32093242
}
32103243
}
32113244

3245+
static void
3246+
_outForeignKeyCacheInfo(StringInfo str, const ForeignKeyCacheInfo *node)
3247+
{
3248+
int i;
3249+
3250+
WRITE_NODE_TYPE("FOREIGNKEYCACHEINFO");
3251+
3252+
WRITE_OID_FIELD(conrelid);
3253+
WRITE_OID_FIELD(confrelid);
3254+
WRITE_INT_FIELD(nkeys);
3255+
appendStringInfoString(str, " :conkey");
3256+
for (i = 0; i < node->nkeys; i++)
3257+
appendStringInfo(str, " %d", node->conkey[i]);
3258+
appendStringInfoString(str, " :confkey");
3259+
for (i = 0; i < node->nkeys; i++)
3260+
appendStringInfo(str, " %d", node->confkey[i]);
3261+
appendStringInfoString(str, " :conpfeqop");
3262+
for (i = 0; i < node->nkeys; i++)
3263+
appendStringInfo(str, " %u", node->conpfeqop[i]);
3264+
}
3265+
32123266

32133267
/*
32143268
* outNode -
@@ -3609,6 +3663,9 @@ outNode(StringInfo str, const void *obj)
36093663
case T_IndexOptInfo:
36103664
_outIndexOptInfo(str, obj);
36113665
break;
3666+
case T_ForeignKeyOptInfo:
3667+
_outForeignKeyOptInfo(str, obj);
3668+
break;
36123669
case T_EquivalenceClass:
36133670
_outEquivalenceClass(str, obj);
36143671
break;
@@ -3785,6 +3842,9 @@ outNode(StringInfo str, const void *obj)
37853842
case T_XmlSerialize:
37863843
_outXmlSerialize(str, obj);
37873844
break;
3845+
case T_ForeignKeyCacheInfo:
3846+
_outForeignKeyCacheInfo(str, obj);
3847+
break;
37883848

37893849
default:
37903850

0 commit comments

Comments
 (0)