Исходный код СУБД 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
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
#!/bin/sh
#
# src/test/performance/cfs/pgbench_writeheavy.sh
#
# Phase 13 / A13.1 -- heavy write (hot UPDATE) harness for CFS.
#
# Unlike pgbench_readwrite.sh, this one is designed to EXPOSE the CPU cost of
# CFS compression by forcing pages to be evicted from the buffer cache while
# the workload is running.  It does so by:
#
#   * starting a throwaway cluster with a SMALL shared_buffers (so the table is
#     several times larger than the cache), AND
#   * running a random-UPDATE workload against that oversubscribed table.
#
# Each evicted dirty page must be compressed (lz4/zstd) and appended to the
# .cfp, leaving the previous compressed record as dead space.  The report
# therefore also prints dead_pages / dead_bytes -- a non-zero value is the
# proof that the CFS write path actually ran (on a cache-resident table it
# stays at zero and the test shows no CFS overhead at all).
#
# Usage:
#   PG_BINDIR=/path/to/pg/bin ./pgbench_writeheavy.sh
#
# Environment:
#   PG_BINDIR   directory containing initdb/pg_ctl/pgbench/psql (required)
#   SCALE       rows loaded into each table (default 50000 -> ~20 MB @ 400 chars)
#   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)
#
# The harness always starts its own throwaway cluster under
# $TMPDIR/cfsevic-PID (default /tmp) with the small buffers; it never touches
# a running server, because oversubscription is the whole point.
 
set -eu
 
HERE=$(cd "$(dirname "$0")" && pwd)
 
: "${PG_BINDIR:?PG_BINDIR must point at the postgres bin directory}"
: "${SCALE:=20000}"
: "${PAYLOAD_LEN:=400}"
: "${CLIENTS:=1 8}"
: "${DURATION:=20}"
: "${BUF_KB:=16}"
: "${LICENSE_FILE:=/tmp/license.xml}"
 
PSQL="$PG_BINDIR/psql"
PGBENCH="$PG_BINDIR/pgbench"
 
TABLES="evic_plain evic_lz4_1 evic_zstd_1 evic_zstd_3"
 
PGDATA="$(mktemp -d "${TMPDIR:-/tmp}/cfsevic.XXXXXX")"
PGPORT=55413
 
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 write-heavy 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 must be several x larger than shared_buffers for eviction)\n\n'
 
# Bring up a throwaway cluster with SMALL buffers.
"$PG_BINDIR/initdb" -D "$PGDATA" --license "$LICENSE_FILE" --auth trust --no-locale -E UTF8 >/dev/null
"$PG_BINDIR/pg_ctl" -D "$PGDATA" -l "$PGDATA/evic.log" \
    -o "-p $PGPORT -c shared_buffers=${BUF_KB}MB -c checkpoint_timeout=600s -c max_wal_size=4GB -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"
 
# Schema: identical data, only the storage policy differs.
"$PSQL" $PSQLARGS <<SQL
DROP TABLE IF EXISTS evic_plain;
DROP TABLE IF EXISTS evic_lz4_1;
DROP TABLE IF EXISTS evic_zstd_1;
DROP TABLE IF EXISTS evic_zstd_3;
 
CREATE TABLE evic_plain  (id int primary key, payload text);
CREATE TABLE evic_lz4_1  (id int primary key, payload text) WITH (algorithm = 'lz4',  level = 1);
CREATE TABLE evic_zstd_1 (id int primary key, payload text) WITH (algorithm = 'zstd', level = 1);
CREATE TABLE evic_zstd_3 (id int primary key, payload text) WITH (algorithm = 'zstd', level = 3);
 
INSERT INTO evic_plain SELECT g, repeat('p', $PAYLOAD_LEN) FROM generate_series(1, $SCALE) g;
INSERT INTO evic_lz4_1  SELECT * FROM evic_plain;
INSERT INTO evic_zstd_1 SELECT * FROM evic_plain;
INSERT INTO evic_zstd_3 SELECT * FROM evic_plain;
CHECKPOINT;
ANALYZE;
SQL
 
# Sizes BEFORE the workload (baseline).
printf '\n--- sizes before workload ---\n'
"$PSQL" $PSQLARGS -c "
SELECT c.relname,
       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 'evic_%' AND c.relkind='r'
ORDER BY c.relname;"
 
printf '\n=== hot-UPDATE pgbench (scale=%s, payload=%s, %ss runs) ===\n\n' \
    "$SCALE" "$PAYLOAD_LEN" "$DURATION"
printf '%-14s %-8s %12s %12s %14s\n' TABLE CLIENTS TPS lat_avg_ms overhead_vs_plain
printf '%-14s %-8s %12s %12s %14s\n' ----- ------ --- ---------- ---------------
 
# plain_tps[c] = TPS of evic_plain at client count c, so overhead is compared
# against the plain baseline AT THE SAME concurrency (not against 1 client).
plain_tps=""
# Assoc array: client count -> plain TPS at that count.
for c in $CLIENTS; do eval "plain_at_$c="; done
 
for tbl in $TABLES; do
    for c in $CLIENTS; do
        # Run pgbench; capture exit code so a client abort (e.g. a CFS
        # bug surfacing under the hot-UPDATE workload) is reported as a
        # row instead of aborting the whole matrix.
        set +e
        out=$("$PGBENCH" $PGBARGS -n -c "$c" -j "$c" -T "$DURATION" \
            -f "$HERE/pgbench_hotupdate.sql" \
            -D "scale=$SCALE" -D "payload_len=$PAYLOAD_LEN" -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}')
        # Record plain baseline for this client count, then compare CFS to it.
        eval "p=\$plain_at_$c"
        if [ "$tbl" = "evic_plain" ]; then
            eval "plain_at_$c=$tps"
            ovh="-"
        elif [ -n "$p" ]; then
            ovh=$(awk -v t="$tps" -v pp="$p" \
                'BEGIN { if (pp==""||pp==0||t=="") print "-"; else printf "%+.0f", (t/pp-1)*100 }')
        else
            ovh="-"
        fi
        printf '%-14s %-8s %12s %12s %14s\n' "$tbl" "$c" "${tps:-0}" "${lat:-0}" "$ovh"
    done
done
 
# dead_bytes after a CHECKPOINT (exact at a checkpoint): non-zero proves the
# write path left superseded records behind; write_count proves the writes.
"$PSQL" $PSQLARGS -c "CHECKPOINT;" >/dev/null
printf '\n--- CFS accounting after workload (dead_bytes > 0 = superseded records exist) ---\n'
"$PSQL" $PSQLARGS -c "
SELECT s.relname,
       s.dead_pages,
       pg_size_pretty(s.dead_bytes) AS dead_bytes,
       pg_size_pretty(s.physical_size) AS physical,
       round(s.compression_ratio::numeric,2) AS ratio,
       s.write_count
FROM pg_stat_cfs s
WHERE s.relname LIKE 'evic_%' AND s.relname NOT LIKE '%_pkey'
ORDER BY s.relname;"
 
printf '\n--- physical on-disk size (incl. sidecars) ---\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_incl_sidecars
FROM pg_class c
WHERE c.relname LIKE 'evic_%' AND c.relkind='r'
ORDER BY c.relname;"