Skip to content

Commit a12fc4f

Browse files
committed
Remove EE dependencies from multimaster
1 parent 7c7095c commit a12fc4f

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

contrib/mmts/pglogical_apply.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ find_pkey_tuple(ScanKey skey, Relation rel, Relation idxrel,
101101
InitDirtySnapshot(snap);
102102
scan = index_beginscan(rel, idxrel,
103103
&snap,
104-
IndexRelationGetNumberOfKeyAttributes(idxrel),
104+
IndexRelationGetNumberOfAttributes(idxrel),
105105
0);
106106

107107
retry:
108108
found = false;
109109

110-
index_rescan(scan, skey, IndexRelationGetNumberOfKeyAttributes(idxrel), NULL, 0);
110+
index_rescan(scan, skey, RelationGetNumberOfAttributes(idxrel), NULL, 0);
111111

112112
if ((scantuple = index_getnext(scan, ForwardScanDirection)) != NULL)
113113
{
@@ -236,7 +236,7 @@ build_index_scan_key(ScanKey skey, Relation rel, Relation idxrel, TupleData *tup
236236
indkey = (int2vector *) DatumGetPointer(indkeyDatum);
237237

238238

239-
for (attoff = 0; attoff < IndexRelationGetNumberOfKeyAttributes(idxrel); attoff++)
239+
for (attoff = 0; attoff < RelationGetNumberOfAttributes(idxrel); attoff++)
240240
{
241241
Oid operator;
242242
Oid opfamily;

src/backend/tcop/postgres.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4570,8 +4570,10 @@ typedef struct
45704570
int n_params; /* number of parameters extracted for this query */
45714571
int16 format; /* portal output format */
45724572
bool disable_autoprepare; /* disable preparing of this query */
4573-
uint64 non_prepared_time; /* averge time of original (non-prepared) query execution (sum of autoprepare_threshold query execution times) */
4574-
uint64 prepared_time; /* averge time of prepared query execution (sum of autoprepare_threshold query execution times) */
4573+
uint64 non_prepared_time_sum; /* sum of times of non-prepared query execution (up to autoprepare_threshold measurements) */
4574+
uint64 prepared_time_sum; /* sum of times of prepared query execution (up to autoprepare_threshold measurements) */
4575+
double non_prepared_time_sum2; /* sum of squares of non-prepared query execution (up to autoprepare_threshold measurements) */
4576+
double prepared_time_sum2; /* sum of squares of prepared query execution (up to autoprepare_threshold measurements) */
45754577
} plan_cache_entry;
45764578

45774579
static uint32 plan_cache_hash_fn(const void *key, Size keysize)
@@ -4652,11 +4654,13 @@ static void end_exec_simple(void)
46524654
{
46534655
long secs;
46544656
int usecs;
4657+
uint64 elapsed;
46554658
TimestampDifference(exec_start_timestamp,
46564659
GetCurrentTimestamp(),
46574660
&secs, &usecs);
4658-
4659-
entry->non_prepared_time += secs * USECS_PER_SEC + usecs;
4661+
elapsed = secs * USECS_PER_SEC + usecs;
4662+
entry->non_prepared_time_sum += elapsed;
4663+
entry->non_prepared_time_sum2 += elapsed*elapsed;
46604664
}
46614665
}
46624666

@@ -5031,8 +5035,10 @@ static bool exec_cached_query(const char *query_string, List *parsetree_list)
50315035
autoprepare_cached_plans -= 1;
50325036
}
50335037
entry->exec_count = 0;
5034-
entry->prepared_time = 0;
5035-
entry->non_prepared_time = 0;
5038+
entry->prepared_time_sum = 0;
5039+
entry->non_prepared_time_sum = 0;
5040+
entry->prepared_time_sum2 = 0;
5041+
entry->non_prepared_time_sum2 = 0;
50365042
entry->plan = NULL;
50375043
entry->disable_autoprepare = false;
50385044
}
@@ -5358,24 +5364,32 @@ static bool exec_cached_query(const char *query_string, List *parsetree_list)
53585364
/* Calculate average time of execution of prepared query */
53595365
long secs;
53605366
int usecs;
5367+
uint64 elapsed;
53615368
TimestampDifference(exec_start_timestamp,
53625369
GetCurrentTimestamp(),
53635370
&secs, &usecs);
5364-
5365-
entry->prepared_time += secs * USECS_PER_SEC + usecs;
5371+
elapsed = secs * USECS_PER_SEC + usecs;
5372+
entry->prepared_time_sum += elapsed;
5373+
entry->prepared_time_sum2 += elapsed*elapsed;
53665374

53675375
if (entry->exec_count == autoprepare_threshold*2)
53685376
{
53695377
/* Now we can compare average times of prepared and non-prepared queries execution */
5370-
if (entry->prepared_time > entry->non_prepared_time)
5378+
int n = autoprepare_threshold;
5379+
double prepared_time_deviation = sqrt((entry->prepared_time_sum2 - (double)entry->prepared_time_sum*entry->prepared_time_sum/n)/n);
5380+
double non_prepared_time_deviation = sqrt((entry->non_prepared_time_sum2 - (double)entry->non_prepared_time_sum*entry->non_prepared_time_sum/n)/n);
5381+
if (entry->prepared_time_sum - prepared_time_deviation*n > entry->non_prepared_time_sum + non_prepared_time_deviation*n)
53715382
{
53725383
/*
53735384
* Disable autoprepare if average time of execution of prepared query
53745385
* is worser than of non-prepared query
53755386
*/
53765387
entry->disable_autoprepare = true;
5377-
elog(LOG, "Disable autoprepared plan for %s because its average time %ld is greater than time of non-prepared query %ld",
5378-
query_string, entry->prepared_time, entry->non_prepared_time);
5388+
elog(LOG, "Disable autoprepared plan for %s (generic cost=%lg, avg custom cost=%lg, avg=%lg, dev=%lg) because its worser than non-prepared plan (avg=%lg, dev=%lg)",
5389+
query_string,
5390+
psrc->generic_cost, psrc->num_custom_plans != 0 ? psrc->total_custom_cost/psrc->num_custom_plans : 0,
5391+
(double)entry->prepared_time_sum/n, prepared_time_deviation,
5392+
(double)entry->non_prepared_time_sum/n, non_prepared_time_deviation);
53795393
}
53805394
}
53815395
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy