Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
#!/bin/sh
#
# src/test/performance/cfs/pgbench_coldread.sh
#
# Phase 13 / A13.1 -- cold-read (decompression-cost) harness for CFS.
#
# The opposite of pgbench_readwrite.sh: here the table is deliberately made
# several times larger than shared_buffers, so random point lookups cause a
# steady stream of cache misses.  Each miss on a CFS table has to read the
# compressed record from .cfp and DECOMPRESS it into the buffer -- this is
# where the CPU cost of CFS decompression shows up as a TPS drop vs plain.
#
# On a cache-resident table (the default of pgbench_readonly.sh) this cost is
# invisible because pages are already decompressed in shared_buffers.
#
# Usage:
#   PG_BINDIR=/path/to/pg/bin ./pgbench_coldread.sh
#
# Environment:
#   PG_BINDIR   directory containing initdb/pg_ctl/pgbench/psql (required)
#   SCALE       rows loaded into each table (default 300000 -> ~120 MB @ 400)
#   PAYLOAD_LEN payload length in chars (default 400)
#   CLIENTS     space-separated client counts (default "1 8 32")
#   DURATION    per-run duration in seconds (default 20)
#   BUF_KB      shared_buffers for the throwaway cluster, in MB (default 16)
#
# Always starts its own throwaway cluster; oversubscription is the whole point.
 
set -eu
 
HERE=$(cd "$(dirname "$0")" && pwd)
 
: "${PG_BINDIR:?PG_BINDIR must point at the postgres bin directory}"
: "${SCALE:=300000}"
: "${PAYLOAD_LEN:=400}"
: "${CLIENTS:=1 8 32}"
: "${DURATION:=20}"
: "${BUF_KB:=16}"
: "${LICENSE_FILE:=/tmp/license.xml}"
 
PSQL="$PG_BINDIR/psql"
PGBENCH="$PG_BINDIR/pgbench"
 
TABLES="cold_plain cold_lz4_1 cold_zstd_1"
 
PGDATA="$(mktemp -d "${TMPDIR:-/tmp}/cfscold.XXXXXX")"
PGPORT=55414
 
cleanup()
{
    "$PG_BINDIR/pg_ctl" -D "$PGDATA" -m fast stop >/dev/null 2>&1 || true
    rm -rf "$PGDATA"
}
trap cleanup EXIT INT TERM
 
printf '=== CFS cold-read (decompression-cost) harness ===\n'
printf '  cluster: throwaway, shared_buffers=%sMB\n' "$BUF_KB"
printf '  scale:   %s rows, payload %s chars (~%s MB raw per table)\n' \
    "$SCALE" "$PAYLOAD_LEN" "$(awk -v s=$SCALE -v p=$PAYLOAD_LEN 'BEGIN{printf "%.0f", s*p/1048576}')"
printf '  (table >> shared_buffers -> ~all reads are cache misses)\n\n'
 
"$PG_BINDIR/initdb" -D "$PGDATA" --license "$LICENSE_FILE" --auth trust --no-locale -E UTF8 >/dev/null
"$PG_BINDIR/pg_ctl" -D "$PGDATA" -l "$PGDATA/cold.log" \
    -o "-p $PGPORT -c shared_buffers=${BUF_KB}MB -c max_connections=200" \
    start >/dev/null 2>&1
 
PSQLARGS="-h 127.0.0.1 -p $PGPORT -d postgres -v ON_ERROR_STOP=1"
PGBARGS="-h 127.0.0.1 -p $PGPORT -d postgres"
 
# Identical data; storage policy is the only difference.
"$PSQL" $PSQLARGS <<SQL
DROP TABLE IF EXISTS cold_plain;
DROP TABLE IF EXISTS cold_lz4_1;
DROP TABLE IF EXISTS cold_zstd_1;
 
CREATE TABLE cold_plain  (id int primary key, payload text);
CREATE TABLE cold_lz4_1  (id int primary key, payload text) WITH (algorithm = 'lz4',  level = 1);
CREATE TABLE cold_zstd_1 (id int primary key, payload text) WITH (algorithm = 'zstd', level = 1);
 
INSERT INTO cold_plain SELECT g, repeat('p', $PAYLOAD_LEN) FROM generate_series(1, $SCALE) g;
INSERT INTO cold_lz4_1  SELECT * FROM cold_plain;
INSERT INTO cold_zstd_1 SELECT * FROM cold_plain;
CHECKPOINT;
ANALYZE;
SQL
 
printf '\n--- on-disk footprint (proof of compression) ---\n'
"$PSQL" $PSQLARGS -c "
SELECT c.relname,
       pg_size_pretty(pg_relation_size(c.oid)) AS main_fork,
       pg_size_pretty(pg_total_relation_size(c.oid)) AS total,
       coalesce(round(s.compression_ratio::numeric,2), 1.00) AS ratio
FROM pg_class c LEFT JOIN pg_stat_cfs s ON s.relid = c.oid
WHERE c.relname LIKE 'cold_%' AND c.relkind='r' ORDER BY c.relname;"
 
printf '\n=== random point-lookup pgbench (scale=%s, %ss runs, cache COLD) ===\n\n' \
    "$SCALE" "$DURATION"
printf '%-14s %-8s %12s %12s %14s\n' TABLE CLIENTS TPS lat_avg_ms decompress_vs_plain
printf '%-14s %-8s %12s %12s %14s\n' ----- ------ --- ---------- --------------
# plain_at_c = TPS of cold_plain at client count c, for per-concurrency overhead.
for c in $CLIENTS; do eval "plain_at_$c="; done
for tbl in $TABLES; do
    for c in $CLIENTS; do
        set +e
        out=$("$PGBENCH" $PGBARGS -n -c "$c" -j "$c" -T "$DURATION" \
            -f "$HERE/pgbench_readonly.sql" \
            -D "scale=$SCALE" -D "table=$tbl" 2>&1)
        rc=$?
        set -e
        if [ "$rc" -ne 0 ]; then
            printf '%-14s %-8s %12s\n' "$tbl" "$c" "FAILED ($rc)"
            printf '  %s\n' "$(printf '%s\n' "$out" | grep -iE 'error|aborted' | head -3 | tr '\n' '|')"
            continue
        fi
        tps=$(printf '%s\n' "$out" | awk '/^tps = / {print $3}')
        lat=$(printf '%s\n' "$out" | awk '/latency average = / {print $4}')
        eval "p=\$plain_at_$c"
        if [ "$tbl" = "cold_plain" ]; then
            eval "plain_at_$c=$tps"
            cost="-"
        elif [ -n "$p" ]; then
            cost=$(awk -v t="$tps" -v pp="$p" \
                'BEGIN { if (pp==""||pp==0||t=="") print "-"; else printf "%+.0f", (t/pp-1)*100 }')
        else
            cost="-"
        fi
        printf '%-14s %-8s %12s %12s %14s\n' "$tbl" "$c" "${tps:-0}" "${lat:-0}" "$cost"
    done
done
 
printf '\n--- CFS read accounting (decompress_count > 0 = decompression ran) ---\n'
"$PSQL" $PSQLARGS -c "
SELECT s.relname, s.read_count, s.decompress_count,
       pg_size_pretty(s.physical_size) AS physical, round(s.compression_ratio::numeric,2) AS ratio
FROM pg_stat_cfs s
WHERE s.relname LIKE 'cold_%' AND s.relname NOT LIKE '%_pkey'
ORDER BY s.relname;"