Skip to content

Instantly share code, notes, and snippets.

@rueian
rueian / libpq.go
Last active March 16, 2020 12:04
libpq prep test
package main
import (
"crypto/rand"
"database/sql"
_ "github.com/lib/pq"
"time"
)
const (
@rueian
rueian / pgproxy.go
Last active March 16, 2020 10:09
pgproxy for query, parse, bind messages
package main
import (
"encoding/hex"
"fmt"
"github.com/rueian/pgbroker/backend"
"github.com/rueian/pgbroker/message"
"github.com/rueian/pgbroker/proxy"
"net"
)
postgres=# explain analyze select * from counters where id like 'ms:149895:%';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------
Index Scan using counters_pkey on counters (cost=0.56..8.58 rows=49 width=36) (actual time=0.015..0.023 rows=10 loops=1)
Index Cond: ((id >= 'ms:149895:'::text) AND (id < 'ms:149895;'::text))
Filter: (id ~~ 'ms:149895:%'::text)
Planning Time: 0.146 ms
Execution Time: 0.036 ms
(5 rows)
/* In any case, don't believe extremely small or large estimates. */
if (selec < 0.00001)
selec = 0.00001;
else if (selec > 0.9999)
selec = 0.9999;
else if (IsA(rawreq, SupportRequestIndexCondition))
{
/* Try to convert operator/function call to index conditions */
SupportRequestIndexCondition *req = (SupportRequestIndexCondition *) rawreq;
/*
* Currently we have no "reverse" match operators with the pattern on
* the left, so we only need consider cases with the indexkey on the
* left.
*/
postgres=# explain analyze select * from counters where id >= 'ms:149895:' and id <= 'ms:149895;' and id like 'ms:149895:%';
 QUERY PLAN
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 Index Scan using counters_pkey on counters (cost=0.56..8.58 rows=1 width=36) (actual time=0.023..0.039 rows=10 loops=1)
 Index Cond: ((id >= 'ms:149895:'::text) AND (id <= 'ms:149895;'::text) AND (id >= 'ms:149895:'::text) AND (id < 'ms:149895;'::text))
 Filter: (id ~~ 'ms:149895:%'::text)
 Planning Time: 0.156 ms
 Execution Time: 0.036 ms
(5 rows)
postgres=# EXPLAIN SELECT * FROM counters;
 QUERY PLAN
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Seq Scan on counters (cost=0.00..89169.11 rows=4880311 width=36)
(1 row)
postgres=# SELECT unnest(histogram_bounds::text::text[]) FROM pg_stats WHERE tablename='counters' AND attname='id';
 unnest
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 ms:103734:p
 ms:1074631:cl
 ms:1103301:c
 ms:1137423:pn
 ms:1168158:pr
 ms:1192862:p
 ms:1221019:cl
postgres=# SELECT array_length(histogram_bounds, 1) FROM pg_stats WHERE tablename='counters' AND attname='id';
 array_length
 - - - - - - - 
 101
(1 row)
/*
* If we have most-common-values info, add up the fractions of the MCV
* entries that satisfy MCV OP PATTERN. These fractions contribute
* directly to the result selectivity. Also add up the total fraction
* represented by MCV entries.
*/
mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
&sumcommon);
/*