-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path__open_relation
executable file
·123 lines (109 loc) · 4.31 KB
/
__open_relation
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
#!/bin/bash
#
# This script contains code that is common to all convenience scripts.
#
# Utility scripts mostly consist of some logic for exporting wxHexEditor-format
# offset cache entries to the OFFSETS environment variable, and/or logic for
# printing something to stdout that's useful to the user. Everything else
# happens here.
#
# Most convenience scripts set the $OFFSETS env var to something useful for us.
# Utility scripts must have run "source ./hexedit.cfg" for us.
relname=$1
# Spoof home director, to avoid clobbering real /.wxHexEditor file
HOME=$(pwd)
wxconfig="$HOME/.wxHexEditor"
if ! psql --no-psqlrc -c "CHECKPOINT"
then
echo "CHECKPOINT failed"
exit 1
fi
if ! RFN=$(psql --no-psqlrc -tA -c "SELECT pg_relation_filepath('$relname')")
then
echo "invoking pg_relation_filepath() failed"
exit 1
fi
if [ "$RFN" = "" ]
then
echo "relation $relname has no associated relfile"
exit 1
fi
# Put minimal .wxHexEditor registry style config file in place, so old tags are
# forgotten. This is also where we generate convenience "Go to Offset" dialog
# offsets in the registry/cache (this comes from OFFSETS env var).
echo "Replacing $wxconfig with pg_hexedit optimized settings..."
cat > "$wxconfig" <<- EOM
UpdateCheck=0
FakeBlockLines=1
FakeBlockSize=8k
ColourHexBackground=#FFFFFF
ColourHexBackgroundZebra=#FFFFFF
UseBytesPerLineLimit=1
BytesPerLineLimit=$BYTES_PER_LINE_LIMIT
FontSize=$FONTSIZE
CharacterEncodingFamily=Code for Information Interchange
CharacterEncoding=ASCII - American Standard Code for Information Interchange
ScreenFullScreen=1
AutoShowTagPanel=0
GoToOptions=7
$OFFSETS
EOM
PGDATA=$(psql --no-psqlrc -tA -c "SELECT setting FROM pg_settings WHERE name = 'data_directory'")
echo "Determined that data directory is $PGDATA"
# wxHexEditor XML encoding is always UTF-8. Make sure that attname fields come
# back as UTF-8.
export PGCLIENTENCODING="UTF8"
if ! ATTRLIST=$(psql --no-psqlrc -tA -c "SELECT string_agg(attlen || ',\"' || attname || '\",' || attalign::text, ',')
FROM
(SELECT * FROM pg_attribute WHERE attrelid = '$relname'::regclass AND attnum > 0 ORDER BY attnum ASC) t;")
then
echo "failed to generate attrlist string for -D option"
exit 1
fi
# Check if data file exists locally, download $MAX_BLOCK_TAGS to /tmp if it doesn't
FULLPATH="$PGDATA/$RFN"
if [ -f "$FULLPATH" ]; then
echo "Running pg_hexedit against $FULLPATH, the first segment in relation $relname..."
echo "Note: Only blocks $MIN_BLOCK_TAGS - $MAX_BLOCK_TAGS will be annotated, to keep overhead low."
else
PAGEINSPECT_INSTALLED=$(psql -XAtc "select count(*) from pg_extension where extname='pageinspect'")
if (( PAGEINSPECT_INSTALLED && MIN_BLOCK_TAGS == 0 )); then
TMPDIR="/tmp/postgresql-relfiles/$PGHOST-$PGPORT-DB/$(dirname "$RFN")"
mkdir -p "$TMPDIR"
FULLPATH="$TMPDIR/$(basename "$RFN")"
echo "Downloading blocks from $MIN_BLOCK_TAGS to $MAX_BLOCK_TAGS inclusive from relation $relname to $FULLPATH..."
psql -XAtc "SELECT encode(get_raw_page('$relname', block::int4),'base64')
FROM generate_series(0, least(pg_relation_size('$relname') / 8192 - 1, $MAX_BLOCK_TAGS))
block" | base64 -d > "$FULLPATH"
echo "Opening local temp file..."
else
echo "File $FULLPATH doesn't exist."
if (( MIN_BLOCK_TAGS == 0 )); then
echo "Tip: Install pageinspect to automatically download remote files."
else
echo "Note: Cannot automatically download remote files when MIN_BLOCK_TAGS is non-zero."
fi
exit 1
fi
fi
# Deliberately put space before pg_hexedit frontend debug output here:
echo -e "pg_hexedit frontend utility debug/notice output:\n"
# Generate tags at a path that we know wxHexEditor will look for them:
# shellcheck disable=SC2086
if ! ./pg_hexedit $EXTRAFLAGS -D "$ATTRLIST" -z -R "$MIN_BLOCK_TAGS" "$MAX_BLOCK_TAGS" "$FULLPATH" > "$FULLPATH.tags"
then
echo "Error encountered by pg_hexedit. Could not generate all tags."
echo "You may still wish to run: $HEXEDITOR $FULLPATH"
exit 1
fi
if [ ! -f "$HEXEDITOR" ]
then
echo "\"$HEXEDITOR\" executable not found"
echo "\"$FULLPATH.tags\" annotation file was still created"
exit 1
fi
# Deliberately put space after pg_hexedit frontend debug output here:
echo
echo "Opening $FULLPATH with $HEXEDITOR..."
# Output from wxHexEditor is verbose, and not terribly useful, so we redirect:
$HEXEDITOR "$FULLPATH" &> /dev/null