forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignorance.c
184 lines (159 loc) · 4.58 KB
/
ignorance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "aqo.h"
#include "ignorance.h"
#include "access/heapam.h"
#include "access/parallel.h"
#include "executor/spi.h"
#include "utils/lsyscache.h"
#include "miscadmin.h"
bool aqo_log_ignorance;
void
set_ignorance(bool newval, void *extra)
{
/*
* On postgres start we can't create any table.
* It is not problem. We will check existence at each update and create this
* table in dynamic mode, if needed.
*/
if (IsUnderPostmaster && !IsParallelWorker() && newval &&
(aqo_log_ignorance != newval))
/* Create storage and no error, if it exists already. */
create_ignorance_table(true);
aqo_log_ignorance = newval;
}
bool
create_ignorance_table(bool fail_ok)
{
Oid nspid = get_aqo_schema();
char *nspname;
char *sql;
int rc;
if (nspid == InvalidOid)
{
if (!fail_ok)
ereport(ERROR,
(errmsg("AQO extension is not installed"),
errdetail("AQO shared library is enabled but extension isn't installed.")));
else
return false;
}
nspname = get_namespace_name(nspid);
Assert(nspname != NULL);
/* Check the table existence. */
if (get_relname_relid("aqo_ignorance", nspid) != InvalidOid)
{
if (!fail_ok)
elog(PANIC, "aqo_ignorance table exists yet.");
else
return false;
}
sql = psprintf("CREATE TABLE %s.aqo_ignorance (qhash int, fhash int, fss_hash int, node_type int, node text);"
"CREATE UNIQUE INDEX aqo_ignorance_idx ON aqo_ignorance (qhash, fhash, fss_hash);",
nspname);
SPI_connect();
rc = SPI_execute(sql, false, 0);
SPI_finish();
if (rc < 0)
/* Can't ignore this problem. */
elog(ERROR, "Failed to create aqo_ignorance table %s. status: %d",
sql, rc);
pfree(nspname);
pfree(sql);
return true;
}
void
update_ignorance(int qhash, int fhash, int fss_hash, Plan *plan)
{
RangeVar *rv;
Relation hrel;
Relation irel;
SnapshotData snap;
TupleTableSlot *slot;
TupleDesc tupDesc;
HeapTuple tuple;
Datum values[5];
bool isnull[5] = { false, false, false, false, false };
bool shouldFree;
Oid reloid;
IndexScanDesc scan;
ScanKeyData key[3];
LOCKTAG tag;
Oid nspid = get_aqo_schema();
char *nspname;
if (!OidIsValid(nspid))
elog(PANIC, "AQO schema does not exists!");
nspname = get_namespace_name(nspid);
Assert(nspname != 0);
rv = makeRangeVar(nspname, "aqo_ignorance_idx", -1);
reloid = RangeVarGetRelid(rv, NoLock, true);
if (!OidIsValid(reloid))
{
/* This table doesn't created on instance startup. Create now. */
create_ignorance_table(false);
reloid = RangeVarGetRelid(rv, NoLock, true);
if (!OidIsValid(reloid))
elog(PANIC, "Ignorance table does not exists!");
}
init_lock_tag(&tag, (uint32) fhash, (uint32) fss_hash);
LockAcquire(&tag, ExclusiveLock, false, false);
rv = makeRangeVar(nspname, "aqo_ignorance", -1);
hrel = table_openrv(rv, RowExclusiveLock);
irel = index_open(reloid, RowExclusiveLock);
tupDesc = RelationGetDescr(hrel);
InitDirtySnapshot(snap);
scan = index_beginscan(hrel, irel, &snap, 3, 0);
ScanKeyInit(&key[0], 1, BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(qhash));
ScanKeyInit(&key[1], 2, BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(fhash));
ScanKeyInit(&key[2], 3, BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(fss_hash));
index_rescan(scan, key, 3, NULL, 0);
slot = MakeSingleTupleTableSlot(tupDesc, &TTSOpsBufferHeapTuple);
if (!index_getnext_slot(scan, ForwardScanDirection, slot))
{
if (plan->predicted_cardinality < 0.)
{
char nodestr[1024];
char *qplan = nodeToString(plan);
memset(nodestr, 0, 1024);
strncpy(nodestr, qplan, 1023);
pfree(qplan);
/*
* AQO failed to predict cardinality for this node.
*/
values[0] = Int32GetDatum(qhash);
values[1] = Int32GetDatum(fhash);
values[2] = Int32GetDatum(fss_hash);
values[3] = Int32GetDatum(nodeTag(plan));
values[4] = CStringGetTextDatum(nodestr);
tuple = heap_form_tuple(tupDesc, values, isnull);
simple_heap_insert(hrel, tuple);
my_index_insert(irel, values, isnull, &(tuple->t_self),
hrel, UNIQUE_CHECK_YES);
}
else
{
/* AQO works as expected. */
}
}
else if (!TransactionIdIsValid(snap.xmin) &&
!TransactionIdIsValid(snap.xmax))
{
/*
* AQO made prediction for this node. Delete it from the ignorance
* table.
*/
tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
Assert(shouldFree != true);
simple_heap_delete(hrel, &(tuple->t_self));
}
else
{
/*
* The data exists. We can't do anything for now.
*/
}
ExecDropSingleTupleTableSlot(slot);
index_endscan(scan);
index_close(irel, RowExclusiveLock);
table_close(hrel, RowExclusiveLock);
CommandCounterIncrement();
LockRelease(&tag, ExclusiveLock, false);
}