Skip to content

Commit 1ff3190

Browse files
committed
[PGPRO-7183] bring in line stable 13, 14, 15
Cherry-pick commit: 690776a Bugfix. AQO plan node must have reasonable set of serialization routines
1 parent c9fb67f commit 1ff3190

File tree

5 files changed

+45
-50
lines changed

5 files changed

+45
-50
lines changed

‎aqo.h

-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ void aqo_ExecutorEnd(QueryDesc *queryDesc);
283283
extern void automatical_query_tuning(uint64 query_hash, struct StatEntry *stat);
284284

285285
/* Utilities */
286-
extern int int64_compare(const void *a, const void *b);
287286
extern int int_cmp(const void *a, const void *b);
288287
extern int double_cmp(const void *a, const void *b);
289288
extern int *argsort(void *a, int n, size_t es,

‎cardinality_estimation.c

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ predict_for_relation(List *clauses, List *selectivities, List *relsigns,
103103
result = OkNNr_predict(data, features);
104104
}
105105
}
106+
106107
#ifdef AQO_DEBUG_PRINT
107108
predict_debug_output(clauses, selectivities, relsigns, *fss, result);
108109
#endif

‎hash.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static int get_node_hash(Node *node);
3333
static int get_unsorted_unsafe_int_array_hash(int *arr, int len);
3434
static int get_unordered_int_list_hash(List *lst);
3535

36-
static int64 get_relations_hash(List *relsigns);
36+
static int get_relations_hash(List *relsigns);
3737
static int get_fss_hash(int clauses_hash, int eclasses_hash,
3838
int relidslist_hash);
3939

@@ -297,7 +297,7 @@ get_fss_for_object(List *relsigns, List *clauselist,
297297

298298
clauses_hash = get_int_array_hash(sorted_clauses, n - sh);
299299
eclasses_hash = get_int_array_hash(eclass_hash, nargs);
300-
relations_hash = (int) get_relations_hash(relsigns);
300+
relations_hash = get_relations_hash(relsigns);
301301
fss_hash = get_fss_hash(clauses_hash, eclasses_hash, relations_hash);
302302

303303
if (nfeatures != NULL)
@@ -465,26 +465,26 @@ get_fss_hash(int clauses_hash, int eclasses_hash, int relidslist_hash)
465465
* Hash is supposed to be relations-order-insensitive.
466466
* Each element of a list must have a String type,
467467
*/
468-
static int64
468+
static int
469469
get_relations_hash(List *relsigns)
470470
{
471471
int nhashes = 0;
472-
int64 *hashes = palloc(list_length(relsigns) * sizeof(uint64));
472+
uint32 *hashes = palloc(list_length(relsigns) * sizeof(uint32));
473473
ListCell *lc;
474-
int64 result;
474+
int result;
475475

476476
foreach(lc, relsigns)
477477
{
478-
hashes[nhashes++] = *(int64 *) lfirst(lc);
478+
hashes[nhashes++] = (uint32) lfirst_int(lc);
479479
}
480480

481481
/* Sort the array to make query insensitive to input order of relations. */
482-
qsort(hashes, nhashes, sizeof(int64), int64_compare);
482+
qsort(hashes, nhashes, sizeof(uint32), int_cmp);
483483

484484
/* Make a final hash value */
485485

486-
result = DatumGetInt64(hash_any_extended((const unsigned char *) hashes,
487-
nhashes * sizeof(int64), 0));
486+
result = DatumGetInt32(hash_any((const unsigned char *) hashes,
487+
nhashes * sizeof(uint32)));
488488

489489
return result;
490490
}

‎path_utils.c

+35-28
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,22 @@ get_selectivities(PlannerInfo *root,
135135
/*
136136
* Based on the hashTupleDesc() routine
137137
*/
138-
static uint64
138+
static uint32
139139
hashTempTupleDesc(TupleDesc desc)
140140
{
141-
uint64 s;
141+
uint32 s;
142142
int i;
143143

144144
s = hash_combine(0, hash_uint32(desc->natts));
145145

146146
for (i = 0; i < desc->natts; ++i)
147147
{
148148
const char *attname = NameStr(TupleDescAttr(desc, i)->attname);
149-
uint64 s1;
149+
uint32 s1;
150150

151-
s = hash_combine64(s, hash_uint32(TupleDescAttr(desc, i)->atttypid));
152-
s1 = hash_bytes_extended((const unsigned char *) attname, strlen(attname), 0);
153-
s = hash_combine64(s, s1);
151+
s = hash_combine(s, hash_uint32(TupleDescAttr(desc, i)->atttypid));
152+
s1 = hash_bytes((const unsigned char *) attname, strlen(attname));
153+
s = hash_combine(s, s1);
154154
}
155155
return s;
156156
}
@@ -186,8 +186,8 @@ get_list_of_relids(PlannerInfo *root, Relids relids, RelSortOut *rels)
186186

187187
if (!OidIsValid(entry->relid))
188188
{
189-
/* Invalid oid */
190-
hashes = lappend_uint64(hashes, (UINT64_MAX / 7));
189+
/* TODO: Explain this logic. */
190+
hashes = lappend_int(hashes, INT32_MAX / 3);
191191
continue;
192192
}
193193

@@ -212,7 +212,7 @@ get_list_of_relids(PlannerInfo *root, Relids relids, RelSortOut *rels)
212212
trel = relation_open(entry->relid, NoLock);
213213
tdesc = RelationGetDescr(trel);
214214
Assert(CheckRelationLockedByMe(trel, AccessShareLock, true));
215-
hashes = lappend_uint64(hashes, hashTempTupleDesc(tdesc));
215+
hashes = lappend_int(hashes, hashTempTupleDesc(tdesc));
216216
relation_close(trel, NoLock);
217217
}
218218
else
@@ -222,9 +222,9 @@ get_list_of_relids(PlannerInfo *root, Relids relids, RelSortOut *rels)
222222
get_namespace_name(get_rel_namespace(entry->relid)),
223223
relrewrite ? get_rel_name(relrewrite) : relname);
224224

225-
hashes = lappend_uint64(hashes, DatumGetInt64(hash_any_extended(
225+
hashes = lappend_int(hashes, DatumGetInt32(hash_any(
226226
(unsigned char *) relname,
227-
strlen(relname), 0)));
227+
strlen(relname))));
228228

229229
hrels = lappend_oid(hrels, entry->relid);
230230
}
@@ -591,7 +591,7 @@ AQOnodeCopy(struct ExtensibleNode *enew, const struct ExtensibleNode *eold)
591591
/* These lists couldn't contain AQO nodes. Use basic machinery */
592592
new->rels = palloc(sizeof(RelSortOut));
593593
new->rels->hrels = list_copy(old->rels->hrels);
594-
new->rels->signatures = list_copy_uint64(old->rels->signatures);
594+
new->rels->signatures = list_copy(old->rels->signatures);
595595

596596
new->clauses = copyObject(old->clauses);
597597
new->grouping_exprs = copyObject(old->grouping_exprs);
@@ -626,21 +626,24 @@ AQOnodeEqual(const struct ExtensibleNode *a, const struct ExtensibleNode *b)
626626
#define WRITE_FLOAT_FIELD(fldname,format) \
627627
appendStringInfo(str, " :" CppAsString(fldname) " " format, node->fldname)
628628

629+
/*
630+
* Serialize AQO plan node to a string.
631+
*
632+
* Right now we can't correctly serialize all fields of the node. Taking into
633+
* account that this action needed when a plan moves into parallel workers or
634+
* just during debugging, we serialize it only partially, just for debug
635+
* purposes.
636+
* Some extensions may manipulate by parts of serialized plan too.
637+
*/
629638
static void
630639
AQOnodeOut(struct StringInfoData *str, const struct ExtensibleNode *enode)
631640
{
632641
AQOPlanNode *node = (AQOPlanNode *) enode;
633642

634-
Assert(0);
635-
WRITE_BOOL_FIELD(had_path);
636-
WRITE_NODE_FIELD(rels);
637-
WRITE_NODE_FIELD(clauses);
638-
WRITE_NODE_FIELD(selectivities);
639-
WRITE_NODE_FIELD(grouping_exprs);
640-
641-
WRITE_ENUM_FIELD(jointype, JoinType);
642-
WRITE_FLOAT_FIELD(parallel_divisor, "%.5f");
643-
WRITE_BOOL_FIELD(was_parametrized);
643+
node->had_path = false;
644+
node->jointype = 0;
645+
node->parallel_divisor = 1.0;
646+
node->was_parametrized = false;
644647

645648
/* For Adaptive optimization DEBUG purposes */
646649
WRITE_INT_FIELD(fss);
@@ -677,24 +680,28 @@ AQOnodeOut(struct StringInfoData *str, const struct ExtensibleNode *enode)
677680
(void) token; /* in case not used elsewhere */ \
678681
local_node->fldname = nodeRead(NULL, 0)
679682

683+
/*
684+
* Deserialize AQO plan node from a string to internal representation.
685+
*
686+
* Should work in coherence with AQOnodeOut().
687+
*/
680688
static void
681689
AQOnodeRead(struct ExtensibleNode *enode)
682690
{
683691
AQOPlanNode *local_node = (AQOPlanNode *) enode;
684692
const char *token;
685693
int length;
686694

687-
Assert(0);
688695
READ_BOOL_FIELD(had_path);
689-
READ_NODE_FIELD(rels);
690-
READ_NODE_FIELD(clauses);
691-
READ_NODE_FIELD(selectivities);
692-
READ_NODE_FIELD(grouping_exprs);
693-
694696
READ_ENUM_FIELD(jointype, JoinType);
695697
READ_FLOAT_FIELD(parallel_divisor);
696698
READ_BOOL_FIELD(was_parametrized);
697699

700+
local_node->rels = palloc0(sizeof(RelSortOut));
701+
local_node->clauses = NIL;
702+
local_node->selectivities = NIL;
703+
local_node->grouping_exprs = NIL;
704+
698705
/* For Adaptive optimization DEBUG purposes */
699706
READ_INT_FIELD(fss);
700707
READ_FLOAT_FIELD(prediction);

‎utils.c

-12
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@ static int argsort_cmp(const void *a, const void *b);
2828
* qsort comparator functions
2929
*/
3030

31-
/* int64 comparator for pg_qsort. */
32-
int
33-
int64_compare(const void *va, const void *vb)
34-
{
35-
int64 a = *((const int64 *) va);
36-
int64 b = *((const int64 *) vb);
37-
38-
if (a == b)
39-
return 0;
40-
return (a > b) ? 1 : -1;
41-
}
42-
4331
/*
4432
* Function for qsorting an integer arrays
4533
*/

0 commit comments

Comments
 (0)