From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-961458-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 49EFF139694
	for <garchives@archives.gentoo.org>; Thu, 13 Jul 2017 19:07:55 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 2E40B234019;
	Thu, 13 Jul 2017 19:07:52 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id F38E9234019
	for <gentoo-commits@lists.gentoo.org>; Thu, 13 Jul 2017 19:07:51 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 78BDF34180F
	for <gentoo-commits@lists.gentoo.org>; Thu, 13 Jul 2017 19:07:49 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id ECBD574AB
	for <gentoo-commits@lists.gentoo.org>; Thu, 13 Jul 2017 19:07:47 +0000 (UTC)
From: "Aaron Swenson" <titanofold@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Aaron Swenson" <titanofold@gentoo.org>
Message-ID: <1499972855.099885d492e52d178f6d8b6bb27e747f83d29b65.titanofold@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: dev-db/postgresql/
X-VCS-Repository: repo/gentoo
X-VCS-Files: dev-db/postgresql/postgresql-10_beta2.ebuild dev-db/postgresql/postgresql-9.2.21-r2.ebuild dev-db/postgresql/postgresql-9.3.17-r2.ebuild dev-db/postgresql/postgresql-9.4.12-r2.ebuild dev-db/postgresql/postgresql-9.5.7-r1.ebuild dev-db/postgresql/postgresql-9.6.3-r2.ebuild dev-db/postgresql/postgresql-9999.ebuild
X-VCS-Directories: dev-db/postgresql/
X-VCS-Committer: titanofold
X-VCS-Committer-Name: Aaron Swenson
X-VCS-Revision: 099885d492e52d178f6d8b6bb27e747f83d29b65
X-VCS-Branch: master
Date: Thu, 13 Jul 2017 19:07:47 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Archives-Salt: 96933804-fb53-444e-8604-67e9dea53b08
X-Archives-Hash: 80f3215ceccacfa05604b9ff52708bb6

commit:     099885d492e52d178f6d8b6bb27e747f83d29b65
Author:     Aaron W. Swenson <titanofold <AT> gentoo <DOT> org>
AuthorDate: Thu Jul 13 19:07:35 2017 +0000
Commit:     Aaron Swenson <titanofold <AT> gentoo <DOT> org>
CommitDate: Thu Jul 13 19:07:35 2017 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=099885d4

dev-db/postgresql: Spinlocks, pushd, and man

All versions now generate slotted man pages instead of using
symlinks. This is cleaner, and won’t result in broken links. You can
now run ‘man psql96’ (or similar) and actually get the man page for
psql, version 9.6.x.

10_beta2 and 9999 now dies if it can’t pushd into the directory it
needs to be in when generating the man pages.

Re-disabled spinlocks for versions 9.5 and after. Somehow this got
excluded from the 9.6 release.

Package-Manager: Portage-2.3.6, Repoman-2.3.1

 dev-db/postgresql/postgresql-10_beta2.ebuild       |   3 +-
 ...10_beta2.ebuild => postgresql-9.2.21-r2.ebuild} | 144 +++++++++------------
 ...10_beta2.ebuild => postgresql-9.3.17-r2.ebuild} | 133 +++++++++----------
 ...10_beta2.ebuild => postgresql-9.4.12-r2.ebuild} |  95 ++++++++------
 ...-10_beta2.ebuild => postgresql-9.5.7-r1.ebuild} |  99 ++++++++------
 ...-10_beta2.ebuild => postgresql-9.6.3-r2.ebuild} |  70 +++++++---
 dev-db/postgresql/postgresql-9999.ebuild           |   2 +-
 7 files changed, 286 insertions(+), 260 deletions(-)

diff --git a/dev-db/postgresql/postgresql-10_beta2.ebuild b/dev-db/postgresql/postgresql-10_beta2.ebuild
index 6ccc0c76d15..b723fe577cb 100644
--- a/dev-db/postgresql/postgresql-10_beta2.ebuild
+++ b/dev-db/postgresql/postgresql-10_beta2.ebuild
@@ -168,6 +168,7 @@ src_configure() {
 		--mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
 		--sysconfdir="${PO}/etc/postgresql-${SLOT}" \
 		--with-system-tzdata="${PO}/usr/share/zoneinfo" \
+		$(use_enable !alpha spinlocks) \
 		$(use_enable !pg_legacytimestamp integer-datetimes) \
 		$(use_enable threads thread-safety) \
 		$(use_with kerberos gssapi) \
@@ -223,7 +224,7 @@ src_install() {
 		local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
 
 		mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
-		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null
+		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
 
 		for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
 			bn=$(basename "${f}")

diff --git a/dev-db/postgresql/postgresql-10_beta2.ebuild b/dev-db/postgresql/postgresql-9.2.21-r2.ebuild
similarity index 79%
copy from dev-db/postgresql/postgresql-10_beta2.ebuild
copy to dev-db/postgresql/postgresql-9.2.21-r2.ebuild
index 6ccc0c76d15..1c518175abf 100644
--- a/dev-db/postgresql/postgresql-10_beta2.ebuild
+++ b/dev-db/postgresql/postgresql-9.2.21-r2.ebuild
@@ -1,31 +1,27 @@
 # Copyright 1999-2017 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-EAPI="6"
+EAPI="5"
 
 PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
 
 inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
 		systemd user versionator
 
-KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
+KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-SLOT=$(get_major_version)
+SLOT="$(get_version_component_range 1-2)"
 
-MY_PV=${PV/_/}
-S="${WORKDIR}/${PN}-${MY_PV}"
-
-SRC_URI="mirror://postgresql/source/v${MY_PV}/postgresql-${MY_PV}.tar.bz2"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
 HOMEPAGE="http://www.postgresql.org/"
 
-LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr zh_CN
-		 zh_TW"
-IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp
-	  python +readline selinux +server systemd ssl static-libs tcl threads uuid
-	  xml zlib"
+LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
+		 zh_CN zh_TW"
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp python
+	  +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
 	IUSE+=" linguas_${lingua}"
@@ -52,39 +48,16 @@ pam? ( virtual/pam )
 perl? ( >=dev-lang/perl-5.8:= )
 python? ( ${PYTHON_DEPS} )
 readline? ( sys-libs/readline:0= )
-server? ( systemd? ( sys-apps/systemd ) )
 ssl? (
 	!libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
 	libressl? ( dev-libs/libressl:= )
 )
 tcl? ( >=dev-lang/tcl-8:0= )
+uuid? ( dev-libs/ossp-uuid )
 xml? ( dev-libs/libxml2 dev-libs/libxslt )
 zlib? ( sys-libs/zlib )
 "
 
-# uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
-# supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
-# the libc includes UUID functions.
-UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
-BSD_LIBC=( elibc_{Free,Net,Open}BSD )
-
-nest_usedep() {
-	local front back
-	while [[ ${#} -gt 1 ]]; do
-		front+="${1}? ( "
-		back+=" )"
-		shift
-	done
-	echo "${front}${1}${back}"
-}
-
-IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
-CDEPEND+="
-uuid? (
-	${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
-	$(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} dev-libs/ossp-uuid)
-)"
-
 DEPEND="${CDEPEND}
 !!<sys-apps/sandbox-2.0
 sys-devel/bison
@@ -117,16 +90,11 @@ src_prepare() {
 	sed "s|\(PGSOCKET_DIR\s\+\)\"/tmp\"|\1\"${EPREFIX}/run/postgresql\"|" \
 		-i src/include/pg_config_manual.h || die
 
-	# Rely on $PATH being in the proper order so that the correct
-	# install program is used for modules utilizing PGXS in both
-	# hardened and non-hardened environments. (Bug #528786)
-	sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
-
-	use server || eapply "${FILESDIR}/${PN}-${SLOT}beta2-no-server.patch"
+	use server || epatch "${FILESDIR}/${PN}-${SLOT}-no-server.patch"
 
 	# Fix bug 486556 where the server would crash at start up because of
 	# an infinite loop caused by a self-referencing symlink.
-	#eapply "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+	epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
 
 	if use pam ; then
 		sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
@@ -134,7 +102,7 @@ src_prepare() {
 			die 'PGSQL_PAM_SERVICE rename failed.'
 	fi
 
-	eapply_user
+	epatch_user
 }
 
 src_configure() {
@@ -149,17 +117,6 @@ src_configure() {
 
 	local PO="${EPREFIX%/}"
 
-	local i uuid_config=""
-	if use uuid; then
-		for i in ${UTIL_LINUX_LIBC[@]}; do
-			use ${i} && uuid_config="--with-uuid=e2fs"
-		done
-		for i in ${BSD_LIBC[@]}; do
-			use ${i} && uuid_config="--with-uuid=bsd"
-		done
-		[[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
-	fi
-
 	econf \
 		--prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
 		--datadir="${PO}/usr/share/postgresql-${SLOT}" \
@@ -171,15 +128,15 @@ src_configure() {
 		$(use_enable !pg_legacytimestamp integer-datetimes) \
 		$(use_enable threads thread-safety) \
 		$(use_with kerberos gssapi) \
+		$(use_with kerberos krb5) \
 		$(use_with ldap) \
 		$(use_with pam) \
 		$(use_with perl) \
 		$(use_with python) \
 		$(use_with readline) \
 		$(use_with ssl openssl) \
-		$(usex server "$(use_with systemd)" '--without-systemd') \
 		$(use_with tcl) \
-		${uuid_config} \
+		$(use_with uuid ossp-uuid) \
 		$(use_with xml libxml) \
 		$(use_with xml libxslt) \
 		$(use_with zlib) \
@@ -205,13 +162,7 @@ src_install() {
 	doins -r doc/src/sgml/man{1,3,7}
 	if ! use server; then
 		# Remove man pages for non-existent binaries
-		serverman=(
-			initdb
-			pg_{archivecleanup,controldata,ctl,resetwal,rewind,standby}
-			pg_{test_{fsync,timing},upgrade,waldump}
-			post{gres,master}
-		)
-		for m in ${serverman[@]} ; do
+		for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
 			rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
 		done
 	fi
@@ -223,11 +174,11 @@ src_install() {
 		local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
 
 		mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
-		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null
+		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
 
 		for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
 			bn=$(basename "${f}")
-			slotted_name=${bn%.${mansec}}${SLOT}.${mansec}
+			slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
 			case ${bn} in
 				TABLE.7|WITH.7)
 					echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
@@ -255,7 +206,7 @@ src_install() {
 		# issue. This is only necessary for 9.7 and earlier. 10 never
 		# had this issue.
 		dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
-			  "/usr/bin/${bn}${SLOT/.}"
+			  "/usr/bin/${bn}${SLOT/.}tmp"
 	done
 
 	if use doc ; then
@@ -268,16 +219,14 @@ src_install() {
 
 	if use server; then
 		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-			"${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
+			"${FILESDIR}/${PN}.confd-9.2" | newconfd - ${PN}-${SLOT}
 
 		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-			"${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
+			"${FILESDIR}/${PN}.init-9.2" | newinitd - ${PN}-${SLOT}
 
-		if use systemd; then
-			sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-				"${FILESDIR}/${PN}.service-9.6" | \
-				systemd_newunit - ${PN}-${SLOT}.service
-		fi
+		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+			"${FILESDIR}/${PN}.service" | \
+			systemd_newunit - ${PN}-${SLOT}.service
 
 		newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
 
@@ -290,6 +239,40 @@ src_install() {
 	fi
 }
 
+pkg_preinst() {
+	# Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+	# /usr/bin/psql96). They may have been created by the
+	# postgresql.eselect module, but they're handled within this ebuild
+	# now. It's alright if we momentarily delete /usr/bin/psql as it
+	# will be recreated by the eselect module in pkg_ppostinst(). This
+	# is only necessary for 9.7 and earlier. 10 and later were never
+	# handled in this manner.
+	local canonicalise
+	if type -p realpath > /dev/null; then
+		canonicalise=realpath
+	elif type -p readlink > /dev/null; then
+		canonicalise='readlink -f'
+	else
+		# can't die, subshell
+		die "No readlink nor realpath found, cannot canonicalise"
+	fi
+
+	local l
+	# First remove any symlinks in /usr/bin that may have been created
+	# by the old eselect
+	for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
+		if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
+			rm "${l}" || ewarn "Couldn't remove ${l}"
+		fi
+	done
+
+	# Then move the symlinks created by the ebuild to their proper place.
+	for l in "${ED}"/usr/bin/*tmp ; do
+		mv "${l}" "${l%tmp}" \
+			|| ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
+	done
+}
+
 pkg_postinst() {
 	postgresql-config update
 
@@ -416,7 +399,7 @@ pkg_config() {
 
 	# unix_socket_directory has no effect in postgresql.conf as it's
 	# overridden in the initscript
-	sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
+	sed '/^#unix_socket_directory/d' -i "${PGDATA%/}"/postgresql.conf
 
 	cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
 		# This is here because of https://bugs.gentoo.org/show_bug.cgi?id=518522
@@ -430,11 +413,9 @@ pkg_config() {
 	einfo "by default. You can disable it in the cluster's:"
 	einfo "    ${PGDATA%/}/postgresql.conf"
 	einfo
-	if ! use systemd; then
-		einfo "The PostgreSQL server, by default, will log events to:"
-		einfo "    ${DATA_DIR%/}/postmaster.log"
-		einfo
-	fi
+	einfo "The PostgreSQL server, by default, will log events to:"
+	einfo "    ${DATA_DIR%/}/postmaster.log"
+	einfo
 	if use prefix ; then
 		einfo "The location of the configuration files have moved to:"
 		einfo "    ${PGDATA}"
@@ -445,9 +426,6 @@ pkg_config() {
 		einfo
 		einfo "Or move the configuration files back:"
 		einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
-	elif use systemd; then
-		einfo "You should use the 'postgresql-${SLOT}.service' unit to run PostgreSQL"
-		einfo "instead of 'pg_ctl'."
 	else
 		einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
 		einfo "instead of 'pg_ctl'."

diff --git a/dev-db/postgresql/postgresql-10_beta2.ebuild b/dev-db/postgresql/postgresql-9.3.17-r2.ebuild
similarity index 83%
copy from dev-db/postgresql/postgresql-10_beta2.ebuild
copy to dev-db/postgresql/postgresql-9.3.17-r2.ebuild
index 6ccc0c76d15..ba71038863b 100644
--- a/dev-db/postgresql/postgresql-10_beta2.ebuild
+++ b/dev-db/postgresql/postgresql-9.3.17-r2.ebuild
@@ -1,31 +1,27 @@
 # Copyright 1999-2017 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-EAPI="6"
+EAPI="5"
 
 PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
 
 inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
 		systemd user versionator
 
-KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
+KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-SLOT=$(get_major_version)
+SLOT="$(get_version_component_range 1-2)"
 
-MY_PV=${PV/_/}
-S="${WORKDIR}/${PN}-${MY_PV}"
-
-SRC_URI="mirror://postgresql/source/v${MY_PV}/postgresql-${MY_PV}.tar.bz2"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
 HOMEPAGE="http://www.postgresql.org/"
 
-LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr zh_CN
-		 zh_TW"
-IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp
-	  python +readline selinux +server systemd ssl static-libs tcl threads uuid
-	  xml zlib"
+LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
+		 zh_CN zh_TW"
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp python
+	  +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
 	IUSE+=" linguas_${lingua}"
@@ -52,39 +48,16 @@ pam? ( virtual/pam )
 perl? ( >=dev-lang/perl-5.8:= )
 python? ( ${PYTHON_DEPS} )
 readline? ( sys-libs/readline:0= )
-server? ( systemd? ( sys-apps/systemd ) )
 ssl? (
 	!libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
 	libressl? ( dev-libs/libressl:= )
 )
 tcl? ( >=dev-lang/tcl-8:0= )
+uuid? ( dev-libs/ossp-uuid )
 xml? ( dev-libs/libxml2 dev-libs/libxslt )
 zlib? ( sys-libs/zlib )
 "
 
-# uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
-# supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
-# the libc includes UUID functions.
-UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
-BSD_LIBC=( elibc_{Free,Net,Open}BSD )
-
-nest_usedep() {
-	local front back
-	while [[ ${#} -gt 1 ]]; do
-		front+="${1}? ( "
-		back+=" )"
-		shift
-	done
-	echo "${front}${1}${back}"
-}
-
-IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
-CDEPEND+="
-uuid? (
-	${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
-	$(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} dev-libs/ossp-uuid)
-)"
-
 DEPEND="${CDEPEND}
 !!<sys-apps/sandbox-2.0
 sys-devel/bison
@@ -122,11 +95,11 @@ src_prepare() {
 	# hardened and non-hardened environments. (Bug #528786)
 	sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
-	use server || eapply "${FILESDIR}/${PN}-${SLOT}beta2-no-server.patch"
+	use server || epatch "${FILESDIR}/${PN}-${SLOT}-no-server.patch"
 
 	# Fix bug 486556 where the server would crash at start up because of
 	# an infinite loop caused by a self-referencing symlink.
-	#eapply "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+	epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
 
 	if use pam ; then
 		sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
@@ -134,7 +107,7 @@ src_prepare() {
 			die 'PGSQL_PAM_SERVICE rename failed.'
 	fi
 
-	eapply_user
+	epatch_user
 }
 
 src_configure() {
@@ -149,17 +122,6 @@ src_configure() {
 
 	local PO="${EPREFIX%/}"
 
-	local i uuid_config=""
-	if use uuid; then
-		for i in ${UTIL_LINUX_LIBC[@]}; do
-			use ${i} && uuid_config="--with-uuid=e2fs"
-		done
-		for i in ${BSD_LIBC[@]}; do
-			use ${i} && uuid_config="--with-uuid=bsd"
-		done
-		[[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
-	fi
-
 	econf \
 		--prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
 		--datadir="${PO}/usr/share/postgresql-${SLOT}" \
@@ -171,15 +133,15 @@ src_configure() {
 		$(use_enable !pg_legacytimestamp integer-datetimes) \
 		$(use_enable threads thread-safety) \
 		$(use_with kerberos gssapi) \
+		$(use_with kerberos krb5) \
 		$(use_with ldap) \
 		$(use_with pam) \
 		$(use_with perl) \
 		$(use_with python) \
 		$(use_with readline) \
 		$(use_with ssl openssl) \
-		$(usex server "$(use_with systemd)" '--without-systemd') \
 		$(use_with tcl) \
-		${uuid_config} \
+		$(use_with uuid ossp-uuid) \
 		$(use_with xml libxml) \
 		$(use_with xml libxslt) \
 		$(use_with zlib) \
@@ -205,13 +167,7 @@ src_install() {
 	doins -r doc/src/sgml/man{1,3,7}
 	if ! use server; then
 		# Remove man pages for non-existent binaries
-		serverman=(
-			initdb
-			pg_{archivecleanup,controldata,ctl,resetwal,rewind,standby}
-			pg_{test_{fsync,timing},upgrade,waldump}
-			post{gres,master}
-		)
-		for m in ${serverman[@]} ; do
+		for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
 			rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
 		done
 	fi
@@ -223,11 +179,11 @@ src_install() {
 		local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
 
 		mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
-		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null
+		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
 
 		for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
 			bn=$(basename "${f}")
-			slotted_name=${bn%.${mansec}}${SLOT}.${mansec}
+			slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
 			case ${bn} in
 				TABLE.7|WITH.7)
 					echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
@@ -255,7 +211,7 @@ src_install() {
 		# issue. This is only necessary for 9.7 and earlier. 10 never
 		# had this issue.
 		dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
-			  "/usr/bin/${bn}${SLOT/.}"
+			  "/usr/bin/${bn}${SLOT/.}tmp"
 	done
 
 	if use doc ; then
@@ -273,11 +229,9 @@ src_install() {
 		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
 			"${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
 
-		if use systemd; then
-			sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-				"${FILESDIR}/${PN}.service-9.6" | \
-				systemd_newunit - ${PN}-${SLOT}.service
-		fi
+		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+			"${FILESDIR}/${PN}.service" | \
+			systemd_newunit - ${PN}-${SLOT}.service
 
 		newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
 
@@ -290,6 +244,40 @@ src_install() {
 	fi
 }
 
+pkg_preinst() {
+	# Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+	# /usr/bin/psql96). They may have been created by the
+	# postgresql.eselect module, but they're handled within this ebuild
+	# now. It's alright if we momentarily delete /usr/bin/psql as it
+	# will be recreated by the eselect module in pkg_ppostinst(). This
+	# is only necessary for 9.7 and earlier. 10 and later were never
+	# handled in this manner.
+	local canonicalise
+	if type -p realpath > /dev/null; then
+		canonicalise=realpath
+	elif type -p readlink > /dev/null; then
+		canonicalise='readlink -f'
+	else
+		# can't die, subshell
+		die "No readlink nor realpath found, cannot canonicalise"
+	fi
+
+	local l
+	# First remove any symlinks in /usr/bin that may have been created
+	# by the old eselect
+	for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
+		if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
+			rm "${l}" || ewarn "Couldn't remove ${l}"
+		fi
+	done
+
+	# Then move the symlinks created by the ebuild to their proper place.
+	for l in "${ED}"/usr/bin/*tmp ; do
+		mv "${l}" "${l%tmp}" \
+			|| ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
+	done
+}
+
 pkg_postinst() {
 	postgresql-config update
 
@@ -430,11 +418,9 @@ pkg_config() {
 	einfo "by default. You can disable it in the cluster's:"
 	einfo "    ${PGDATA%/}/postgresql.conf"
 	einfo
-	if ! use systemd; then
-		einfo "The PostgreSQL server, by default, will log events to:"
-		einfo "    ${DATA_DIR%/}/postmaster.log"
-		einfo
-	fi
+	einfo "The PostgreSQL server, by default, will log events to:"
+	einfo "    ${DATA_DIR%/}/postmaster.log"
+	einfo
 	if use prefix ; then
 		einfo "The location of the configuration files have moved to:"
 		einfo "    ${PGDATA}"
@@ -445,9 +431,6 @@ pkg_config() {
 		einfo
 		einfo "Or move the configuration files back:"
 		einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
-	elif use systemd; then
-		einfo "You should use the 'postgresql-${SLOT}.service' unit to run PostgreSQL"
-		einfo "instead of 'pg_ctl'."
 	else
 		einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
 		einfo "instead of 'pg_ctl'."

diff --git a/dev-db/postgresql/postgresql-10_beta2.ebuild b/dev-db/postgresql/postgresql-9.4.12-r2.ebuild
similarity index 84%
copy from dev-db/postgresql/postgresql-10_beta2.ebuild
copy to dev-db/postgresql/postgresql-9.4.12-r2.ebuild
index 6ccc0c76d15..1fad162edec 100644
--- a/dev-db/postgresql/postgresql-10_beta2.ebuild
+++ b/dev-db/postgresql/postgresql-9.4.12-r2.ebuild
@@ -1,31 +1,27 @@
 # Copyright 1999-2017 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-EAPI="6"
+EAPI="5"
 
 PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
 
 inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
 		systemd user versionator
 
-KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
+KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-SLOT=$(get_major_version)
+SLOT="$(get_version_component_range 1-2)"
 
-MY_PV=${PV/_/}
-S="${WORKDIR}/${PN}-${MY_PV}"
-
-SRC_URI="mirror://postgresql/source/v${MY_PV}/postgresql-${MY_PV}.tar.bz2"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
 HOMEPAGE="http://www.postgresql.org/"
 
-LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr zh_CN
-		 zh_TW"
-IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp
-	  python +readline selinux +server systemd ssl static-libs tcl threads uuid
-	  xml zlib"
+LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
+		 zh_CN zh_TW"
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp python
+	  +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
 	IUSE+=" linguas_${lingua}"
@@ -52,7 +48,6 @@ pam? ( virtual/pam )
 perl? ( >=dev-lang/perl-5.8:= )
 python? ( ${PYTHON_DEPS} )
 readline? ( sys-libs/readline:0= )
-server? ( systemd? ( sys-apps/systemd ) )
 ssl? (
 	!libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
 	libressl? ( dev-libs/libressl:= )
@@ -122,11 +117,11 @@ src_prepare() {
 	# hardened and non-hardened environments. (Bug #528786)
 	sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
-	use server || eapply "${FILESDIR}/${PN}-${SLOT}beta2-no-server.patch"
+	use server || epatch "${FILESDIR}/${PN}-9.4.10-no-server.patch"
 
 	# Fix bug 486556 where the server would crash at start up because of
 	# an infinite loop caused by a self-referencing symlink.
-	#eapply "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+	epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
 
 	if use pam ; then
 		sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
@@ -134,7 +129,7 @@ src_prepare() {
 			die 'PGSQL_PAM_SERVICE rename failed.'
 	fi
 
-	eapply_user
+	epatch_user
 }
 
 src_configure() {
@@ -177,7 +172,6 @@ src_configure() {
 		$(use_with python) \
 		$(use_with readline) \
 		$(use_with ssl openssl) \
-		$(usex server "$(use_with systemd)" '--without-systemd') \
 		$(use_with tcl) \
 		${uuid_config} \
 		$(use_with xml libxml) \
@@ -205,13 +199,7 @@ src_install() {
 	doins -r doc/src/sgml/man{1,3,7}
 	if ! use server; then
 		# Remove man pages for non-existent binaries
-		serverman=(
-			initdb
-			pg_{archivecleanup,controldata,ctl,resetwal,rewind,standby}
-			pg_{test_{fsync,timing},upgrade,waldump}
-			post{gres,master}
-		)
-		for m in ${serverman[@]} ; do
+		for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
 			rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
 		done
 	fi
@@ -223,11 +211,11 @@ src_install() {
 		local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
 
 		mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
-		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null
+		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
 
 		for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
 			bn=$(basename "${f}")
-			slotted_name=${bn%.${mansec}}${SLOT}.${mansec}
+			slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
 			case ${bn} in
 				TABLE.7|WITH.7)
 					echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
@@ -255,7 +243,7 @@ src_install() {
 		# issue. This is only necessary for 9.7 and earlier. 10 never
 		# had this issue.
 		dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
-			  "/usr/bin/${bn}${SLOT/.}"
+			  "/usr/bin/${bn}${SLOT/.}tmp"
 	done
 
 	if use doc ; then
@@ -273,11 +261,9 @@ src_install() {
 		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
 			"${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
 
-		if use systemd; then
-			sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-				"${FILESDIR}/${PN}.service-9.6" | \
-				systemd_newunit - ${PN}-${SLOT}.service
-		fi
+		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+			"${FILESDIR}/${PN}.service" | \
+			systemd_newunit - ${PN}-${SLOT}.service
 
 		newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
 
@@ -290,6 +276,40 @@ src_install() {
 	fi
 }
 
+pkg_preinst() {
+	# Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+	# /usr/bin/psql96). They may have been created by the
+	# postgresql.eselect module, but they're handled within this ebuild
+	# now. It's alright if we momentarily delete /usr/bin/psql as it
+	# will be recreated by the eselect module in pkg_ppostinst(). This
+	# is only necessary for 9.7 and earlier. 10 and later were never
+	# handled in this manner.
+	local canonicalise
+	if type -p realpath > /dev/null; then
+		canonicalise=realpath
+	elif type -p readlink > /dev/null; then
+		canonicalise='readlink -f'
+	else
+		# can't die, subshell
+		die "No readlink nor realpath found, cannot canonicalise"
+	fi
+
+	local l
+	# First remove any symlinks in /usr/bin that may have been created
+	# by the old eselect
+	for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
+		if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
+			rm "${l}" || ewarn "Couldn't remove ${l}"
+		fi
+	done
+
+	# Then move the symlinks created by the ebuild to their proper place.
+	for l in "${ED}"/usr/bin/*tmp ; do
+		mv "${l}" "${l%tmp}" \
+			|| ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
+	done
+}
+
 pkg_postinst() {
 	postgresql-config update
 
@@ -430,11 +450,9 @@ pkg_config() {
 	einfo "by default. You can disable it in the cluster's:"
 	einfo "    ${PGDATA%/}/postgresql.conf"
 	einfo
-	if ! use systemd; then
-		einfo "The PostgreSQL server, by default, will log events to:"
-		einfo "    ${DATA_DIR%/}/postmaster.log"
-		einfo
-	fi
+	einfo "The PostgreSQL server, by default, will log events to:"
+	einfo "    ${DATA_DIR%/}/postmaster.log"
+	einfo
 	if use prefix ; then
 		einfo "The location of the configuration files have moved to:"
 		einfo "    ${PGDATA}"
@@ -445,9 +463,6 @@ pkg_config() {
 		einfo
 		einfo "Or move the configuration files back:"
 		einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
-	elif use systemd; then
-		einfo "You should use the 'postgresql-${SLOT}.service' unit to run PostgreSQL"
-		einfo "instead of 'pg_ctl'."
 	else
 		einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
 		einfo "instead of 'pg_ctl'."

diff --git a/dev-db/postgresql/postgresql-10_beta2.ebuild b/dev-db/postgresql/postgresql-9.5.7-r1.ebuild
similarity index 84%
copy from dev-db/postgresql/postgresql-10_beta2.ebuild
copy to dev-db/postgresql/postgresql-9.5.7-r1.ebuild
index 6ccc0c76d15..2f776382307 100644
--- a/dev-db/postgresql/postgresql-10_beta2.ebuild
+++ b/dev-db/postgresql/postgresql-9.5.7-r1.ebuild
@@ -1,7 +1,7 @@
 # Copyright 1999-2017 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-EAPI="6"
+EAPI="5"
 
 PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
 
@@ -10,22 +10,18 @@ inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
 
 KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-SLOT=$(get_major_version)
+SLOT="$(get_version_component_range 1-2)"
 
-MY_PV=${PV/_/}
-S="${WORKDIR}/${PN}-${MY_PV}"
-
-SRC_URI="mirror://postgresql/source/v${MY_PV}/postgresql-${MY_PV}.tar.bz2"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
 HOMEPAGE="http://www.postgresql.org/"
 
-LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr zh_CN
-		 zh_TW"
-IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp
-	  python +readline selinux +server systemd ssl static-libs tcl threads uuid
-	  xml zlib"
+LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
+		 zh_CN zh_TW"
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp python
+	  +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
 	IUSE+=" linguas_${lingua}"
@@ -52,7 +48,6 @@ pam? ( virtual/pam )
 perl? ( >=dev-lang/perl-5.8:= )
 python? ( ${PYTHON_DEPS} )
 readline? ( sys-libs/readline:0= )
-server? ( systemd? ( sys-apps/systemd ) )
 ssl? (
 	!libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
 	libressl? ( dev-libs/libressl:= )
@@ -122,11 +117,11 @@ src_prepare() {
 	# hardened and non-hardened environments. (Bug #528786)
 	sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
-	use server || eapply "${FILESDIR}/${PN}-${SLOT}beta2-no-server.patch"
+	use server || epatch "${FILESDIR}/${PN}-9.5.5-no-server.patch"
 
 	# Fix bug 486556 where the server would crash at start up because of
 	# an infinite loop caused by a self-referencing symlink.
-	#eapply "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+	epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
 
 	if use pam ; then
 		sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
@@ -134,7 +129,7 @@ src_prepare() {
 			die 'PGSQL_PAM_SERVICE rename failed.'
 	fi
 
-	eapply_user
+	epatch_user
 }
 
 src_configure() {
@@ -168,6 +163,7 @@ src_configure() {
 		--mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
 		--sysconfdir="${PO}/etc/postgresql-${SLOT}" \
 		--with-system-tzdata="${PO}/usr/share/zoneinfo" \
+		$(use_enable !alpha spinlocks) \
 		$(use_enable !pg_legacytimestamp integer-datetimes) \
 		$(use_enable threads thread-safety) \
 		$(use_with kerberos gssapi) \
@@ -177,7 +173,6 @@ src_configure() {
 		$(use_with python) \
 		$(use_with readline) \
 		$(use_with ssl openssl) \
-		$(usex server "$(use_with systemd)" '--without-systemd') \
 		$(use_with tcl) \
 		${uuid_config} \
 		$(use_with xml libxml) \
@@ -205,13 +200,7 @@ src_install() {
 	doins -r doc/src/sgml/man{1,3,7}
 	if ! use server; then
 		# Remove man pages for non-existent binaries
-		serverman=(
-			initdb
-			pg_{archivecleanup,controldata,ctl,resetwal,rewind,standby}
-			pg_{test_{fsync,timing},upgrade,waldump}
-			post{gres,master}
-		)
-		for m in ${serverman[@]} ; do
+		for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
 			rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
 		done
 	fi
@@ -223,11 +212,11 @@ src_install() {
 		local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
 
 		mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
-		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null
+		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
 
 		for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
 			bn=$(basename "${f}")
-			slotted_name=${bn%.${mansec}}${SLOT}.${mansec}
+			slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
 			case ${bn} in
 				TABLE.7|WITH.7)
 					echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
@@ -255,7 +244,7 @@ src_install() {
 		# issue. This is only necessary for 9.7 and earlier. 10 never
 		# had this issue.
 		dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
-			  "/usr/bin/${bn}${SLOT/.}"
+			  "/usr/bin/${bn}${SLOT/.}tmp"
 	done
 
 	if use doc ; then
@@ -273,11 +262,9 @@ src_install() {
 		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
 			"${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
 
-		if use systemd; then
-			sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-				"${FILESDIR}/${PN}.service-9.6" | \
-				systemd_newunit - ${PN}-${SLOT}.service
-		fi
+		sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+			"${FILESDIR}/${PN}.service" | \
+			systemd_newunit - ${PN}-${SLOT}.service
 
 		newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
 
@@ -290,9 +277,48 @@ src_install() {
 	fi
 }
 
+pkg_preinst() {
+	# Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+	# /usr/bin/psql96). They may have been created by the
+	# postgresql.eselect module, but they're handled within this ebuild
+	# now. It's alright if we momentarily delete /usr/bin/psql as it
+	# will be recreated by the eselect module in pkg_ppostinst(). This
+	# is only necessary for 9.7 and earlier. 10 and later were never
+	# handled in this manner.
+	local canonicalise
+	if type -p realpath > /dev/null; then
+		canonicalise=realpath
+	elif type -p readlink > /dev/null; then
+		canonicalise='readlink -f'
+	else
+		# can't die, subshell
+		die "No readlink nor realpath found, cannot canonicalise"
+	fi
+
+	local l
+	# First remove any symlinks in /usr/bin that may have been created
+	# by the old eselect
+	for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
+		if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
+			rm "${l}" || ewarn "Couldn't remove ${l}"
+		fi
+	done
+
+	# Then move the symlinks created by the ebuild to their proper place.
+	for l in "${ED}"/usr/bin/*tmp ; do
+		mv "${l}" "${l%tmp}" \
+			|| ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
+	done
+}
+
 pkg_postinst() {
 	postgresql-config update
 
+	if use alpha && use server ; then
+		ewarn "PostgreSQL 9.5+ no longer has native spinlock support on Alpha platforms."
+		ewarn "As a result, performance will be extremely degraded."
+	fi
+
 	elog "If you need a global psqlrc-file, you can place it in:"
 	elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
 
@@ -430,11 +456,9 @@ pkg_config() {
 	einfo "by default. You can disable it in the cluster's:"
 	einfo "    ${PGDATA%/}/postgresql.conf"
 	einfo
-	if ! use systemd; then
-		einfo "The PostgreSQL server, by default, will log events to:"
-		einfo "    ${DATA_DIR%/}/postmaster.log"
-		einfo
-	fi
+	einfo "The PostgreSQL server, by default, will log events to:"
+	einfo "    ${DATA_DIR%/}/postmaster.log"
+	einfo
 	if use prefix ; then
 		einfo "The location of the configuration files have moved to:"
 		einfo "    ${PGDATA}"
@@ -445,9 +469,6 @@ pkg_config() {
 		einfo
 		einfo "Or move the configuration files back:"
 		einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
-	elif use systemd; then
-		einfo "You should use the 'postgresql-${SLOT}.service' unit to run PostgreSQL"
-		einfo "instead of 'pg_ctl'."
 	else
 		einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
 		einfo "instead of 'pg_ctl'."

diff --git a/dev-db/postgresql/postgresql-10_beta2.ebuild b/dev-db/postgresql/postgresql-9.6.3-r2.ebuild
similarity index 88%
copy from dev-db/postgresql/postgresql-10_beta2.ebuild
copy to dev-db/postgresql/postgresql-9.6.3-r2.ebuild
index 6ccc0c76d15..b9e82309949 100644
--- a/dev-db/postgresql/postgresql-10_beta2.ebuild
+++ b/dev-db/postgresql/postgresql-9.6.3-r2.ebuild
@@ -1,7 +1,7 @@
 # Copyright 1999-2017 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-EAPI="6"
+EAPI="5"
 
 PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
 
@@ -10,7 +10,7 @@ inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
 
 KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-SLOT=$(get_major_version)
+SLOT="$(get_version_component_range 1-2)"
 
 MY_PV=${PV/_/}
 S="${WORKDIR}/${PN}-${MY_PV}"
@@ -21,11 +21,10 @@ LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
 HOMEPAGE="http://www.postgresql.org/"
 
-LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr zh_CN
-		 zh_TW"
-IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp
-	  python +readline selinux +server systemd ssl static-libs tcl threads uuid
-	  xml zlib"
+LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
+		 zh_CN zh_TW"
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp python
+	  +readline selinux +server systemd ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
 	IUSE+=" linguas_${lingua}"
@@ -52,11 +51,11 @@ pam? ( virtual/pam )
 perl? ( >=dev-lang/perl-5.8:= )
 python? ( ${PYTHON_DEPS} )
 readline? ( sys-libs/readline:0= )
-server? ( systemd? ( sys-apps/systemd ) )
 ssl? (
 	!libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
 	libressl? ( dev-libs/libressl:= )
 )
+server? ( systemd? ( sys-apps/systemd ) )
 tcl? ( >=dev-lang/tcl-8:0= )
 xml? ( dev-libs/libxml2 dev-libs/libxslt )
 zlib? ( sys-libs/zlib )
@@ -122,11 +121,11 @@ src_prepare() {
 	# hardened and non-hardened environments. (Bug #528786)
 	sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
-	use server || eapply "${FILESDIR}/${PN}-${SLOT}beta2-no-server.patch"
+	use server || epatch "${FILESDIR}/${PN}-${SLOT}.1-no-server.patch"
 
 	# Fix bug 486556 where the server would crash at start up because of
 	# an infinite loop caused by a self-referencing symlink.
-	#eapply "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+	epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
 
 	if use pam ; then
 		sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
@@ -134,7 +133,7 @@ src_prepare() {
 			die 'PGSQL_PAM_SERVICE rename failed.'
 	fi
 
-	eapply_user
+	epatch_user
 }
 
 src_configure() {
@@ -168,6 +167,7 @@ src_configure() {
 		--mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
 		--sysconfdir="${PO}/etc/postgresql-${SLOT}" \
 		--with-system-tzdata="${PO}/usr/share/zoneinfo" \
+		$(use_enable !alpha spinlocks) \
 		$(use_enable !pg_legacytimestamp integer-datetimes) \
 		$(use_enable threads thread-safety) \
 		$(use_with kerberos gssapi) \
@@ -205,13 +205,7 @@ src_install() {
 	doins -r doc/src/sgml/man{1,3,7}
 	if ! use server; then
 		# Remove man pages for non-existent binaries
-		serverman=(
-			initdb
-			pg_{archivecleanup,controldata,ctl,resetwal,rewind,standby}
-			pg_{test_{fsync,timing},upgrade,waldump}
-			post{gres,master}
-		)
-		for m in ${serverman[@]} ; do
+		for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
 			rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
 		done
 	fi
@@ -223,11 +217,11 @@ src_install() {
 		local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
 
 		mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
-		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null
+		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
 
 		for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
 			bn=$(basename "${f}")
-			slotted_name=${bn%.${mansec}}${SLOT}.${mansec}
+			slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
 			case ${bn} in
 				TABLE.7|WITH.7)
 					echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
@@ -255,7 +249,7 @@ src_install() {
 		# issue. This is only necessary for 9.7 and earlier. 10 never
 		# had this issue.
 		dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
-			  "/usr/bin/${bn}${SLOT/.}"
+			  "/usr/bin/${bn}${SLOT/.}tmp"
 	done
 
 	if use doc ; then
@@ -290,6 +284,40 @@ src_install() {
 	fi
 }
 
+pkg_preinst() {
+	# Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+	# /usr/bin/psql96). They may have been created by the
+	# postgresql.eselect module, but they're handled within this ebuild
+	# now. It's alright if we momentarily delete /usr/bin/psql as it
+	# will be recreated by the eselect module in pkg_ppostinst(). This
+	# is only necessary for 9.7 and earlier. 10 and later were never
+	# handled in this manner.
+	local canonicalise
+	if type -p realpath > /dev/null; then
+		canonicalise=realpath
+	elif type -p readlink > /dev/null; then
+		canonicalise='readlink -f'
+	else
+		# can't die, subshell
+		die "No readlink nor realpath found, cannot canonicalise"
+	fi
+
+	local l
+	# First remove any symlinks in /usr/bin that may have been created
+	# by the old eselect
+	for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
+		if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
+			rm "${l}" || ewarn "Couldn't remove ${l}"
+		fi
+	done
+
+	# Then move the symlinks created by the ebuild to their proper place.
+	for l in "${ED}"/usr/bin/*tmp ; do
+		mv "${l}" "${l%tmp}" \
+			|| ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
+	done
+}
+
 pkg_postinst() {
 	postgresql-config update
 

diff --git a/dev-db/postgresql/postgresql-9999.ebuild b/dev-db/postgresql/postgresql-9999.ebuild
index 0245d247103..6c6a2cb3946 100644
--- a/dev-db/postgresql/postgresql-9999.ebuild
+++ b/dev-db/postgresql/postgresql-9999.ebuild
@@ -213,7 +213,7 @@ src_install() {
 		local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
 
 		mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
-		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null
+		pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
 
 		for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
 			bn=$(basename "${f}")