postgresql/src/tools/findoidjoins/make_oidjoins_check
Tom Lane 795c382e8c Improve findoidjoins to cover more cases.
Teach the program and script to deal with OID-array referencing columns,
which we now have several of.  Also, modify the recommended usage process
to specify that the program should be run against the regression database
rather than template1.  This lets it find numerous joins that cannot be
found in template1 because the relevant catalogs are entirely empty.

Together these changes add seventeen formerly-missed cases to the oidjoins
regression test.
2011-04-23 19:33:17 -04:00

81 lines
1.8 KiB
Bash
Executable File

#! /bin/sh
# src/tools/findoidjoins/make_oidjoins_check
# You first run findoidjoins on the regression database, then send that
# output into this script to generate a list of SQL statements.
# NOTE: any field that findoidjoins thinks joins to more than one table
# will NOT be checked by the output of this script. You should be
# suspicious of multiple entries in findoidjoins' output.
# Caution: you may need to use GNU awk.
AWK=${AWK:-awk}
# Create a temporary directory with the proper permissions so no one can
# intercept our temporary files and cause a security breach.
TMP="${TMPDIR:-/tmp}/make_oidjoins_check.$$"
OMASK="`umask`"
umask 077
if ! mkdir $TMP
then echo "Can't create temporary directory $TMP." 1>&2
exit 1
fi
trap "rm -rf $TMP" 0 1 2 3 15
umask "$OMASK"
unset OMASK
INPUTFILE="$TMP/a"
DUPSFILE="$TMP/b"
NONDUPSFILE="$TMP/c"
# Read input
cat "$@" >$INPUTFILE
# Look for fields with multiple references.
cat $INPUTFILE | cut -d' ' -f2 | sort | uniq -d >$DUPSFILE
if [ -s $DUPSFILE ] ; then
echo "Ignoring these fields that link to multiple tables:" 1>&2
cat $DUPSFILE 1>&2
fi
# Get the fields without multiple references.
cat $INPUTFILE | while read LINE
do
set -- $LINE
grep "^$2\$" $DUPSFILE >/dev/null 2>&1 || echo $LINE
done >$NONDUPSFILE
# Generate the output.
cat $NONDUPSFILE |
$AWK -F'[ .]' '\
BEGIN \
{
printf "\
--\n\
-- This is created by pgsql/src/tools/findoidjoins/make_oidjoins_check\n\
--\n";
}
$5 == "=>" \
{
printf "\
SELECT ctid, %s\n\
FROM %s.%s fk\n\
WHERE %s != 0 AND\n\
NOT EXISTS(SELECT 1 FROM %s.%s pk WHERE pk.oid = fk.%s);\n",
$4, $2, $3, $4,
$6, $7, $4;
}
$5 == "[]=>" \
{
printf "\
SELECT ctid, %s\n\
FROM (SELECT ctid, unnest(%s) AS %s FROM %s.%s) fk\n\
WHERE %s != 0 AND\n\
NOT EXISTS(SELECT 1 FROM %s.%s pk WHERE pk.oid = fk.%s);\n",
$4, $4, $4, $2, $3, $4,
$6, $7, $4;
}'
exit 0