#!/bin/bash
#===============================================================================
#
#          FILE: pg_nice
#
#         USAGE: pg_nice [Directory to locate pg_nice.so]
#
#   DESCRIPTION: Creating a nice function in C for lowering process priority to 
#                postmaster(server-side process of PostgreSQL) which you are 
#                connected.
#                This can makes your query has lower priority either.
#
#  REQUIREMENTS: gcc, pg_config
#
#         NOTES: For root user, postmaster has no permission to reach your $HOME.
#                So you will have to specify a public directory, for example:
#                # pg_nice /usr/local/lib
#
#                If you are using Linux kernel < 2.6.23, the system call function
#                setpriority() using a old scheduling mechanism. It limits you can 
#                only lower the priority, back to higher priority is not possible.
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Mon Nov  5 07:51:16 CET 2012
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

# Build directory
[ "$1" == "" ] && BUILD_DIR="$HOME/pg_nice" || BUILD_DIR="$1/pg_nice"
[ "$USER" == "root" ] && BUILD_DIR="/usr/local/lib/pg_nice"
[ "$1" == "" ] && [ "$USER" != "root" ] && chmod 755 $HOME

mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"

# Source file
cat > pg_nice.c << __EOF__
#include <sys/time.h>
#include <sys/resource.h>
#include "postgres.h"
#include "fmgr.h"

// For PostgreSQL 8.2+
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

PG_FUNCTION_INFO_V1(pg_nice);

Datum
pg_nice(PG_FUNCTION_ARGS)
{
    int32 priority = PG_GETARG_INT32(0);
    int result = setpriority(PRIO_PROCESS, 0, priority);

    if (result == 0) {
        PG_RETURN_BOOL(1);
    } else {
        PG_RETURN_BOOL(0);
    }
}
__EOF__

# Check compiler and pg_config binary
echo "Checking gcc and pg_config ... "
which gcc || CAN_NOT_COMPILE=1
if which pg_config ; then 
   PG_CONFIG="$(which pg_config)"
else # If pg_config not found
   echo -n "Trying to find pg_config ... "
   PG_CONFIG="$(rpm -ql $(rpm -qa | grep postgresql | grep -v debuginfo)|grep "/bin/pg_config$")"
   echo $PG_CONFIG
   echo -n "Testing pg_config ... "
   $PG_CONFIG | grep VERSION || CAN_NOT_COMPILE=1
fi
echo

# Compile
if [ "$CAN_NOT_COMPILE" == "" ] ; then
   echo Compiling ...
   rm -f pg_nice.so
   gcc -Wall -I `$PG_CONFIG --includedir-server` -fpic -c pg_nice.c
   gcc -shared -o pg_nice.so pg_nice.o
fi

[ -e pg_nice.so ] && echo Compiled successfully || echo Compile failed. Use pre-built shared library.

# If we cannot compile a shared library file, use pre-compiled shared library instead.
if ! [ -e pg_nice.so ] ; then
   POSTGRESQL_VERSION="$(psql --version|awk '/^psql / {print $NF}'|cut -c 1-3)"
   PSQL_ARCHITECTURE="$(file `which psql`|awk -F" ELF " '{print $2}'|awk -F- '{print $1}'|grep "^[0-9]\+$")" \
   || PSQL_ARCHITECTURE="$(getconf LONG_BIT)" # No "file" command? try to get the architecture from the kernel
   if [ "$POSTGRESQL_VERSION" == "9.1" ] ; then
	if [ "$PSQL_ARCHITECTURE" == "32" ] ; then # 9.1 32-bit
	    # pg_nice.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
	    cat << __EOF__ | base64 -i -d | gzip -d -c > pg_nice.so
H4sIAGEAplACA9VXfWgcRRSf3dylSZq2iSZprQEPOSWt7ZIm6YcieE1yrS3XrySt6edyudskW+9u491eTbRiy9aPcJ6oVRD/0NKK
CiKIRkEFTbnSKhYttYJ/iaAtVw2thShBiut7M7ObufUq6H/O8e7Nb+bNe29m3szOezwcWSdJEnFKBfwQba0gpAP49BzW3kECxEda
yGKykMxKs9LtZ+RHUEdoP21rZHQdsA8aK3m/DGwP9O9pZDRJGGG/D2jbRTNOyhSnH1yjtmTe9jQ5JnWeCuWxrQqoBuiPz9Jvffzj
Cwe7wt9OffdQf/NH+Wvzf/7m6vxj40bHmXlvXPDqFucUALrLg/fx+n1ANwE1c7wLBt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GoO
Z1MPKrHRss0D0Ny9c/PaTRu6/pYtl0mJWe78T6lwKDSb3ZYm0GJK7AykqbGbMosZ+awezJhZ5v4XTFv085gRAAA=
__EOF__
	else # 9.1 64-bit
	    # pg_nice.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped
	    cat << __EOF__ | base64 -i -d | gzip -d -c > pg_nice.so
H4sIAJQIplACA81YfWwURRSfvWuvWyht+RAawLAx1BSVszQtFATt9XrtgaVAPwwG4rK92/YO76Pc7UHLRyDhQ5vaBI3Rxj+UmKio
CRL/UNQIxaJATEwVTYgxpgYwRTDSPzTVEM43uzPbnbk9WhNjnMvcm/nNvHlv3s68mXn7fY31DkFANDnRowjXjuUY9RqCH5tmdgGs
GuXC/wJUovfNRdnTVRdLEZL0f8yHoVGCj7okht6iKjlYPgfhK8kz0JI8iaEHCB+lIuHeeE0L4inVEHk8XYxYSqaPNgGfC009iaYd
DT3pPJ8dlITanMF+XM6HPB3yX2cSxz++8sJer++7m5d3bF74Uf9Y4S+Xfis81huvPD/jzW8nk+UEaZtzWEwwLcWmB7PgCyHfZ6nf
T+z2FORZFryU0Cd0fBo6Qj76OoI/A7kYjaVHRVaKRvDLHL4nC16sj585r7k6Ph2J3MdYT/TpIfrMIPhjBI9wOJLlzmg8Jic1JaHJ
MpLDsbCG5A4g0BToVnBRiYR3q0het1NuVjvDSU1NeCNKMqkm0cZOOap0hgNyRyoWQF2duHdHXIZCLBxQUVLVuhLheCKs9aBIuD3g
Tsbdy5GsBhVNgeHbk0lDMECxIGpoXFvrlSvcFe4qcz4O+Ancz/iuAtpO7INTan44H/d+jtqNrH+Rqw8Q+7qsWwnvZwvutODHLbj1
E1wgeB6HD1twqx/wH7wpjuKPMPo9/lMFDJ0Th9JVtTkSSpfWwX/Rohoo4XoIs1wfSUMqXYXreNLXh6GKx/H35f7qlJD/8KDmSA/r
wxylqe3AmhJgQP7ea57WllS7/+CaYeiLtAJ//5qHHRIafQWP0p87G8qN/aU5mCwZ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P7/MDov36Fzn6OcdP7xMnSYeySfi/ImXKT99rl8Wp2f9SljsC5f95Ev83k3uf0HSaXGB+mIRf4N49pl+Y
SfwIuajNJ76a9z/5WeSfmkv8oHB3+Xd/vwvmOcTiDvN8YXGneW6weI55HrB4runnWdxl+m8WzzP98mRxBgPPN/0oi08z/SOLZ77f
DbzA9GcsPsP0UyxeaPofFi8y/QuLF5t+g8Vnmv6AxWeZ+5zFZ9veP51wGx8Q7fB7zP3G4nPRZVt8XgZmrN9Me87UffVY2i6eI9jY
4QEdz7RDtY5n2sGn4xN60n3RQvARkX0ntRM9Bzk9uwlekzs1/Q8R/W9xej6v44VohPvur2XR/10id5iTezaL3C/1cSbWIb1X/Ujw
1S72fLyJccdYmv++DgHjmeunSMfBW3L4QsEYh19XpYJ9vK4yC+4X7ONjTYJ9fEwW7ON+ewT7uN+Lgn087XXBPo53QrCPE34g2McJ
P8kyzmeCfTwQBZRIxBLKQ4GEltRSHR3uAJJlb+uGZrlxbUsrjvDJdUxtndes4PeJHA52u5cvq1yB4OHRFVE1NYirVdAzGJc7I/F2
JSLrDxlZSXUj47kTTEWjPVSOr6lOH7i+2bPeZ9awGFqeGChgDkRChqCuGVPUn0iVldXVKNpjBBahtnIFkhsaN9R6GuUN9fUtvla5
1VPb6NOHTcblkBILRlQ6SSKw7skmz/q13oxYp01A04h8ZgQy2VBoTY01VGkJcrINbHDUGu40h8VhTxoO1Z+VfwNUwkMjgBgAAA==
__EOF__
	fi # 9.1
   elif [ "$POSTGRESQL_VERSION" == "8.4" ] ; then
	if [ "$PSQL_ARCHITECTURE" == "32" ] ; then # 8.4 32-bit
	    # pg_nice.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped
	    cat << __EOF__ | base64 -i -d | gzip -d -c > pg_nice.so
H4sIABMJplACA81XfWwURRSf3d6VFgq0QAGxiRdzmoKwKVA+Q+LR9vjK8WFbtHxurnfbdvHutt7tYasQIYtic55R0cT4hxIwamJM
jFYTNdGSI3wEog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/d6USnOvD6TSXO5zqTQXPCeV5oKXPPJXpdJc8rpUmguSWDSRENI7EkubGTPb06PE
IPlq7dzerkY2d3Ri1qe2laAtrS7AN4OqxweUFavWLCfwGOhPaKYWR7gUJOOG2pswuqMJlT4u1Gh2gNBHhxrPJpODjp3wtjaqeEP7
+q1hF6EZpz6pKOYq4mkkuOvmmfTZ0gzvLZIcZMkmojVE3RjZ3rI+om7fsKEj3Kl2rm+JhKnajKH2RVPxhOZM0jGor1y9UunVTLU/
ppp92dSjSmygbHM3NLft2rZ+6+bWf2TLZVJiljv/WyocCk1mt6UJtJgSOwNpauymzGJGPqkHM2aWuf8N9DUUnpgRAAA=
__EOF__
	else # 8.4 64-bit
	    # pg_nice.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped
	    cat << __EOF__ | base64 -i -d | gzip -d -c > pg_nice.so
H4sIAFAKplACA9VYfWwURRSfvWuv21La8iE0UMPGUAMqZ2lavgTt9XrtFctXPwwG4rK92/YO76Pc7UHLRyAB0aY2QWO08Q8lJipq
gsQ/FDVCsSgQE1NFE2KMqQFMEYztH5pqCOeb3Zntztwe7R/GxLnMvZnfzJv35u3Mm5l30NdU7xAERJMTPYpw7XiOUa8h+PECswtg
q1Au/C9EpXrfXJQ9XXOxFCFJ/8d8GBol+KhLYugYVcnB8jkIX2megZbmSQw9RPgoFQn35utaEE+phsjj6WLEUjJ9tAX4XGj6STTt
aOhJ5/ncoCTU5gz243I+5BmQ/z6bOPHJ1Rf3e33f37qya2vZx/3jRb9e/r3oeG+86sLMt76bSpYTpG3NYTHBtBSbHsyCl0G+z1K/
n9jtKcizLXg5oU/oeAE6Sj76eoI/C7kEjadHRVaKRvArHL4vC16ij585r3k6PgOJ3MfYQPTpIfrMJPhjBI9wOJLlzmg8Jic1JaHJ
MpLDsbCG5A4g0BToVnBRiYT3qkhev1tuVjvDSU1NeCNKMqkm0eZOOap0hgNyRyoWQF2duHdHXIZCLBxQUVLVuhLheCKs9aBIuD3g
T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" == "9.1" ] ; then
fi #if ! [ -e pg_nice.so ] ; then
chmod 755 pg_nice.so

echo

# Create Function pg_nice(int)
echo "Creating function for databases ... "
for DB in $(psql -U postgres <<< "copy (select datname from pg_database where datname not like 'template%') to stdout csv;") ; do
	echo -en "                            $DB: " | tail -c 28
        psql -U postgres $DB <<< "CREATE OR REPLACE FUNCTION pg_nice(INTEGER,INTEGER) RETURNS VOID AS '$BUILD_DIR/pg_nice' LANGUAGE C RETURNS NULL ON NULL INPUT;"
done
