prioritize-1.0.4/0000755000175000017500000000000011747031564012040 5ustar cybcybprioritize-1.0.4/prioritize.sql.in0000644000175000017500000000045411747031564015371 0ustar cybcybCREATE OR REPLACE FUNCTION set_backend_priority(integer, integer) RETURNS boolean AS '$libdir/prioritize.so', 'set_backend_priority' LANGUAGE C VOLATILE; CREATE OR REPLACE FUNCTION get_backend_priority(integer) RETURNS integer AS '$libdir/prioritize.so', 'get_backend_priority' LANGUAGE C VOLATILE; prioritize-1.0.4/META.json0000644000175000017500000000171111747031564013461 0ustar cybcyb{ "name":"prioritize", "abstract":"get/set priorities of PostgreSQL backends", "description":"This module provides methods to get and set the priorities of PostgreSQL backends.", "version":"1.0.4", "maintainer":"Josh Kupershmidt ", "license":"postgresql", "provides":{ "prioritize":{ "abstract":"get and set the priorities of backends", "version":"1.0.4", "file":"prioritize.sql", "docfile":"README.md" } }, "resources":{ "bugtracker":{ "web":"http://github.com/schmiddy/pg_prioritize/issues/" }, "repository":{ "url":"git://github.com/schmiddy/pg_prioritize.git", "web":"http://github.com/schmiddy/pg_prioritize", "type":"git" } }, "meta-spec":{ "version":"1.0.0", "url":"http://pgxn.org/meta/spec.txt" }, "tags":[ "priority", "prioritize", "nice", "renice" ] } prioritize-1.0.4/Makefile0000644000175000017500000000110711747031564013477 0ustar cybcybMODULES = prioritize EXTENSION = $(MODULES) EXTVERSION = 1.0 EXTSQL = $(MODULES)--$(EXTVERSION).sql DATA_built = $(MODULES).sql DATA = uninstall_$(MODULES).sql DOCS = README.md REGRESS = $(MODULES) SQL_IN = $(MODULES).sql.in EXTRA_CLEAN = sql/$(MODULES).sql expected/$(MODULES).out USE_EXTENSION = $(shell pg_config --version | grep -qE " 8\.|9\.0" && echo no || echo yes) ifeq ($(USE_EXTENSION),yes) all: $(EXTSQL) $(EXTSQL): $(EXTENSION).sql cp $< $@ DATA = $(EXTSQL) EXTRA_CLEAN += $(EXTSQL) endif PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) prioritize-1.0.4/prioritize.control0000644000175000017500000000024511747031564015643 0ustar cybcyb# prioritize extension comment = 'get and set the priority of PostgreSQL backends' default_version = '1.0' module_pathname = '$libdir/prioritize' relocatable = true prioritize-1.0.4/prioritize.c0000644000175000017500000000736611747031564014420 0ustar cybcyb#include "postgres.h" #include #include #include #include "fmgr.h" #include "miscadmin.h" #include "storage/proc.h" #include "storage/procarray.h" PG_MODULE_MAGIC; PG_FUNCTION_INFO_V1(set_backend_priority); PG_FUNCTION_INFO_V1(get_backend_priority); extern Datum get_backend_priority(PG_FUNCTION_ARGS); extern Datum set_backend_priority(PG_FUNCTION_ARGS); Datum get_backend_priority(PG_FUNCTION_ARGS) { int pid = PG_GETARG_INT32(0); int priority; int save_errno = errno; if (!IsBackendPid(pid)) { ereport(WARNING, (errmsg("PID %d is not a PostgreSQL server process", pid))); PG_RETURN_NULL(); } errno = 0; priority = getpriority(PRIO_PROCESS, pid); if (priority == -1) { /* We need to check errno to determine whether an error has occurred or if the priority of the process is just '-1'. */ if (errno == ESRCH || errno == EINVAL) { errno = save_errno; ereport(ERROR, (errcode(ERRCODE_IO_ERROR), (errmsg("getpriority() could not find the requested backend")))); } } errno = save_errno; PG_RETURN_INT32(priority); } /* Set the 'nice' priority of the given backend. The priority passed in should * typically be between 1 and 20, inclusive, since priorities may only * be adjusted upwards by non-root users. If the backend had been manually * set (by a root user) to a negative nice value, it may be possible to pass * in a greater-but-still-negative value as the new priority. */ Datum set_backend_priority(PG_FUNCTION_ARGS) { PGPROC *proc; int pid = PG_GETARG_INT32(0); int prio = PG_GETARG_INT32(1); int save_errno = errno; bool success = true; if (pid == MyProcPid) { /* Quick check: if we are setting the priority of our own backend, * skip permissions checks and chekcs of whether 'pid' is a valid * backend. */ } else if (!superuser()) { /* * Since the user is not superuser, check for matching roles. Trust * that BackendPidGetProc will return NULL if the pid isn't valid, * even though the check for whether it's a backend process is below. * The IsBackendPid check can't be relied on as definitive even if it * was first. The process might end between successive checks * regardless of their order. There's no way to acquire a lock on an * arbitrary process to prevent that. */ proc = BackendPidGetProc(pid); if (proc == NULL) { /* * This is just a warning so a loop-through-resultset will not * abort if one backend terminated on its own during the run */ ereport(WARNING, (errmsg("PID %d is not a PostgreSQL server process", pid))); success = false; } else if (proc->roleId != GetUserId()) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("must be superuser to nice arbitrary backends")))); /* Otherwise, the backend PID is valid and our user is allowed * to set its priority. */ } else if (!IsBackendPid(pid)) { ereport(WARNING, (errmsg("PID %d is not a PostgreSQL server process", pid))); success = false; } if (success) { errno = 0; if (setpriority(PRIO_PROCESS, pid, prio) == 0) { ereport(NOTICE, (errmsg("Set priority of backend %d to %d", pid, prio))); } else { if (errno == ESRCH || errno == EINVAL) { errno = save_errno; ereport(ERROR, (errcode(ERRCODE_IO_ERROR), (errmsg("setpriority(): could not find the requested backend")))); } else { /* Assume EPERM or EACCES */ ereport(WARNING, (errmsg("setpriority(): permission denied"))); success = false; } } } errno = save_errno; PG_RETURN_BOOL(success); } prioritize-1.0.4/README.md0000644000175000017500000000653211747031564013325 0ustar cybcybprioritize ================= This module implements an interface to getpriority() and setpriority() for PostgreSQL backends, callable from SQL functions. Essentially, this module allows users to `renice' their backends. Synopsis -------- SELECT get_backend_priority(pg_backend_pid()); SELECT set_backend_priority(pg_backend_pid(), 10); SELECT set_backend_priority(pid, get_backend_priority(pid) + 5) FROM pg_stat_activity WHERE usename = CURRENT_USER; Description ----------- This module allows users to query and set the priority of PostgreSQL backend processes. The priority values are used by getpriority() and setpriority(), which you may be familiar with from the `nice' or `renice' programs. 1) get_backend_priority(process_id integer) ---------------------------------------------- Returns the current priority of the selected backend. Any user may query the priority of any other user's backend. 2) set_backend_priority(process_id integer, priority_value integer) ---------------------------------------------- Set the priority of the given backend, specified by its process ID. Superusers are allowed to set the priority of any backends. Unprivileged users are only allowed to set the priority of backends with the same role. Note, it is only possible to adjust the priority of a process upwards (meaning, the process will run at a lower priority). This restriction arises from [SUSv1](http://www.opengroup.org/sud/sud1/xsh/getpriority.htm), which declares: > Only a process with appropriate privileges can raise its own priority > (that is, assign a lower numerical priority value). and UNIX-like platforms take this to mandate that only root users may adjust priority values downwards. Your PostgreSQL processes will (hopefully!) not be running under root, hence priority values may only be adjusted upwards. Installation ------- Installation should be a simple: $ make install $ CREATE EXTENSION prioritize; Support ------- This library is stored in an open [GitHub repository](https://github.com/schmiddy/pg_prioritize). Feel free to fork and contribute! Please file bug reports via [GitHub Issues](http://github.com/schmiddy/pg_prioritize/issues/). Author ------ [Josh Kupershmidt](mailto:schmiddy@gmail.com) Copyright and License --------------------- Copyright (c) Josh Kupershmidt This module is free software; you can redistribute it and/or modify it under the [PostgreSQL License](http://www.opensource.org/licenses/postgresql). Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies. In no event shall Josh Kupershmidt be liable to any party for direct, indirect, special, incidental, or consequential damages, including lost profits, arising out of the use of this software and its documentation, even if Josh Kupershmidt has been advised of the possibility of such damage. Josh Kupershmidt specifically disclaims any warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The software provided hereunder is on an "as is" basis, and Josh Kupershmidt has no obligations to provide maintenance, support, updates, enhancements, or modifications. prioritize-1.0.4/uninstall_prioritize.sql0000644000175000017500000000014411747031564017051 0ustar cybcyb DROP FUNCTION set_backend_priority(integer, integer); DROP FUNCTION get_backend_priority(integer);