-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbtree_hexedit
executable file
·50 lines (41 loc) · 1.52 KB
/
btree_hexedit
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
#!/bin/bash
#
# btree_hexedit: Similar to relation_hexedit, but sets the B-Tree non-leaf
# children of the root as offsets, as well as an offset to the root itself.
usage() {
cat <<EOM
Usage:
$(basename "$0") relname
EOM
exit 0
}
[ -z "$1" ] && { usage; }
source ./hexedit.cfg
relname=$1
# Generate offsets to all non-leaf children of the root pages in "Go to Offset"
# dialog cache (only do this when they're non-leaf pages to avoid having an
# offset to every page in the index).
if ! psql --no-psqlrc -c "CREATE EXTENSION IF NOT EXISTS pageinspect"
then
echo "creating pageinspect extension failed"
exit 1
fi
if ! TRUE_ROOT_OFFSET=$(psql --no-psqlrc -tA -c "SELECT 'GoToOffset0=' || (root * 2^13)::text FROM bt_metap('$relname')")
then
echo "obtaining true root block for offset dialog failed; is $relname a btree?"
exit 1
fi
if ! ROOT_CHILD_OFFSETS=$(psql --no-psqlrc -tA -c "WITH offset_strings(line) AS (
SELECT 'GoToOffset' || ROW_NUMBER() OVER() || '=' || (substring(ctid::text from '([0-9]+)')::int8 * (2^13))
FROM bt_page_items('$relname', (SELECT root::int4 FROM bt_metap('$relname') where level > 1))
)
SELECT string_agg(line, chr(10)) FROM offset_strings;")
then
echo "obtaining non-leaf children of root block for offset dialog failed"
exit 1
fi
# The root page is also added as a separate offset.
OFFSETS="${TRUE_ROOT_OFFSET}"$'\n'"${ROOT_CHILD_OFFSETS}"
export OFFSETS
echo "Tip: 'Go to Offset' dialog (shortcut: Ctrl + G) will have children of root and root offsets cached"
./__open_relation "$relname"