From: Sebastian Harl Date: Sat, 28 Apr 2012 15:33:10 +0000 (+0200) Subject: Initial import of PostRR -- the PostgreSQL Round-Robin Extension. X-Git-Tag: postrr-0.0.0^0 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ac5961c9983ead0da2d96af6f08ab5701bbdd258;p=postrr.git Initial import of PostRR -- the PostgreSQL Round-Robin Extension. For now, this is an early and highly experimental incomplete draft ;-) The current version provides a new data type "RRTimeslice" implementing basic round-robin features. It is defined by the length of the slice and the number of slices before wrapping around, both specified using the type modifier. It provides basic comparison operators and an operator class for the btree indexing method. --- ac5961c9983ead0da2d96af6f08ab5701bbdd258 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6848d40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +# build system stuff +.deps +Makefile +Makefile.in +Makefile.pgxs +aclocal.m4 +autom4te.cache +compile +config.* +configure +depcomp +install-sh +missing +postrr_config.h* +stamp-h1 +version + +# ltdl stuff +libtool +ltmain.sh + +# build output +.libs +*.la +*.lo +*.o +postrr.7 +postrr.h +postrr.sql +postrr-*.so + diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..b90bd54 --- /dev/null +++ b/COPYING @@ -0,0 +1,24 @@ +Copyright (c) 2012 Sebastian 'tokkee' Harl +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000..23e5f25 --- /dev/null +++ b/INSTALL @@ -0,0 +1,236 @@ +Installation Instructions +************************* + +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005 Free +Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. + +Basic Installation +================== + +These are generic installation instructions. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. (Caching is +disabled by default to prevent problems with accidental use of stale +cache files.) + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You only need +`configure.ac' if you want to change it or regenerate `configure' using +a newer version of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. If you're + using `csh' on an old version of System V, you might need to type + `sh ./configure' instead to prevent `csh' from trying to execute + `configure' itself. + + Running `configure' takes awhile. While running, it prints some + messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + +Compilers and Options +===================== + +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c89 CFLAGS=-O2 LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + +You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you must use a version of `make' that +supports the `VPATH' variable, such as GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + If you have to use a `make' that does not support the `VPATH' +variable, you have to compile the package for one architecture at a +time in the source code directory. After you have installed the +package for one architecture, use `make distclean' before reconfiguring +for another architecture. + +Installation Names +================== + +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + +Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + +Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). Here is a another example: + + /bin/bash ./configure CONFIG_SHELL=/bin/bash + +Here the `CONFIG_SHELL=/bin/bash' operand causes subsequent +configuration-related scripts to be executed by `/bin/bash'. + +`configure' Invocation +====================== + +`configure' recognizes the following options to control how it operates. + +`--help' +`-h' + Print a summary of the options to `configure', and exit. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. + diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..8fd30a5 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,11 @@ +SUBDIRS = src +if BUILD_DOCUMENTATION +SUBDIRS += doc +endif + +EXTRA_DIST = autogen.sh version-gen.sh +CLEANFILES = version + +version: ./version-gen.sh + @./version-gen.sh > /dev/null + diff --git a/README b/README new file mode 100644 index 0000000..2f3581e --- /dev/null +++ b/README @@ -0,0 +1,47 @@ +PostgreSQL Round-Robin Extension +================================ + + PostRR (\ˈpōs-tər\) is an extension for the PostgreSQL database management + system providing round-robin functionality. It provides additional data + types and functions to handle discrete time-series of data and consolidation + thereof. + + For now, the software is in an early and very experimental state. + + This is free and open source software, licensed under the 2-clause BSD + license. See COPYING for details. + +Prerequisites +------------- + + To compile the PostRR package from source you need: + + * A build environment: autotools, libtool, C compiler, ... + + * A POSIX + Single UNIX Specification compatible C library. + + * asciidoc, xsltproc: + The AsciiDoc text document format is used to write the manpages. + + * PostgreSQL server-side development files + (currently tested with PostgreSQL 9.1) + +Configuring / Compiling / Installing +------------------------------------ + + To configure, build and install PostRR with the default settings, run + `./configure && make && make install'. For detailed, generic instructions + see INSTALL. For a complete list of configure options and their description, + run `./configure --help'. + + By default, PostRR will be installed into `/opt/postrr'. You can adjust this + setting by specifying the `--prefix' configure option - see INSTALL for + details. If you pass DESTDIR= to `make install', will be + prefixed to all installation directories. This might be useful when creating + packages for PostRR. + +Author +------ + + Sebastian "tokkee" Harl + diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..baf89b6 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,21 @@ +#! /bin/sh + +libtoolize=libtoolize + +if which glibtoolize > /dev/null 2>&1; then + libtoolize=glibtoolize +fi + +set -ex + +aclocal --force --warnings=all +$libtoolize --automake --copy --force +aclocal +autoconf --force --warnings=all +autoheader --force --warnings=all +automake --add-missing --copy --foreign --warnings=all + +echo "Run './configure' to configure the software. See './configure --help'" +echo "for details on command line options and environment variables affecting" +echo "the build." + diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..6e802cc --- /dev/null +++ b/configure.ac @@ -0,0 +1,275 @@ +dnl Process this file with autoconf to produce a configure script. +dnl +dnl This is the PostRR configure script. +dnl +dnl Copyright (C) 2012 Sebastian 'tokkee' Harl +dnl All rights reserved. +dnl +dnl Redistribution and use in source and binary forms, with or without +dnl modification, are permitted provided that the following conditions +dnl are met: +dnl 1. Redistributions of source code must retain the above copyright +dnl notice, this list of conditions and the following disclaimer. +dnl 2. Redistributions in binary form must reproduce the above copyright +dnl notice, this list of conditions and the following disclaimer in the +dnl documentation and/or other materials provided with the distribution. +dnl +dnl THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +dnl ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +dnl TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +dnl PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +dnl CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +dnl EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +dnl PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +dnl OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +dnl WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +dnl OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +dnl ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +AC_INIT([PostgreSQL Round-Robin Extension], + [m4_esyscmd(./version-gen.sh)], + [sh@tokkee.org], + [postrr], + [http://git.tokkee.org/?p=postrr.git]) +PACKAGE_MAINTAINER="Sebastian 'tokkee' Harl " +AC_DEFINE_UNQUOTED([PACKAGE_MAINTAINER], ["$PACKAGE_MAINTAINER"], + [Define to the name of the maintainer of this package.]) +AC_CONFIG_SRCDIR([src/base.c]) +AC_CONFIG_HEADERS([src/postrr_config.h]) +AC_PREFIX_DEFAULT([/opt/postrr]) + +AM_INIT_AUTOMAKE([foreign -Wall]) + +AC_LANG(C) + +AC_SYS_LARGEFILE + +AC_PROG_CC +AC_PROG_CPP +AC_PROG_INSTALL +AC_PROG_LN_S +AC_PROG_MAKE_SET + +AM_PROG_CC_C_O + +m4_ifdef([LT_INIT], + [ + LT_INIT + ], + # else + # (older libtools) + [ + AC_PROG_LIBTOOL + ] +) + +test_cc_flags() { + AC_LANG_CONFTEST([AC_LANG_PROGRAM([[ ]], [[ ]])]) + $CC -c conftest.c $CFLAGS $@ > /dev/null 2> /dev/null + ret=$? + rm -f conftest.o + return $ret +} + +m4_divert_once([HELP_ENABLE], [ +Build options:]) + +dnl Optionally stick to standard C99 and POSIX:2001 as close as possible. +AC_ARG_ENABLE([standards], + AS_HELP_STRING([--enable-standards], + [C99 / POSIX standards compliance mode @<:@default=no@:>@]), + [enable_standards="$enableval"], + [enable_standards="no"]) + +if test "x$enable_standards" = "xyes"; then + AC_DEFINE([_ISOC99_SOURCE], 1, + [Define to enforce ISO/IEC 9899:1999 (C99) compliance.]) + AC_DEFINE([_POSIX_C_SOURCE], 200112L, + [Define to enforce IEEE 1003.1-2001 (POSIX:2001) compliance.]) + AC_DEFINE([_XOPEN_SOURCE], 600, + [Define to enforce X/Open 6 (XSI) compliance.]) + AC_DEFINE([_REENTRANT], 1, + [Define to enable reentrant interfaces.]) + AC_DEFINE([_THREAD_SAFE], 1, + [Define to enable reentrant interfaces.]) + + for flag in -std=c99 -pedantic; do + AC_MSG_CHECKING([whether $CC accepts $flag]) + + if test_cc_flags $flag; then + CFLAGS="$CFLAGS $flag" + AC_MSG_RESULT([yes]) + else + AC_MSG_RESULT([no]) + fi + done +fi + +dnl Hardening (see e.g. http://wiki.debian.org/Hardening for a motivation). +AC_DEFINE([_FORTIFY_SOURCE], 2, + [Define to enable protection against static sized buffer overflows.]) +AC_ARG_ENABLE([hardening], + AS_HELP_STRING([--disable-hardening], + [hardening options @<:@default=yes@:>@]), + [enable_hardening="$enableval"], + [enable_hardening="yes"]) + +if test "x$enable_hardening" = "xyes"; then + hardening=0 + hardening_tests=0 + for flag in -Wformat -Wformat-security; do + hardening_tests=$(($hardening_tests + 1)) + AC_MSG_CHECKING([whether $CC accepts $flag]) + + if test_cc_flags $flag; then + CFLAGS="$CFLAGS $flag" + hardening=$(($hardening + 1)) + AC_MSG_RESULT([yes]) + else + AC_MSG_RESULT([no]) + fi + done + if test $hardening -ne $hardening_tests; then + AC_MSG_WARN( + [Some hardening options are not supported by your compiler!]) + fi +fi + +dnl Strict checking for potential problems. +AC_ARG_ENABLE([strict-checks], + AS_HELP_STRING([--disable-strict-checks], + [strict compiler checks @<:@default=yes@:>@]), + [enable_strict_checks="$enableval"], + [enable_strict_checks="yes"]) + +STRICT_CPPFLAGS="" +for flag in -Wall -Werror; do + AC_MSG_CHECKING([whether $CC accepts $flag]) + + if test_cc_flags $flag; then + STRICT_CPPFLAGS="$STRICT_CPPFLAGS $flag" + AC_MSG_RESULT([yes]) + else + AC_MSG_RESULT([no]) + fi +done + +if test "x$enable_strict_checks" = "xyes"; then + for flag in -Wextra \ + -Wbad-function-cast \ + -Wcast-align \ + -Wcast-qual \ + -Wconversion \ + -Wdeclaration-after-statement \ + -Wmissing-prototypes \ + -Wpointer-arith \ + -Wshadow \ + -Wstrict-prototypes \ + -Wunreachable-code; do + AC_MSG_CHECKING([whether $CC accepts $flag]) + + if test_cc_flags $flag; then + STRICT_CPPFLAGS="$STRICT_CPPFLAGS $flag" + AC_MSG_RESULT([yes]) + else + AC_MSG_RESULT([no]) + fi + done +fi +AC_SUBST([STRICT_CPPFLAGS]) + +AC_CHECK_HEADERS(libgen.h) + +dnl Check for dependencies. +build_documentation="yes" + +have_xsltproc="yes" +AC_PATH_PROG([XSLTPROC], [xsltproc]) +if test "x$XSLTPROC" = "x"; then + have_xsltproc="no" + build_documentation="no (missing xsltproc)" +fi + +have_a2x="yes" +AC_PATH_PROG([A2X], [a2x]) +if test "x$A2X" = "x"; then + have_a2x="no" + build_documentation="no (missing a2x)" +fi +AC_SUBST([A2X]) + +dnl pg_config +AC_ARG_WITH([pgconfig], + [AS_HELP_STRING([--with-pgconfig=FILE], [foo bar],)], + [PG_CONFIG="$withval"], [PG_CONFIG=""]) + +if test "x$PG_CONFIG" = "x"; then + AC_PATH_PROG([PG_CONFIG], [pg_config]) + + if test "x$PG_CONFIG" = "x"; then + AC_MSG_ERROR([could not find pg_config in \$PATH; rerun configure with the --with-pgconfig parameter.]) + fi +else + if test "x$PG_CONFIG" = "xyes" -o "x$PG_CONFIG" = "xno"; then + AC_MSG_ERROR([please specify a parameter for --with-pgconfig]) + else + if ! test -f "$PG_CONFIG"; then + AC_MSG_ERROR([the specified path to pg_config does not exist: $PG_CONFIG]) + fi + fi +fi +AC_SUBST([PG_CONFIG]) + +PGXS=`$PG_CONFIG --pgxs` +if ! test -f "$PGXS"; then + AC_MSG_ERROR([could not find PGXS Makefile: '$PGXS']) +fi +AC_SUBST([PGXS]) + +PG_VERSION=`$PG_CONFIG --version` + +AC_DEFINE_UNQUOTED([PG_VERSION], ["$PG_VERSION"], + [Define to the version of PostgreSQL the package was built against.]) + +POSTRR_MAJOR_VERSION=`cat version | grep VERSION_MAJOR | cut -d= -f2` +POSTRR_MINOR_VERSION=`cat version | grep VERSION_MINOR | cut -d= -f2` +POSTRR_PATCH_VERSION=`cat version | grep VERSION_PATCH | cut -d= -f2` + +AC_SUBST([POSTRR_MAJOR_VERSION]) +AC_SUBST([POSTRR_MINOR_VERSION]) +AC_SUBST([POSTRR_PATCH_VERSION]) + +AM_CONDITIONAL([BUILD_DOCUMENTATION], test "x$build_documentation" = "xyes") + +AC_CONFIG_FILES([Makefile doc/Makefile + src/Makefile src/Makefile.pgxs + src/postrr.sql]) +AC_OUTPUT + +AC_MSG_RESULT() +AC_MSG_RESULT([$PACKAGE_NAME has been configured successfully.]) +AC_MSG_RESULT() +AC_MSG_RESULT([Run 'make' to compile the software and use 'make install' to]) +AC_MSG_RESULT([install the package into $prefix.]) +AC_MSG_RESULT() +AC_MSG_RESULT([Configuration summary:]) +AC_MSG_RESULT() +AC_MSG_RESULT([ package version: $PACKAGE_VERSION]) +AC_MSG_RESULT([ build date: `date --utc '+%F %T'` (UTC)]) +AC_MSG_RESULT() +AC_MSG_RESULT([ Tools:]) +AC_MSG_RESULT([ AsciiDoc (a2x): . . . . . . $have_a2x]) +AC_MSG_RESULT([ xsltproc: . . . . . . . . . $have_xsltproc]) +AC_MSG_RESULT() +AC_MSG_RESULT([ PostgreSQL tools and helpers:]) +AC_MSG_RESULT([ pg_config: . . . . . . . . $PG_CONFIG]) +AC_MSG_RESULT([ PGXS Makefile: . . . . . . $PGXS]) +AC_MSG_RESULT([ Version: . . . . . . . . . $PG_VERSION]) +AC_MSG_RESULT() +AC_MSG_RESULT([ Features:]) +AC_MSG_RESULT([ documentation: . . . . . . $build_documentation]) +AC_MSG_RESULT() +AC_MSG_RESULT([This package is maintained by $PACKAGE_MAINTAINER.]) +AC_MSG_RESULT([Please report bugs to $PACKAGE_BUGREPORT.]) +AC_MSG_RESULT() + diff --git a/doc/Makefile.am b/doc/Makefile.am new file mode 100644 index 0000000..4f400c1 --- /dev/null +++ b/doc/Makefile.am @@ -0,0 +1,14 @@ +EXTRA_DIST = postrr.txt +CLEANFILES = postrr.7 + +man_MANS = postrr.7 + +postrr.7: postrr.txt + +.txt.7: + @(cd ..; ./version-gen.sh > /dev/null) + @A2X@ -d manpage -f manpage \ + -apackage_version=$(PACKAGE_VERSION) \ + -abuild_date="$$( date --utc '+%F' )" \ + $< + diff --git a/doc/postrr.txt b/doc/postrr.txt new file mode 100644 index 0000000..dd6c556 --- /dev/null +++ b/doc/postrr.txt @@ -0,0 +1,39 @@ +POSTRR(7) +========= +Sebastian "tokkee" Harl +version {package_version}, {build_date} +:doctype: manpage + +NAME +---- +postrr - PostgreSQL Round-Robin Extension + +DESCRIPTION +----------- +PostRR is an extension for the PostgreSQL database management system providing +round-robin functionality. It provides additional data types and functions to +handle discrete time-series of data and consolidation thereof. + +DATA TYPES +~~~~~~~~~~ +The following data types are provided as a base for building round-robin +archives: + +* RRTimeslice: + + A timeslice implementing round-robin features. It is defined by the length + of the slice and the number of slices before wrapping around. + +AUTHOR +------ +PostRR was written by Sebastian "tokkee" Harl . + +COPYRIGHT +--------- +Copyright (C) 2012 Sebastian "tokkee" Harl + +This is free software under the terms of the BSD license, see the source for +copying conditions. There is NO WARRANTY; not even for MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. + +// vim: set tw=78 sw=4 ts=4 noexpandtab spell spelllang=en_us : + diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..db41c00 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,46 @@ +AM_CFLAGS = @STRICT_CFLAGS@ + +BUILT_SOURCES = postrr.h +CLEANFILES = postrr.h + +all-local: postrr.h postrr.sql Makefile.pgxs ../version-gen.sh + @echo Building $(PACKAGE_NAME) version $$( cd ..; ./version-gen.sh ) + make -f Makefile.pgxs all + +check-local: Makefile.pgxs + make -f Makefile.pgxs check + +# XXX: make it possible to install to the directories passed to configure +install-data-local: Makefile.pgxs + make -f Makefile.pgxs install + +uninstall-local: Makefile.pgxs + make -f Makefile.pgxs uninstall + +installdirs-local: Makefile.pgxs + make -f Makefile.pgxs installdirs + +installcheck-local: Makefile.pgxs + make -f Makefile.pgxs installcheck + +clean-local: Makefile.pgxs + make -f Makefile.pgxs clean + +distclean-local: Makefile.pgxs + make -f Makefile.pgxs distclean + +maintainer-clean-local: Makefile.pgxs + make -f Makefile.pgxs maintainer-clean + +../version: + (cd ..; make version) + +postrr.h: postrr.h.in ../version + . ../version; sed \ + -e "s/@POSTRR_VERSION_MAJOR@/$$VERSION_MAJOR/g" \ + -e "s/@POSTRR_VERSION_MINOR@/$$VERSION_MINOR/g" \ + -e "s/@POSTRR_VERSION_PATCH@/$$VERSION_PATCH/g" \ + -e "s/@POSTRR_VERSION_EXTRA@/$$VERSION_EXTRA/g" \ + -e "s/@POSTRR_VERSION_STRING@/$$VERSION_STRING/g" \ + postrr.h.in > postrr.h + diff --git a/src/Makefile.pgxs.in b/src/Makefile.pgxs.in new file mode 100644 index 0000000..0ccc4f1 --- /dev/null +++ b/src/Makefile.pgxs.in @@ -0,0 +1,44 @@ +# PostRR - src/Makefile.pgxs +# Copyright (C) 2012 Sebastian 'tokkee' Harl +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +PG_CPPFLAGS = @STRICT_CPPFLAGS@ -I@abs_builddir@ + +MODULE_big=postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@ +MODULEDIR=contrib/$(MODULE) + +PG_OBJS=base.o \ + rrtimeslice.o \ + utils/pg_spi.o + +DATA=postrr_comments.sql uninstall_postrr.sql +DATA_built=postrr.sql + +# objects to be build by PGXS +OBJS=$(PG_OBJS) + +# PGXS configuration +PG_CONFIG=@PG_CONFIG@ +PGXS=@PGXS@ +include $(PGXS) + diff --git a/src/base.c b/src/base.c new file mode 100644 index 0000000..f29e265 --- /dev/null +++ b/src/base.c @@ -0,0 +1,76 @@ +/* + * PostRR - src/base.c + * Copyright (C) 2012 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * The base PostRR components. + */ + +#include "postrr.h" + +#include +#include + +#include +#include +#include + +#ifdef PG_MODULE_MAGIC +PG_MODULE_MAGIC; +#endif + +/* + * prototypes for PostgreSQL functions + */ + +PG_FUNCTION_INFO_V1(postrr_version); + +/* + * public API + */ + +Datum +postrr_version(PG_FUNCTION_ARGS) +{ + char version[1024]; + char *result; + + if (PG_NARGS() != 0) + ereport(NOTICE, (errmsg("PostRR_Version() " + "does not accept any arguments"))); + + snprintf(version, sizeof(version), + "PostgreSQL Round-Robin Extension, version %s\n" + "Built against PostgreSQL version "PG_VERSION, + POSTRR_VERSION_STRING); + version[sizeof(version) - 1] = '\0'; + + result = pstrdup(version); + PG_RETURN_CSTRING(result); +} /* postrr_version */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/src/postrr.h.in b/src/postrr.h.in new file mode 100644 index 0000000..3db5045 --- /dev/null +++ b/src/postrr.h.in @@ -0,0 +1,108 @@ +/* + * PostRR - src/postrr.h + * Copyright (C) 2012 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * PostgreSQL Round-Robin Extension + */ + +#ifndef POSTRR_H +#define POSTRR_H 1 + +#include +#include + +#define POSTRR_VERSION_MAJOR @POSTRR_VERSION_MAJOR@ +#define POSTRR_VERSION_MINOR @POSTRR_VERSION_MINOR@ +#define POSTRR_VERSION_PATCH @POSTRR_VERSION_PATCH@ + +#define POSTRR_VERSION_EXTRA "@POSTRR_VERSION_EXTRA@" + +#define POSTRR_VERSION_STRING "@POSTRR_VERSION_STRING@" + +#define POSTRR_VERSION_ENCODE(major, minor, patch) \ + ((major) * 10000 + (minor) * 100 + (patch)) + +#define POSTRR_VERSION POSTRR_VERSION_ENCODE(POSTRR_VERSION_MAJOR, \ + POSTRR_VERSION_MINOR, POSTRR_VERSION_PATCH) + +Datum +postrr_version(PG_FUNCTION_ARGS); + +/* + * RRTimeslice data type + */ + +struct rrtimeslice; +typedef struct rrtimeslice rrtimeslice_t; + +#define PG_GETARG_RRTIMESLICE_P(n) (rrtimeslice_t *)PG_GETARG_POINTER(n) +#define PG_RETURN_RRTIMESLICE_P(p) PG_RETURN_POINTER(p) + +Datum +rrtimeslice_validate(PG_FUNCTION_ARGS); + +/* I/O functions */ +Datum +rrtimeslice_in(PG_FUNCTION_ARGS); +Datum +rrtimeslice_out(PG_FUNCTION_ARGS); +Datum +rrtimeslice_typmodin(PG_FUNCTION_ARGS); +Datum +rrtimeslice_typmodout(PG_FUNCTION_ARGS); + +/* casts */ +Datum +rrtimeslice_to_rrtimeslice(PG_FUNCTION_ARGS); + +/* comparison operators */ +Datum +rrtimeslice_eq(PG_FUNCTION_ARGS); +Datum +rrtimeslice_ne(PG_FUNCTION_ARGS); +Datum +rrtimeslice_lt(PG_FUNCTION_ARGS); +Datum +rrtimeslice_le(PG_FUNCTION_ARGS); +Datum +rrtimeslice_gt(PG_FUNCTION_ARGS); +Datum +rrtimeslice_ge(PG_FUNCTION_ARGS); +Datum +rrtimeslice_cmp(PG_FUNCTION_ARGS); + +/* + * internal (not fmgr-callable) functions + */ + +int +rrtimeslice_cmp_internal(rrtimeslice_t *ts1, rrtimeslice_t *ts2); + +#endif /* ! POSTRR_H */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/src/postrr.sql.in b/src/postrr.sql.in new file mode 100644 index 0000000..bc0d20a --- /dev/null +++ b/src/postrr.sql.in @@ -0,0 +1,206 @@ +-- PostRR - src/postrr.sql +-- Copyright (C) 2012 Sebastian 'tokkee' Harl +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions +-- are met: +-- 1. Redistributions of source code must retain the above copyright +-- notice, this list of conditions and the following disclaimer. +-- 2. Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +-- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +-- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +-- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +-- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +-- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +-- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-- +-- PostRR - PostgreSQL Round-Robin Extension +-- + +-- suppress messages like 'return type foo is only a shell' +SET client_min_messages TO WARNING; + +BEGIN; + +CREATE SCHEMA postrr; + +CREATE SEQUENCE postrr.tsid INCREMENT BY 1 + MINVALUE 1 MAXVALUE 2147483647 START WITH 1 NO CYCLE; + +CREATE TABLE postrr.rrtimeslices ( + tsid integer NOT NULL PRIMARY KEY + DEFAULT nextval('postrr.tsid'::regclass) + CHECK (0 < tsid), + tslen integer NOT NULL, + tsnum integer NOT NULL +); + +CREATE OR REPLACE FUNCTION PostRR_Version() + RETURNS cstring + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'postrr_version' + LANGUAGE 'C' IMMUTABLE; + +CREATE TYPE RRTimeslice; + +CREATE OR REPLACE FUNCTION RRTimeslice_validate(integer) + RETURNS cstring + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_validate' + LANGUAGE 'C' IMMUTABLE STRICT; + +-- this will abort the transaction in case the expected internal length does +-- not match the actual length +SELECT RRTimeslice_validate(16); + +CREATE OR REPLACE FUNCTION RRTimeslice_in(cstring, oid, integer) + RETURNS RRTimeslice + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_in' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_out(RRTimeslice) + RETURNS cstring + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_out' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_typmodin(cstring[]) + RETURNS integer + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_typmodin' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_typmodout(integer) + RETURNS cstring + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_typmodout' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE TYPE RRTimeslice ( + INTERNALLENGTH = 16, + INPUT = RRTimeslice_in, + OUTPUT = RRTimeslice_out, + TYPMOD_IN = RRTimeslice_typmodin, + TYPMOD_OUT = RRTimeslice_typmodout, + ALIGNMENT = double, + STORAGE = plain +); + +CREATE OR REPLACE FUNCTION RRTimeslice(rrtimeslice, integer, boolean) + RETURNS rrtimeslice + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_to_rrtimeslice' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE CAST (rrtimeslice AS rrtimeslice) + WITH FUNCTION RRTimeslice(rrtimeslice, integer, boolean) + AS IMPLICIT; + +CREATE OR REPLACE FUNCTION RRTimeslice_eq(rrtimeslice, rrtimeslice) + RETURNS boolean + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_eq' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_ne(rrtimeslice, rrtimeslice) + RETURNS boolean + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_ne' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_lt(rrtimeslice, rrtimeslice) + RETURNS boolean + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_lt' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_le(rrtimeslice, rrtimeslice) + RETURNS boolean + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_le' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_gt(rrtimeslice, rrtimeslice) + RETURNS boolean + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_gt' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_ge(rrtimeslice, rrtimeslice) + RETURNS boolean + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_ge' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OR REPLACE FUNCTION RRTimeslice_cmp(rrtimeslice, rrtimeslice) + RETURNS integer + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_cmp' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE OPERATOR = ( + LEFTARG = RRTimeslice, + RIGHTARG = RRTimeslice, + PROCEDURE = RRTimeslice_eq, + COMMUTATOR = =, + NEGATOR = <>, + RESTRICT = eqsel +); + +CREATE OPERATOR <> ( + LEFTARG = RRTimeslice, + RIGHTARG = RRTimeslice, + PROCEDURE = RRTimeslice_ne, + COMMUTATOR = <>, + NEGATOR = =, + RESTRICT = neqsel +); + +CREATE OPERATOR < ( + LEFTARG = RRTimeslice, + RIGHTARG = RRTimeslice, + PROCEDURE = RRTimeslice_lt, + COMMUTATOR = >, + NEGATOR = <=, + RESTRICT = scalarltsel +); + +CREATE OPERATOR <= ( + LEFTARG = RRTimeslice, + RIGHTARG = RRTimeslice, + PROCEDURE = RRTimeslice_le, + COMMUTATOR = >=, + NEGATOR = <, + RESTRICT = scalarltsel +); + +CREATE OPERATOR > ( + LEFTARG = RRTimeslice, + RIGHTARG = RRTimeslice, + PROCEDURE = RRTimeslice_gt, + COMMUTATOR = <, + NEGATOR = >=, + RESTRICT = scalargtsel +); + +CREATE OPERATOR >= ( + LEFTARG = RRTimeslice, + RIGHTARG = RRTimeslice, + PROCEDURE = RRTimeslice_ge, + COMMUTATOR = <=, + NEGATOR = >, + RESTRICT = scalargtsel +); + +CREATE OPERATOR CLASS rrtimeslice_ops + DEFAULT FOR TYPE RRTimeslice USING btree AS + OPERATOR 1 < , + OPERATOR 2 <= , + OPERATOR 3 = , + OPERATOR 4 >= , + OPERATOR 5 > , + FUNCTION 1 RRTimeslice_cmp(rrtimeslice, rrtimeslice); + +COMMIT; + +SET client_min_messages TO DEFAULT; + +-- vim: set tw=78 sw=4 ts=4 noexpandtab : + diff --git a/src/postrr_comments.sql b/src/postrr_comments.sql new file mode 100644 index 0000000..58ac536 --- /dev/null +++ b/src/postrr_comments.sql @@ -0,0 +1,33 @@ +-- PostRR - src/postrr_comments.sql +-- Copyright (C) 2012 Sebastian 'tokkee' Harl +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions +-- are met: +-- 1. Redistributions of source code must retain the above copyright +-- notice, this list of conditions and the following disclaimer. +-- 2. Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +-- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +-- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +-- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +-- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +-- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +-- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-- +-- PostRR - PostgreSQL Round-Robin Extension +-- + +COMMENT ON TYPE RRTimeslice IS 'postrr type: A timeslice implementing round-robin features. It is defined by the length of the slice and the number of slices before wrapping around.'; + +-- vim: set tw=78 sw=4 ts=4 noexpandtab : + diff --git a/src/rrtimeslice.c b/src/rrtimeslice.c new file mode 100644 index 0000000..d34da91 --- /dev/null +++ b/src/rrtimeslice.c @@ -0,0 +1,565 @@ +/* + * PostRR - src/rrtimeslice.c + * Copyright (C) 2012 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * A PostgreSQL data-type providing a timeslice implementing round-robin + * features. + */ + +#include "postrr.h" +#include "utils/pg_spi.h" + +#include + +#include +#include + +/* Postgres utilities */ +#include +#include +#include +#include +#include /* DateStyle, IntervalStyle */ + +#ifdef HAVE_INT64_TIMESTAMP +# define TSTAMP_TO_INT64(t) (t) +# define INT64_TO_TSTAMP(i) (i) +#else /* ! HAVE_INT64_TIMESTAMP */ +# define TSTAMP_TO_INT64(t) (int64)((t) * (double)USECS_PER_SEC) +# define INT64_TO_TSTAMP(i) ((double)(i) / (double)USECS_PER_SEC) +#endif + +/* + * data type + */ + +struct rrtimeslice { + Timestamp tstamp; + int32 tsid; + uint32 seq; +}; + +/* + * internal helper functions + */ + +static int32 +rrtimeslice_set_spec(int32 len, int32 num) +{ + int spi_rc; + + char query[256]; + int32 typmod = 0; + + if ((len <= 0) || (num <= 0)) + ereport(ERROR, ( + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("rrtimeslice(%i, %i) " + "length/num may not be less than zero", + len, num) + )); + + if ((spi_rc = SPI_connect()) != SPI_OK_CONNECT) + ereport(ERROR, ( + errmsg("failed to store rrtimeslice spec: " + "could not connect to SPI manager: %s", + SPI_result_code_string(spi_rc)) + )); + + snprintf(query, sizeof(query), + "SELECT tsid FROM postrr.rrtimeslices " + "WHERE tslen = %d AND tsnum = %d " + "LIMIT 1", len, num); + query[sizeof(query) - 1] = '\0'; + + spi_rc = pg_spi_get_int(query, 1, &typmod); + if (spi_rc == PG_SPI_OK) { + SPI_finish(); + return typmod; + } + else if (spi_rc != PG_SPI_ERROR_NO_VALUES) + pg_spi_ereport(ERROR, "store rrtimeslice spec", spi_rc); + + snprintf(query, sizeof(query), + "SELECT nextval('postrr.tsid'::regclass)"); + query[sizeof(query) - 1] = '\0'; + + spi_rc = pg_spi_get_int(query, 1, &typmod); + if ((spi_rc != PG_SPI_OK) || (typmod <= 0)) + pg_spi_ereport(ERROR, "retrieve nextval(postrr.tsid)", spi_rc); + + snprintf(query, sizeof(query), + "INSERT INTO postrr.rrtimeslices(tsid, tslen, tsnum) " + "VALUES (%d, %d, %d)", typmod, len, num); + query[sizeof(query) - 1] = '\0'; + + spi_rc = SPI_exec(query, /* max num rows = */ 1); + if (spi_rc != SPI_OK_INSERT) + ereport(ERROR, ( + errmsg("failed to store rrtimeslice spec: " + "failed to execute query: %s", + SPI_result_code_string(spi_rc)) + )); + + SPI_finish(); + return typmod; +} /* rrtimeslice_set_spec */ + +static int +rrtimeslice_get_spec(int32 typmod, int32 *len, int32 *num) +{ + int spi_rc; + + char query[256]; + + if (typmod <= 0) + return -1; + + if ((spi_rc = SPI_connect()) != SPI_OK_CONNECT) + ereport(ERROR, ( + errmsg("failed to determine rrtimeslice spec: " + "could not connect to SPI manager: %s", + SPI_result_code_string(spi_rc)) + )); + + snprintf(query, sizeof(query), + "SELECT tslen, tsnum FROM postrr.rrtimeslices " + "WHERE tsid = %d", typmod); + query[sizeof(query) - 1] = '\0'; + + spi_rc = pg_spi_get_int(query, 2, len, num); + if (spi_rc != PG_SPI_OK) + pg_spi_ereport(ERROR, "determine rrtimeslice spec", spi_rc); + + SPI_finish(); + return 0; +} /* rrtimeslice_get_spec */ + +static int +rrtimeslice_apply_typmod(rrtimeslice_t *tslice, int32 typmod) +{ + int64 tstamp; + int64 length; + int64 seq; + + int32 len = 0; + int32 num = 0; + + if (rrtimeslice_get_spec(typmod, &len, &num)) + return -1; + + if ((len <= 0) || (num <= 0)) + ereport(ERROR, ( + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("rrtimeslice(%i, %i) " + "length/num may not be less than zero", + len, num) + )); + + tstamp = TSTAMP_TO_INT64(tslice->tstamp); + + length = len * USECS_PER_SEC; + if (tstamp % length != 0) + tstamp = tstamp - (tstamp % length) + length; + seq = tstamp % (length * num) / length; + seq = seq % num; + + tslice->tstamp = INT64_TO_TSTAMP(tstamp); + tslice->tsid = typmod; + tslice->seq = (uint32)seq; + return 0; +} /* rrtimeslice_apply_typmod */ + +/* + * prototypes for PostgreSQL functions + */ + +PG_FUNCTION_INFO_V1(rrtimeslice_validate); + +PG_FUNCTION_INFO_V1(rrtimeslice_in); +PG_FUNCTION_INFO_V1(rrtimeslice_out); +PG_FUNCTION_INFO_V1(rrtimeslice_typmodin); +PG_FUNCTION_INFO_V1(rrtimeslice_typmodout); + +PG_FUNCTION_INFO_V1(rrtimeslice_to_rrtimeslice); + +PG_FUNCTION_INFO_V1(rrtimeslice_eq); +PG_FUNCTION_INFO_V1(rrtimeslice_ne); +PG_FUNCTION_INFO_V1(rrtimeslice_lt); +PG_FUNCTION_INFO_V1(rrtimeslice_gt); +PG_FUNCTION_INFO_V1(rrtimeslice_le); +PG_FUNCTION_INFO_V1(rrtimeslice_ge); +PG_FUNCTION_INFO_V1(rrtimeslice_cmp); + +/* + * public API + */ + +Datum +rrtimeslice_validate(PG_FUNCTION_ARGS) +{ + char type_info[1024]; + char *result; + size_t req_len; + size_t len; + + if (PG_NARGS() != 1) + ereport(ERROR, ( + errmsg("rrtimeslice_validate() expect one argument"), + errhint("Usage rrtimeslice_validate(expected_size)") + )); + + req_len = (size_t)PG_GETARG_UINT32(0); + len = sizeof(rrtimeslice_t); + + if (req_len != len) + ereport(ERROR, ( + errmsg("length of the rrtimeslice type " + "does not match the expected length"), + errhint("Please report a bug against PostRR") + )); + + snprintf(type_info, sizeof(type_info), + "rrtimeslice validated successfully; type length = %zu", len); + type_info[sizeof(type_info) - 1] = '\0'; + + result = pstrdup(type_info); + PG_RETURN_CSTRING(result); +} /* rrtimeslice_validate */ + +Datum +rrtimeslice_in(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *tslice; + + Timestamp tstamp = 0; + int32 typmod; + + struct pg_tm tm; + fsec_t fsec = 0; + int tz = 0; + + char *time_str; + int pg_dt_err; + char buf[MAXDATELEN + MAXDATEFIELDS]; + char *field[MAXDATEFIELDS]; + int ftype[MAXDATEFIELDS]; + int num_fields = 0; + int dtype = 0; + + if (PG_NARGS() != 3) + ereport(ERROR, ( + errmsg("rrtimeslice_in() expects three arguments"), + errhint("Usage: rrtimeslice_in(col_name, oid, typmod)") + )); + + tslice = (rrtimeslice_t *)palloc0(sizeof(*tslice)); + time_str = PG_GETARG_CSTRING(0); + typmod = PG_GETARG_INT32(2); + + pg_dt_err = ParseDateTime(time_str, buf, sizeof(buf), + field, ftype, MAXDATEFIELDS, &num_fields); + + if (! pg_dt_err) + pg_dt_err = DecodeDateTime(field, ftype, num_fields, + &dtype, &tm, &fsec, &tz); + if (pg_dt_err) + DateTimeParseError(pg_dt_err, time_str, "rrtimeslice"); + + switch (dtype) { + case DTK_DATE: + if (tm2timestamp(&tm, fsec, NULL, &tstamp)) + ereport(ERROR, ( + errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("timestamp out of range: %s", time_str) + )); + break; + + case DTK_EPOCH: + tstamp = SetEpochTimestamp(); + break; + + default: + ereport(ERROR, ( + errmsg("unexpected dtype %d while " + "parsing rrtimeslice: %s", dtype, time_str) + )); + } + + tslice->tstamp = tstamp; + + /* most likely, this won't happen … coerce_type + * (src/backend/parser/parse_coerce.c) does not pass that information to + * the input function but rather lets a length conversion cast do that + * work */ + if (typmod > 0) + rrtimeslice_apply_typmod(tslice, typmod); + + PG_RETURN_RRTIMESLICE_P(tslice); +} /* rrtimeslice_in */ + +Datum +rrtimeslice_out(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *tslice; + + Interval interval; + + struct pg_tm tm; + fsec_t fsec = 0; + char *tz = NULL; + + char ts_str[MAXDATELEN + 1]; + char buf_ts[MAXDATELEN + 1]; + char buf_l[MAXDATELEN + 1]; + char *result; + + int32 len = 0; + int32 num = 0; + + if (PG_NARGS() != 1) + ereport(ERROR, ( + errmsg("rrtimeslice_out() expects one argument"), + errhint("Usage: rrtimeslice_out(rrtimeslice)") + )); + + tslice = PG_GETARG_RRTIMESLICE_P(0); + + if (TIMESTAMP_NOT_FINITE(tslice->tstamp) + || (timestamp2tm(tslice->tstamp, NULL, &tm, &fsec, NULL, NULL) != 0)) + ereport(ERROR, ( + errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("invalid (non-finite) timestamp") + )); + else + EncodeDateTime(&tm, fsec, NULL, &tz, DateStyle, buf_ts); + + if (! rrtimeslice_get_spec(tslice->tsid, &len, &num)) { + memset(&interval, 0, sizeof(interval)); + interval.time = len * USECS_PER_SEC; + fsec = 0; + + if (interval2tm(interval, &tm, &fsec)) + ereport(ERROR, ( + errmsg("could not convert interval to tm") + )); + } + else { + strncpy(buf_l, "ERR", sizeof(buf_l)); + buf_l[sizeof(buf_l) - 1] = '\0'; + } + + EncodeInterval(&tm, fsec, IntervalStyle, buf_l); + + snprintf(ts_str, sizeof(ts_str), "%s -%s (#%i)", + buf_ts, buf_l, tslice->seq); + + result = pstrdup(ts_str); + PG_RETURN_CSTRING(result); +} /* rrtimeslice_out */ + +Datum +rrtimeslice_typmodin(PG_FUNCTION_ARGS) +{ + ArrayType *tm_array; + + int32 *spec; + int spec_elems = 0; + int32 typmod; + + if (PG_NARGS() != 1) + ereport(ERROR, ( + errmsg("rrtimeslice_typmodin() expects one argument"), + errhint("Usage: rrtimeslice_typmodin(array)") + )); + + tm_array = PG_GETARG_ARRAYTYPE_P(0); + + spec = ArrayGetIntegerTypmods(tm_array, &spec_elems); + if (spec_elems != 2) + ereport(ERROR, ( + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid rrtimeslice type modifier"), + errhint("Usage: rrtimeslice(, )") + )); + + typmod = rrtimeslice_set_spec(spec[0], spec[1]); + PG_RETURN_INT32(typmod); +} /* rrtimeslice_typmodin */ + +Datum +rrtimeslice_typmodout(PG_FUNCTION_ARGS) +{ + int32 typmod; + char tm_str[1024]; + char *result; + + int32 len = 0; + int32 num = 0; + + if (PG_NARGS() != 1) + ereport(ERROR, ( + errmsg("rrtimeslice_typmodout() expects one argument"), + errhint("Usage: rrtimeslice_typmodout(typmod)") + )); + + typmod = PG_GETARG_INT32(0); + if (rrtimeslice_get_spec(typmod, &len, &num)) + tm_str[0] = '\0'; + else if ((len <= 0) || (num <= 0)) + snprintf(tm_str, sizeof(tm_str), "(#ERR, #ERR)"); + else + snprintf(tm_str, sizeof(tm_str), "(%d, %d)", len, num); + + result = pstrdup(tm_str); + PG_RETURN_CSTRING(result); +} /* rrtimeslice_typmodout */ + +Datum +rrtimeslice_to_rrtimeslice(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *tslice; + int32 typmod; + + if (PG_NARGS() != 3) + ereport(ERROR, ( + errmsg("rrtimeslice_to_rrtimeslice() " + "expects three arguments"), + errhint("Usage: rrtimeslice_to_rrtimeslice" + "(rrtimeslice, typmod, is_explicit)") + )); + + tslice = PG_GETARG_RRTIMESLICE_P(0); + typmod = PG_GETARG_INT32(1); + + if (typmod > 0) { + if ((! tslice->tsid) && (! tslice->seq)) + rrtimeslice_apply_typmod(tslice, typmod); + else + ereport(ERROR, ( + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid cast: cannot cast rrtimeslices " + "with different typmod (yet)") + )); + } + + PG_RETURN_RRTIMESLICE_P(tslice); +} /* rrtimeslice_to_rrtimeslice */ + +int +rrtimeslice_cmp_internal(rrtimeslice_t *ts1, rrtimeslice_t *ts2) +{ + if ((! ts1) && (! ts2)) + return 0; + else if (! ts1) + return -1; + else if (! ts2) + return 1; + + if (ts1->tsid && (! ts2->tsid)) + rrtimeslice_apply_typmod(ts2, ts1->tsid); + else if ((! ts1->tsid) && ts2->tsid) + rrtimeslice_apply_typmod(ts1, ts2->tsid); + + if (ts1->tsid != ts2->tsid) /* XXX: compare len/num */ + ereport(ERROR, ( + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid comparison: cannot compare " + "rrtimeslices with different typmods (yet)") + )); + + if (ts1->seq == ts2->seq) + return 0; + + return timestamp_cmp_internal(ts1->tstamp, ts2->tstamp); +} /* rrtimeslice_cmp_internal */ + +Datum +rrtimeslice_eq(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *ts1 = PG_GETARG_RRTIMESLICE_P(0); + rrtimeslice_t *ts2 = PG_GETARG_RRTIMESLICE_P(1); + + PG_RETURN_BOOL(rrtimeslice_cmp_internal(ts1, ts2) == 0); +} /* rrtimeslice_eq */ + +Datum +rrtimeslice_ne(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *ts1 = PG_GETARG_RRTIMESLICE_P(0); + rrtimeslice_t *ts2 = PG_GETARG_RRTIMESLICE_P(1); + + PG_RETURN_BOOL(rrtimeslice_cmp_internal(ts1, ts2) != 0); +} /* rrtimeslice_ne */ + +Datum +rrtimeslice_lt(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *ts1 = PG_GETARG_RRTIMESLICE_P(0); + rrtimeslice_t *ts2 = PG_GETARG_RRTIMESLICE_P(1); + + PG_RETURN_BOOL(rrtimeslice_cmp_internal(ts1, ts2) < 0); +} /* rrtimeslice_lt */ + +Datum +rrtimeslice_le(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *ts1 = PG_GETARG_RRTIMESLICE_P(0); + rrtimeslice_t *ts2 = PG_GETARG_RRTIMESLICE_P(1); + + PG_RETURN_BOOL(rrtimeslice_cmp_internal(ts1, ts2) <= 0); +} /* rrtimeslice_le */ + +Datum +rrtimeslice_gt(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *ts1 = PG_GETARG_RRTIMESLICE_P(0); + rrtimeslice_t *ts2 = PG_GETARG_RRTIMESLICE_P(1); + + PG_RETURN_BOOL(rrtimeslice_cmp_internal(ts1, ts2) > 0); +} /* rrtimeslice_gt */ + +Datum +rrtimeslice_ge(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *ts1 = PG_GETARG_RRTIMESLICE_P(0); + rrtimeslice_t *ts2 = PG_GETARG_RRTIMESLICE_P(1); + + PG_RETURN_BOOL(rrtimeslice_cmp_internal(ts1, ts2) >= 0); +} /* rrtimeslice_ge */ + +Datum +rrtimeslice_cmp(PG_FUNCTION_ARGS) +{ + rrtimeslice_t *ts1 = PG_GETARG_RRTIMESLICE_P(0); + rrtimeslice_t *ts2 = PG_GETARG_RRTIMESLICE_P(1); + + PG_RETURN_INT32(rrtimeslice_cmp_internal(ts1, ts2)); +} /* rrtimeslice_ge */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/src/uninstall_postrr.sql b/src/uninstall_postrr.sql new file mode 100644 index 0000000..473394f --- /dev/null +++ b/src/uninstall_postrr.sql @@ -0,0 +1,50 @@ +-- PostRR - src/uninstall_postrr.sql +-- Copyright (C) 2012 Sebastian 'tokkee' Harl +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions +-- are met: +-- 1. Redistributions of source code must retain the above copyright +-- notice, this list of conditions and the following disclaimer. +-- 2. Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +-- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +-- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +-- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +-- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +-- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +-- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +-- +-- PostRR - PostgreSQL Round-Robin Extension +-- + +SET client_min_messages TO WARNING; + +DROP FUNCTION IF EXISTS RRTimeslice_validate(integer); + +DROP CAST IF EXISTS (rrtimeslice AS rrtimeslice); +DROP FUNCTION IF EXISTS RRTimeslice(rrtimeslice, integer, boolean); +DROP TYPE RRTimeslice CASCADE; +DROP FUNCTION IF EXISTS RRTimeslice_typmodin(cstring[]); +DROP FUNCTION IF EXISTS RRTimeslice_typmodout(integer); + +DROP FUNCTION IF EXISTS PostRR_Version(); + +DROP TABLE postrr.rrtimeslices; +DROP SEQUENCE postrr.tsid; + +DROP SCHEMA postrr; + +SET client_min_messages TO DEFAULT; + +-- vim: set tw=78 sw=4 ts=4 noexpandtab : + diff --git a/src/utils/pg_spi.c b/src/utils/pg_spi.c new file mode 100644 index 0000000..82fc470 --- /dev/null +++ b/src/utils/pg_spi.c @@ -0,0 +1,103 @@ +/* + * PostRR - src/utils/pg_spi.c + * Copyright (C) 2012 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Helpers for using the PostgreSQL Server Programming Interface (SPI). + */ + +#include "utils/pg_spi.h" + +#include +#include + +#include + +/* + * public API + */ + +int +pg_spi_get_int(const char *query, int num_vals, ...) +{ + int spi_rc; + int i; + + va_list ap; + + spi_rc = SPI_exec(query, /* max num rows = */ 1); + if (spi_rc != SPI_OK_SELECT) { + errmsg("failed to execute query: %s", + SPI_result_code_string(spi_rc)); + return PG_SPI_MAKE_RC(ERROR_EXEC_QUERY, spi_rc); + } + + if (SPI_processed <= 0) + return PG_SPI_MAKE_RC(ERROR_NO_VALUES, 0); + + va_start(ap, num_vals); + /* column count starts at 1 */ + for (i = 1; i <= num_vals; ++i) { + char *tmp; + int32 *valp; + + valp = va_arg(ap, int32 *); + + tmp = SPI_getvalue(SPI_tuptable->vals[0], + SPI_tuptable->tupdesc, /* col = */ i); + + *valp = atoi(tmp); + } + va_end(ap); + return PG_SPI_OK; +} /* pg_spi_get_int */ + +int +pg_spi_ereport(int elevel, const char *context, int rc) +{ + int status = PG_SPI_GET_STATUS(rc); + int spi_status = PG_SPI_GET_SPI_STATUS(rc); + + switch (status) { + case PG_SPI_OK: + return 0; + case PG_SPI_ERROR_EXEC_QUERY: + ereport(elevel, ( + errmsg("failed to %s: failed to execute query: %s", + context, SPI_result_code_string(spi_status)) + )); + case PG_SPI_ERROR_NO_VALUES: + ereport(elevel, ( + errmsg("failed to %s: " + "query did not return any values", + context) + )); + } + return 0; +} /* pg_spi_ereport */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/src/utils/pg_spi.h b/src/utils/pg_spi.h new file mode 100644 index 0000000..c997137 --- /dev/null +++ b/src/utils/pg_spi.h @@ -0,0 +1,50 @@ +/* + * PostRR - src/utils/pg_spi.c + * Copyright (C) 2012 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Helpers for using the PostgreSQL Server Programming Interface (SPI). + */ + +enum { + PG_SPI_OK = 0, + PG_SPI_ERROR_EXEC_QUERY, + PG_SPI_ERROR_NO_VALUES, +}; + +#define PG_SPI_GET_STATUS(rc) ((rc) & 0xF) +#define PG_SPI_GET_SPI_STATUS(rc) ((rc) >> 4) +#define PG_SPI_MAKE_RC(status, spi_status) \ + (((spi_status) << 4) | ((PG_SPI_ ## status) & 0xF)) + +int +pg_spi_get_int(const char *query, int num_vals, ...); + +int +pg_spi_ereport(int elevel, const char *context, int rc); + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/version-gen.sh b/version-gen.sh new file mode 100755 index 0000000..c49372f --- /dev/null +++ b/version-gen.sh @@ -0,0 +1,44 @@ +#! /bin/sh + +DEFAULT_VERSION="0.0.0.git" + +VERSION="$( git describe --tags 2> /dev/null \ + | sed -e 's/postrr-//' || true )" + +if test -z "$VERSION"; then + VERSION="$DEFAULT_VERSION" +else + git update-index -q --refresh || true + if test -n "$( git diff-index --name-only HEAD || true )"; then + VERSION="$VERSION-dirty" + fi +fi + +VERSION="$( echo "$VERSION" | sed -e 's/-/./g' )" +if test "x`uname -s`" = "xAIX" || test "x`uname -s`" = "xSunOS" ; then + echo "$VERSION\c" +else + echo -n "$VERSION" +fi + +OLD_VERSION="" +if test -e version; then + OLD_VERSION=$( sed -ne 's/^VERSION="\(.*\)"/\1/p' version ) +fi + +if test "$OLD_VERSION" != "$VERSION"; then + VERSION_MAJOR=$( echo $VERSION | cut -d'.' -f1 ) + VERSION_MINOR=$( echo $VERSION | cut -d'.' -f2 ) + VERSION_PATCH=$( echo $VERSION | cut -d'.' -f3 ) + VERSION_EXTRA="\"$( echo $VERSION | cut -d'.' -f4- )\"" + test -z "$VERSION_EXTRA" || VERSION_EXTRA=".$VERSION_EXTRA" + ( + echo "VERSION=\"$VERSION\"" + echo "VERSION_MAJOR=$VERSION_MAJOR" + echo "VERSION_MINOR=$VERSION_MINOR" + echo "VERSION_PATCH=$VERSION_PATCH" + echo "VERSION_EXTRA=\"$VERSION_EXTRA\"" + echo "VERSION_STRING=\"$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH\"" + ) > version +fi +