commit 6e28e474982a3da5095dab2bfc0d9a3241feda05 Author: David Fetter Date: Thu Jul 21 23:34:21 2016 -0700 require_where: a contrib hook This adds a process utility hook which makes simple UPDATE and DELETE statements require a WHERE clause when loaded. It is not intended to provide a general capability. Instead, its job is to prevent common human errors made by people who only rarely use SQL. The hook is small enough to be usable as part of a short lesson on hooks. diff --git a/contrib/Makefile b/contrib/Makefile index 25263c0..4bd456f 100644 --- a/contrib/Makefile +++ b/contrib/Makefile @@ -40,6 +40,7 @@ SUBDIRS = \ pgstattuple \ pg_visibility \ postgres_fdw \ + require_where \ seg \ spi \ tablefunc \ diff --git a/contrib/require_where/Makefile b/contrib/require_where/Makefile new file mode 100644 index 0000000..933eb00 --- /dev/null +++ b/contrib/require_where/Makefile @@ -0,0 +1,19 @@ +# contrib/require_where/Makefile + +MODULE_big = require_where +OBJS = require_where.o + +PGFILEDESC = 'require_where - require simple DELETEs and UPDATEs to have a WHERE clause' + +REGRESS = require_where + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS = $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = contrib/require_where +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_builddir)/contrib/contrib-global.mk +endif diff --git a/contrib/require_where/expected/require_where.out b/contrib/require_where/expected/require_where.out new file mode 100644 index 0000000..a36dd1f --- /dev/null +++ b/contrib/require_where/expected/require_where.out @@ -0,0 +1,16 @@ +-- +-- Test require_where +-- +\set echo all +CREATE TABLE test_require_where(t TEXT); +UPDATE test_require_where SET t=t; -- succeeds +DELETE FROM test_require_where; -- succeeds +LOAD 'require_where'; +UPDATE test_require_where SET t=t; -- fails +ERROR: UPDATE requires a WHERE clause when require_where.delete is set to on +HINT: To update all rows, use "WHERE true" or similar. +UPDATE test_require_where SET t=t WHERE true; -- succeeds +DELETE FROM test_require_where; -- fails +ERROR: DELETE requires a WHERE clause when require_where.delete is set to on +HINT: To delete all rows, use "WHERE true" or similar. +DELETE FROM test_require_where WHERE true; -- succeeds diff --git a/contrib/require_where/require_where.c b/contrib/require_where/require_where.c new file mode 100644 index 0000000..09f2578 --- /dev/null +++ b/contrib/require_where/require_where.c @@ -0,0 +1,73 @@ +/* + * -------------------------------------------------------------------------- + * + * require_where.c + * + * Copyright (C) 2017, PostgreSQL Global Development Group + * + * IDENTIFICATION + * contrib/require_where/require_where.c + * + * -------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "fmgr.h" + +#include "parser/analyze.h" + +#include "utils/elog.h" +#include "utils/guc.h" + +PG_MODULE_MAGIC; + +void _PG_init(void); +void _PG_fini(void); + +static post_parse_analyze_hook_type original_post_parse_analyze_hook = NULL; + +/* + * This module makes simple UPDATE and DELETE statements require a WHERE clause + * and complains when this is not present. + */ +static void +require_where_check(ParseState *pstate, Query *query) +{ + + if (query->commandType == CMD_DELETE) + { + /* Make sure there's something to look at. */ + Assert(query->jointree != NULL); + if (query->jointree->quals == NULL) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("DELETE requires a WHERE clause when require_where.delete is set to on"), + errhint("To delete all rows, use \"WHERE true\" or similar."))); + } + + if (query->commandType == CMD_UPDATE) + { + Assert(query->jointree != NULL); + if (query->jointree->quals == NULL) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("UPDATE requires a WHERE clause when require_where.delete is set to on"), + errhint("To update all rows, use \"WHERE true\" or similar."))); + } + + if (original_post_parse_analyze_hook != NULL) + (*original_post_parse_analyze_hook) (pstate, query); +} + +void +_PG_init(void) +{ + original_post_parse_analyze_hook = post_parse_analyze_hook; + post_parse_analyze_hook = require_where_check; +} + +void +_PG_fini(void) +{ + post_parse_analyze_hook = original_post_parse_analyze_hook; +} diff --git a/contrib/require_where/sql/require_where.sql b/contrib/require_where/sql/require_where.sql new file mode 100644 index 0000000..db8e078 --- /dev/null +++ b/contrib/require_where/sql/require_where.sql @@ -0,0 +1,21 @@ +-- +-- Test require_where +-- + +\set echo all + +CREATE TABLE test_require_where(t TEXT); + +UPDATE test_require_where SET t=t; -- succeeds + +DELETE FROM test_require_where; -- succeeds + +LOAD 'require_where'; + +UPDATE test_require_where SET t=t; -- fails + +UPDATE test_require_where SET t=t WHERE true; -- succeeds + +DELETE FROM test_require_where; -- fails + +DELETE FROM test_require_where WHERE true; -- succeeds diff --git a/doc/src/sgml/contrib.sgml b/doc/src/sgml/contrib.sgml index c8708ec..48ca717 100644 --- a/doc/src/sgml/contrib.sgml +++ b/doc/src/sgml/contrib.sgml @@ -135,6 +135,7 @@ CREATE EXTENSION module_name FROM unpackaged; &pgtrgm; &pgvisibility; &postgres-fdw; + &require-where; &seg; &sepgsql; &contrib-spi; diff --git a/doc/src/sgml/filelist.sgml b/doc/src/sgml/filelist.sgml index 69649a7..4552273 100644 --- a/doc/src/sgml/filelist.sgml +++ b/doc/src/sgml/filelist.sgml @@ -141,6 +141,7 @@ + diff --git a/doc/src/sgml/require_where.sgml b/doc/src/sgml/require_where.sgml new file mode 100644 index 0000000..290ce7b --- /dev/null +++ b/doc/src/sgml/require_where.sgml @@ -0,0 +1,54 @@ + + + + require_where + + + require_where + + + + This module makes it a requirement that WHERE on both DELETE or UPDATE be present. + + + + To use this module, you need to include require_where in + the either the parameter, or + LOAD the library directly. + + + + Here is an example showing how to set up a database cluster with + require_where. + +$ psql -U postgres +# SHOW shared_preload_libraries; /* Make sure not to clobber something by accident */ + +If you found something, +# ALTER SYSTEM SET shared_preload_libraries='the,stuff,you,found,require_where'; + +Otherwise, +# ALTER SYSTEM SET shared_preload_libraries='require_where'; + +Then restart PostgreSQL + + + + + Here is an example using LOAD: + +$ psql -U postgres +# LOAD '$libdir/require_where'; + + + + + Authors + + + David Fetter david@fetter.org, + Robert Haas robertmhaas@gmail.com and + Andrew Gierth andrew@tao11.riddles.org.uk. + + +