PostgreSQL Audit Extension

Started by David Steeleabout 10 years ago36 messages
#1David Steele
david@pgmasters.net
1 attachment(s)

The attached patch implements audit logging for PostgreSQL as an
extension. I believe I have addressed the concerns that were raised at
the end of the 9.5 development cycle.

There are some caveats:

1) This patch depends on the ereport-v1 patch that has also been
submitted to the 2016-01 CF. The ereport patch allows audit log
messages to be suppressed on the client which has been a common ask. If
the ereport patch is not accepted then the same functionality could be
implemented with elog callbacks or removed.

2) The included documentation is currently in Markdown format. It was
converted for the community project but I'll convert it back to SGML.

3) 'make installcheck-world' isn't going to work on the build farm
because of the shared_preload_libraries requirement. 'install-check' on
the extension works fine if pgaudit is added to shared_preload_libraries
before running - I'll be happy to help look at this issue.

I would also like to thank Abhijit Menon-sen and Ian Barwick of
2ndQuadrant who authored the version of pgaudit that this work has been
based on.

--
-David
david@pgmasters.net

Attachments:

pgaudit-v1.patchtext/plain; charset=UTF-8; name=pgaudit-v1.patch; x-mac-creator=0; x-mac-type=0Download
diff --git a/contrib/Makefile b/contrib/Makefile
index bd251f6..0046610 100644
--- a/contrib/Makefile
+++ b/contrib/Makefile
@@ -34,6 +34,7 @@ SUBDIRS = \
 		pg_standby	\
 		pg_stat_statements \
 		pg_trgm		\
+		pgaudit		\
 		pgcrypto	\
 		pgrowlocks	\
 		pgstattuple	\
diff --git a/contrib/pgaudit/.gitignore b/contrib/pgaudit/.gitignore
new file mode 100644
index 0000000..e8d2612
--- /dev/null
+++ b/contrib/pgaudit/.gitignore
@@ -0,0 +1,8 @@
+log/
+results/
+tmp_check/
+regression.diffs
+regression.out
+*.o
+*.so
+.vagrant
diff --git a/contrib/pgaudit/LICENSE b/contrib/pgaudit/LICENSE
new file mode 100644
index 0000000..998f814
--- /dev/null
+++ b/contrib/pgaudit/LICENSE
@@ -0,0 +1,4 @@
+This code is released under the PostgreSQL licence, as given at
+http://www.postgresql.org/about/licence/
+
+Copyright is novated to the PostgreSQL Global Development Group.
diff --git a/contrib/pgaudit/Makefile b/contrib/pgaudit/Makefile
new file mode 100644
index 0000000..b3a488b
--- /dev/null
+++ b/contrib/pgaudit/Makefile
@@ -0,0 +1,22 @@
+# contrib/pg_audit/Makefile
+
+MODULE_big = pgaudit
+OBJS = pgaudit.o $(WIN32RES)
+
+EXTENSION = pgaudit
+DATA = pgaudit--1.0.sql
+PGFILEDESC = "pgAudit - An audit logging extension for PostgreSQL"
+
+REGRESS = pgaudit
+REGRESS_OPTS = --temp-config=$(top_srcdir)/contrib/pgaudit/pgaudit.conf
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/pgaudit
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/pgaudit/README.md b/contrib/pgaudit/README.md
new file mode 100644
index 0000000..3d14d7d
--- /dev/null
+++ b/contrib/pgaudit/README.md
@@ -0,0 +1,331 @@
+# PostgreSQL Audit Extension
+
+The PostgreSQL Audit extension (`pgaudit`) provides detailed session and/or object audit logging via the standard PostgreSQL logging facility.
+
+The goal of the PostgreSQL Audit extension (`pgaudit`) is to provide PostgreSQL users with capability to produce audit logs often required to comply with government, financial, or ISO certifications.
+
+An audit is an official inspection of an individual's or organization's accounts, typically by an independent body.  The information gathered by the PostgreSQL Audit extension (`pgaudit`) is properly called an audit trail or audit log.  The term audit log is used in this documentation.
+
+## Why PostgreSQL Audit Extension?
+
+Basic statement logging can be provided by the standard logging facility
+`log_statement = all`.  This is acceptable for monitoring and other usages but does not provide the level of detail generally required for an audit.  It is not enough to have a list of all the operations performed against the database. It must also be possible to find particular statements that are of interest to an auditor.  The standard logging facility shows what the user requested, while `pgaudit` focuses on the details of what happened while the database was satisfying the request.
+
+For example, an auditor may want to verify that a particular table was created inside a documented maintenance window.  This might seem like a simple job for grep, but what if you are presented with something like this (intentionally obfuscated) example:
+```
+BEGIN
+    EXECUTE 'CREATE TABLE import' || 'ant_table (id INT)';
+END $$;
+```
+Standard logging will give you this:
+```
+LOG:  statement: DO $$
+BEGIN
+    EXECUTE 'CREATE TABLE import' || 'ant_table (id INT)';
+END $$;
+```
+It appears that finding the table of interest may require some knowledge of the code in cases where tables are created dynamically.  This is not ideal since it would be preferable to just search on the table name. This is where `pgaudit` comes in.  For the same input, it will produce this output in the log:
+```
+AUDIT: SESSION,33,1,FUNCTION,DO,,,"DO $$
+BEGIN
+    EXECUTE 'CREATE TABLE import' || 'ant_table (id INT)';
+END $$;"
+AUDIT: SESSION,33,2,DDL,CREATE TABLE,TABLE,public.important_table,CREATE TABLE important_table (id INT)
+```
+Not only is the `DO` block logged, but substatement 2 contains the full text of the `CREATE TABLE` with the statement type, object type, and full-qualified name to make searches easy.
+
+When logging `SELECT` and `DML` statements, `pgaudit` can be configured to log a separate entry for each relation referenced in a statement.  No parsing is required to find all statements that touch a particular table.  In fact, the goal is that the statement text is provided primarily for deep forensics and should not be required for an audit.
+
+## Usage Considerations
+
+Depending on settings, it is possible for `pgaudit` to generate an enormous volume of logging.  Be careful to determine exactly what needs to be audit logged in your environment to avoid logging too much.
+
+For example, when working in an OLAP environment it would probably not be wise to audit log inserts into a large fact table.  The size of the log file will likely be many times the actual data size of the inserts because the log file is expressed as text.  Since logs are generally stored with the OS this may lead to disk space being exhausted very quickly.  In cases where it is not possible to limit audit logging to certain tables, be sure to assess the performance impact while testing and allocate plenty of space on the log volume.  This may also be true for OLTP environments.  Even if the insert volume is not as high, the performance impact of audit logging may still noticeably affect latency.
+
+To limit the number of relations audit logged for `SELECT` and `DML` statments, consider using object audit logging (see [Object Auditing](#object-auditing)).  Object audit logging allows selection of the relations to be logged allowing for reduction of the overall log volume.  However, when new relations are added they must be explicitly added to object audit logging.  A programmatic solution where specified tables are excluded from logging and all others are included may be a good option in this case.
+
+## Compile and Install
+
+Clone the PostgreSQL repository:
+```
+git clone https://github.com/postgres/postgres.git
+```
+Checkout REL9_5_STABLE branch:
+```
+git checkout REL9_5_STABLE
+```
+Make PostgreSQL:
+```
+./configure
+make install -s
+```
+Change to the contrib directory:
+```
+cd contrib
+```
+Clone the `pgaudit` extension:
+```
+git clone https://github.com/pgaudit/pgaudit.git
+```
+Change to `pgaudit` directory:
+```
+cd pgaudit
+```
+Build ``pgaudit`` and run regression tests:
+```
+make -s check
+```
+Install `pgaudit`:
+```
+make install
+```
+
+## Settings
+
+Settings may be modified only by a superuser. Allowing normal users to change their settings would defeat the point of an audit log.
+
+Settings can be specified globally (in `postgresql.conf` or using `ALTER SYSTEM ... SET`), at the database level (using `ALTER DATABASE ... SET`), or at the role level (using `ALTER ROLE ... SET`).  Note that settings are not inherited through normal role inheritance and `SET ROLE` will not alter a user's `pgaudit` settings.  This is a limitation of the roles system and not inherent to `pgaudit`.
+
+The `pgaudit` extension must be loaded in [shared_preload_libraries](http://www.postgresql.org/docs/9.5/static/runtime-config-client.html#GUC-SHARED-PRELOAD-LIBRARIES).  Otherwise, an error will be raised at load time and no audit logging will occur.
+
+### pgaudit.log
+
+Specifies which classes of statements will be logged by session audit logging.  Possible values are:
+
+* __READ__: `SELECT` and `COPY` when the source is a relation or a query.
+
+* __WRITE__: `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, and `COPY` when the destination is a relation.
+
+* __FUNCTION__: Function calls and `DO` blocks.
+
+* __ROLE__: Statements related to roles and privileges: `GRANT`, `REVOKE`, `CREATE/ALTER/DROP ROLE`.
+
+* __DDL__: All `DDL` that is not included in the `ROLE` class.
+
+* __MISC__: Miscellaneous commands, e.g. `DISCARD`, `FETCH`, `CHECKPOINT`, `VACUUM`.
+
+Multiple classes can be provided using a comma-separated list and classes can be subtracted by prefacing the class with a `-` sign (see [Session Audit Logging](#session-audit-logging)).
+
+The default is `none`.
+
+### pgaudit.log_catalog
+
+Specifies that session logging should be enabled in the case where all relations in a statement are in pg_catalog.  Disabling this setting will reduce noise in the log from tools like psql and PgAdmin that query the catalog heavily.
+
+The default is `on`.
+
+### pgaudit.log_level
+
+Specifies the log level that will be used for log entries (see [Message Severity Levels] (http://www.postgresql.org/docs/9.1/static/runtime-config-logging.html#RUNTIME-CONFIG-SEVERITY-LEVELS) for valid levels but note that `ERROR`, `FATAL`, and `PANIC` are not allowed). This setting is used for regression testing and may also be useful to end users for testing or other purposes.
+
+The default is `log`.
+
+### pgaudit.log_parameter
+
+Specifies that audit logging should include the parameters that were passed with the statement.  When parameters are present they will be included in CSV format after the statement text.
+
+The default is `off`.
+
+### pgaudit.log_relation
+
+Specifies whether session audit logging should create a separate log entry for each relation (`TABLE`, `VIEW`, etc.) referenced in a `SELECT` or `DML` statement.  This is a useful shortcut for exhaustive logging without using object audit logging.
+
+The default is `off`.
+
+### pgaudit.log_statement_once
+
+Specifies whether logging will include the statement text and parameters with the first log entry for a statement/substatement combination or with every entry.  Disabling this setting will result in less verbose logging but may make it more difficult to determine the statement that generated a log entry, though the statement/substatement pair along with the process id should suffice to identify the statement text logged with a previous entry.
+
+The default is `off`.
+
+### pgaudit.role
+
+Specifies the master role to use for object audit logging.  Muliple audit roles can be defined by granting them to the master role. This allows multiple groups to be in charge of different aspects of audit logging.
+
+There is no default.
+
+## Session Audit Logging
+
+Session audit logging provides detailed logs of all statements executed by a user in the backend.
+
+### Configuration
+
+Session logging is enabled with the [pgaudit.log](#pgauditlog) setting.
+
+Enable session logging for all `DML` and `DDL` and log all relations in `DML` statements:
+```
+set pgaudit.log = 'write, ddl';
+set pgaudit.log_relation = on;
+```
+Enable session logging for all commands except `MISC` and raise audit log messages as `NOTICE`:
+```
+set pgaudit.log = 'all, -misc';
+set pgaudit.log_level = notice;
+```
+
+### Example
+
+In this example session audit logging is used for logging `DDL` and `SELECT` statements.  Note that the insert statement is not logged since the `WRITE` class is not enabled
+
+SQL:
+```
+set pgaudit.log = 'read, ddl';
+
+create table account
+(
+    id int,
+    name text,
+    password text,
+    description text
+);
+
+insert into account (id, name, password, description)
+             values (1, 'user1', 'HASH1', 'blah, blah');
+
+select *
+    from account;
+```
+Log Output:
+```
+AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.account,create table account
+(
+    id int,
+    name text,
+    password text,
+    description text
+);
+AUDIT: SESSION,2,1,READ,SELECT,,,select *
+    from account
+```
+
+## Object Auditing
+
+Object audit logging logs statements that affect a particular relation. Only `SELECT`, `INSERT`, `UPDATE` and `DELETE` commands are supported.  `TRUNCATE` is not included in object audit logging.
+
+Object audit logging is intended to be a finer-grained replacement for `pgaudit.log = 'read, write'`.  As such, it may not make sense to use them in conjunction but one possible scenario would be to use session logging to capture each statement and then supplement that with object logging to get more detail about specific relations.
+
+### Configuration
+
+Object-level audit logging is implemented via the roles system.  The [pgaudit.role](#pgauditrole) setting defines the role that will be used for audit logging.  A relation (`TABLE`, `VIEW`, etc.) will be audit logged when the audit role has permissions for the command executed or inherits the permissions from another role.  This allows you to effectively have multiple audit roles even though there is a single master role in any context.
+
+Set [pgaudit.role](#pgauditrole) to `auditor` and grant `SELECT` and `DELETE` privileges on the `account` table. Any `SELECT` or `DELETE` statements on `account` will now be logged:
+```
+set pgaudit.role = 'auditor';
+
+grant select, delete
+   on public.account
+   to auditor;
+```
+
+### Example
+
+In this example object audit logging is used to illustrate how a granular approach may be taken towards logging of `SELECT` and `DML` statements.  Note that logging on the `account` table is controlled by column-level permissions, while logging on `account_role_map` is table-level.
+
+SQL:
+```
+set pgaudit.role = 'auditor';
+
+create table account
+(
+    id int,
+    name text,
+    password text,
+    description text
+);
+
+grant select (password)
+   on public.account
+   to auditor;
+
+select id, name
+  from account;
+
+select password
+  from account;
+
+grant update (name, password)
+   on public.account
+   to auditor;
+
+update account
+   set description = 'yada, yada';
+
+update account
+   set password = 'HASH2';
+
+create table account_role_map
+(
+    account_id int,
+    role_id int
+);
+
+grant select
+   on public.account_role_map
+   to auditor;
+
+select account.password,
+       account_role_map.role_id
+  from account
+       inner join account_role_map
+            on account.id = account_role_map.account_id
+```
+Log Output:
+```
+AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account,select password
+  from account
+AUDIT: OBJECT,2,1,WRITE,UPDATE,TABLE,public.account,update account
+   set password = 'HASH2'
+AUDIT: OBJECT,3,1,READ,SELECT,TABLE,public.account,select account.password,
+       account_role_map.role_id
+  from account
+       inner join account_role_map
+            on account.id = account_role_map.account_id
+AUDIT: OBJECT,3,1,READ,SELECT,TABLE,public.account_role_map,select account.password,
+       account_role_map.role_id
+  from account
+       inner join account_role_map
+            on account.id = account_role_map.account_id
+```
+
+## Format
+
+Audit entries are written to the standard logging facility and contain the following columns in comma-separated format:
+
+Output is compliant CSV format only if the log line prefix portion of each log entry is removed.
+
+* __AUDIT_TYPE__ - `SESSION` or `OBJECT`.
+* __STATEMENT_ID__ - Unique statement ID for this session. Each statement ID represents a backend call.  Statement IDs are sequential even if some statements are not logged.  There may be multiple entries for a statement ID when more than one relation is logged.
+
+* __SUBSTATEMENT_ID__ - Sequential ID for each substatement within the main statement.  For example, calling a function from a query.  Substatement IDs are continuous even if some substatements are not logged.  There may be multiple entries for a substatement ID when more than one relation is logged.
+
+* __CLASS__ - e.g. (`READ`, `ROLE`) (see [pgaudit.log](#pgauditlog)).
+* __COMMAND__ - e.g. `ALTER TABLE`, `SELECT`.
+
+* __OBJECT_TYPE__ - `TABLE`, `INDEX`, `VIEW`, etc. Available for `SELECT`, `DML` and most `DDL` statements.
+
+* __OBJECT_NAME__ - The fully-qualified object name (e.g. public.account).  Available for `SELECT`, `DML` and most `DDL` statements.
+
+* __STATEMENT__ - Statement executed on the backend.
+
+* __PARAMETER__ - If `pgaudit.log_parameter` is set then this field will contain the statement parameters as quoted CSV.
+
+Use [log_line_prefix](http://www.postgresql.org/docs/9.5/static/runtime-config-logging.html#GUC-LOG-LINE-PREFIX) to add any other fields that are needed to satisfy your audit log requirements.  A typical log line prefix might be `'%m %u %d: '` which would provide the date/time, user name, and database name for each audit log.
+
+## Caveats
+
+* Object renames are logged under the name they were renamed to. For example, renaming a table will produce the following result:
+
+```
+ALTER TABLE test RENAME TO test2;
+
+AUDIT: SESSION,36,1,DDL,ALTER TABLE,TABLE,public.test2,ALTER TABLE test RENAME TO test2
+```
+* It is possible to have a command logged more than once.  For example, when a table is created with a primary key specified at creation time the index for the primary key will be logged independently and another audit log will be made for the index under the create entry.  The multiple entries will however be contained within one statement ID.
+
+* Autovacuum and Autoanalyze are not logged.
+
+* Statements that are executed after a transaction enters an aborted state will not be audit logged.  However, the statement that caused the error and any subsequent statements executed in the aborted transaction will be logged as ERRORs by the standard logging facility.
+
+## Authors
+
+The PostgreSQL Audit Extension is based on the pgaudit project at https://github.com/2ndQuadrant authored by Abhijit Menon-Sen and Ian Barwick.  Further development has been done by David Steele.
diff --git a/contrib/pgaudit/expected/pgaudit.out b/contrib/pgaudit/expected/pgaudit.out
new file mode 100644
index 0000000..e7ee93b
--- /dev/null
+++ b/contrib/pgaudit/expected/pgaudit.out
@@ -0,0 +1,1123 @@
+\set VERBOSITY terse
+-- Create pgaudit extension
+CREATE EXTENSION IF NOT EXISTS pgaudit;
+--
+-- Audit log fields are:
+--     AUDIT_TYPE - SESSION or OBJECT
+--     STATEMENT_ID - ID of the statement in the current backend
+--     SUBSTATEMENT_ID - ID of the substatement in the current backend
+--     CLASS - Class of statement being logged (e.g. ROLE, READ, WRITE)
+--     COMMAND - e.g. SELECT, CREATE ROLE, UPDATE
+--     OBJECT_TYPE - When available, type of object acted on (e.g. TABLE, VIEW)
+--     OBJECT_NAME - When available, fully-qualified table of object
+--     STATEMENT - The statement being logged
+--     PARAMETER - If parameter logging is requested, they will follow the
+--                 statement
+SELECT current_user \gset
+--
+-- Set pgaudit parameters for the current (super)user.
+ALTER ROLE :current_user SET pgaudit.log = 'Role';
+ALTER ROLE :current_user SET pgaudit.log_level = 'notice';
+ALTER ROLE :current_user SET pgaudit.log_client = ON;
+-- After each connect, we need to load pgaudit, as if it was
+-- being loaded from shared_preload_libraries.  Otherwise, the hooks
+-- won't be set up and called correctly, leading to lots of ugly
+-- errors.
+\connect - :current_user;
+--
+-- Create auditor role
+CREATE ROLE auditor;
+NOTICE:  AUDIT: SESSION,1,1,ROLE,CREATE ROLE,,,CREATE ROLE auditor;,<not logged>
+--
+-- Create first test user
+CREATE USER user1;
+NOTICE:  AUDIT: SESSION,2,1,ROLE,CREATE ROLE,,,CREATE USER user1;,<not logged>
+ALTER ROLE user1 SET pgaudit.log = 'ddl, ROLE';
+NOTICE:  AUDIT: SESSION,3,1,ROLE,ALTER ROLE,,,"ALTER ROLE user1 SET pgaudit.log = 'ddl, ROLE';",<not logged>
+ALTER ROLE user1 SET pgaudit.log_level = 'notice';
+NOTICE:  AUDIT: SESSION,4,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log_level = 'notice';,<not logged>
+ALTER ROLE user1 SET pgaudit.log_client = ON;
+NOTICE:  AUDIT: SESSION,5,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log_client = ON;,<not logged>
+--
+-- Create, select, drop (select will not be audited)
+\connect - user1
+CREATE TABLE public.test
+(
+	id INT
+);
+NOTICE:  AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.test,"CREATE TABLE public.test
+(
+	id INT
+);",<not logged>
+SELECT *
+  FROM test;
+ id 
+----
+(0 rows)
+
+DROP TABLE test;
+NOTICE:  AUDIT: SESSION,2,1,DDL,DROP TABLE,TABLE,public.test,DROP TABLE test;,<not logged>
+--
+-- Create second test user
+\connect - :current_user
+CREATE USER user2;
+NOTICE:  AUDIT: SESSION,1,1,ROLE,CREATE ROLE,,,CREATE USER user2;,<not logged>
+ALTER ROLE user2 SET pgaudit.log = 'Read, writE';
+NOTICE:  AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,"ALTER ROLE user2 SET pgaudit.log = 'Read, writE';",<not logged>
+ALTER ROLE user2 SET pgaudit.log_catalog = OFF;
+NOTICE:  AUDIT: SESSION,3,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_catalog = OFF;,<not logged>
+ALTER ROLE user2 SET pgaudit.log_level = 'warning';
+NOTICE:  AUDIT: SESSION,4,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_level = 'warning';,<not logged>
+ALTER ROLE user2 SET pgaudit.log_client = ON;
+NOTICE:  AUDIT: SESSION,5,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_client = ON;,<not logged>
+ALTER ROLE user2 SET pgaudit.role = auditor;
+NOTICE:  AUDIT: SESSION,6,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.role = auditor;,<not logged>
+ALTER ROLE user2 SET pgaudit.log_statement_once = ON;
+NOTICE:  AUDIT: SESSION,7,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_statement_once = ON;,<not logged>
+--
+-- Setup role-based tests
+CREATE TABLE test2
+(
+	id INT
+);
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test2
+   TO user2, user1;
+NOTICE:  AUDIT: SESSION,8,1,ROLE,GRANT,TABLE,,"GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test2
+   TO user2, user1;",<not logged>
+GRANT SELECT, UPDATE
+   ON TABLE public.test2
+   TO auditor;
+NOTICE:  AUDIT: SESSION,9,1,ROLE,GRANT,TABLE,,"GRANT SELECT, UPDATE
+   ON TABLE public.test2
+   TO auditor;",<not logged>
+CREATE TABLE test3
+(
+	id INT
+);
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test3
+   TO user2;
+NOTICE:  AUDIT: SESSION,10,1,ROLE,GRANT,TABLE,,"GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test3
+   TO user2;",<not logged>
+GRANT INSERT
+   ON TABLE public.test3
+   TO auditor;
+NOTICE:  AUDIT: SESSION,11,1,ROLE,GRANT,TABLE,,"GRANT INSERT
+   ON TABLE public.test3
+   TO auditor;",<not logged>
+CREATE FUNCTION test2_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 90
+	 WHERE id = new.id;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_insert() OWNER TO user1;
+CREATE TRIGGER test2_insert_trg
+	AFTER INSERT ON test2
+	FOR EACH ROW EXECUTE PROCEDURE test2_insert();
+CREATE FUNCTION test2_change(change_id int) RETURNS void AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 1
+	 WHERE id = change_id;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_change(int) OWNER TO user2;
+CREATE VIEW vw_test3 AS
+SELECT *
+  FROM test3;
+GRANT SELECT
+   ON vw_test3
+   TO user2;
+NOTICE:  AUDIT: SESSION,12,1,ROLE,GRANT,TABLE,,"GRANT SELECT
+   ON vw_test3
+   TO user2;",<not logged>
+GRANT SELECT
+   ON vw_test3
+   TO auditor;
+NOTICE:  AUDIT: SESSION,13,1,ROLE,GRANT,TABLE,,"GRANT SELECT
+   ON vw_test3
+   TO auditor;",<not logged>
+\connect - user2
+--
+-- Role-based tests
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	  LIMIT 1
+) SUBQUERY;
+ count 
+-------
+     1
+(1 row)
+
+SELECT *
+  FROM test3, test2;
+WARNING:  AUDIT: SESSION,1,1,READ,SELECT,,,"SELECT *
+  FROM test3, test2;",<not logged>
+WARNING:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+ id | id 
+----+----
+(0 rows)
+
+--
+-- Object logged because of:
+-- select on vw_test3
+-- select on test2
+SELECT *
+  FROM vw_test3, test2;
+WARNING:  AUDIT: SESSION,2,1,READ,SELECT,,,"SELECT *
+  FROM vw_test3, test2;",<not logged>
+WARNING:  AUDIT: OBJECT,2,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+WARNING:  AUDIT: OBJECT,2,1,READ,SELECT,VIEW,public.vw_test3,<previously logged>,<previously logged>
+ id | id 
+----+----
+(0 rows)
+
+--
+-- Object logged because of:
+-- insert on test3
+-- select on test2
+WITH CTE AS
+(
+	SELECT id
+	  FROM test2
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+WARNING:  AUDIT: SESSION,3,1,WRITE,INSERT,,,"WITH CTE AS
+(
+	SELECT id
+	  FROM test2
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;",<not logged>
+WARNING:  AUDIT: OBJECT,3,1,WRITE,INSERT,TABLE,public.test3,<previously logged>,<previously logged>
+WARNING:  AUDIT: OBJECT,3,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Object logged because of:
+-- insert on test3
+WITH CTE AS
+(
+	INSERT INTO test3 VALUES (1)
+				   RETURNING id
+)
+INSERT INTO test2
+SELECT id
+  FROM cte;
+WARNING:  AUDIT: SESSION,4,1,WRITE,INSERT,,,"WITH CTE AS
+(
+	INSERT INTO test3 VALUES (1)
+				   RETURNING id
+)
+INSERT INTO test2
+SELECT id
+  FROM cte;",<not logged>
+WARNING:  AUDIT: OBJECT,4,1,WRITE,INSERT,TABLE,public.test3,<previously logged>,<previously logged>
+DO $$ BEGIN PERFORM test2_change(91); END $$;
+WARNING:  AUDIT: SESSION,5,1,READ,SELECT,,,SELECT test2_change(91),<not logged>
+WARNING:  AUDIT: SESSION,5,2,WRITE,UPDATE,,,"UPDATE test2
+	   SET id = id + 1
+	 WHERE id = change_id",<not logged>
+WARNING:  AUDIT: OBJECT,5,2,WRITE,UPDATE,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Object logged because of:
+-- insert on test3
+-- update on test2
+WITH CTE AS
+(
+	UPDATE test2
+	   SET id = 45
+	 WHERE id = 92
+	RETURNING id
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+WARNING:  AUDIT: SESSION,6,1,WRITE,INSERT,,,"WITH CTE AS
+(
+	UPDATE test2
+	   SET id = 45
+	 WHERE id = 92
+	RETURNING id
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;",<not logged>
+WARNING:  AUDIT: OBJECT,6,1,WRITE,INSERT,TABLE,public.test3,<previously logged>,<previously logged>
+WARNING:  AUDIT: OBJECT,6,1,WRITE,UPDATE,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Object logged because of:
+-- insert on test2
+WITH CTE AS
+(
+	INSERT INTO test2 VALUES (37)
+				   RETURNING id
+)
+UPDATE test3
+   SET id = cte.id
+  FROM cte
+ WHERE test3.id <> cte.id;
+WARNING:  AUDIT: SESSION,7,1,WRITE,UPDATE,,,"WITH CTE AS
+(
+	INSERT INTO test2 VALUES (37)
+				   RETURNING id
+)
+UPDATE test3
+   SET id = cte.id
+  FROM cte
+ WHERE test3.id <> cte.id;",<not logged>
+WARNING:  AUDIT: OBJECT,7,1,WRITE,INSERT,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Be sure that test has correct contents
+SELECT *
+  FROM test2
+ ORDER BY ID;
+WARNING:  AUDIT: SESSION,8,1,READ,SELECT,,,"SELECT *
+  FROM test2
+ ORDER BY ID;",<not logged>
+WARNING:  AUDIT: OBJECT,8,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+ id  
+-----
+  45
+ 127
+(2 rows)
+
+--
+-- Change permissions of user 2 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user2 SET pgaudit.log = 'NONE';
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log = 'NONE';,<not logged>
+\connect - user2
+--
+-- Create test4 and add permissions
+CREATE TABLE test4
+(
+	id int,
+	name text
+);
+GRANT SELECT (name)
+   ON TABLE public.test4
+   TO auditor;
+GRANT UPDATE (id)
+   ON TABLE public.test4
+   TO auditor;
+GRANT insert (name)
+   ON TABLE public.test4
+   TO auditor;
+--
+-- Not object logged
+SELECT id
+  FROM public.test4;
+ id 
+----
+(0 rows)
+
+--
+-- Object logged because of:
+-- select (name) on test4
+SELECT name
+  FROM public.test4;
+WARNING:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.test4,"SELECT name
+  FROM public.test4;",<not logged>
+ name 
+------
+(0 rows)
+
+--
+-- Not object logged
+INSERT INTO public.test4 (id)
+				  VALUES (1);
+--
+-- Object logged because of:
+-- insert (name) on test4
+INSERT INTO public.test4 (name)
+				  VALUES ('test');
+WARNING:  AUDIT: OBJECT,2,1,WRITE,INSERT,TABLE,public.test4,"INSERT INTO public.test4 (name)
+				  VALUES ('test');",<not logged>
+--
+-- Not object logged
+UPDATE public.test4
+   SET name = 'foo';
+--
+-- Object logged because of:
+-- update (id) on test4
+UPDATE public.test4
+   SET id = 1;
+WARNING:  AUDIT: OBJECT,3,1,WRITE,UPDATE,TABLE,public.test4,"UPDATE public.test4
+   SET id = 1;",<not logged>
+--
+-- Object logged because of:
+-- update (name) on test4
+-- update (name) takes precedence over select (name) due to ordering
+update public.test4 set name = 'foo' where name = 'bar';
+WARNING:  AUDIT: OBJECT,4,1,WRITE,UPDATE,TABLE,public.test4,update public.test4 set name = 'foo' where name = 'bar';,<not logged>
+--
+-- Change permissions of user 1 so that session logging will be done
+\connect - :current_user
+--
+-- Drop test tables
+DROP TABLE test2;
+DROP VIEW vw_test3;
+DROP TABLE test3;
+DROP TABLE test4;
+DROP FUNCTION test2_insert();
+DROP FUNCTION test2_change(int);
+ALTER ROLE user1 SET pgaudit.log = 'DDL, READ';
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,"ALTER ROLE user1 SET pgaudit.log = 'DDL, READ';",<not logged>
+\connect - user1
+--
+-- Create table is session logged
+CREATE TABLE public.account
+(
+	id INT,
+	name TEXT,
+	password TEXT,
+	description TEXT
+);
+NOTICE:  AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.account,"CREATE TABLE public.account
+(
+	id INT,
+	name TEXT,
+	password TEXT,
+	description TEXT
+);",<not logged>
+--
+-- Select is session logged
+SELECT *
+  FROM account;
+NOTICE:  AUDIT: SESSION,2,1,READ,SELECT,,,"SELECT *
+  FROM account;",<not logged>
+ id | name | password | description 
+----+------+----------+-------------
+(0 rows)
+
+--
+-- Insert is not logged
+INSERT INTO account (id, name, password, description)
+			 VALUES (1, 'user1', 'HASH1', 'blah, blah');
+--
+-- Change permissions of user 1 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log = 'none';
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log = 'none';,<not logged>
+ALTER ROLE user1 SET pgaudit.role = 'auditor';
+NOTICE:  AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.role = 'auditor';,<not logged>
+\connect - user1
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT (password),
+	  UPDATE (name, password)
+   ON TABLE public.account
+   TO auditor;
+--
+-- Not object logged
+SELECT id,
+	   name
+  FROM account;
+ id | name  
+----+-------
+  1 | user1
+(1 row)
+
+--
+-- Object logged because of:
+-- select (password) on account
+SELECT password
+  FROM account;
+NOTICE:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account,"SELECT password
+  FROM account;",<not logged>
+ password 
+----------
+ HASH1
+(1 row)
+
+--
+-- Not object logged
+UPDATE account
+   SET description = 'yada, yada';
+--
+-- Object logged because of:
+-- update (password) on account
+UPDATE account
+   SET password = 'HASH2';
+NOTICE:  AUDIT: OBJECT,2,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET password = 'HASH2';",<not logged>
+--
+-- Change permissions of user 1 so that session relation logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log_relation = on;
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log_relation = on;,<not logged>
+ALTER ROLE user1 SET pgaudit.log = 'read, WRITE';
+NOTICE:  AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,"ALTER ROLE user1 SET pgaudit.log = 'read, WRITE';",<not logged>
+\connect - user1
+--
+-- Not logged
+CREATE TABLE ACCOUNT_ROLE_MAP
+(
+	account_id INT,
+	role_id INT
+);
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT
+   ON TABLE public.account_role_map
+   TO auditor;
+--
+-- Object logged because of:
+-- select (password) on account
+-- select on account_role_map
+-- Session logged on all tables because log = read and log_relation = on
+SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;
+NOTICE:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+NOTICE:  AUDIT: SESSION,1,1,READ,SELECT,TABLE,public.account,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+NOTICE:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account_role_map,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+NOTICE:  AUDIT: SESSION,1,1,READ,SELECT,TABLE,public.account_role_map,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+ password | role_id 
+----------+---------
+(0 rows)
+
+--
+-- Object logged because of:
+-- select (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+SELECT password
+  FROM account;
+NOTICE:  AUDIT: OBJECT,2,1,READ,SELECT,TABLE,public.account,"SELECT password
+  FROM account;",<not logged>
+NOTICE:  AUDIT: SESSION,2,1,READ,SELECT,TABLE,public.account,"SELECT password
+  FROM account;",<not logged>
+ password 
+----------
+ HASH2
+(1 row)
+
+--
+-- Not object logged
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada';
+NOTICE:  AUDIT: SESSION,3,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET description = 'yada, yada';",<not logged>
+--
+-- Object logged because of:
+-- select (password) on account (in the where clause)
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';
+NOTICE:  AUDIT: OBJECT,4,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';",<not logged>
+NOTICE:  AUDIT: SESSION,4,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';",<not logged>
+--
+-- Object logged because of:
+-- update (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET password = 'HASH2';
+NOTICE:  AUDIT: OBJECT,5,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET password = 'HASH2';",<not logged>
+NOTICE:  AUDIT: SESSION,5,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET password = 'HASH2';",<not logged>
+--
+-- Change back to superuser to do exhaustive tests
+\connect - :current_user
+SET pgaudit.log = 'ALL';
+NOTICE:  AUDIT: SESSION,1,1,MISC,SET,,,SET pgaudit.log = 'ALL';,<not logged>
+SET pgaudit.log_level = 'notice';
+NOTICE:  AUDIT: SESSION,2,1,MISC,SET,,,SET pgaudit.log_level = 'notice';,<not logged>
+SET pgaudit.log_client = ON;
+NOTICE:  AUDIT: SESSION,3,1,MISC,SET,,,SET pgaudit.log_client = ON;,<not logged>
+SET pgaudit.log_relation = ON;
+NOTICE:  AUDIT: SESSION,4,1,MISC,SET,,,SET pgaudit.log_relation = ON;,<not logged>
+SET pgaudit.log_parameter = ON;
+NOTICE:  AUDIT: SESSION,5,1,MISC,SET,,,SET pgaudit.log_parameter = ON;,<none>
+--
+-- Simple DO block
+DO $$
+BEGIN
+	raise notice 'test';
+END $$;
+NOTICE:  AUDIT: SESSION,6,1,FUNCTION,DO,,,"DO $$
+BEGIN
+	raise notice 'test';
+END $$;",<none>
+NOTICE:  test
+--
+-- Create test schema
+CREATE SCHEMA test;
+NOTICE:  AUDIT: SESSION,7,1,DDL,CREATE SCHEMA,SCHEMA,test,CREATE SCHEMA test;,<none>
+--
+-- Copy account to stdout
+COPY account TO stdout;
+NOTICE:  AUDIT: SESSION,8,1,READ,SELECT,TABLE,public.account,COPY account TO stdout;,<none>
+1	user1	HASH2	yada, yada
+--
+-- Create a table from a query
+CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;
+NOTICE:  AUDIT: SESSION,9,1,READ,SELECT,TABLE,public.account,"CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;",<none>
+NOTICE:  AUDIT: SESSION,9,1,WRITE,INSERT,TABLE,test.account_copy,"CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;",<none>
+NOTICE:  AUDIT: SESSION,9,2,DDL,CREATE TABLE AS,TABLE,test.account_copy,"CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;",<none>
+--
+-- Copy from stdin to account copy
+COPY test.account_copy from stdin;
+NOTICE:  AUDIT: SESSION,10,1,WRITE,INSERT,TABLE,test.account_copy,COPY test.account_copy from stdin;,<none>
+--
+-- Test prepared statement
+PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;
+NOTICE:  AUDIT: SESSION,11,1,READ,PREPARE,,,"PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;",<none>
+EXECUTE pgclassstmt (1);
+NOTICE:  AUDIT: SESSION,12,1,READ,SELECT,TABLE,public.account,"PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;",1
+NOTICE:  AUDIT: SESSION,12,2,MISC,EXECUTE,,,EXECUTE pgclassstmt (1);,<none>
+ id | name  | password | description 
+----+-------+----------+-------------
+  1 | user1 | HASH2    | yada, yada
+(1 row)
+
+DEALLOCATE pgclassstmt;
+NOTICE:  AUDIT: SESSION,13,1,MISC,DEALLOCATE,,,DEALLOCATE pgclassstmt;,<none>
+--
+-- Test cursor
+BEGIN;
+NOTICE:  AUDIT: SESSION,14,1,MISC,BEGIN,,,BEGIN;,<none>
+DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+NOTICE:  AUDIT: SESSION,15,1,READ,SELECT,TABLE,pg_catalog.pg_class,"DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;",<none>
+NOTICE:  AUDIT: SESSION,15,2,READ,DECLARE CURSOR,,,"DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;",<none>
+FETCH NEXT FROM ctest;
+NOTICE:  AUDIT: SESSION,16,1,MISC,FETCH,,,FETCH NEXT FROM ctest;,<none>
+ count 
+-------
+     1
+(1 row)
+
+CLOSE ctest;
+NOTICE:  AUDIT: SESSION,17,1,MISC,CLOSE CURSOR,,,CLOSE ctest;,<none>
+COMMIT;
+NOTICE:  AUDIT: SESSION,18,1,MISC,COMMIT,,,COMMIT;,<none>
+--
+-- Turn off log_catalog and pg_class will not be logged
+SET pgaudit.log_catalog = OFF;
+NOTICE:  AUDIT: SESSION,19,1,MISC,SET,,,SET pgaudit.log_catalog = OFF;,<none>
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+ count 
+-------
+     1
+(1 row)
+
+--
+-- Test prepared insert
+CREATE TABLE test.test_insert
+(
+	id INT
+);
+NOTICE:  AUDIT: SESSION,20,1,DDL,CREATE TABLE,TABLE,test.test_insert,"CREATE TABLE test.test_insert
+(
+	id INT
+);",<none>
+PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);
+NOTICE:  AUDIT: SESSION,21,1,WRITE,PREPARE,,,"PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);",<none>
+EXECUTE pgclassstmt (1);
+NOTICE:  AUDIT: SESSION,22,1,WRITE,INSERT,TABLE,test.test_insert,"PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);",1
+NOTICE:  AUDIT: SESSION,22,2,MISC,EXECUTE,,,EXECUTE pgclassstmt (1);,<none>
+--
+-- Check that primary key creation is logged
+CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);
+NOTICE:  AUDIT: SESSION,23,1,DDL,CREATE TABLE,TABLE,public.test,"CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);",<none>
+NOTICE:  AUDIT: SESSION,23,1,DDL,CREATE INDEX,INDEX,public.test_pkey,"CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);",<none>
+--
+-- Check that analyze is logged
+ANALYZE test;
+NOTICE:  AUDIT: SESSION,24,1,MISC,ANALYZE,,,ANALYZE test;,<none>
+--
+-- Grants to public should not cause object logging (session logging will
+-- still happen)
+GRANT SELECT
+  ON TABLE public.test
+  TO PUBLIC;
+NOTICE:  AUDIT: SESSION,25,1,ROLE,GRANT,TABLE,,"GRANT SELECT
+  ON TABLE public.test
+  TO PUBLIC;",<none>
+SELECT *
+  FROM test;
+NOTICE:  AUDIT: SESSION,26,1,READ,SELECT,TABLE,public.test,"SELECT *
+  FROM test;",<none>
+ id | name | description 
+----+------+-------------
+(0 rows)
+
+-- Check that statements without columns log
+SELECT
+  FROM test;
+NOTICE:  AUDIT: SESSION,27,1,READ,SELECT,TABLE,public.test,"SELECT
+  FROM test;",<none>
+--
+(0 rows)
+
+SELECT 1,
+	   substring('Thomas' from 2 for 3);
+NOTICE:  AUDIT: SESSION,28,1,READ,SELECT,,,"SELECT 1,
+	   substring('Thomas' from 2 for 3);",<none>
+ ?column? | substring 
+----------+-----------
+        1 | hom
+(1 row)
+
+DO $$
+DECLARE
+	test INT;
+BEGIN
+	SELECT 1
+	  INTO test;
+END $$;
+NOTICE:  AUDIT: SESSION,29,1,FUNCTION,DO,,,"DO $$
+DECLARE
+	test INT;
+BEGIN
+	SELECT 1
+	  INTO test;
+END $$;",<none>
+NOTICE:  AUDIT: SESSION,29,2,READ,SELECT,,,SELECT 1,<none>
+explain select 1;
+NOTICE:  AUDIT: SESSION,30,1,READ,SELECT,,,explain select 1;,<none>
+NOTICE:  AUDIT: SESSION,30,2,MISC,EXPLAIN,,,explain select 1;,<none>
+                QUERY PLAN                
+------------------------------------------
+ Result  (cost=0.00..0.01 rows=1 width=0)
+(1 row)
+
+--
+-- Test that looks inside of do blocks log
+INSERT INTO TEST (id)
+		  VALUES (1);
+NOTICE:  AUDIT: SESSION,31,1,WRITE,INSERT,TABLE,public.test,"INSERT INTO TEST (id)
+		  VALUES (1);",<none>
+INSERT INTO TEST (id)
+		  VALUES (2);
+NOTICE:  AUDIT: SESSION,32,1,WRITE,INSERT,TABLE,public.test,"INSERT INTO TEST (id)
+		  VALUES (2);",<none>
+INSERT INTO TEST (id)
+		  VALUES (3);
+NOTICE:  AUDIT: SESSION,33,1,WRITE,INSERT,TABLE,public.test,"INSERT INTO TEST (id)
+		  VALUES (3);",<none>
+DO $$
+DECLARE
+	result RECORD;
+BEGIN
+	FOR result IN
+		SELECT id
+		  FROM test
+	LOOP
+		INSERT INTO test (id)
+			 VALUES (result.id + 100);
+	END LOOP;
+END $$;
+NOTICE:  AUDIT: SESSION,34,1,FUNCTION,DO,,,"DO $$
+DECLARE
+	result RECORD;
+BEGIN
+	FOR result IN
+		SELECT id
+		  FROM test
+	LOOP
+		INSERT INTO test (id)
+			 VALUES (result.id + 100);
+	END LOOP;
+END $$;",<none>
+NOTICE:  AUDIT: SESSION,34,2,READ,SELECT,TABLE,public.test,"SELECT id
+		  FROM test",<none>
+NOTICE:  AUDIT: SESSION,34,3,WRITE,INSERT,TABLE,public.test,"INSERT INTO test (id)
+			 VALUES (result.id + 100)","f,,"
+NOTICE:  AUDIT: SESSION,34,4,WRITE,INSERT,TABLE,public.test,"INSERT INTO test (id)
+			 VALUES (result.id + 100)","t,,"
+NOTICE:  AUDIT: SESSION,34,5,WRITE,INSERT,TABLE,public.test,"INSERT INTO test (id)
+			 VALUES (result.id + 100)","t,,"
+--
+-- Test obfuscated dynamic sql for clean logging
+DO $$
+DECLARE
+	table_name TEXT = 'do_table';
+BEGIN
+	EXECUTE 'CREATE TABLE ' || table_name || ' ("weird name" INT)';
+	EXECUTE 'DROP table ' || table_name;
+END $$;
+NOTICE:  AUDIT: SESSION,35,1,FUNCTION,DO,,,"DO $$
+DECLARE
+	table_name TEXT = 'do_table';
+BEGIN
+	EXECUTE 'CREATE TABLE ' || table_name || ' (""weird name"" INT)';
+	EXECUTE 'DROP table ' || table_name;
+END $$;",<none>
+NOTICE:  AUDIT: SESSION,35,2,DDL,CREATE TABLE,TABLE,public.do_table,"CREATE TABLE do_table (""weird name"" INT)",<none>
+NOTICE:  AUDIT: SESSION,35,3,DDL,DROP TABLE,TABLE,public.do_table,DROP table do_table,<none>
+--
+-- Generate an error and make sure the stack gets cleared
+DO $$
+BEGIN
+	CREATE TABLE bogus.test_block
+	(
+		id INT
+	);
+END $$;
+NOTICE:  AUDIT: SESSION,36,1,FUNCTION,DO,,,"DO $$
+BEGIN
+	CREATE TABLE bogus.test_block
+	(
+		id INT
+	);
+END $$;",<none>
+ERROR:  schema "bogus" does not exist at character 14
+--
+-- Test alter table statements
+ALTER TABLE public.test
+	DROP COLUMN description ;
+NOTICE:  AUDIT: SESSION,37,1,DDL,ALTER TABLE,TABLE COLUMN,public.test.description,"ALTER TABLE public.test
+	DROP COLUMN description ;",<none>
+NOTICE:  AUDIT: SESSION,37,1,DDL,ALTER TABLE,TABLE,public.test,"ALTER TABLE public.test
+	DROP COLUMN description ;",<none>
+ALTER TABLE public.test
+	RENAME TO test2;
+NOTICE:  AUDIT: SESSION,38,1,DDL,ALTER TABLE,TABLE,public.test2,"ALTER TABLE public.test
+	RENAME TO test2;",<none>
+ALTER TABLE public.test2
+	SET SCHEMA test;
+NOTICE:  AUDIT: SESSION,39,1,DDL,ALTER TABLE,TABLE,test.test2,"ALTER TABLE public.test2
+	SET SCHEMA test;",<none>
+ALTER TABLE test.test2
+	ADD COLUMN description TEXT;
+NOTICE:  AUDIT: SESSION,40,1,DDL,ALTER TABLE,TABLE,test.test2,"ALTER TABLE test.test2
+	ADD COLUMN description TEXT;",<none>
+ALTER TABLE test.test2
+	DROP COLUMN description;
+NOTICE:  AUDIT: SESSION,41,1,DDL,ALTER TABLE,TABLE COLUMN,test.test2.description,"ALTER TABLE test.test2
+	DROP COLUMN description;",<none>
+NOTICE:  AUDIT: SESSION,41,1,DDL,ALTER TABLE,TABLE,test.test2,"ALTER TABLE test.test2
+	DROP COLUMN description;",<none>
+DROP TABLE test.test2;
+NOTICE:  AUDIT: SESSION,42,1,DDL,DROP TABLE,TABLE,test.test2,DROP TABLE test.test2;,<none>
+NOTICE:  AUDIT: SESSION,42,1,DDL,DROP TABLE,TABLE CONSTRAINT,test_pkey on test.test2,DROP TABLE test.test2;,<none>
+NOTICE:  AUDIT: SESSION,42,1,DDL,DROP TABLE,INDEX,test.test_pkey,DROP TABLE test.test2;,<none>
+--
+-- Test multiple statements with one semi-colon
+CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);
+NOTICE:  AUDIT: SESSION,43,1,DDL,CREATE SCHEMA,SCHEMA,foo,"CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);",<none>
+NOTICE:  AUDIT: SESSION,43,1,DDL,CREATE TABLE,TABLE,foo.bar,"CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);",<none>
+NOTICE:  AUDIT: SESSION,43,1,DDL,CREATE TABLE,TABLE,foo.baz,"CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);",<none>
+--
+-- Test aggregate
+CREATE FUNCTION public.int_add
+(
+	a INT,
+	b INT
+)
+	RETURNS INT LANGUAGE plpgsql AS $$
+BEGIN
+	return a + b;
+END $$;
+NOTICE:  AUDIT: SESSION,44,1,DDL,CREATE FUNCTION,FUNCTION,"public.int_add(integer,integer)","CREATE FUNCTION public.int_add
+(
+	a INT,
+	b INT
+)
+	RETURNS INT LANGUAGE plpgsql AS $$
+BEGIN
+	return a + b;
+END $$;",<none>
+SELECT int_add(1, 1);
+NOTICE:  AUDIT: SESSION,45,1,READ,SELECT,,,"SELECT int_add(1, 1);",<none>
+NOTICE:  AUDIT: SESSION,45,2,FUNCTION,EXECUTE,FUNCTION,public.int_add,"SELECT int_add(1, 1);",<none>
+ int_add 
+---------
+       2
+(1 row)
+
+CREATE AGGREGATE public.sum_test(INT) (SFUNC=public.int_add, STYPE=INT, INITCOND='0');
+NOTICE:  AUDIT: SESSION,46,1,DDL,CREATE AGGREGATE,AGGREGATE,public.sum_test(integer),"CREATE AGGREGATE public.sum_test(INT) (SFUNC=public.int_add, STYPE=INT, INITCOND='0');",<none>
+ALTER AGGREGATE public.sum_test(integer) RENAME TO sum_test2;
+NOTICE:  AUDIT: SESSION,47,1,DDL,ALTER AGGREGATE,AGGREGATE,public.sum_test2(integer),ALTER AGGREGATE public.sum_test(integer) RENAME TO sum_test2;,<none>
+--
+-- Test conversion
+CREATE CONVERSION public.conversion_test FOR 'SQL_ASCII' TO 'MULE_INTERNAL' FROM pg_catalog.ascii_to_mic;
+NOTICE:  AUDIT: SESSION,48,1,DDL,CREATE CONVERSION,CONVERSION,public.conversion_test,CREATE CONVERSION public.conversion_test FOR 'SQL_ASCII' TO 'MULE_INTERNAL' FROM pg_catalog.ascii_to_mic;,<none>
+ALTER CONVERSION public.conversion_test RENAME TO conversion_test2;
+NOTICE:  AUDIT: SESSION,49,1,DDL,ALTER CONVERSION,CONVERSION,public.conversion_test2,ALTER CONVERSION public.conversion_test RENAME TO conversion_test2;,<none>
+--
+-- Test create/alter/drop database
+CREATE DATABASE contrib_regression_pgaudit;
+NOTICE:  AUDIT: SESSION,50,1,DDL,CREATE DATABASE,,,CREATE DATABASE contrib_regression_pgaudit;,<none>
+ALTER DATABASE contrib_regression_pgaudit RENAME TO contrib_regression_pgaudit2;
+NOTICE:  AUDIT: SESSION,51,1,DDL,ALTER DATABASE,,,ALTER DATABASE contrib_regression_pgaudit RENAME TO contrib_regression_pgaudit2;,<none>
+DROP DATABASE contrib_regression_pgaudit2;
+NOTICE:  AUDIT: SESSION,52,1,DDL,DROP DATABASE,,,DROP DATABASE contrib_regression_pgaudit2;,<none>
+-- Test role as a substmt
+SET pgaudit.log = 'ROLE';
+CREATE TABLE t ();
+CREATE ROLE alice;
+NOTICE:  AUDIT: SESSION,53,1,ROLE,CREATE ROLE,,,CREATE ROLE alice;,<none>
+CREATE SCHEMA foo2
+	GRANT SELECT
+	   ON public.t
+	   TO alice;
+NOTICE:  AUDIT: SESSION,54,1,ROLE,GRANT,TABLE,,"CREATE SCHEMA foo2
+	GRANT SELECT
+	   ON public.t
+	   TO alice;",<none>
+drop table public.t;
+drop role alice;
+NOTICE:  AUDIT: SESSION,55,1,ROLE,DROP ROLE,,,drop role alice;,<none>
+--
+-- Test that frees a memory context earlier than expected
+SET pgaudit.log = 'ALL';
+NOTICE:  AUDIT: SESSION,56,1,MISC,SET,,,SET pgaudit.log = 'ALL';,<none>
+CREATE TABLE hoge
+(
+	id int
+);
+NOTICE:  AUDIT: SESSION,57,1,DDL,CREATE TABLE,TABLE,public.hoge,"CREATE TABLE hoge
+(
+	id int
+);",<none>
+CREATE FUNCTION test()
+	RETURNS INT AS $$
+DECLARE
+	cur1 cursor for select * from hoge;
+	tmp int;
+BEGIN
+	OPEN cur1;
+	FETCH cur1 into tmp;
+	RETURN tmp;
+END $$
+LANGUAGE plpgsql ;
+NOTICE:  AUDIT: SESSION,58,1,DDL,CREATE FUNCTION,FUNCTION,public.test(),"CREATE FUNCTION test()
+	RETURNS INT AS $$
+DECLARE
+	cur1 cursor for select * from hoge;
+	tmp int;
+BEGIN
+	OPEN cur1;
+	FETCH cur1 into tmp;
+	RETURN tmp;
+END $$
+LANGUAGE plpgsql ;",<none>
+SELECT test();
+NOTICE:  AUDIT: SESSION,59,1,READ,SELECT,,,SELECT test();,<none>
+NOTICE:  AUDIT: SESSION,59,2,FUNCTION,EXECUTE,FUNCTION,public.test,SELECT test();,<none>
+NOTICE:  AUDIT: SESSION,59,3,READ,SELECT,TABLE,public.hoge,select * from hoge,<none>
+ test 
+------
+     
+(1 row)
+
+--
+-- Delete all rows then delete 1 row
+SET pgaudit.log = 'write';
+SET pgaudit.role = 'auditor';
+create table bar
+(
+	col int
+);
+grant delete
+   on bar
+   to auditor;
+insert into bar (col)
+		 values (1);
+NOTICE:  AUDIT: SESSION,60,1,WRITE,INSERT,TABLE,public.bar,"insert into bar (col)
+		 values (1);",<none>
+delete from bar;
+NOTICE:  AUDIT: OBJECT,61,1,WRITE,DELETE,TABLE,public.bar,delete from bar;,<none>
+NOTICE:  AUDIT: SESSION,61,1,WRITE,DELETE,TABLE,public.bar,delete from bar;,<none>
+insert into bar (col)
+		 values (1);
+NOTICE:  AUDIT: SESSION,62,1,WRITE,INSERT,TABLE,public.bar,"insert into bar (col)
+		 values (1);",<none>
+delete from bar
+ where col = 1;
+NOTICE:  AUDIT: OBJECT,63,1,WRITE,DELETE,TABLE,public.bar,"delete from bar
+ where col = 1;",<none>
+NOTICE:  AUDIT: SESSION,63,1,WRITE,DELETE,TABLE,public.bar,"delete from bar
+ where col = 1;",<none>
+drop table bar;
+--
+-- Grant roles to each other
+SET pgaudit.log = 'role';
+GRANT user1 TO user2;
+NOTICE:  AUDIT: SESSION,64,1,ROLE,GRANT ROLE,,,GRANT user1 TO user2;,<none>
+REVOKE user1 FROM user2;
+NOTICE:  AUDIT: SESSION,65,1,ROLE,REVOKE ROLE,,,REVOKE user1 FROM user2;,<none>
+--
+-- Test that FK references do not log but triggers still do
+SET pgaudit.log = 'READ,WRITE';
+SET pgaudit.role TO 'auditor';
+SET pgaudit.log_parameter TO OFF;
+CREATE TABLE aaa
+(
+	ID int primary key
+);
+CREATE TABLE bbb
+(
+	id int
+		references aaa(id)
+);
+CREATE FUNCTION bbb_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE bbb set id = new.id + 1;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql;
+CREATE TRIGGER bbb_insert_trg
+	AFTER INSERT ON bbb
+	FOR EACH ROW EXECUTE PROCEDURE bbb_insert();
+GRANT SELECT
+   ON aaa
+   TO auditor;
+GRANT UPDATE
+   ON bbb
+   TO auditor;
+INSERT INTO aaa VALUES (generate_series(1,100));
+NOTICE:  AUDIT: SESSION,66,1,WRITE,INSERT,TABLE,public.aaa,"INSERT INTO aaa VALUES (generate_series(1,100));",<not logged>
+INSERT INTO bbb VALUES (1);
+NOTICE:  AUDIT: SESSION,67,1,WRITE,INSERT,TABLE,public.bbb,INSERT INTO bbb VALUES (1);,<not logged>
+NOTICE:  AUDIT: OBJECT,67,2,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+NOTICE:  AUDIT: SESSION,67,2,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+NOTICE:  AUDIT: OBJECT,67,3,WRITE,UPDATE,TABLE,public.bbb,UPDATE bbb set id = new.id + 1,<not logged>
+NOTICE:  AUDIT: SESSION,67,3,WRITE,UPDATE,TABLE,public.bbb,UPDATE bbb set id = new.id + 1,<not logged>
+NOTICE:  AUDIT: OBJECT,67,4,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+NOTICE:  AUDIT: SESSION,67,4,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+DROP TABLE bbb;
+DROP TABLE aaa;
+-- Cleanup
+-- Set client_min_messages up to warning to avoid noise
+SET client_min_messages = 'warning';
+ALTER ROLE :current_user RESET pgaudit.log;
+ALTER ROLE :current_user RESET pgaudit.log_catalog;
+ALTER ROLE :current_user RESET pgaudit.log_level;
+ALTER ROLE :current_user RESET pgaudit.log_parameter;
+ALTER ROLE :current_user RESET pgaudit.log_relation;
+ALTER ROLE :current_user RESET pgaudit.log_statement_once;
+ALTER ROLE :current_user RESET pgaudit.role;
+RESET pgaudit.log;
+RESET pgaudit.log_catalog;
+RESET pgaudit.log_level;
+RESET pgaudit.log_parameter;
+RESET pgaudit.log_relation;
+RESET pgaudit.log_statement_once;
+RESET pgaudit.role;
+DROP TABLE test.account_copy;
+DROP TABLE test.test_insert;
+DROP SCHEMA test;
+DROP TABLE foo.bar;
+DROP TABLE foo.baz;
+DROP SCHEMA foo;
+DROP TABLE hoge;
+DROP TABLE account;
+DROP TABLE account_role_map;
+DROP USER user2;
+DROP USER user1;
+DROP ROLE auditor;
+RESET client_min_messages;
diff --git a/contrib/pgaudit/pgaudit--1.0.sql b/contrib/pgaudit/pgaudit--1.0.sql
new file mode 100644
index 0000000..e0a12b7
--- /dev/null
+++ b/contrib/pgaudit/pgaudit--1.0.sql
@@ -0,0 +1,22 @@
+/* pgaudit/pgaudit--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION pgaudit" to load this file.\quit
+
+CREATE FUNCTION pgaudit_ddl_command_end()
+	RETURNS event_trigger
+	LANGUAGE C
+	AS 'MODULE_PATHNAME', 'pgaudit_ddl_command_end';
+
+CREATE EVENT TRIGGER pgaudit_ddl_command_end
+	ON ddl_command_end
+	EXECUTE PROCEDURE pgaudit_ddl_command_end();
+
+CREATE FUNCTION pgaudit_sql_drop()
+	RETURNS event_trigger
+	LANGUAGE C
+	AS 'MODULE_PATHNAME', 'pgaudit_sql_drop';
+
+CREATE EVENT TRIGGER pgaudit_sql_drop
+	ON sql_drop
+	EXECUTE PROCEDURE pgaudit_sql_drop();
diff --git a/contrib/pgaudit/pgaudit.c b/contrib/pgaudit/pgaudit.c
new file mode 100644
index 0000000..00be0c2
--- /dev/null
+++ b/contrib/pgaudit/pgaudit.c
@@ -0,0 +1,1922 @@
+/*------------------------------------------------------------------------------
+ * pgaudit.c
+ *
+ * An audit logging extension for PostgreSQL. Provides detailed logging classes,
+ * object level logging, and fully-qualified object names for all DML and DDL
+ * statements where possible (See pgaudit.sgml for details).
+ *
+ * Copyright (c) 2014-2015, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		  contrib/pgaudit/pgaudit.c
+ *------------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/htup_details.h"
+#include "access/sysattr.h"
+#include "access/xact.h"
+#include "catalog/catalog.h"
+#include "catalog/objectaccess.h"
+#include "catalog/pg_class.h"
+#include "catalog/namespace.h"
+#include "commands/dbcommands.h"
+#include "catalog/pg_proc.h"
+#include "commands/event_trigger.h"
+#include "executor/executor.h"
+#include "executor/spi.h"
+#include "miscadmin.h"
+#include "libpq/auth.h"
+#include "nodes/nodes.h"
+#include "tcop/utility.h"
+#include "tcop/deparse_utility.h"
+#include "utils/acl.h"
+#include "utils/builtins.h"
+#include "utils/guc.h"
+#include "utils/lsyscache.h"
+#include "utils/memutils.h"
+#include "utils/rel.h"
+#include "utils/syscache.h"
+#include "utils/timestamp.h"
+
+PG_MODULE_MAGIC;
+
+void _PG_init(void);
+
+PG_FUNCTION_INFO_V1(pgaudit_ddl_command_end);
+PG_FUNCTION_INFO_V1(pgaudit_sql_drop);
+
+/*
+ * Log Classes
+ *
+ * pgAudit categorizes actions into classes (eg: DDL, FUNCTION calls, READ
+ * queries, WRITE queries).  A GUC is provided for the administrator to
+ * configure which class (or classes) of actions to include in the
+ * audit log.  We track the currently active set of classes using
+ * auditLogBitmap.
+ */
+
+/* Bits within auditLogBitmap, defines the classes we understand */
+#define LOG_DDL			(1 << 0)	/* CREATE/DROP/ALTER objects */
+#define LOG_FUNCTION	(1 << 1)	/* Functions and DO blocks */
+#define LOG_MISC		(1 << 2)	/* Statements not covered */
+#define LOG_READ		(1 << 3)	/* SELECTs */
+#define LOG_ROLE		(1 << 4)	/* GRANT/REVOKE, CREATE/ALTER/DROP ROLE */
+#define LOG_WRITE		(1 << 5)	/* INSERT, UPDATE, DELETE, TRUNCATE */
+
+#define LOG_NONE		0				/* nothing */
+#define LOG_ALL			(0xFFFFFFFF)	/* All */
+
+/* GUC variable for pgaudit.log, which defines the classes to log. */
+char *auditLog = NULL;
+
+/* Bitmap of classes selected */
+static int auditLogBitmap = LOG_NONE;
+
+/*
+ * String constants for log classes - used when processing tokens in the
+ * pgaudit.log GUC.
+ */
+#define CLASS_DDL		"DDL"
+#define CLASS_FUNCTION	"FUNCTION"
+#define CLASS_MISC		"MISC"
+#define CLASS_READ		"READ"
+#define CLASS_ROLE		"ROLE"
+#define CLASS_WRITE		"WRITE"
+
+#define CLASS_NONE		"NONE"
+#define CLASS_ALL		"ALL"
+
+/*
+ * GUC variable for pgaudit.log_catalog
+ *
+ * Administrators can choose to NOT log queries when all relations used in
+ * the query are in pg_catalog.  Interactive sessions (eg: psql) can cause
+ * a lot of noise in the logs which might be uninteresting.
+ */
+bool auditLogCatalog = true;
+
+/*
+ * GUC variable for pgaudit.log_client
+ *
+ * Specifies whether audit messages should be visible to the client.  This
+ * setting should generally be left disabled but may be useful for debugging or
+ * other purposes.
+ */
+bool auditLogClient = false;
+
+/*
+ * GUC variable for pgaudit.log_level
+ *
+ * Administrators can choose which log level the audit log is to be logged
+ * at.  The default level is LOG, which goes into the server log but does
+ * not go to the client.  Set to NOTICE in the regression tests.
+ */
+char *auditLogLevelString = NULL;
+int auditLogLevel = LOG;
+
+/*
+ * GUC variable for pgaudit.log_parameter
+ *
+ * Administrators can choose if parameters passed into a statement are
+ * included in the audit log.
+ */
+bool auditLogParameter = false;
+
+/*
+ * GUC variable for pgaudit.log_relation
+ *
+ * Administrators can choose, in SESSION logging, to log each relation involved
+ * in READ/WRITE class queries.  By default, SESSION logs include the query but
+ * do not have a log entry for each relation.
+ */
+bool auditLogRelation = false;
+
+/*
+ * GUC variable for pgaudit.log_statement_once
+ *
+ * Administrators can choose to have the statement run logged only once instead
+ * of on every line.  By default, the statement is repeated on every line of
+ * the audit log to facilitate searching, but this can cause the log to be
+ * unnecessairly bloated in some environments.
+ */
+bool auditLogStatementOnce = false;
+
+/*
+ * GUC variable for pgaudit.role
+ *
+ * Administrators can choose which role to base OBJECT auditing off of.
+ * Object-level auditing uses the privileges which are granted to this role to
+ * determine if a statement should be logged.
+ */
+char *auditRole = NULL;
+
+/*
+ * String constants for the audit log fields.
+ */
+
+/*
+ * Audit type, which is responsbile for the log message
+ */
+#define AUDIT_TYPE_OBJECT	"OBJECT"
+#define AUDIT_TYPE_SESSION	"SESSION"
+
+/*
+ * Command, used for SELECT/DML and function calls.
+ *
+ * We hook into the executor, but we do not have access to the parsetree there.
+ * Therefore we can't simply call CreateCommandTag() to get the command and have
+ * to build it ourselves based on what information we do have.
+ *
+ * These should be updated if new commands are added to what the exectuor
+ * currently handles.  Note that most of the interesting commands do not go
+ * through the executor but rather ProcessUtility, where we have the parsetree.
+ */
+#define COMMAND_SELECT		"SELECT"
+#define COMMAND_INSERT		"INSERT"
+#define COMMAND_UPDATE		"UPDATE"
+#define COMMAND_DELETE		"DELETE"
+#define COMMAND_EXECUTE		"EXECUTE"
+#define COMMAND_UNKNOWN		"UNKNOWN"
+
+/*
+ * Object type, used for SELECT/DML statements and function calls.
+ *
+ * For relation objects, this is essentially relkind (though we do not have
+ * access to a function which will just return a string given a relkind;
+ * getRelationTypeDescription() comes close but is not public currently).
+ *
+ * We also handle functions, so it isn't quite as simple as just relkind.
+ *
+ * This should be kept consistent with what is returned from
+ * pg_event_trigger_ddl_commands(), as that's what we use for DDL.
+ */
+#define OBJECT_TYPE_TABLE			"TABLE"
+#define OBJECT_TYPE_INDEX			"INDEX"
+#define OBJECT_TYPE_SEQUENCE		"SEQUENCE"
+#define OBJECT_TYPE_TOASTVALUE		"TOAST TABLE"
+#define OBJECT_TYPE_VIEW			"VIEW"
+#define OBJECT_TYPE_MATVIEW			"MATERIALIZED VIEW"
+#define OBJECT_TYPE_COMPOSITE_TYPE	"COMPOSITE TYPE"
+#define OBJECT_TYPE_FOREIGN_TABLE	"FOREIGN TABLE"
+#define OBJECT_TYPE_FUNCTION		"FUNCTION"
+
+#define OBJECT_TYPE_UNKNOWN			"UNKNOWN"
+
+/*
+ * String constants for testing role commands.  Rename and drop role statements
+ * are assigned the nodeTag T_RenameStmt and T_DropStmt respectively.  This is
+ * not very useful for classification, so we resort to comparing strings
+ * against the result of CreateCommandTag(parsetree).
+ */
+#define COMMAND_ALTER_ROLE	"ALTER ROLE"
+#define COMMAND_DROP_ROLE	"DROP ROLE"
+#define COMMAND_GRANT		"GRANT"
+#define COMMAND_REVOKE		"REVOKE"
+
+/*
+ * An AuditEvent represents an operation that potentially affects a single
+ * object.  If a statement affects multiple objects then multiple AuditEvents
+ * are created to represent them.
+ */
+typedef struct
+{
+	int64 statementId;			/* Simple counter */
+	int64 substatementId;		/* Simple counter */
+
+	LogStmtLevel logStmtLevel;	/* From GetCommandLogLevel when possible,
+								   generated when not. */
+	NodeTag commandTag;			/* same here */
+	const char *command;		/* same here */
+	const char *objectType;		/* From event trigger when possible,
+								   generated when not. */
+	char *objectName;			/* Fully qualified object identification */
+	const char *commandText;	/* sourceText / queryString */
+	ParamListInfo paramList;	/* QueryDesc/ProcessUtility parameters */
+
+	bool granted;				/* Audit role has object permissions? */
+	bool logged;				/* Track if we have logged this event, used
+								   post-ProcessUtility to make sure we log */
+	bool statementLogged;		/* Track if we have logged the statement */
+} AuditEvent;
+
+/*
+ * A simple FIFO queue to keep track of the current stack of audit events.
+ */
+typedef struct AuditEventStackItem
+{
+	struct AuditEventStackItem *next;
+
+	AuditEvent auditEvent;
+
+	int64 stackId;
+
+	MemoryContext contextAudit;
+	MemoryContextCallback contextCallback;
+} AuditEventStackItem;
+
+AuditEventStackItem *auditEventStack = NULL;
+
+/*
+ * pgAudit runs queries of its own when using the event trigger system.
+ *
+ * Track when we are running a query and don't log it.
+ */
+static bool internalStatement = false;
+
+/*
+ * Track running total for statements and substatements and whether or not
+ * anything has been logged since the current statement began.
+ */
+static int64 statementTotal = 0;
+static int64 substatementTotal = 0;
+static int64 stackTotal = 0;
+
+static bool statementLogged = false;
+
+/*
+ * Stack functions
+ *
+ * Audit events can go down to multiple levels so a stack is maintained to keep
+ * track of them.
+ */
+
+/*
+ * Respond to callbacks registered with MemoryContextRegisterResetCallback().
+ * Removes the event(s) off the stack that have become obsolete once the
+ * MemoryContext has been freed.  The callback should always be freeing the top
+ * of the stack, but the code is tolerant of out-of-order callbacks.
+ */
+static void
+stack_free(void *stackFree)
+{
+	AuditEventStackItem *nextItem = auditEventStack;
+
+	/* Only process if the stack contains items */
+	while (nextItem != NULL)
+	{
+		/* Check if this item matches the item to be freed */
+		if (nextItem == (AuditEventStackItem *) stackFree)
+		{
+			/* Move top of stack to the item after the freed item */
+			auditEventStack = nextItem->next;
+
+			/* If the stack is not empty */
+			if (auditEventStack == NULL)
+			{
+				/*
+				 * Reset internal statement to false.  Normally this will be
+				 * reset but in case of an error it might be left set.
+				 */
+				internalStatement = false;
+
+				/*
+				 * Reset sub statement total so the next statement will start
+				 * from 1.
+				 */
+				substatementTotal = 0;
+
+				/*
+				 * Reset statement logged so that next statement will be
+				 * logged.
+				 */
+				statementLogged = false;
+			}
+
+			return;
+		}
+
+		nextItem = nextItem->next;
+	}
+}
+
+/*
+ * Push a new audit event onto the stack and create a new memory context to
+ * store it.
+ */
+static AuditEventStackItem *
+stack_push()
+{
+	MemoryContext contextAudit;
+	MemoryContext contextOld;
+	AuditEventStackItem *stackItem;
+
+	/*
+	 * Create a new memory context to contain the stack item.  This will be
+	 * free'd on stack_pop, or by our callback when the parent context is
+	 * destroyed.
+	 */
+	contextAudit = AllocSetContextCreate(CurrentMemoryContext,
+										 "pgaudit stack context",
+										 ALLOCSET_DEFAULT_MINSIZE,
+										 ALLOCSET_DEFAULT_INITSIZE,
+										 ALLOCSET_DEFAULT_MAXSIZE);
+
+	/* Save the old context to switch back to at the end */
+	contextOld = MemoryContextSwitchTo(contextAudit);
+
+	/* Create our new stack item in our context */
+	stackItem = palloc0(sizeof(AuditEventStackItem));
+	stackItem->contextAudit = contextAudit;
+	stackItem->stackId = ++stackTotal;
+
+	/*
+	 * Setup a callback in case an error happens.  stack_free() will truncate
+	 * the stack at this item.
+	 */
+	stackItem->contextCallback.func = stack_free;
+	stackItem->contextCallback.arg = (void *) stackItem;
+	MemoryContextRegisterResetCallback(contextAudit,
+									   &stackItem->contextCallback);
+
+	/* Push new item onto the stack */
+	if (auditEventStack != NULL)
+		stackItem->next = auditEventStack;
+	else
+		stackItem->next = NULL;
+
+	auditEventStack = stackItem;
+
+	MemoryContextSwitchTo(contextOld);
+
+	return stackItem;
+}
+
+/*
+ * Pop an audit event from the stack by deleting the memory context that
+ * contains it.  The callback to stack_free() does the actual pop.
+ */
+static void
+stack_pop(int64 stackId)
+{
+	/* Make sure what we want to delete is at the top of the stack */
+	if (auditEventStack != NULL && auditEventStack->stackId == stackId)
+		MemoryContextDelete(auditEventStack->contextAudit);
+	else
+		elog(ERROR, "pgaudit stack item " INT64_FORMAT
+			 " not found on top - cannot pop",
+			 stackId);
+}
+
+/*
+ * Check that an item is on the stack.  If not, an error will be raised since
+ * this is a bad state to be in and it might mean audit records are being lost.
+ */
+static void
+stack_valid(int64 stackId)
+{
+	AuditEventStackItem *nextItem = auditEventStack;
+
+	/* Look through the stack for the stack entry */
+	while (nextItem != NULL && nextItem->stackId != stackId)
+		nextItem = nextItem->next;
+
+	/* If we didn't find it, something went wrong. */
+	if (nextItem == NULL)
+		elog(ERROR, "pgaudit stack item " INT64_FORMAT
+			 " not found - top of stack is " INT64_FORMAT "",
+			 stackId,
+			 auditEventStack == NULL ? (int64) -1 : auditEventStack->stackId);
+}
+
+/*
+ * Appends a properly quoted CSV field to StringInfo.
+ */
+static void
+append_valid_csv(StringInfoData *buffer, const char *appendStr)
+{
+	const char *pChar;
+
+	/*
+	 * If the append string is null then do nothing.  NULL fields are not
+	 * quoted in CSV.
+	 */
+	if (appendStr == NULL)
+		return;
+
+	/* Only format for CSV if appendStr contains: ", comma, \n, \r */
+	if (strstr(appendStr, ",") || strstr(appendStr, "\"") ||
+		strstr(appendStr, "\n") || strstr(appendStr, "\r"))
+	{
+		appendStringInfoCharMacro(buffer, '"');
+
+		for (pChar = appendStr; *pChar; pChar++)
+		{
+			if (*pChar == '"')	/* double single quotes */
+				appendStringInfoCharMacro(buffer, *pChar);
+
+			appendStringInfoCharMacro(buffer, *pChar);
+		}
+
+		appendStringInfoCharMacro(buffer, '"');
+	}
+	/* Else just append */
+	else
+		appendStringInfoString(buffer, appendStr);
+}
+
+/*
+ * Takes an AuditEvent, classifies it, then logs it if appropriate.
+ *
+ * Logging is decided based on if the statement is in one of the classes being
+ * logged or if an object used has been marked for auditing.
+ *
+ * Objects are marked for auditing by the auditor role being granted access
+ * to the object.  The kind of access (INSERT, UPDATE, etc) is also considered
+ * and logging is only performed when the kind of access matches the granted
+ * right on the object.
+ *
+ * This will need to be updated if new kinds of GRANTs are added.
+ */
+static void
+log_audit_event(AuditEventStackItem *stackItem)
+{
+	/* By default, put everything in the MISC class. */
+	int class = LOG_MISC;
+	const char *className = CLASS_MISC;
+	MemoryContext contextOld;
+	StringInfoData auditStr;
+
+	/* Classify the statement using log stmt level and the command tag */
+	switch (stackItem->auditEvent.logStmtLevel)
+	{
+			/* All mods go in WRITE class, except EXECUTE */
+		case LOGSTMT_MOD:
+			className = CLASS_WRITE;
+			class = LOG_WRITE;
+
+			switch (stackItem->auditEvent.commandTag)
+			{
+					/* Currently, only EXECUTE is different */
+				case T_ExecuteStmt:
+					className = CLASS_MISC;
+					class = LOG_MISC;
+					break;
+				default:
+					break;
+			}
+			break;
+
+			/* These are DDL, unless they are ROLE */
+		case LOGSTMT_DDL:
+			className = CLASS_DDL;
+			class = LOG_DDL;
+
+			/* Identify role statements */
+			switch (stackItem->auditEvent.commandTag)
+			{
+					/* We know these are all role statements */
+				case T_GrantStmt:
+				case T_GrantRoleStmt:
+				case T_CreateRoleStmt:
+				case T_DropRoleStmt:
+				case T_AlterRoleStmt:
+				case T_AlterRoleSetStmt:
+				case T_AlterDefaultPrivilegesStmt:
+					className = CLASS_ROLE;
+					class = LOG_ROLE;
+					break;
+
+					/*
+					 * Rename and Drop are general and therefore we have to do
+					 * an additional check against the command string to see
+					 * if they are role or regular DDL.
+					 */
+				case T_RenameStmt:
+				case T_DropStmt:
+					if (pg_strcasecmp(stackItem->auditEvent.command,
+									  COMMAND_ALTER_ROLE) == 0 ||
+						pg_strcasecmp(stackItem->auditEvent.command,
+									  COMMAND_DROP_ROLE) == 0)
+					{
+						className = CLASS_ROLE;
+						class = LOG_ROLE;
+					}
+					break;
+
+				default:
+					break;
+			}
+			break;
+
+			/* Classify the rest */
+		case LOGSTMT_ALL:
+			switch (stackItem->auditEvent.commandTag)
+			{
+					/* READ statements */
+				case T_CopyStmt:
+				case T_SelectStmt:
+				case T_PrepareStmt:
+				case T_PlannedStmt:
+					className = CLASS_READ;
+					class = LOG_READ;
+					break;
+
+					/* FUNCTION statements */
+				case T_DoStmt:
+					className = CLASS_FUNCTION;
+					class = LOG_FUNCTION;
+					break;
+
+				default:
+					break;
+			}
+			break;
+
+		case LOGSTMT_NONE:
+			break;
+	}
+
+	/*
+	 * Only log the statement if:
+	 *
+	 * 1. If object was selected for audit logging (granted), or
+	 * 2. The statement belongs to a class that is being logged
+	 *
+	 * If neither of these is true, return.
+	 */
+	if (!stackItem->auditEvent.granted && !(auditLogBitmap & class))
+		return;
+
+	/*
+	 * Use audit memory context in case something is not free'd while
+	 * appending strings and parameters.
+	 */
+	contextOld = MemoryContextSwitchTo(stackItem->contextAudit);
+
+	/* Set statement and substatement IDs */
+	if (stackItem->auditEvent.statementId == 0)
+	{
+		/* If nothing has been logged yet then create a new statement Id */
+		if (!statementLogged)
+		{
+			statementTotal++;
+			statementLogged = true;
+		}
+
+		stackItem->auditEvent.statementId = statementTotal;
+		stackItem->auditEvent.substatementId = ++substatementTotal;
+	}
+
+	/*
+	 * Create the audit substring
+	 *
+	 * The type-of-audit-log and statement/substatement ID are handled below,
+	 * this string is everything else.
+	 */
+	initStringInfo(&auditStr);
+	append_valid_csv(&auditStr, stackItem->auditEvent.command);
+
+	appendStringInfoCharMacro(&auditStr, ',');
+	append_valid_csv(&auditStr, stackItem->auditEvent.objectType);
+
+	appendStringInfoCharMacro(&auditStr, ',');
+	append_valid_csv(&auditStr, stackItem->auditEvent.objectName);
+
+	/*
+	 * If auditLogStatmentOnce is true, then only log the statement and
+	 * parameters if they have not already been logged for this substatement.
+	 */
+	appendStringInfoCharMacro(&auditStr, ',');
+	if (!stackItem->auditEvent.statementLogged || !auditLogStatementOnce)
+	{
+		append_valid_csv(&auditStr, stackItem->auditEvent.commandText);
+
+		appendStringInfoCharMacro(&auditStr, ',');
+
+		/* Handle parameter logging, if enabled. */
+		if (auditLogParameter)
+		{
+			int paramIdx;
+			int numParams;
+			StringInfoData paramStrResult;
+			ParamListInfo paramList = stackItem->auditEvent.paramList;
+
+			numParams = paramList == NULL ? 0 : paramList->numParams;
+
+			/* Create the param substring */
+			initStringInfo(&paramStrResult);
+
+			/* Iterate through all params */
+			for (paramIdx = 0; paramList != NULL && paramIdx < numParams;
+				 paramIdx++)
+			{
+				ParamExternData *prm = &paramList->params[paramIdx];
+				Oid typeOutput;
+				bool typeIsVarLena;
+				char *paramStr;
+
+				/* Add a comma for each param */
+				if (paramIdx != 0)
+					appendStringInfoCharMacro(&paramStrResult, ',');
+
+				/* Skip if null or if oid is invalid */
+				if (prm->isnull || !OidIsValid(prm->ptype))
+					continue;
+
+				/* Output the string */
+				getTypeOutputInfo(prm->ptype, &typeOutput, &typeIsVarLena);
+				paramStr = OidOutputFunctionCall(typeOutput, prm->value);
+
+				append_valid_csv(&paramStrResult, paramStr);
+				pfree(paramStr);
+			}
+
+			if (numParams == 0)
+				appendStringInfoString(&auditStr, "<none>");
+			else
+				append_valid_csv(&auditStr, paramStrResult.data);
+		}
+		else
+			appendStringInfoString(&auditStr, "<not logged>");
+
+		stackItem->auditEvent.statementLogged = true;
+	}
+	else
+		/* we were asked to not log it */
+		appendStringInfoString(&auditStr,
+							   "<previously logged>,<previously logged>");
+
+	/*
+	 * Log the audit entry.  Note: use of INT64_FORMAT here is bad for
+	 * translatability, but we currently haven't got translation support in
+	 * pgaudit anyway.
+	 */
+	if (!auditLogClient)
+		LimitClientLogOutput(true);
+
+	ereport(auditLogLevel,
+			(errmsg("AUDIT: %s," INT64_FORMAT "," INT64_FORMAT ",%s,%s",
+					stackItem->auditEvent.granted ?
+					AUDIT_TYPE_OBJECT : AUDIT_TYPE_SESSION,
+					stackItem->auditEvent.statementId,
+					stackItem->auditEvent.substatementId,
+					className,
+					auditStr.data),
+					errhidestmt(true),
+					errhidecontext(true)));
+
+	if (!auditLogClient)
+		LimitClientLogOutput(false);
+
+	stackItem->auditEvent.logged = true;
+
+	MemoryContextSwitchTo(contextOld);
+}
+
+/*
+ * Check if the role or any inherited role has any permission in the mask.  The
+ * public role is excluded from this check and superuser permissions are not
+ * considered.
+ */
+static bool
+audit_on_acl(Datum aclDatum,
+			 Oid auditOid,
+			 AclMode mask)
+{
+	bool result = false;
+	Acl *acl;
+	AclItem *aclItemData;
+	int aclIndex;
+	int aclTotal;
+
+	/* Detoast column's ACL if necessary */
+	acl = DatumGetAclP(aclDatum);
+
+	/* Get the acl list and total number of items */
+	aclTotal = ACL_NUM(acl);
+	aclItemData = ACL_DAT(acl);
+
+	/* Check privileges granted directly to auditOid */
+	for (aclIndex = 0; aclIndex < aclTotal; aclIndex++)
+	{
+		AclItem *aclItem = &aclItemData[aclIndex];
+
+		if (aclItem->ai_grantee == auditOid &&
+			aclItem->ai_privs & mask)
+		{
+			result = true;
+			break;
+		}
+	}
+
+	/*
+	 * Check privileges granted indirectly via role memberships. We do this in
+	 * a separate pass to minimize expensive indirect membership tests.  In
+	 * particular, it's worth testing whether a given ACL entry grants any
+	 * privileges still of interest before we perform the has_privs_of_role
+	 * test.
+	 */
+	if (!result)
+	{
+		for (aclIndex = 0; aclIndex < aclTotal; aclIndex++)
+		{
+			AclItem *aclItem = &aclItemData[aclIndex];
+
+			/* Don't test public or auditOid (it has been tested already) */
+			if (aclItem->ai_grantee == ACL_ID_PUBLIC ||
+				aclItem->ai_grantee == auditOid)
+				continue;
+
+			/*
+			 * Check that the role has the required privileges and that it is
+			 * inherited by auditOid.
+			 */
+			if (aclItem->ai_privs & mask &&
+				has_privs_of_role(auditOid, aclItem->ai_grantee))
+			{
+				result = true;
+				break;
+			}
+		}
+	}
+
+	/* if we have a detoasted copy, free it */
+	if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
+		pfree(acl);
+
+	return result;
+}
+
+/*
+ * Check if a role has any of the permissions in the mask on a relation.
+ */
+static bool
+audit_on_relation(Oid relOid,
+				  Oid auditOid,
+				  AclMode mask)
+{
+	bool result = false;
+	HeapTuple tuple;
+	Datum aclDatum;
+	bool isNull;
+
+	/* Get relation tuple from pg_class */
+	tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
+	if (!HeapTupleIsValid(tuple))
+		return false;
+
+	/* Get the relation's ACL */
+	aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
+							   &isNull);
+
+	/* Only check if non-NULL, since NULL means no permissions */
+	if (!isNull)
+		result = audit_on_acl(aclDatum, auditOid, mask);
+
+	/* Free the relation tuple */
+	ReleaseSysCache(tuple);
+
+	return result;
+}
+
+/*
+ * Check if a role has any of the permissions in the mask on a column.
+ */
+static bool
+audit_on_attribute(Oid relOid,
+				   AttrNumber attNum,
+				   Oid auditOid,
+				   AclMode mask)
+{
+	bool result = false;
+	HeapTuple attTuple;
+	Datum aclDatum;
+	bool isNull;
+
+	/* Get the attribute's ACL */
+	attTuple = SearchSysCache2(ATTNUM,
+							   ObjectIdGetDatum(relOid),
+							   Int16GetDatum(attNum));
+	if (!HeapTupleIsValid(attTuple))
+		return false;
+
+	/* Only consider attributes that have not been dropped */
+	if (!((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
+	{
+		aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
+								   &isNull);
+
+		if (!isNull)
+			result = audit_on_acl(aclDatum, auditOid, mask);
+	}
+
+	/* Free attribute */
+	ReleaseSysCache(attTuple);
+
+	return result;
+}
+
+/*
+ * Check if a role has any of the permissions in the mask on a column in
+ * the provided set.  If the set is empty, then all valid columns in the
+ * relation will be tested.
+ */
+static bool
+audit_on_any_attribute(Oid relOid,
+					   Oid auditOid,
+					   Bitmapset *attributeSet,
+					   AclMode mode)
+{
+	bool result = false;
+	AttrNumber col;
+	Bitmapset *tmpSet;
+
+	/* If bms is empty then check for any column match */
+	if (bms_is_empty(attributeSet))
+	{
+		HeapTuple classTuple;
+		AttrNumber nattrs;
+		AttrNumber curr_att;
+
+		/* Get relation to determine total columns */
+		classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
+
+		if (!HeapTupleIsValid(classTuple))
+			return false;
+
+		nattrs = ((Form_pg_class) GETSTRUCT(classTuple))->relnatts;
+		ReleaseSysCache(classTuple);
+
+		/* Check each column */
+		for (curr_att = 1; curr_att <= nattrs; curr_att++)
+			if (audit_on_attribute(relOid, curr_att, auditOid, mode))
+				return true;
+	}
+
+	/* bms_first_member is destructive, so make a copy before using it. */
+	tmpSet = bms_copy(attributeSet);
+
+	/* Check each column */
+	while ((col = bms_first_member(tmpSet)) >= 0)
+	{
+		col += FirstLowInvalidHeapAttributeNumber;
+
+		if (col != InvalidAttrNumber &&
+			audit_on_attribute(relOid, col, auditOid, mode))
+		{
+			result = true;
+			break;
+		}
+	}
+
+	bms_free(tmpSet);
+
+	return result;
+}
+
+/*
+ * Create AuditEvents for SELECT/DML operations via executor permissions checks.
+ */
+static void
+log_select_dml(Oid auditOid, List *rangeTabls)
+{
+	ListCell *lr;
+	bool first = true;
+	bool found = false;
+
+	/* Do not log if this is an internal statement */
+	if (internalStatement)
+		return;
+
+	foreach(lr, rangeTabls)
+	{
+		Oid relOid;
+		Relation rel;
+		RangeTblEntry *rte = lfirst(lr);
+
+		/* We only care about tables, and can ignore subqueries etc. */
+		if (rte->rtekind != RTE_RELATION)
+			continue;
+
+		found = true;
+
+		/*
+		 * Don't log if the session user is not a member of the current
+		 * role.  This prevents contents of security definer functions
+		 * from being logged and supresses foreign key queries unless the
+		 * session user is the owner of the referenced table.
+		 */
+		if (!is_member_of_role_nosuper(GetSessionUserId(), GetUserId()))
+			return;
+
+		/*
+		 * If we are not logging all-catalog queries (auditLogCatalog is
+		 * false) then filter out any system relations here.
+		 */
+		relOid = rte->relid;
+		rel = relation_open(relOid, NoLock);
+
+		if (!auditLogCatalog && IsSystemNamespace(RelationGetNamespace(rel)))
+		{
+			relation_close(rel, NoLock);
+			continue;
+		}
+
+		/*
+		 * Default is that this was not through a grant, to support session
+		 * logging.  Will be updated below if a grant is found.
+		 */
+		auditEventStack->auditEvent.granted = false;
+
+		/*
+		 * If this is the first RTE then session log unless auditLogRelation
+		 * is set.
+		 */
+		if (first && !auditLogRelation)
+		{
+			log_audit_event(auditEventStack);
+
+			first = false;
+		}
+
+		/*
+		 * We don't have access to the parsetree here, so we have to generate
+		 * the node type, object type, and command tag by decoding
+		 * rte->requiredPerms and rte->relkind.
+		 */
+		if (rte->requiredPerms & ACL_INSERT)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_MOD;
+			auditEventStack->auditEvent.commandTag = T_InsertStmt;
+			auditEventStack->auditEvent.command = COMMAND_INSERT;
+		}
+		else if (rte->requiredPerms & ACL_UPDATE)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_MOD;
+			auditEventStack->auditEvent.commandTag = T_UpdateStmt;
+			auditEventStack->auditEvent.command = COMMAND_UPDATE;
+		}
+		else if (rte->requiredPerms & ACL_DELETE)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_MOD;
+			auditEventStack->auditEvent.commandTag = T_DeleteStmt;
+			auditEventStack->auditEvent.command = COMMAND_DELETE;
+		}
+		else if (rte->requiredPerms & ACL_SELECT)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_ALL;
+			auditEventStack->auditEvent.commandTag = T_SelectStmt;
+			auditEventStack->auditEvent.command = COMMAND_SELECT;
+		}
+		else
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_ALL;
+			auditEventStack->auditEvent.commandTag = T_Invalid;
+			auditEventStack->auditEvent.command = COMMAND_UNKNOWN;
+		}
+
+		/* Use the relation type to assign object type */
+		switch (rte->relkind)
+		{
+			case RELKIND_RELATION:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_TABLE;
+				break;
+
+			case RELKIND_INDEX:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_INDEX;
+				break;
+
+			case RELKIND_SEQUENCE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_SEQUENCE;
+				break;
+
+			case RELKIND_TOASTVALUE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_TOASTVALUE;
+				break;
+
+			case RELKIND_VIEW:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_VIEW;
+				break;
+
+			case RELKIND_COMPOSITE_TYPE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_COMPOSITE_TYPE;
+				break;
+
+			case RELKIND_FOREIGN_TABLE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_FOREIGN_TABLE;
+				break;
+
+			case RELKIND_MATVIEW:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_MATVIEW;
+				break;
+
+			default:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_UNKNOWN;
+				break;
+		}
+
+		/* Get a copy of the relation name and assign it to object name */
+		auditEventStack->auditEvent.objectName =
+			quote_qualified_identifier(get_namespace_name(
+									   RelationGetNamespace(rel)),
+									   RelationGetRelationName(rel));
+		relation_close(rel, NoLock);
+
+		/* Perform object auditing only if the audit role is valid */
+		if (auditOid != InvalidOid)
+		{
+			AclMode auditPerms =
+				(ACL_SELECT | ACL_UPDATE | ACL_INSERT | ACL_DELETE) &
+				rte->requiredPerms;
+
+			/*
+			 * If any of the required permissions for the relation are granted
+			 * to the audit role then audit the relation
+			 */
+			if (audit_on_relation(relOid, auditOid, auditPerms))
+				auditEventStack->auditEvent.granted = true;
+
+			/*
+			 * Else check if the audit role has column-level permissions for
+			 * select, insert, or update.
+			 */
+			else if (auditPerms != 0)
+			{
+				/*
+				 * Check the select columns
+				 */
+				if (auditPerms & ACL_SELECT)
+					auditEventStack->auditEvent.granted =
+						audit_on_any_attribute(relOid, auditOid,
+											   rte->selectedCols,
+											   ACL_SELECT);
+
+				/*
+				 * Check the insert columns
+				 */
+				if (!auditEventStack->auditEvent.granted &&
+					auditPerms & ACL_INSERT)
+					auditEventStack->auditEvent.granted =
+						audit_on_any_attribute(relOid, auditOid,
+											   rte->insertedCols,
+											   auditPerms);
+
+				/*
+				 * Check the update columns
+				 */
+				if (!auditEventStack->auditEvent.granted &&
+					auditPerms & ACL_UPDATE)
+					auditEventStack->auditEvent.granted =
+						audit_on_any_attribute(relOid, auditOid,
+											   rte->updatedCols,
+											   auditPerms);
+			}
+		}
+
+		/* Do relation level logging if a grant was found */
+		if (auditEventStack->auditEvent.granted)
+		{
+			auditEventStack->auditEvent.logged = false;
+			log_audit_event(auditEventStack);
+		}
+
+		/* Do relation level logging if auditLogRelation is set */
+		if (auditLogRelation)
+		{
+			auditEventStack->auditEvent.logged = false;
+			auditEventStack->auditEvent.granted = false;
+			log_audit_event(auditEventStack);
+		}
+
+		pfree(auditEventStack->auditEvent.objectName);
+	}
+
+	/*
+	 * If no tables were found that means that RangeTbls was empty or all
+	 * relations were in the system schema.  In that case still log a session
+	 * record.
+	 */
+	if (!found)
+	{
+		auditEventStack->auditEvent.granted = false;
+		auditEventStack->auditEvent.logged = false;
+
+		log_audit_event(auditEventStack);
+	}
+}
+
+/*
+ * Create AuditEvents for non-catalog function execution, as detected by
+ * log_object_access() below.
+ */
+static void
+log_function_execute(Oid objectId)
+{
+	HeapTuple proctup;
+	Form_pg_proc proc;
+	AuditEventStackItem *stackItem;
+
+	/* Get info about the function. */
+	proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(objectId));
+
+	if (!proctup)
+		elog(ERROR, "cache lookup failed for function %u", objectId);
+
+	proc = (Form_pg_proc) GETSTRUCT(proctup);
+
+	/*
+	 * Logging execution of all pg_catalog functions would make the log
+	 * unusably noisy.
+	 */
+	if (IsSystemNamespace(proc->pronamespace))
+	{
+		ReleaseSysCache(proctup);
+		return;
+	}
+
+	/* Push audit event onto the stack */
+	stackItem = stack_push();
+
+	/* Generate the fully-qualified function name. */
+	stackItem->auditEvent.objectName =
+		quote_qualified_identifier(get_namespace_name(proc->pronamespace),
+								   NameStr(proc->proname));
+	ReleaseSysCache(proctup);
+
+	/* Log the function call */
+	stackItem->auditEvent.logStmtLevel = LOGSTMT_ALL;
+	stackItem->auditEvent.commandTag = T_DoStmt;
+	stackItem->auditEvent.command = COMMAND_EXECUTE;
+	stackItem->auditEvent.objectType = OBJECT_TYPE_FUNCTION;
+	stackItem->auditEvent.commandText = stackItem->next->auditEvent.commandText;
+
+	log_audit_event(stackItem);
+
+	/* Pop audit event from the stack */
+	stack_pop(stackItem->stackId);
+}
+
+/*
+ * Hook functions
+ */
+static ExecutorCheckPerms_hook_type next_ExecutorCheckPerms_hook = NULL;
+static ProcessUtility_hook_type next_ProcessUtility_hook = NULL;
+static object_access_hook_type next_object_access_hook = NULL;
+static ExecutorStart_hook_type next_ExecutorStart_hook = NULL;
+
+/*
+ * Hook ExecutorStart to get the query text and basic command type for queries
+ * that do not contain a table and so can't be idenitified accurately in
+ * ExecutorCheckPerms.
+ */
+static void
+pgaudit_ExecutorStart_hook(QueryDesc *queryDesc, int eflags)
+{
+	AuditEventStackItem *stackItem = NULL;
+
+	if (!internalStatement)
+	{
+		/* Push the audit even onto the stack */
+		stackItem = stack_push();
+
+		/* Initialize command using queryDesc->operation */
+		switch (queryDesc->operation)
+		{
+			case CMD_SELECT:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_ALL;
+				stackItem->auditEvent.commandTag = T_SelectStmt;
+				stackItem->auditEvent.command = COMMAND_SELECT;
+				break;
+
+			case CMD_INSERT:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_MOD;
+				stackItem->auditEvent.commandTag = T_InsertStmt;
+				stackItem->auditEvent.command = COMMAND_INSERT;
+				break;
+
+			case CMD_UPDATE:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_MOD;
+				stackItem->auditEvent.commandTag = T_UpdateStmt;
+				stackItem->auditEvent.command = COMMAND_UPDATE;
+				break;
+
+			case CMD_DELETE:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_MOD;
+				stackItem->auditEvent.commandTag = T_DeleteStmt;
+				stackItem->auditEvent.command = COMMAND_DELETE;
+				break;
+
+			default:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_ALL;
+				stackItem->auditEvent.commandTag = T_Invalid;
+				stackItem->auditEvent.command = COMMAND_UNKNOWN;
+				break;
+		}
+
+		/* Initialize the audit event */
+		stackItem->auditEvent.commandText = queryDesc->sourceText;
+		stackItem->auditEvent.paramList = queryDesc->params;
+	}
+
+	/* Call the previous hook or standard function */
+	if (next_ExecutorStart_hook)
+		next_ExecutorStart_hook(queryDesc, eflags);
+	else
+		standard_ExecutorStart(queryDesc, eflags);
+
+	/*
+	 * Move the stack memory context to the query memory context.  This needs
+	 * to be done here because the query context does not exist before the
+	 * call to standard_ExecutorStart() but the stack item is required by
+	 * pgaudit_ExecutorCheckPerms_hook() which is called during
+	 * standard_ExecutorStart().
+	 */
+	if (stackItem)
+		MemoryContextSetParent(stackItem->contextAudit,
+							   queryDesc->estate->es_query_cxt);
+}
+
+/*
+ * Hook ExecutorCheckPerms to do session and object auditing for DML.
+ */
+static bool
+pgaudit_ExecutorCheckPerms_hook(List *rangeTabls, bool abort)
+{
+	Oid auditOid;
+
+	/* Get the audit oid if the role exists */
+	auditOid = get_role_oid(auditRole, true);
+
+	/* Log DML if the audit role is valid or session logging is enabled */
+	if ((auditOid != InvalidOid || auditLogBitmap != 0) &&
+		!IsAbortedTransactionBlockState())
+		log_select_dml(auditOid, rangeTabls);
+
+	/* Call the next hook function */
+	if (next_ExecutorCheckPerms_hook &&
+		!(*next_ExecutorCheckPerms_hook) (rangeTabls, abort))
+		return false;
+
+	return true;
+}
+
+/*
+ * Hook ProcessUtility to do session auditing for DDL and utility commands.
+ */
+static void
+pgaudit_ProcessUtility_hook(Node *parsetree,
+							 const char *queryString,
+							 ProcessUtilityContext context,
+							 ParamListInfo params,
+							 DestReceiver *dest,
+							 char *completionTag)
+{
+	AuditEventStackItem *stackItem = NULL;
+	int64 stackId = 0;
+
+	/*
+	 * Don't audit substatements.  All the substatements we care about should
+	 * be covered by the event triggers.
+	 */
+	if (context <= PROCESS_UTILITY_QUERY && !IsAbortedTransactionBlockState())
+	{
+		/* Process top level utility statement */
+		if (context == PROCESS_UTILITY_TOPLEVEL)
+		{
+			if (auditEventStack != NULL)
+				elog(ERROR, "pgaudit stack is not empty");
+
+			stackItem = stack_push();
+			stackItem->auditEvent.paramList = params;
+		}
+		else
+			stackItem = stack_push();
+
+		stackId = stackItem->stackId;
+		stackItem->auditEvent.logStmtLevel = GetCommandLogLevel(parsetree);
+		stackItem->auditEvent.commandTag = nodeTag(parsetree);
+		stackItem->auditEvent.command = CreateCommandTag(parsetree);
+		stackItem->auditEvent.commandText = queryString;
+
+		/*
+		 * If this is a DO block log it before calling the next ProcessUtility
+		 * hook.
+		 */
+		if (auditLogBitmap & LOG_FUNCTION &&
+			stackItem->auditEvent.commandTag == T_DoStmt &&
+			!IsAbortedTransactionBlockState())
+			log_audit_event(stackItem);
+	}
+
+	/* Call the standard process utility chain. */
+	if (next_ProcessUtility_hook)
+		(*next_ProcessUtility_hook) (parsetree, queryString, context,
+									 params, dest, completionTag);
+	else
+		standard_ProcessUtility(parsetree, queryString, context,
+								params, dest, completionTag);
+
+	/*
+	 * Process the audit event if there is one.  Also check that this event
+	 * was not popped off the stack by a memory context being free'd
+	 * elsewhere.
+	 */
+	if (stackItem && !IsAbortedTransactionBlockState())
+	{
+		/*
+		 * Make sure the item we want to log is still on the stack - if not
+		 * then something has gone wrong and an error will be raised.
+		 */
+		stack_valid(stackId);
+
+		/*
+		 * Log the utility command if logging is on, the command has not
+		 * already been logged by another hook, and the transaction is not
+		 * aborted.
+		 */
+		if (auditLogBitmap != 0 && !stackItem->auditEvent.logged)
+			log_audit_event(stackItem);
+	}
+}
+
+/*
+ * Hook object_access_hook to provide fully-qualified object names for function
+ * calls.
+ */
+static void
+pgaudit_object_access_hook(ObjectAccessType access,
+						   Oid classId,
+						   Oid objectId,
+						   int subId,
+						   void *arg)
+{
+	if (auditLogBitmap & LOG_FUNCTION && access == OAT_FUNCTION_EXECUTE &&
+		auditEventStack && !IsAbortedTransactionBlockState())
+		log_function_execute(objectId);
+
+	if (next_object_access_hook)
+		(*next_object_access_hook) (access, classId, objectId, subId, arg);
+}
+
+/*
+ * Event trigger functions
+ */
+
+/*
+ * Supply additional data for (non drop) statements that have event trigger
+ * support and can be deparsed.
+ *
+ * Drop statements are handled below through the older sql_drop event trigger.
+ */
+Datum
+pgaudit_ddl_command_end(PG_FUNCTION_ARGS)
+{
+	EventTriggerData *eventData;
+	int result,
+		row;
+	TupleDesc spiTupDesc;
+	const char *query;
+	MemoryContext contextQuery;
+	MemoryContext contextOld;
+
+	/* Continue only if session DDL logging is enabled */
+	if (~auditLogBitmap & LOG_DDL && ~auditLogBitmap & LOG_ROLE)
+		PG_RETURN_NULL();
+
+	/* Be sure the module was loaded */
+	if (!auditEventStack)
+		elog(ERROR, "pgaudit not loaded before call to "
+			 "pgaudit_ddl_command_end()");
+
+	/* This is an internal statement - do not log it */
+	internalStatement = true;
+
+	/* Make sure the fuction was fired as a trigger */
+	if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+		elog(ERROR, "not fired by event trigger manager");
+
+	/* Switch memory context for query */
+	contextQuery = AllocSetContextCreate(
+						CurrentMemoryContext,
+						"pgaudit_func_ddl_command_end temporary context",
+						ALLOCSET_DEFAULT_MINSIZE,
+						ALLOCSET_DEFAULT_INITSIZE,
+						ALLOCSET_DEFAULT_MAXSIZE);
+	contextOld = MemoryContextSwitchTo(contextQuery);
+
+	/* Get information about triggered events */
+	eventData = (EventTriggerData *) fcinfo->context;
+
+	auditEventStack->auditEvent.logStmtLevel =
+		GetCommandLogLevel(eventData->parsetree);
+	auditEventStack->auditEvent.commandTag =
+		nodeTag(eventData->parsetree);
+	auditEventStack->auditEvent.command =
+		CreateCommandTag(eventData->parsetree);
+
+	/* Return objects affected by the (non drop) DDL statement */
+	query = "SELECT UPPER(object_type), object_identity, UPPER(command_tag)\n"
+			"  FROM pg_catalog.pg_event_trigger_ddl_commands()";
+
+	/* Attempt to connect */
+	result = SPI_connect();
+	if (result < 0)
+		elog(ERROR, "pgaudit_ddl_command_end: SPI_connect returned %d",
+			 result);
+
+	/* Execute the query */
+	result = SPI_execute(query, true, 0);
+	if (result != SPI_OK_SELECT)
+		elog(ERROR, "pgaudit_ddl_command_end: SPI_execute returned %d",
+			 result);
+
+	/* Iterate returned rows */
+	spiTupDesc = SPI_tuptable->tupdesc;
+	for (row = 0; row < SPI_processed; row++)
+	{
+		HeapTuple	spiTuple;
+
+		spiTuple = SPI_tuptable->vals[row];
+
+		/* Supply object name and type for audit event */
+		auditEventStack->auditEvent.objectType =
+			SPI_getvalue(spiTuple, spiTupDesc, 1);
+		auditEventStack->auditEvent.objectName =
+			SPI_getvalue(spiTuple, spiTupDesc, 2);
+		auditEventStack->auditEvent.command =
+			SPI_getvalue(spiTuple, spiTupDesc, 3);
+
+		/*
+		 * Identify grant/revoke commands - these are the only non-DDL class
+		 * commands that should be coming through the event triggers.
+		 */
+		if (pg_strcasecmp(auditEventStack->auditEvent.command,
+						  COMMAND_GRANT) == 0 ||
+			pg_strcasecmp(auditEventStack->auditEvent.command,
+						  COMMAND_REVOKE) == 0)
+		{
+			NodeTag currentCommandTag = auditEventStack->auditEvent.commandTag;
+
+			auditEventStack->auditEvent.commandTag = T_GrantStmt;
+			log_audit_event(auditEventStack);
+
+			auditEventStack->auditEvent.commandTag = currentCommandTag;
+		}
+		else
+			log_audit_event(auditEventStack);
+	}
+
+	/* Complete the query */
+	SPI_finish();
+
+	MemoryContextSwitchTo(contextOld);
+	MemoryContextDelete(contextQuery);
+
+	/* No longer in an internal statement */
+	internalStatement = false;
+
+	PG_RETURN_NULL();
+}
+
+/*
+ * Supply additional data for drop statements that have event trigger support.
+ */
+Datum
+pgaudit_sql_drop(PG_FUNCTION_ARGS)
+{
+	int result,
+		row;
+	TupleDesc spiTupDesc;
+	const char *query;
+	MemoryContext contextQuery;
+	MemoryContext contextOld;
+
+	if (~auditLogBitmap & LOG_DDL)
+		PG_RETURN_NULL();
+
+	/* Be sure the module was loaded */
+	if (!auditEventStack)
+		elog(ERROR, "pgaudit not loaded before call to "
+			 "pgaudit_sql_drop()");
+
+	/* This is an internal statement - do not log it */
+	internalStatement = true;
+
+	/* Make sure the fuction was fired as a trigger */
+	if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+		elog(ERROR, "not fired by event trigger manager");
+
+	/* Switch memory context for the query */
+	contextQuery = AllocSetContextCreate(
+						CurrentMemoryContext,
+						"pgaudit_func_ddl_command_end temporary context",
+						ALLOCSET_DEFAULT_MINSIZE,
+						ALLOCSET_DEFAULT_INITSIZE,
+						ALLOCSET_DEFAULT_MAXSIZE);
+	contextOld = MemoryContextSwitchTo(contextQuery);
+
+	/* Return objects affected by the drop statement */
+	query = "SELECT UPPER(object_type),\n"
+			"       object_identity\n"
+			"  FROM pg_catalog.pg_event_trigger_dropped_objects()\n"
+			" WHERE lower(object_type) <> 'type'\n"
+			"   AND schema_name <> 'pg_toast'";
+
+	/* Attempt to connect */
+	result = SPI_connect();
+	if (result < 0)
+		elog(ERROR, "pgaudit_ddl_drop: SPI_connect returned %d",
+			 result);
+
+	/* Execute the query */
+	result = SPI_execute(query, true, 0);
+	if (result != SPI_OK_SELECT)
+		elog(ERROR, "pgaudit_ddl_drop: SPI_execute returned %d",
+			 result);
+
+	/* Iterate returned rows */
+	spiTupDesc = SPI_tuptable->tupdesc;
+	for (row = 0; row < SPI_processed; row++)
+	{
+		HeapTuple spiTuple;
+
+		spiTuple = SPI_tuptable->vals[row];
+
+		auditEventStack->auditEvent.objectType =
+			SPI_getvalue(spiTuple, spiTupDesc, 1);
+		auditEventStack->auditEvent.objectName =
+			SPI_getvalue(spiTuple, spiTupDesc, 2);
+
+		log_audit_event(auditEventStack);
+	}
+
+	/* Complete the query */
+	SPI_finish();
+
+	MemoryContextSwitchTo(contextOld);
+	MemoryContextDelete(contextQuery);
+
+	/* No longer in an internal statement */
+	internalStatement = false;
+
+	PG_RETURN_NULL();
+}
+
+/*
+ * GUC check and assign functions
+ */
+
+/*
+ * Take a pgaudit.log value such as "read, write, dml", verify that each of the
+ * comma-separated tokens corresponds to a LogClass value, and convert them into
+ * a bitmap that log_audit_event can check.
+ */
+static bool
+check_pgaudit_log(char **newVal, void **extra, GucSource source)
+{
+	List *flagRawList;
+	char *rawVal;
+	ListCell *lt;
+	int *flags;
+
+	/* Make sure newval is a comma-separated list of tokens. */
+	rawVal = pstrdup(*newVal);
+	if (!SplitIdentifierString(rawVal, ',', &flagRawList))
+	{
+		GUC_check_errdetail("List syntax is invalid");
+		list_free(flagRawList);
+		pfree(rawVal);
+		return false;
+	}
+
+	/*
+	 * Check that we recognise each token, and add it to the bitmap we're
+	 * building up in a newly-allocated int *f.
+	 */
+	if (!(flags = (int *) malloc(sizeof(int))))
+		return false;
+
+	*flags = 0;
+
+	foreach(lt, flagRawList)
+	{
+		char *token = (char *) lfirst(lt);
+		bool subtract = false;
+		int class;
+
+		/* If token is preceded by -, then the token is subtractive */
+		if (token[0] == '-')
+		{
+			token++;
+			subtract = true;
+		}
+
+		/* Test each token */
+		if (pg_strcasecmp(token, CLASS_NONE) == 0)
+			class = LOG_NONE;
+		else if (pg_strcasecmp(token, CLASS_ALL) == 0)
+			class = LOG_ALL;
+		else if (pg_strcasecmp(token, CLASS_DDL) == 0)
+			class = LOG_DDL;
+		else if (pg_strcasecmp(token, CLASS_FUNCTION) == 0)
+			class = LOG_FUNCTION;
+		else if (pg_strcasecmp(token, CLASS_MISC) == 0)
+			class = LOG_MISC;
+		else if (pg_strcasecmp(token, CLASS_READ) == 0)
+			class = LOG_READ;
+		else if (pg_strcasecmp(token, CLASS_ROLE) == 0)
+			class = LOG_ROLE;
+		else if (pg_strcasecmp(token, CLASS_WRITE) == 0)
+			class = LOG_WRITE;
+		else
+		{
+			free(flags);
+			pfree(rawVal);
+			list_free(flagRawList);
+			return false;
+		}
+
+		/* Add or subtract class bits from the log bitmap */
+		if (subtract)
+			*flags &= ~class;
+		else
+			*flags |= class;
+	}
+
+	pfree(rawVal);
+	list_free(flagRawList);
+
+	/* Store the bitmap for assign_pgaudit_log */
+	*extra = flags;
+
+	return true;
+}
+
+/*
+ * Set pgaudit_log from extra (ignoring newVal, which has already been
+ * converted to a bitmap above). Note that extra may not be set if the
+ * assignment is to be suppressed.
+ */
+static void
+assign_pgaudit_log(const char *newVal, void *extra)
+{
+	if (extra)
+		auditLogBitmap = *(int *) extra;
+}
+
+/*
+ * Take a pgaudit.log_level value such as "debug" and check that is is valid.
+ * Return the enum value so it does not have to be checked again in the assign
+ * function.
+ */
+static bool
+check_pgaudit_log_level(char **newVal, void **extra, GucSource source)
+{
+	int *logLevel;
+
+	/* Allocate memory to store the log level */
+	if (!(logLevel = (int *) malloc(sizeof(int))))
+		return false;
+
+	/* Find the log level enum */
+	if (pg_strcasecmp(*newVal, "debug") == 0)
+		*logLevel = DEBUG2;
+	else if (pg_strcasecmp(*newVal, "debug5") == 0)
+		*logLevel = DEBUG5;
+	else if (pg_strcasecmp(*newVal, "debug4") == 0)
+		*logLevel = DEBUG4;
+	else if (pg_strcasecmp(*newVal, "debug3") == 0)
+		*logLevel = DEBUG3;
+	else if (pg_strcasecmp(*newVal, "debug2") == 0)
+		*logLevel = DEBUG2;
+	else if (pg_strcasecmp(*newVal, "debug1") == 0)
+		*logLevel = DEBUG1;
+	else if (pg_strcasecmp(*newVal, "info") == 0)
+		*logLevel = INFO;
+	else if (pg_strcasecmp(*newVal, "notice") == 0)
+		*logLevel = NOTICE;
+	else if (pg_strcasecmp(*newVal, "warning") == 0)
+		*logLevel = WARNING;
+	else if (pg_strcasecmp(*newVal, "log") == 0)
+		*logLevel = LOG;
+
+	/* Error if the log level enum is not found */
+	else
+	{
+		free(logLevel);
+		return false;
+	}
+
+	/* Return the log level enum */
+	*extra = logLevel;
+
+	return true;
+}
+
+/*
+ * Set pgaudit_log from extra (ignoring newVal, which has already been
+ * converted to an enum above). Note that extra may not be set if the
+ * assignment is to be suppressed.
+ */
+static void
+assign_pgaudit_log_level(const char *newVal, void *extra)
+{
+	if (extra)
+		auditLogLevel = *(int *) extra;
+}
+
+/*
+ * Define GUC variables and install hooks upon module load.
+ */
+void
+_PG_init(void)
+{
+	/* Must be loaded with shared_preload_libaries */
+	if (IsUnderPostmaster)
+		ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+				errmsg("pgaudit must be loaded via shared_preload_libraries")));
+
+	/* Define pgaudit.log */
+	DefineCustomStringVariable(
+		"pgaudit.log",
+
+		"Specifies which classes of statements will be logged by session audit "
+		"logging. Multiple classes can be provided using a comma-separated "
+		"list and classes can be subtracted by prefacing the class with a "
+		"- sign.",
+
+		NULL,
+		&auditLog,
+		"none",
+		PGC_SUSET,
+		GUC_LIST_INPUT | GUC_NOT_IN_SAMPLE,
+		check_pgaudit_log,
+		assign_pgaudit_log,
+		NULL);
+
+	/* Define pgaudit.log_catalog */
+	DefineCustomBoolVariable(
+		"pgaudit.log_catalog",
+
+		"Specifies that session logging should be enabled in the case where "
+		"all relations in a statement are in pg_catalog.  Disabling this "
+		 "setting will reduce noise in the log from tools like psql and PgAdmin "
+		"that query the catalog heavily.",
+
+		NULL,
+		&auditLogCatalog,
+		true,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_client */
+	DefineCustomBoolVariable(
+		"pgaudit.log_client",
+
+		"Specifies whether audit messages should be visible to the client. "
+		"This setting should generally be left disabled but may be useful for "
+		"debugging or other purposes.",
+
+		NULL,
+		&auditLogClient,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_level */
+	DefineCustomStringVariable(
+		"pgaudit.log_level",
+
+		"Specifies the log level that will be used for log entries. This "
+		"setting is used for regression testing and may also be useful to end "
+		"users for testing or other purposes.  It is not intended to be used "
+		"in a production environment as it may leak which statements are being "
+		"logged to the user.",
+
+		NULL,
+		&auditLogLevelString,
+		"log",
+		PGC_SUSET,
+		GUC_LIST_INPUT | GUC_NOT_IN_SAMPLE,
+		check_pgaudit_log_level,
+		assign_pgaudit_log_level,
+		NULL);
+
+	/* Define pgaudit.log_parameter */
+	DefineCustomBoolVariable(
+		"pgaudit.log_parameter",
+
+		"Specifies that audit logging should include the parameters that were "
+		"passed with the statement. When parameters are present they will be "
+		"be included in CSV format after the statement text.",
+
+		NULL,
+		&auditLogParameter,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_relation */
+	DefineCustomBoolVariable(
+		"pgaudit.log_relation",
+
+		"Specifies whether session audit logging should create a separate log "
+		"entry for each relation referenced in a SELECT or DML statement. "
+		"This is a useful shortcut for exhaustive logging without using object "
+		"audit logging.",
+
+		NULL,
+		&auditLogRelation,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_statement_once */
+	DefineCustomBoolVariable(
+		"pgaudit.log_statement_once",
+
+		"Specifies whether logging will include the statement text and "
+		"parameters with the first log entry for a statement/substatement "
+		"combination or with every entry.  Disabling this setting will result "
+		"in less verbose logging but may make it more difficult to determine "
+		"the statement that generated a log entry, though the "
+		"statement/substatement pair along with the process id should suffice "
+		"to identify the statement text logged with a previous entry.",
+
+		NULL,
+		&auditLogStatementOnce,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.role */
+	DefineCustomStringVariable(
+		"pgaudit.role",
+
+		"Specifies the master role to use for object audit logging. Multiple "
+		"audit roles can be defined by granting them to the master role. This "
+		"allows multiple groups to be in charge of different aspects of audit "
+		"logging.",
+
+		NULL,
+		&auditRole,
+		"",
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/*
+	 * Install our hook functions after saving the existing pointers to
+	 * preserve the chains.
+	 */
+	next_ExecutorStart_hook = ExecutorStart_hook;
+	ExecutorStart_hook = pgaudit_ExecutorStart_hook;
+
+	next_ExecutorCheckPerms_hook = ExecutorCheckPerms_hook;
+	ExecutorCheckPerms_hook = pgaudit_ExecutorCheckPerms_hook;
+
+	next_ProcessUtility_hook = ProcessUtility_hook;
+	ProcessUtility_hook = pgaudit_ProcessUtility_hook;
+
+	next_object_access_hook = object_access_hook;
+	object_access_hook = pgaudit_object_access_hook;
+
+	/* Log that the extension has completed initialization */
+	ereport(LOG, (errmsg("pgaudit extension initialized")));
+}
diff --git a/contrib/pgaudit/pgaudit.conf b/contrib/pgaudit/pgaudit.conf
new file mode 100644
index 0000000..cc6f09c
--- /dev/null
+++ b/contrib/pgaudit/pgaudit.conf
@@ -0,0 +1 @@
+shared_preload_libraries = pgaudit
diff --git a/contrib/pgaudit/pgaudit.control b/contrib/pgaudit/pgaudit.control
new file mode 100644
index 0000000..16612b2
--- /dev/null
+++ b/contrib/pgaudit/pgaudit.control
@@ -0,0 +1,5 @@
+# pgaudit extension
+comment = 'provides auditing functionality'
+default_version = '1.0'
+module_pathname = '$libdir/pgaudit'
+relocatable = true
diff --git a/contrib/pgaudit/sql/pgaudit.sql b/contrib/pgaudit/sql/pgaudit.sql
new file mode 100644
index 0000000..1c81d70
--- /dev/null
+++ b/contrib/pgaudit/sql/pgaudit.sql
@@ -0,0 +1,780 @@
+\set VERBOSITY terse
+
+-- Create pgaudit extension
+CREATE EXTENSION IF NOT EXISTS pgaudit;
+
+--
+-- Audit log fields are:
+--     AUDIT_TYPE - SESSION or OBJECT
+--     STATEMENT_ID - ID of the statement in the current backend
+--     SUBSTATEMENT_ID - ID of the substatement in the current backend
+--     CLASS - Class of statement being logged (e.g. ROLE, READ, WRITE)
+--     COMMAND - e.g. SELECT, CREATE ROLE, UPDATE
+--     OBJECT_TYPE - When available, type of object acted on (e.g. TABLE, VIEW)
+--     OBJECT_NAME - When available, fully-qualified table of object
+--     STATEMENT - The statement being logged
+--     PARAMETER - If parameter logging is requested, they will follow the
+--                 statement
+
+SELECT current_user \gset
+
+--
+-- Set pgaudit parameters for the current (super)user.
+ALTER ROLE :current_user SET pgaudit.log = 'Role';
+ALTER ROLE :current_user SET pgaudit.log_level = 'notice';
+ALTER ROLE :current_user SET pgaudit.log_client = ON;
+
+-- After each connect, we need to load pgaudit, as if it was
+-- being loaded from shared_preload_libraries.  Otherwise, the hooks
+-- won't be set up and called correctly, leading to lots of ugly
+-- errors.
+\connect - :current_user;
+
+--
+-- Create auditor role
+CREATE ROLE auditor;
+
+--
+-- Create first test user
+CREATE USER user1;
+ALTER ROLE user1 SET pgaudit.log = 'ddl, ROLE';
+ALTER ROLE user1 SET pgaudit.log_level = 'notice';
+ALTER ROLE user1 SET pgaudit.log_client = ON;
+
+--
+-- Create, select, drop (select will not be audited)
+\connect - user1
+
+CREATE TABLE public.test
+(
+	id INT
+);
+
+SELECT *
+  FROM test;
+
+DROP TABLE test;
+
+--
+-- Create second test user
+\connect - :current_user
+
+CREATE USER user2;
+ALTER ROLE user2 SET pgaudit.log = 'Read, writE';
+ALTER ROLE user2 SET pgaudit.log_catalog = OFF;
+ALTER ROLE user2 SET pgaudit.log_level = 'warning';
+ALTER ROLE user2 SET pgaudit.log_client = ON;
+ALTER ROLE user2 SET pgaudit.role = auditor;
+ALTER ROLE user2 SET pgaudit.log_statement_once = ON;
+
+--
+-- Setup role-based tests
+CREATE TABLE test2
+(
+	id INT
+);
+
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test2
+   TO user2, user1;
+
+GRANT SELECT, UPDATE
+   ON TABLE public.test2
+   TO auditor;
+
+CREATE TABLE test3
+(
+	id INT
+);
+
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test3
+   TO user2;
+
+GRANT INSERT
+   ON TABLE public.test3
+   TO auditor;
+
+CREATE FUNCTION test2_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 90
+	 WHERE id = new.id;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_insert() OWNER TO user1;
+
+CREATE TRIGGER test2_insert_trg
+	AFTER INSERT ON test2
+	FOR EACH ROW EXECUTE PROCEDURE test2_insert();
+
+CREATE FUNCTION test2_change(change_id int) RETURNS void AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 1
+	 WHERE id = change_id;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_change(int) OWNER TO user2;
+
+CREATE VIEW vw_test3 AS
+SELECT *
+  FROM test3;
+
+GRANT SELECT
+   ON vw_test3
+   TO user2;
+
+GRANT SELECT
+   ON vw_test3
+   TO auditor;
+
+\connect - user2
+
+--
+-- Role-based tests
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	  LIMIT 1
+) SUBQUERY;
+
+SELECT *
+  FROM test3, test2;
+
+--
+-- Object logged because of:
+-- select on vw_test3
+-- select on test2
+SELECT *
+  FROM vw_test3, test2;
+
+--
+-- Object logged because of:
+-- insert on test3
+-- select on test2
+WITH CTE AS
+(
+	SELECT id
+	  FROM test2
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+
+--
+-- Object logged because of:
+-- insert on test3
+WITH CTE AS
+(
+	INSERT INTO test3 VALUES (1)
+				   RETURNING id
+)
+INSERT INTO test2
+SELECT id
+  FROM cte;
+
+DO $$ BEGIN PERFORM test2_change(91); END $$;
+
+--
+-- Object logged because of:
+-- insert on test3
+-- update on test2
+WITH CTE AS
+(
+	UPDATE test2
+	   SET id = 45
+	 WHERE id = 92
+	RETURNING id
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+
+--
+-- Object logged because of:
+-- insert on test2
+WITH CTE AS
+(
+	INSERT INTO test2 VALUES (37)
+				   RETURNING id
+)
+UPDATE test3
+   SET id = cte.id
+  FROM cte
+ WHERE test3.id <> cte.id;
+
+--
+-- Be sure that test has correct contents
+SELECT *
+  FROM test2
+ ORDER BY ID;
+
+--
+-- Change permissions of user 2 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user2 SET pgaudit.log = 'NONE';
+
+\connect - user2
+
+--
+-- Create test4 and add permissions
+CREATE TABLE test4
+(
+	id int,
+	name text
+);
+
+GRANT SELECT (name)
+   ON TABLE public.test4
+   TO auditor;
+
+GRANT UPDATE (id)
+   ON TABLE public.test4
+   TO auditor;
+
+GRANT insert (name)
+   ON TABLE public.test4
+   TO auditor;
+
+--
+-- Not object logged
+SELECT id
+  FROM public.test4;
+
+--
+-- Object logged because of:
+-- select (name) on test4
+SELECT name
+  FROM public.test4;
+
+--
+-- Not object logged
+INSERT INTO public.test4 (id)
+				  VALUES (1);
+
+--
+-- Object logged because of:
+-- insert (name) on test4
+INSERT INTO public.test4 (name)
+				  VALUES ('test');
+
+--
+-- Not object logged
+UPDATE public.test4
+   SET name = 'foo';
+
+--
+-- Object logged because of:
+-- update (id) on test4
+UPDATE public.test4
+   SET id = 1;
+
+--
+-- Object logged because of:
+-- update (name) on test4
+-- update (name) takes precedence over select (name) due to ordering
+update public.test4 set name = 'foo' where name = 'bar';
+
+--
+-- Change permissions of user 1 so that session logging will be done
+\connect - :current_user
+
+--
+-- Drop test tables
+DROP TABLE test2;
+DROP VIEW vw_test3;
+DROP TABLE test3;
+DROP TABLE test4;
+DROP FUNCTION test2_insert();
+DROP FUNCTION test2_change(int);
+
+ALTER ROLE user1 SET pgaudit.log = 'DDL, READ';
+\connect - user1
+
+--
+-- Create table is session logged
+CREATE TABLE public.account
+(
+	id INT,
+	name TEXT,
+	password TEXT,
+	description TEXT
+);
+
+--
+-- Select is session logged
+SELECT *
+  FROM account;
+
+--
+-- Insert is not logged
+INSERT INTO account (id, name, password, description)
+			 VALUES (1, 'user1', 'HASH1', 'blah, blah');
+
+--
+-- Change permissions of user 1 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log = 'none';
+ALTER ROLE user1 SET pgaudit.role = 'auditor';
+\connect - user1
+
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT (password),
+	  UPDATE (name, password)
+   ON TABLE public.account
+   TO auditor;
+
+--
+-- Not object logged
+SELECT id,
+	   name
+  FROM account;
+
+--
+-- Object logged because of:
+-- select (password) on account
+SELECT password
+  FROM account;
+
+--
+-- Not object logged
+UPDATE account
+   SET description = 'yada, yada';
+
+--
+-- Object logged because of:
+-- update (password) on account
+UPDATE account
+   SET password = 'HASH2';
+
+--
+-- Change permissions of user 1 so that session relation logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log_relation = on;
+ALTER ROLE user1 SET pgaudit.log = 'read, WRITE';
+\connect - user1
+
+--
+-- Not logged
+CREATE TABLE ACCOUNT_ROLE_MAP
+(
+	account_id INT,
+	role_id INT
+);
+
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT
+   ON TABLE public.account_role_map
+   TO auditor;
+
+--
+-- Object logged because of:
+-- select (password) on account
+-- select on account_role_map
+-- Session logged on all tables because log = read and log_relation = on
+SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;
+
+--
+-- Object logged because of:
+-- select (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+SELECT password
+  FROM account;
+
+--
+-- Not object logged
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada';
+
+--
+-- Object logged because of:
+-- select (password) on account (in the where clause)
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';
+
+--
+-- Object logged because of:
+-- update (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET password = 'HASH2';
+
+--
+-- Change back to superuser to do exhaustive tests
+\connect - :current_user
+SET pgaudit.log = 'ALL';
+SET pgaudit.log_level = 'notice';
+SET pgaudit.log_client = ON;
+SET pgaudit.log_relation = ON;
+SET pgaudit.log_parameter = ON;
+
+--
+-- Simple DO block
+DO $$
+BEGIN
+	raise notice 'test';
+END $$;
+
+--
+-- Create test schema
+CREATE SCHEMA test;
+
+--
+-- Copy account to stdout
+COPY account TO stdout;
+
+--
+-- Create a table from a query
+CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;
+
+--
+-- Copy from stdin to account copy
+COPY test.account_copy from stdin;
+1	user1	HASH2	yada, yada
+\.
+
+--
+-- Test prepared statement
+PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;
+
+EXECUTE pgclassstmt (1);
+DEALLOCATE pgclassstmt;
+
+--
+-- Test cursor
+BEGIN;
+
+DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+
+FETCH NEXT FROM ctest;
+CLOSE ctest;
+COMMIT;
+
+--
+-- Turn off log_catalog and pg_class will not be logged
+SET pgaudit.log_catalog = OFF;
+
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+
+--
+-- Test prepared insert
+CREATE TABLE test.test_insert
+(
+	id INT
+);
+
+PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);
+EXECUTE pgclassstmt (1);
+
+--
+-- Check that primary key creation is logged
+CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);
+
+--
+-- Check that analyze is logged
+ANALYZE test;
+
+--
+-- Grants to public should not cause object logging (session logging will
+-- still happen)
+GRANT SELECT
+  ON TABLE public.test
+  TO PUBLIC;
+
+SELECT *
+  FROM test;
+
+-- Check that statements without columns log
+SELECT
+  FROM test;
+
+SELECT 1,
+	   substring('Thomas' from 2 for 3);
+
+DO $$
+DECLARE
+	test INT;
+BEGIN
+	SELECT 1
+	  INTO test;
+END $$;
+
+explain select 1;
+
+--
+-- Test that looks inside of do blocks log
+INSERT INTO TEST (id)
+		  VALUES (1);
+INSERT INTO TEST (id)
+		  VALUES (2);
+INSERT INTO TEST (id)
+		  VALUES (3);
+
+DO $$
+DECLARE
+	result RECORD;
+BEGIN
+	FOR result IN
+		SELECT id
+		  FROM test
+	LOOP
+		INSERT INTO test (id)
+			 VALUES (result.id + 100);
+	END LOOP;
+END $$;
+
+--
+-- Test obfuscated dynamic sql for clean logging
+DO $$
+DECLARE
+	table_name TEXT = 'do_table';
+BEGIN
+	EXECUTE 'CREATE TABLE ' || table_name || ' ("weird name" INT)';
+	EXECUTE 'DROP table ' || table_name;
+END $$;
+
+--
+-- Generate an error and make sure the stack gets cleared
+DO $$
+BEGIN
+	CREATE TABLE bogus.test_block
+	(
+		id INT
+	);
+END $$;
+
+--
+-- Test alter table statements
+ALTER TABLE public.test
+	DROP COLUMN description ;
+
+ALTER TABLE public.test
+	RENAME TO test2;
+
+ALTER TABLE public.test2
+	SET SCHEMA test;
+
+ALTER TABLE test.test2
+	ADD COLUMN description TEXT;
+
+ALTER TABLE test.test2
+	DROP COLUMN description;
+
+DROP TABLE test.test2;
+
+--
+-- Test multiple statements with one semi-colon
+CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);
+
+--
+-- Test aggregate
+CREATE FUNCTION public.int_add
+(
+	a INT,
+	b INT
+)
+	RETURNS INT LANGUAGE plpgsql AS $$
+BEGIN
+	return a + b;
+END $$;
+
+SELECT int_add(1, 1);
+
+CREATE AGGREGATE public.sum_test(INT) (SFUNC=public.int_add, STYPE=INT, INITCOND='0');
+ALTER AGGREGATE public.sum_test(integer) RENAME TO sum_test2;
+
+--
+-- Test conversion
+CREATE CONVERSION public.conversion_test FOR 'SQL_ASCII' TO 'MULE_INTERNAL' FROM pg_catalog.ascii_to_mic;
+ALTER CONVERSION public.conversion_test RENAME TO conversion_test2;
+
+--
+-- Test create/alter/drop database
+CREATE DATABASE contrib_regression_pgaudit;
+ALTER DATABASE contrib_regression_pgaudit RENAME TO contrib_regression_pgaudit2;
+DROP DATABASE contrib_regression_pgaudit2;
+
+-- Test role as a substmt
+SET pgaudit.log = 'ROLE';
+
+CREATE TABLE t ();
+CREATE ROLE alice;
+
+CREATE SCHEMA foo2
+	GRANT SELECT
+	   ON public.t
+	   TO alice;
+
+drop table public.t;
+drop role alice;
+
+--
+-- Test that frees a memory context earlier than expected
+SET pgaudit.log = 'ALL';
+
+CREATE TABLE hoge
+(
+	id int
+);
+
+CREATE FUNCTION test()
+	RETURNS INT AS $$
+DECLARE
+	cur1 cursor for select * from hoge;
+	tmp int;
+BEGIN
+	OPEN cur1;
+	FETCH cur1 into tmp;
+	RETURN tmp;
+END $$
+LANGUAGE plpgsql ;
+
+SELECT test();
+
+--
+-- Delete all rows then delete 1 row
+SET pgaudit.log = 'write';
+SET pgaudit.role = 'auditor';
+
+create table bar
+(
+	col int
+);
+
+grant delete
+   on bar
+   to auditor;
+
+insert into bar (col)
+		 values (1);
+delete from bar;
+
+insert into bar (col)
+		 values (1);
+delete from bar
+ where col = 1;
+
+drop table bar;
+
+--
+-- Grant roles to each other
+SET pgaudit.log = 'role';
+GRANT user1 TO user2;
+REVOKE user1 FROM user2;
+
+--
+-- Test that FK references do not log but triggers still do
+SET pgaudit.log = 'READ,WRITE';
+SET pgaudit.role TO 'auditor';
+SET pgaudit.log_parameter TO OFF;
+
+CREATE TABLE aaa
+(
+	ID int primary key
+);
+
+CREATE TABLE bbb
+(
+	id int
+		references aaa(id)
+);
+
+CREATE FUNCTION bbb_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE bbb set id = new.id + 1;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql;
+
+CREATE TRIGGER bbb_insert_trg
+	AFTER INSERT ON bbb
+	FOR EACH ROW EXECUTE PROCEDURE bbb_insert();
+
+GRANT SELECT
+   ON aaa
+   TO auditor;
+
+GRANT UPDATE
+   ON bbb
+   TO auditor;
+
+INSERT INTO aaa VALUES (generate_series(1,100));
+INSERT INTO bbb VALUES (1);
+
+DROP TABLE bbb;
+DROP TABLE aaa;
+
+-- Cleanup
+-- Set client_min_messages up to warning to avoid noise
+SET client_min_messages = 'warning';
+
+ALTER ROLE :current_user RESET pgaudit.log;
+ALTER ROLE :current_user RESET pgaudit.log_catalog;
+ALTER ROLE :current_user RESET pgaudit.log_level;
+ALTER ROLE :current_user RESET pgaudit.log_parameter;
+ALTER ROLE :current_user RESET pgaudit.log_relation;
+ALTER ROLE :current_user RESET pgaudit.log_statement_once;
+ALTER ROLE :current_user RESET pgaudit.role;
+
+RESET pgaudit.log;
+RESET pgaudit.log_catalog;
+RESET pgaudit.log_level;
+RESET pgaudit.log_parameter;
+RESET pgaudit.log_relation;
+RESET pgaudit.log_statement_once;
+RESET pgaudit.role;
+
+DROP TABLE test.account_copy;
+DROP TABLE test.test_insert;
+DROP SCHEMA test;
+DROP TABLE foo.bar;
+DROP TABLE foo.baz;
+DROP SCHEMA foo;
+DROP TABLE hoge;
+DROP TABLE account;
+DROP TABLE account_role_map;
+DROP USER user2;
+DROP USER user1;
+DROP ROLE auditor;
+
+RESET client_min_messages;
#2Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: David Steele (#1)
Re: PostgreSQL Audit Extension

David Steele wrote:

The attached patch implements audit logging for PostgreSQL as an
extension. I believe I have addressed the concerns that were raised at
the end of the 9.5 development cycle.

This patch got no feedback at all during the commitfest. I think there
is some interest on auditing capabilities so it's annoying and
surprising that this has no movement at all.

If the lack of activity means lack of interest, please can we all keep
what we've been doing in this thread, which is to ignore it, so that we
can just mark this as rejected. Otherwise, please chime in. I'm giving
this patch some more time by moving it to next commitfest instead.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Joshua D. Drake
jd@commandprompt.com
In reply to: Alvaro Herrera (#2)
Re: PostgreSQL Audit Extension

On 01/31/2016 05:07 AM, Alvaro Herrera wrote:

David Steele wrote:

The attached patch implements audit logging for PostgreSQL as an
extension. I believe I have addressed the concerns that were raised at
the end of the 9.5 development cycle.

This patch got no feedback at all during the commitfest. I think there
is some interest on auditing capabilities so it's annoying and
surprising that this has no movement at all.

If the lack of activity means lack of interest, please can we all keep
what we've been doing in this thread, which is to ignore it, so that we
can just mark this as rejected. Otherwise, please chime in. I'm giving
this patch some more time by moving it to next commitfest instead.

From my perspective, lack of activity means since it doesn't have a
technical requirement to be in -core, it doesn't need to be.

That said, it is certainly true that people express interest in auditing
(all the time). So I think we need to either punt it as something that
is going to be an external extension or someone needs to pick it up.

It used to be that there was a philosophy of we built what people need
to build other things. This is why replication for the longest time
wasn't incorporated. If that is still our philosophy I don't even know
why the audit extension is still a question.

Sincerely,

JD

--
Command Prompt, Inc. http://the.postgres.company/
+1-503-667-4564
PostgreSQL Centered full stack support, consulting and development.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Joshua D. Drake (#3)
Re: PostgreSQL Audit Extension

Joshua D. Drake wrote:

On 01/31/2016 05:07 AM, Alvaro Herrera wrote:

David Steele wrote:

The attached patch implements audit logging for PostgreSQL as an
extension. I believe I have addressed the concerns that were raised at
the end of the 9.5 development cycle.

This patch got no feedback at all during the commitfest. I think there
is some interest on auditing capabilities so it's annoying and
surprising that this has no movement at all.

If the lack of activity means lack of interest, please can we all keep
what we've been doing in this thread, which is to ignore it, so that we
can just mark this as rejected. Otherwise, please chime in. I'm giving
this patch some more time by moving it to next commitfest instead.

From my perspective, lack of activity means since it doesn't have a
technical requirement to be in -core, it doesn't need to be.

Well, from mine it doesn't mean that. We kind of assume that "no answer
means yes"; but here what it really means is that nobody had time to
look at it and think about it, so it stalled (and so have many other
patches BTW). But if you or others think that this patch belongs into
PGXN, it's good to have that opinion in an email, so that the author can
think about your perspective and agree or disagree with it. Just by
expression that opinion, a thousand other hackers might vote +1 or -1 on
your proposal -- either way a clear sign. Silence doesn't let us move
forward, and we punt the patch to the next CF and let inertia continue.
That's not good.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#5David Steele
david@pgmasters.net
In reply to: Alvaro Herrera (#2)
Re: PostgreSQL Audit Extension

On 1/31/16 8:07 AM, Alvaro Herrera wrote:

David Steele wrote:

The attached patch implements audit logging for PostgreSQL as an
extension. I believe I have addressed the concerns that were raised at
the end of the 9.5 development cycle.

This patch got no feedback at all during the commitfest. I think there
is some interest on auditing capabilities so it's annoying and
surprising that this has no movement at all.

Yes, especially since I've had queries at conferences, by email, and
once on this list asking if it would be submitted again. I would not
have taken the time to prepare it if I hadn't been getting positive
feedback.

If the lack of activity means lack of interest, please can we all keep
what we've been doing in this thread, which is to ignore it, so that we
can just mark this as rejected.

If nobody comments or reviews then that would be the best course of action.

I'm giving
this patch some more time by moving it to next commitfest instead.

I was under the mistaken impression that the CF was two months long so I
appreciate getting more time.

Thanks,
--
-David
david@pgmasters.net

#6Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: David Steele (#5)
Re: PostgreSQL Audit Extension

David Steele wrote:

On 1/31/16 8:07 AM, Alvaro Herrera wrote:

I'm giving
this patch some more time by moving it to next commitfest instead.

I was under the mistaken impression that the CF was two months long so I
appreciate getting a little more time.

Yeah, sorry to disappoint but my intention is to close shop as soon as
possible. Commitfests are supposed to last one month only, leaving the
month before the start of the next one free so that committers and
reviewers can enjoy life.

Anyway, what would *you* do with any additional time? It's reviewers
that need to chime in when a patch is in needs-review state. You
already did your part by submitting. Anyway I think the tests here are
massive and the code is not; perhaps people get the mistaken impression
that this is a huge amount of code which scares them. Perhaps you could
split it up in (1) code and (2) tests, which wouldn't achieve any
technical benefit but would offer some psychological comfort to
potential reviewers. You know it's all psychology in these parts.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#6)
Re: PostgreSQL Audit Extension

Alvaro Herrera <alvherre@2ndquadrant.com> writes:

.... Anyway I think the tests here are
massive and the code is not; perhaps people get the mistaken impression
that this is a huge amount of code which scares them. Perhaps you could
split it up in (1) code and (2) tests, which wouldn't achieve any
technical benefit but would offer some psychological comfort to
potential reviewers. You know it's all psychology in these parts.

Perhaps the tests could be made less bulky. We do not need massive
permanent regression tests for a single feature, IMO.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#8David Steele
david@pgmasters.net
In reply to: David Steele (#5)
3 attachment(s)
Re: PostgreSQL Audit Extension

[I pulled this back in from the other thread that got started. This
happened because my email to pgsql-hackers bounced but was also sent to
Alvaro who replied to that message not the later one that went through
to hackers.]

On 2/1/16 3:59 PM, Alvaro Herrera wrote:

Anyway, what would *you* do with any additional time? It's reviewers
that need to chime in when a patch is in needs-review state. You
already did your part by submitting. Anyway I think the tests
here are massive and the code is not; perhaps people get the mistaken
impression that this is a huge amount of code which scares them.
Perhaps you could split it up in (1) code and (2) tests, which
wouldn't achieve any technical benefit but would offer some
psychological comfort to potential reviewers.

Good suggestion, so that's exactly what I've done. Attached are a set
of patches that apply cleanly against 1d0c3b3.

* 01: Add errhidefromclient() macro for ereport

This is the same patch that I submitted here:

https://commitfest.postgresql.org/9/431/

but since pgaudit requires it I added it to this set for convenience.

* 02: The pgaudit code

And make file, etc. Everything needed to get it compiled and installed
as an extension.

* 03: Docs and regression tests

To run regression tests this patch will need to be applied. They have
been separated to make the code patch not look so large and hopefully
encourage reviews.

--
-David
david@pgmasters.net

Attachments:

pgaudit-v2-01.patchtext/plain; charset=UTF-8; name=pgaudit-v2-01.patch; x-mac-creator=0; x-mac-type=0Download
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 9005b26..c53ef95 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -1091,6 +1091,23 @@ errhidecontext(bool hide_ctx)
 	return 0;					/* return value does not matter */
 }
 
+/*
+ * errhidefromclient --- optionally suppress output of message to client
+ *
+ * Only log levels below ERROR can be hidden from the client.
+ */
+int
+errhidefromclient(bool hide_from_client)
+{
+	ErrorData  *edata = &errordata[errordata_stack_depth];
+
+	/* we don't bother incrementing recursion_depth */
+	CHECK_STACK_DEPTH();
+
+	edata->hide_from_client = hide_from_client;
+
+	return 0;					/* return value does not matter */
+}
 
 /*
  * errfunction --- add reporting function name to the current error
@@ -1467,7 +1484,7 @@ EmitErrorReport(void)
 		send_message_to_server_log(edata);
 
 	/* Send to client, if enabled */
-	if (edata->output_to_client)
+	if (edata->output_to_client && !edata->hide_from_client)
 		send_message_to_frontend(edata);
 
 	MemoryContextSwitchTo(oldcontext);
diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h
index 326896f..14b87b7 100644
--- a/src/include/utils/elog.h
+++ b/src/include/utils/elog.h
@@ -179,6 +179,7 @@ extern int	errcontext_msg(const char *fmt,...) pg_attribute_printf(1, 2);
 
 extern int	errhidestmt(bool hide_stmt);
 extern int	errhidecontext(bool hide_ctx);
+extern int	errhidefromclient(bool hide_from_client);
 
 extern int	errfunction(const char *funcname);
 extern int	errposition(int cursorpos);
@@ -343,6 +344,7 @@ typedef struct ErrorData
 	bool		show_funcname;	/* true to force funcname inclusion */
 	bool		hide_stmt;		/* true to prevent STATEMENT: inclusion */
 	bool		hide_ctx;		/* true to prevent CONTEXT: inclusion */
+	bool		hide_from_client;	/* true to prevent client output */
 	const char *filename;		/* __FILE__ of ereport() call */
 	int			lineno;			/* __LINE__ of ereport() call */
 	const char *funcname;		/* __func__ of ereport() call */
pgaudit-v2-02.patchtext/plain; charset=UTF-8; name=pgaudit-v2-02.patch; x-mac-creator=0; x-mac-type=0Download
diff --git a/contrib/Makefile b/contrib/Makefile
index bd251f6..0046610 100644
--- a/contrib/Makefile
+++ b/contrib/Makefile
@@ -34,6 +34,7 @@ SUBDIRS = \
 		pg_standby	\
 		pg_stat_statements \
 		pg_trgm		\
+		pgaudit		\
 		pgcrypto	\
 		pgrowlocks	\
 		pgstattuple	\
diff --git a/contrib/pgaudit/.gitignore b/contrib/pgaudit/.gitignore
new file mode 100644
index 0000000..e8d2612
--- /dev/null
+++ b/contrib/pgaudit/.gitignore
@@ -0,0 +1,8 @@
+log/
+results/
+tmp_check/
+regression.diffs
+regression.out
+*.o
+*.so
+.vagrant
diff --git a/contrib/pgaudit/LICENSE b/contrib/pgaudit/LICENSE
new file mode 100644
index 0000000..998f814
--- /dev/null
+++ b/contrib/pgaudit/LICENSE
@@ -0,0 +1,4 @@
+This code is released under the PostgreSQL licence, as given at
+http://www.postgresql.org/about/licence/
+
+Copyright is novated to the PostgreSQL Global Development Group.
diff --git a/contrib/pgaudit/Makefile b/contrib/pgaudit/Makefile
new file mode 100644
index 0000000..b3a488b
--- /dev/null
+++ b/contrib/pgaudit/Makefile
@@ -0,0 +1,22 @@
+# contrib/pg_audit/Makefile
+
+MODULE_big = pgaudit
+OBJS = pgaudit.o $(WIN32RES)
+
+EXTENSION = pgaudit
+DATA = pgaudit--1.0.sql
+PGFILEDESC = "pgAudit - An audit logging extension for PostgreSQL"
+
+REGRESS = pgaudit
+REGRESS_OPTS = --temp-config=$(top_srcdir)/contrib/pgaudit/pgaudit.conf
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/pgaudit
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/pgaudit/pgaudit--1.0.sql b/contrib/pgaudit/pgaudit--1.0.sql
new file mode 100644
index 0000000..e0a12b7
--- /dev/null
+++ b/contrib/pgaudit/pgaudit--1.0.sql
@@ -0,0 +1,22 @@
+/* pgaudit/pgaudit--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION pgaudit" to load this file.\quit
+
+CREATE FUNCTION pgaudit_ddl_command_end()
+	RETURNS event_trigger
+	LANGUAGE C
+	AS 'MODULE_PATHNAME', 'pgaudit_ddl_command_end';
+
+CREATE EVENT TRIGGER pgaudit_ddl_command_end
+	ON ddl_command_end
+	EXECUTE PROCEDURE pgaudit_ddl_command_end();
+
+CREATE FUNCTION pgaudit_sql_drop()
+	RETURNS event_trigger
+	LANGUAGE C
+	AS 'MODULE_PATHNAME', 'pgaudit_sql_drop';
+
+CREATE EVENT TRIGGER pgaudit_sql_drop
+	ON sql_drop
+	EXECUTE PROCEDURE pgaudit_sql_drop();
diff --git a/contrib/pgaudit/pgaudit.c b/contrib/pgaudit/pgaudit.c
new file mode 100644
index 0000000..490dc70
--- /dev/null
+++ b/contrib/pgaudit/pgaudit.c
@@ -0,0 +1,1917 @@
+/*------------------------------------------------------------------------------
+ * pgaudit.c
+ *
+ * An audit logging extension for PostgreSQL. Provides detailed logging classes,
+ * object level logging, and fully-qualified object names for all DML and DDL
+ * statements where possible (See pgaudit.sgml for details).
+ *
+ * Copyright (c) 2014-2015, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		  contrib/pgaudit/pgaudit.c
+ *------------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/htup_details.h"
+#include "access/sysattr.h"
+#include "access/xact.h"
+#include "catalog/catalog.h"
+#include "catalog/objectaccess.h"
+#include "catalog/pg_class.h"
+#include "catalog/namespace.h"
+#include "commands/dbcommands.h"
+#include "catalog/pg_proc.h"
+#include "commands/event_trigger.h"
+#include "executor/executor.h"
+#include "executor/spi.h"
+#include "miscadmin.h"
+#include "libpq/auth.h"
+#include "nodes/nodes.h"
+#include "tcop/utility.h"
+#include "tcop/deparse_utility.h"
+#include "utils/acl.h"
+#include "utils/builtins.h"
+#include "utils/guc.h"
+#include "utils/lsyscache.h"
+#include "utils/memutils.h"
+#include "utils/rel.h"
+#include "utils/syscache.h"
+#include "utils/timestamp.h"
+
+PG_MODULE_MAGIC;
+
+void _PG_init(void);
+
+PG_FUNCTION_INFO_V1(pgaudit_ddl_command_end);
+PG_FUNCTION_INFO_V1(pgaudit_sql_drop);
+
+/*
+ * Log Classes
+ *
+ * pgAudit categorizes actions into classes (eg: DDL, FUNCTION calls, READ
+ * queries, WRITE queries).  A GUC is provided for the administrator to
+ * configure which class (or classes) of actions to include in the
+ * audit log.  We track the currently active set of classes using
+ * auditLogBitmap.
+ */
+
+/* Bits within auditLogBitmap, defines the classes we understand */
+#define LOG_DDL			(1 << 0)	/* CREATE/DROP/ALTER objects */
+#define LOG_FUNCTION	(1 << 1)	/* Functions and DO blocks */
+#define LOG_MISC		(1 << 2)	/* Statements not covered */
+#define LOG_READ		(1 << 3)	/* SELECTs */
+#define LOG_ROLE		(1 << 4)	/* GRANT/REVOKE, CREATE/ALTER/DROP ROLE */
+#define LOG_WRITE		(1 << 5)	/* INSERT, UPDATE, DELETE, TRUNCATE */
+
+#define LOG_NONE		0				/* nothing */
+#define LOG_ALL			(0xFFFFFFFF)	/* All */
+
+/* GUC variable for pgaudit.log, which defines the classes to log. */
+char *auditLog = NULL;
+
+/* Bitmap of classes selected */
+static int auditLogBitmap = LOG_NONE;
+
+/*
+ * String constants for log classes - used when processing tokens in the
+ * pgaudit.log GUC.
+ */
+#define CLASS_DDL		"DDL"
+#define CLASS_FUNCTION	"FUNCTION"
+#define CLASS_MISC		"MISC"
+#define CLASS_READ		"READ"
+#define CLASS_ROLE		"ROLE"
+#define CLASS_WRITE		"WRITE"
+
+#define CLASS_NONE		"NONE"
+#define CLASS_ALL		"ALL"
+
+/*
+ * GUC variable for pgaudit.log_catalog
+ *
+ * Administrators can choose to NOT log queries when all relations used in
+ * the query are in pg_catalog.  Interactive sessions (eg: psql) can cause
+ * a lot of noise in the logs which might be uninteresting.
+ */
+bool auditLogCatalog = true;
+
+/*
+ * GUC variable for pgaudit.log_client
+ *
+ * Specifies whether audit messages should be visible to the client.  This
+ * setting should generally be left disabled but may be useful for debugging or
+ * other purposes.
+ */
+bool auditLogClient = false;
+
+/*
+ * GUC variable for pgaudit.log_level
+ *
+ * Administrators can choose which log level the audit log is to be logged
+ * at.  The default level is LOG, which goes into the server log but does
+ * not go to the client.  Set to NOTICE in the regression tests.
+ */
+char *auditLogLevelString = NULL;
+int auditLogLevel = LOG;
+
+/*
+ * GUC variable for pgaudit.log_parameter
+ *
+ * Administrators can choose if parameters passed into a statement are
+ * included in the audit log.
+ */
+bool auditLogParameter = false;
+
+/*
+ * GUC variable for pgaudit.log_relation
+ *
+ * Administrators can choose, in SESSION logging, to log each relation involved
+ * in READ/WRITE class queries.  By default, SESSION logs include the query but
+ * do not have a log entry for each relation.
+ */
+bool auditLogRelation = false;
+
+/*
+ * GUC variable for pgaudit.log_statement_once
+ *
+ * Administrators can choose to have the statement run logged only once instead
+ * of on every line.  By default, the statement is repeated on every line of
+ * the audit log to facilitate searching, but this can cause the log to be
+ * unnecessairly bloated in some environments.
+ */
+bool auditLogStatementOnce = false;
+
+/*
+ * GUC variable for pgaudit.role
+ *
+ * Administrators can choose which role to base OBJECT auditing off of.
+ * Object-level auditing uses the privileges which are granted to this role to
+ * determine if a statement should be logged.
+ */
+char *auditRole = NULL;
+
+/*
+ * String constants for the audit log fields.
+ */
+
+/*
+ * Audit type, which is responsbile for the log message
+ */
+#define AUDIT_TYPE_OBJECT	"OBJECT"
+#define AUDIT_TYPE_SESSION	"SESSION"
+
+/*
+ * Command, used for SELECT/DML and function calls.
+ *
+ * We hook into the executor, but we do not have access to the parsetree there.
+ * Therefore we can't simply call CreateCommandTag() to get the command and have
+ * to build it ourselves based on what information we do have.
+ *
+ * These should be updated if new commands are added to what the exectuor
+ * currently handles.  Note that most of the interesting commands do not go
+ * through the executor but rather ProcessUtility, where we have the parsetree.
+ */
+#define COMMAND_SELECT		"SELECT"
+#define COMMAND_INSERT		"INSERT"
+#define COMMAND_UPDATE		"UPDATE"
+#define COMMAND_DELETE		"DELETE"
+#define COMMAND_EXECUTE		"EXECUTE"
+#define COMMAND_UNKNOWN		"UNKNOWN"
+
+/*
+ * Object type, used for SELECT/DML statements and function calls.
+ *
+ * For relation objects, this is essentially relkind (though we do not have
+ * access to a function which will just return a string given a relkind;
+ * getRelationTypeDescription() comes close but is not public currently).
+ *
+ * We also handle functions, so it isn't quite as simple as just relkind.
+ *
+ * This should be kept consistent with what is returned from
+ * pg_event_trigger_ddl_commands(), as that's what we use for DDL.
+ */
+#define OBJECT_TYPE_TABLE			"TABLE"
+#define OBJECT_TYPE_INDEX			"INDEX"
+#define OBJECT_TYPE_SEQUENCE		"SEQUENCE"
+#define OBJECT_TYPE_TOASTVALUE		"TOAST TABLE"
+#define OBJECT_TYPE_VIEW			"VIEW"
+#define OBJECT_TYPE_MATVIEW			"MATERIALIZED VIEW"
+#define OBJECT_TYPE_COMPOSITE_TYPE	"COMPOSITE TYPE"
+#define OBJECT_TYPE_FOREIGN_TABLE	"FOREIGN TABLE"
+#define OBJECT_TYPE_FUNCTION		"FUNCTION"
+
+#define OBJECT_TYPE_UNKNOWN			"UNKNOWN"
+
+/*
+ * String constants for testing role commands.  Rename and drop role statements
+ * are assigned the nodeTag T_RenameStmt and T_DropStmt respectively.  This is
+ * not very useful for classification, so we resort to comparing strings
+ * against the result of CreateCommandTag(parsetree).
+ */
+#define COMMAND_ALTER_ROLE	"ALTER ROLE"
+#define COMMAND_DROP_ROLE	"DROP ROLE"
+#define COMMAND_GRANT		"GRANT"
+#define COMMAND_REVOKE		"REVOKE"
+
+/*
+ * An AuditEvent represents an operation that potentially affects a single
+ * object.  If a statement affects multiple objects then multiple AuditEvents
+ * are created to represent them.
+ */
+typedef struct
+{
+	int64 statementId;			/* Simple counter */
+	int64 substatementId;		/* Simple counter */
+
+	LogStmtLevel logStmtLevel;	/* From GetCommandLogLevel when possible,
+								   generated when not. */
+	NodeTag commandTag;			/* same here */
+	const char *command;		/* same here */
+	const char *objectType;		/* From event trigger when possible,
+								   generated when not. */
+	char *objectName;			/* Fully qualified object identification */
+	const char *commandText;	/* sourceText / queryString */
+	ParamListInfo paramList;	/* QueryDesc/ProcessUtility parameters */
+
+	bool granted;				/* Audit role has object permissions? */
+	bool logged;				/* Track if we have logged this event, used
+								   post-ProcessUtility to make sure we log */
+	bool statementLogged;		/* Track if we have logged the statement */
+} AuditEvent;
+
+/*
+ * A simple FIFO queue to keep track of the current stack of audit events.
+ */
+typedef struct AuditEventStackItem
+{
+	struct AuditEventStackItem *next;
+
+	AuditEvent auditEvent;
+
+	int64 stackId;
+
+	MemoryContext contextAudit;
+	MemoryContextCallback contextCallback;
+} AuditEventStackItem;
+
+AuditEventStackItem *auditEventStack = NULL;
+
+/*
+ * pgAudit runs queries of its own when using the event trigger system.
+ *
+ * Track when we are running a query and don't log it.
+ */
+static bool internalStatement = false;
+
+/*
+ * Track running total for statements and substatements and whether or not
+ * anything has been logged since the current statement began.
+ */
+static int64 statementTotal = 0;
+static int64 substatementTotal = 0;
+static int64 stackTotal = 0;
+
+static bool statementLogged = false;
+
+/*
+ * Stack functions
+ *
+ * Audit events can go down to multiple levels so a stack is maintained to keep
+ * track of them.
+ */
+
+/*
+ * Respond to callbacks registered with MemoryContextRegisterResetCallback().
+ * Removes the event(s) off the stack that have become obsolete once the
+ * MemoryContext has been freed.  The callback should always be freeing the top
+ * of the stack, but the code is tolerant of out-of-order callbacks.
+ */
+static void
+stack_free(void *stackFree)
+{
+	AuditEventStackItem *nextItem = auditEventStack;
+
+	/* Only process if the stack contains items */
+	while (nextItem != NULL)
+	{
+		/* Check if this item matches the item to be freed */
+		if (nextItem == (AuditEventStackItem *) stackFree)
+		{
+			/* Move top of stack to the item after the freed item */
+			auditEventStack = nextItem->next;
+
+			/* If the stack is not empty */
+			if (auditEventStack == NULL)
+			{
+				/*
+				 * Reset internal statement to false.  Normally this will be
+				 * reset but in case of an error it might be left set.
+				 */
+				internalStatement = false;
+
+				/*
+				 * Reset sub statement total so the next statement will start
+				 * from 1.
+				 */
+				substatementTotal = 0;
+
+				/*
+				 * Reset statement logged so that next statement will be
+				 * logged.
+				 */
+				statementLogged = false;
+			}
+
+			return;
+		}
+
+		nextItem = nextItem->next;
+	}
+}
+
+/*
+ * Push a new audit event onto the stack and create a new memory context to
+ * store it.
+ */
+static AuditEventStackItem *
+stack_push()
+{
+	MemoryContext contextAudit;
+	MemoryContext contextOld;
+	AuditEventStackItem *stackItem;
+
+	/*
+	 * Create a new memory context to contain the stack item.  This will be
+	 * free'd on stack_pop, or by our callback when the parent context is
+	 * destroyed.
+	 */
+	contextAudit = AllocSetContextCreate(CurrentMemoryContext,
+										 "pgaudit stack context",
+										 ALLOCSET_DEFAULT_MINSIZE,
+										 ALLOCSET_DEFAULT_INITSIZE,
+										 ALLOCSET_DEFAULT_MAXSIZE);
+
+	/* Save the old context to switch back to at the end */
+	contextOld = MemoryContextSwitchTo(contextAudit);
+
+	/* Create our new stack item in our context */
+	stackItem = palloc0(sizeof(AuditEventStackItem));
+	stackItem->contextAudit = contextAudit;
+	stackItem->stackId = ++stackTotal;
+
+	/*
+	 * Setup a callback in case an error happens.  stack_free() will truncate
+	 * the stack at this item.
+	 */
+	stackItem->contextCallback.func = stack_free;
+	stackItem->contextCallback.arg = (void *) stackItem;
+	MemoryContextRegisterResetCallback(contextAudit,
+									   &stackItem->contextCallback);
+
+	/* Push new item onto the stack */
+	if (auditEventStack != NULL)
+		stackItem->next = auditEventStack;
+	else
+		stackItem->next = NULL;
+
+	auditEventStack = stackItem;
+
+	MemoryContextSwitchTo(contextOld);
+
+	return stackItem;
+}
+
+/*
+ * Pop an audit event from the stack by deleting the memory context that
+ * contains it.  The callback to stack_free() does the actual pop.
+ */
+static void
+stack_pop(int64 stackId)
+{
+	/* Make sure what we want to delete is at the top of the stack */
+	if (auditEventStack != NULL && auditEventStack->stackId == stackId)
+		MemoryContextDelete(auditEventStack->contextAudit);
+	else
+		elog(ERROR, "pgaudit stack item " INT64_FORMAT
+			 " not found on top - cannot pop",
+			 stackId);
+}
+
+/*
+ * Check that an item is on the stack.  If not, an error will be raised since
+ * this is a bad state to be in and it might mean audit records are being lost.
+ */
+static void
+stack_valid(int64 stackId)
+{
+	AuditEventStackItem *nextItem = auditEventStack;
+
+	/* Look through the stack for the stack entry */
+	while (nextItem != NULL && nextItem->stackId != stackId)
+		nextItem = nextItem->next;
+
+	/* If we didn't find it, something went wrong. */
+	if (nextItem == NULL)
+		elog(ERROR, "pgaudit stack item " INT64_FORMAT
+			 " not found - top of stack is " INT64_FORMAT "",
+			 stackId,
+			 auditEventStack == NULL ? (int64) -1 : auditEventStack->stackId);
+}
+
+/*
+ * Appends a properly quoted CSV field to StringInfo.
+ */
+static void
+append_valid_csv(StringInfoData *buffer, const char *appendStr)
+{
+	const char *pChar;
+
+	/*
+	 * If the append string is null then do nothing.  NULL fields are not
+	 * quoted in CSV.
+	 */
+	if (appendStr == NULL)
+		return;
+
+	/* Only format for CSV if appendStr contains: ", comma, \n, \r */
+	if (strstr(appendStr, ",") || strstr(appendStr, "\"") ||
+		strstr(appendStr, "\n") || strstr(appendStr, "\r"))
+	{
+		appendStringInfoCharMacro(buffer, '"');
+
+		for (pChar = appendStr; *pChar; pChar++)
+		{
+			if (*pChar == '"')	/* double single quotes */
+				appendStringInfoCharMacro(buffer, *pChar);
+
+			appendStringInfoCharMacro(buffer, *pChar);
+		}
+
+		appendStringInfoCharMacro(buffer, '"');
+	}
+	/* Else just append */
+	else
+		appendStringInfoString(buffer, appendStr);
+}
+
+/*
+ * Takes an AuditEvent, classifies it, then logs it if appropriate.
+ *
+ * Logging is decided based on if the statement is in one of the classes being
+ * logged or if an object used has been marked for auditing.
+ *
+ * Objects are marked for auditing by the auditor role being granted access
+ * to the object.  The kind of access (INSERT, UPDATE, etc) is also considered
+ * and logging is only performed when the kind of access matches the granted
+ * right on the object.
+ *
+ * This will need to be updated if new kinds of GRANTs are added.
+ */
+static void
+log_audit_event(AuditEventStackItem *stackItem)
+{
+	/* By default, put everything in the MISC class. */
+	int class = LOG_MISC;
+	const char *className = CLASS_MISC;
+	MemoryContext contextOld;
+	StringInfoData auditStr;
+
+	/* Classify the statement using log stmt level and the command tag */
+	switch (stackItem->auditEvent.logStmtLevel)
+	{
+			/* All mods go in WRITE class, except EXECUTE */
+		case LOGSTMT_MOD:
+			className = CLASS_WRITE;
+			class = LOG_WRITE;
+
+			switch (stackItem->auditEvent.commandTag)
+			{
+					/* Currently, only EXECUTE is different */
+				case T_ExecuteStmt:
+					className = CLASS_MISC;
+					class = LOG_MISC;
+					break;
+				default:
+					break;
+			}
+			break;
+
+			/* These are DDL, unless they are ROLE */
+		case LOGSTMT_DDL:
+			className = CLASS_DDL;
+			class = LOG_DDL;
+
+			/* Identify role statements */
+			switch (stackItem->auditEvent.commandTag)
+			{
+					/* We know these are all role statements */
+				case T_GrantStmt:
+				case T_GrantRoleStmt:
+				case T_CreateRoleStmt:
+				case T_DropRoleStmt:
+				case T_AlterRoleStmt:
+				case T_AlterRoleSetStmt:
+				case T_AlterDefaultPrivilegesStmt:
+					className = CLASS_ROLE;
+					class = LOG_ROLE;
+					break;
+
+					/*
+					 * Rename and Drop are general and therefore we have to do
+					 * an additional check against the command string to see
+					 * if they are role or regular DDL.
+					 */
+				case T_RenameStmt:
+				case T_DropStmt:
+					if (pg_strcasecmp(stackItem->auditEvent.command,
+									  COMMAND_ALTER_ROLE) == 0 ||
+						pg_strcasecmp(stackItem->auditEvent.command,
+									  COMMAND_DROP_ROLE) == 0)
+					{
+						className = CLASS_ROLE;
+						class = LOG_ROLE;
+					}
+					break;
+
+				default:
+					break;
+			}
+			break;
+
+			/* Classify the rest */
+		case LOGSTMT_ALL:
+			switch (stackItem->auditEvent.commandTag)
+			{
+					/* READ statements */
+				case T_CopyStmt:
+				case T_SelectStmt:
+				case T_PrepareStmt:
+				case T_PlannedStmt:
+					className = CLASS_READ;
+					class = LOG_READ;
+					break;
+
+					/* FUNCTION statements */
+				case T_DoStmt:
+					className = CLASS_FUNCTION;
+					class = LOG_FUNCTION;
+					break;
+
+				default:
+					break;
+			}
+			break;
+
+		case LOGSTMT_NONE:
+			break;
+	}
+
+	/*
+	 * Only log the statement if:
+	 *
+	 * 1. If object was selected for audit logging (granted), or
+	 * 2. The statement belongs to a class that is being logged
+	 *
+	 * If neither of these is true, return.
+	 */
+	if (!stackItem->auditEvent.granted && !(auditLogBitmap & class))
+		return;
+
+	/*
+	 * Use audit memory context in case something is not free'd while
+	 * appending strings and parameters.
+	 */
+	contextOld = MemoryContextSwitchTo(stackItem->contextAudit);
+
+	/* Set statement and substatement IDs */
+	if (stackItem->auditEvent.statementId == 0)
+	{
+		/* If nothing has been logged yet then create a new statement Id */
+		if (!statementLogged)
+		{
+			statementTotal++;
+			statementLogged = true;
+		}
+
+		stackItem->auditEvent.statementId = statementTotal;
+		stackItem->auditEvent.substatementId = ++substatementTotal;
+	}
+
+	/*
+	 * Create the audit substring
+	 *
+	 * The type-of-audit-log and statement/substatement ID are handled below,
+	 * this string is everything else.
+	 */
+	initStringInfo(&auditStr);
+	append_valid_csv(&auditStr, stackItem->auditEvent.command);
+
+	appendStringInfoCharMacro(&auditStr, ',');
+	append_valid_csv(&auditStr, stackItem->auditEvent.objectType);
+
+	appendStringInfoCharMacro(&auditStr, ',');
+	append_valid_csv(&auditStr, stackItem->auditEvent.objectName);
+
+	/*
+	 * If auditLogStatmentOnce is true, then only log the statement and
+	 * parameters if they have not already been logged for this substatement.
+	 */
+	appendStringInfoCharMacro(&auditStr, ',');
+	if (!stackItem->auditEvent.statementLogged || !auditLogStatementOnce)
+	{
+		append_valid_csv(&auditStr, stackItem->auditEvent.commandText);
+
+		appendStringInfoCharMacro(&auditStr, ',');
+
+		/* Handle parameter logging, if enabled. */
+		if (auditLogParameter)
+		{
+			int paramIdx;
+			int numParams;
+			StringInfoData paramStrResult;
+			ParamListInfo paramList = stackItem->auditEvent.paramList;
+
+			numParams = paramList == NULL ? 0 : paramList->numParams;
+
+			/* Create the param substring */
+			initStringInfo(&paramStrResult);
+
+			/* Iterate through all params */
+			for (paramIdx = 0; paramList != NULL && paramIdx < numParams;
+				 paramIdx++)
+			{
+				ParamExternData *prm = &paramList->params[paramIdx];
+				Oid typeOutput;
+				bool typeIsVarLena;
+				char *paramStr;
+
+				/* Add a comma for each param */
+				if (paramIdx != 0)
+					appendStringInfoCharMacro(&paramStrResult, ',');
+
+				/* Skip if null or if oid is invalid */
+				if (prm->isnull || !OidIsValid(prm->ptype))
+					continue;
+
+				/* Output the string */
+				getTypeOutputInfo(prm->ptype, &typeOutput, &typeIsVarLena);
+				paramStr = OidOutputFunctionCall(typeOutput, prm->value);
+
+				append_valid_csv(&paramStrResult, paramStr);
+				pfree(paramStr);
+			}
+
+			if (numParams == 0)
+				appendStringInfoString(&auditStr, "<none>");
+			else
+				append_valid_csv(&auditStr, paramStrResult.data);
+		}
+		else
+			appendStringInfoString(&auditStr, "<not logged>");
+
+		stackItem->auditEvent.statementLogged = true;
+	}
+	else
+		/* we were asked to not log it */
+		appendStringInfoString(&auditStr,
+							   "<previously logged>,<previously logged>");
+
+	/*
+	 * Log the audit entry.  Note: use of INT64_FORMAT here is bad for
+	 * translatability, but we currently haven't got translation support in
+	 * pgaudit anyway.
+	 */
+	ereport(auditLogLevel,
+			(errmsg("AUDIT: %s," INT64_FORMAT "," INT64_FORMAT ",%s,%s",
+					stackItem->auditEvent.granted ?
+					AUDIT_TYPE_OBJECT : AUDIT_TYPE_SESSION,
+					stackItem->auditEvent.statementId,
+					stackItem->auditEvent.substatementId,
+					className,
+					auditStr.data),
+					errhidestmt(true),
+					errhidecontext(true),
+					errhidefromclient(!auditLogClient)));
+
+	stackItem->auditEvent.logged = true;
+
+	MemoryContextSwitchTo(contextOld);
+}
+
+/*
+ * Check if the role or any inherited role has any permission in the mask.  The
+ * public role is excluded from this check and superuser permissions are not
+ * considered.
+ */
+static bool
+audit_on_acl(Datum aclDatum,
+			 Oid auditOid,
+			 AclMode mask)
+{
+	bool result = false;
+	Acl *acl;
+	AclItem *aclItemData;
+	int aclIndex;
+	int aclTotal;
+
+	/* Detoast column's ACL if necessary */
+	acl = DatumGetAclP(aclDatum);
+
+	/* Get the acl list and total number of items */
+	aclTotal = ACL_NUM(acl);
+	aclItemData = ACL_DAT(acl);
+
+	/* Check privileges granted directly to auditOid */
+	for (aclIndex = 0; aclIndex < aclTotal; aclIndex++)
+	{
+		AclItem *aclItem = &aclItemData[aclIndex];
+
+		if (aclItem->ai_grantee == auditOid &&
+			aclItem->ai_privs & mask)
+		{
+			result = true;
+			break;
+		}
+	}
+
+	/*
+	 * Check privileges granted indirectly via role memberships. We do this in
+	 * a separate pass to minimize expensive indirect membership tests.  In
+	 * particular, it's worth testing whether a given ACL entry grants any
+	 * privileges still of interest before we perform the has_privs_of_role
+	 * test.
+	 */
+	if (!result)
+	{
+		for (aclIndex = 0; aclIndex < aclTotal; aclIndex++)
+		{
+			AclItem *aclItem = &aclItemData[aclIndex];
+
+			/* Don't test public or auditOid (it has been tested already) */
+			if (aclItem->ai_grantee == ACL_ID_PUBLIC ||
+				aclItem->ai_grantee == auditOid)
+				continue;
+
+			/*
+			 * Check that the role has the required privileges and that it is
+			 * inherited by auditOid.
+			 */
+			if (aclItem->ai_privs & mask &&
+				has_privs_of_role(auditOid, aclItem->ai_grantee))
+			{
+				result = true;
+				break;
+			}
+		}
+	}
+
+	/* if we have a detoasted copy, free it */
+	if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
+		pfree(acl);
+
+	return result;
+}
+
+/*
+ * Check if a role has any of the permissions in the mask on a relation.
+ */
+static bool
+audit_on_relation(Oid relOid,
+				  Oid auditOid,
+				  AclMode mask)
+{
+	bool result = false;
+	HeapTuple tuple;
+	Datum aclDatum;
+	bool isNull;
+
+	/* Get relation tuple from pg_class */
+	tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
+	if (!HeapTupleIsValid(tuple))
+		return false;
+
+	/* Get the relation's ACL */
+	aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
+							   &isNull);
+
+	/* Only check if non-NULL, since NULL means no permissions */
+	if (!isNull)
+		result = audit_on_acl(aclDatum, auditOid, mask);
+
+	/* Free the relation tuple */
+	ReleaseSysCache(tuple);
+
+	return result;
+}
+
+/*
+ * Check if a role has any of the permissions in the mask on a column.
+ */
+static bool
+audit_on_attribute(Oid relOid,
+				   AttrNumber attNum,
+				   Oid auditOid,
+				   AclMode mask)
+{
+	bool result = false;
+	HeapTuple attTuple;
+	Datum aclDatum;
+	bool isNull;
+
+	/* Get the attribute's ACL */
+	attTuple = SearchSysCache2(ATTNUM,
+							   ObjectIdGetDatum(relOid),
+							   Int16GetDatum(attNum));
+	if (!HeapTupleIsValid(attTuple))
+		return false;
+
+	/* Only consider attributes that have not been dropped */
+	if (!((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
+	{
+		aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
+								   &isNull);
+
+		if (!isNull)
+			result = audit_on_acl(aclDatum, auditOid, mask);
+	}
+
+	/* Free attribute */
+	ReleaseSysCache(attTuple);
+
+	return result;
+}
+
+/*
+ * Check if a role has any of the permissions in the mask on a column in
+ * the provided set.  If the set is empty, then all valid columns in the
+ * relation will be tested.
+ */
+static bool
+audit_on_any_attribute(Oid relOid,
+					   Oid auditOid,
+					   Bitmapset *attributeSet,
+					   AclMode mode)
+{
+	bool result = false;
+	AttrNumber col;
+	Bitmapset *tmpSet;
+
+	/* If bms is empty then check for any column match */
+	if (bms_is_empty(attributeSet))
+	{
+		HeapTuple classTuple;
+		AttrNumber nattrs;
+		AttrNumber curr_att;
+
+		/* Get relation to determine total columns */
+		classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
+
+		if (!HeapTupleIsValid(classTuple))
+			return false;
+
+		nattrs = ((Form_pg_class) GETSTRUCT(classTuple))->relnatts;
+		ReleaseSysCache(classTuple);
+
+		/* Check each column */
+		for (curr_att = 1; curr_att <= nattrs; curr_att++)
+			if (audit_on_attribute(relOid, curr_att, auditOid, mode))
+				return true;
+	}
+
+	/* bms_first_member is destructive, so make a copy before using it. */
+	tmpSet = bms_copy(attributeSet);
+
+	/* Check each column */
+	while ((col = bms_first_member(tmpSet)) >= 0)
+	{
+		col += FirstLowInvalidHeapAttributeNumber;
+
+		if (col != InvalidAttrNumber &&
+			audit_on_attribute(relOid, col, auditOid, mode))
+		{
+			result = true;
+			break;
+		}
+	}
+
+	bms_free(tmpSet);
+
+	return result;
+}
+
+/*
+ * Create AuditEvents for SELECT/DML operations via executor permissions checks.
+ */
+static void
+log_select_dml(Oid auditOid, List *rangeTabls)
+{
+	ListCell *lr;
+	bool first = true;
+	bool found = false;
+
+	/* Do not log if this is an internal statement */
+	if (internalStatement)
+		return;
+
+	foreach(lr, rangeTabls)
+	{
+		Oid relOid;
+		Relation rel;
+		RangeTblEntry *rte = lfirst(lr);
+
+		/* We only care about tables, and can ignore subqueries etc. */
+		if (rte->rtekind != RTE_RELATION)
+			continue;
+
+		found = true;
+
+		/*
+		 * Don't log if the session user is not a member of the current
+		 * role.  This prevents contents of security definer functions
+		 * from being logged and supresses foreign key queries unless the
+		 * session user is the owner of the referenced table.
+		 */
+		if (!is_member_of_role_nosuper(GetSessionUserId(), GetUserId()))
+			return;
+
+		/*
+		 * If we are not logging all-catalog queries (auditLogCatalog is
+		 * false) then filter out any system relations here.
+		 */
+		relOid = rte->relid;
+		rel = relation_open(relOid, NoLock);
+
+		if (!auditLogCatalog && IsSystemNamespace(RelationGetNamespace(rel)))
+		{
+			relation_close(rel, NoLock);
+			continue;
+		}
+
+		/*
+		 * Default is that this was not through a grant, to support session
+		 * logging.  Will be updated below if a grant is found.
+		 */
+		auditEventStack->auditEvent.granted = false;
+
+		/*
+		 * If this is the first RTE then session log unless auditLogRelation
+		 * is set.
+		 */
+		if (first && !auditLogRelation)
+		{
+			log_audit_event(auditEventStack);
+
+			first = false;
+		}
+
+		/*
+		 * We don't have access to the parsetree here, so we have to generate
+		 * the node type, object type, and command tag by decoding
+		 * rte->requiredPerms and rte->relkind.
+		 */
+		if (rte->requiredPerms & ACL_INSERT)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_MOD;
+			auditEventStack->auditEvent.commandTag = T_InsertStmt;
+			auditEventStack->auditEvent.command = COMMAND_INSERT;
+		}
+		else if (rte->requiredPerms & ACL_UPDATE)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_MOD;
+			auditEventStack->auditEvent.commandTag = T_UpdateStmt;
+			auditEventStack->auditEvent.command = COMMAND_UPDATE;
+		}
+		else if (rte->requiredPerms & ACL_DELETE)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_MOD;
+			auditEventStack->auditEvent.commandTag = T_DeleteStmt;
+			auditEventStack->auditEvent.command = COMMAND_DELETE;
+		}
+		else if (rte->requiredPerms & ACL_SELECT)
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_ALL;
+			auditEventStack->auditEvent.commandTag = T_SelectStmt;
+			auditEventStack->auditEvent.command = COMMAND_SELECT;
+		}
+		else
+		{
+			auditEventStack->auditEvent.logStmtLevel = LOGSTMT_ALL;
+			auditEventStack->auditEvent.commandTag = T_Invalid;
+			auditEventStack->auditEvent.command = COMMAND_UNKNOWN;
+		}
+
+		/* Use the relation type to assign object type */
+		switch (rte->relkind)
+		{
+			case RELKIND_RELATION:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_TABLE;
+				break;
+
+			case RELKIND_INDEX:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_INDEX;
+				break;
+
+			case RELKIND_SEQUENCE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_SEQUENCE;
+				break;
+
+			case RELKIND_TOASTVALUE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_TOASTVALUE;
+				break;
+
+			case RELKIND_VIEW:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_VIEW;
+				break;
+
+			case RELKIND_COMPOSITE_TYPE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_COMPOSITE_TYPE;
+				break;
+
+			case RELKIND_FOREIGN_TABLE:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_FOREIGN_TABLE;
+				break;
+
+			case RELKIND_MATVIEW:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_MATVIEW;
+				break;
+
+			default:
+				auditEventStack->auditEvent.objectType = OBJECT_TYPE_UNKNOWN;
+				break;
+		}
+
+		/* Get a copy of the relation name and assign it to object name */
+		auditEventStack->auditEvent.objectName =
+			quote_qualified_identifier(get_namespace_name(
+									   RelationGetNamespace(rel)),
+									   RelationGetRelationName(rel));
+		relation_close(rel, NoLock);
+
+		/* Perform object auditing only if the audit role is valid */
+		if (auditOid != InvalidOid)
+		{
+			AclMode auditPerms =
+				(ACL_SELECT | ACL_UPDATE | ACL_INSERT | ACL_DELETE) &
+				rte->requiredPerms;
+
+			/*
+			 * If any of the required permissions for the relation are granted
+			 * to the audit role then audit the relation
+			 */
+			if (audit_on_relation(relOid, auditOid, auditPerms))
+				auditEventStack->auditEvent.granted = true;
+
+			/*
+			 * Else check if the audit role has column-level permissions for
+			 * select, insert, or update.
+			 */
+			else if (auditPerms != 0)
+			{
+				/*
+				 * Check the select columns
+				 */
+				if (auditPerms & ACL_SELECT)
+					auditEventStack->auditEvent.granted =
+						audit_on_any_attribute(relOid, auditOid,
+											   rte->selectedCols,
+											   ACL_SELECT);
+
+				/*
+				 * Check the insert columns
+				 */
+				if (!auditEventStack->auditEvent.granted &&
+					auditPerms & ACL_INSERT)
+					auditEventStack->auditEvent.granted =
+						audit_on_any_attribute(relOid, auditOid,
+											   rte->insertedCols,
+											   auditPerms);
+
+				/*
+				 * Check the update columns
+				 */
+				if (!auditEventStack->auditEvent.granted &&
+					auditPerms & ACL_UPDATE)
+					auditEventStack->auditEvent.granted =
+						audit_on_any_attribute(relOid, auditOid,
+											   rte->updatedCols,
+											   auditPerms);
+			}
+		}
+
+		/* Do relation level logging if a grant was found */
+		if (auditEventStack->auditEvent.granted)
+		{
+			auditEventStack->auditEvent.logged = false;
+			log_audit_event(auditEventStack);
+		}
+
+		/* Do relation level logging if auditLogRelation is set */
+		if (auditLogRelation)
+		{
+			auditEventStack->auditEvent.logged = false;
+			auditEventStack->auditEvent.granted = false;
+			log_audit_event(auditEventStack);
+		}
+
+		pfree(auditEventStack->auditEvent.objectName);
+	}
+
+	/*
+	 * If no tables were found that means that RangeTbls was empty or all
+	 * relations were in the system schema.  In that case still log a session
+	 * record.
+	 */
+	if (!found)
+	{
+		auditEventStack->auditEvent.granted = false;
+		auditEventStack->auditEvent.logged = false;
+
+		log_audit_event(auditEventStack);
+	}
+}
+
+/*
+ * Create AuditEvents for non-catalog function execution, as detected by
+ * log_object_access() below.
+ */
+static void
+log_function_execute(Oid objectId)
+{
+	HeapTuple proctup;
+	Form_pg_proc proc;
+	AuditEventStackItem *stackItem;
+
+	/* Get info about the function. */
+	proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(objectId));
+
+	if (!proctup)
+		elog(ERROR, "cache lookup failed for function %u", objectId);
+
+	proc = (Form_pg_proc) GETSTRUCT(proctup);
+
+	/*
+	 * Logging execution of all pg_catalog functions would make the log
+	 * unusably noisy.
+	 */
+	if (IsSystemNamespace(proc->pronamespace))
+	{
+		ReleaseSysCache(proctup);
+		return;
+	}
+
+	/* Push audit event onto the stack */
+	stackItem = stack_push();
+
+	/* Generate the fully-qualified function name. */
+	stackItem->auditEvent.objectName =
+		quote_qualified_identifier(get_namespace_name(proc->pronamespace),
+								   NameStr(proc->proname));
+	ReleaseSysCache(proctup);
+
+	/* Log the function call */
+	stackItem->auditEvent.logStmtLevel = LOGSTMT_ALL;
+	stackItem->auditEvent.commandTag = T_DoStmt;
+	stackItem->auditEvent.command = COMMAND_EXECUTE;
+	stackItem->auditEvent.objectType = OBJECT_TYPE_FUNCTION;
+	stackItem->auditEvent.commandText = stackItem->next->auditEvent.commandText;
+
+	log_audit_event(stackItem);
+
+	/* Pop audit event from the stack */
+	stack_pop(stackItem->stackId);
+}
+
+/*
+ * Hook functions
+ */
+static ExecutorCheckPerms_hook_type next_ExecutorCheckPerms_hook = NULL;
+static ProcessUtility_hook_type next_ProcessUtility_hook = NULL;
+static object_access_hook_type next_object_access_hook = NULL;
+static ExecutorStart_hook_type next_ExecutorStart_hook = NULL;
+
+/*
+ * Hook ExecutorStart to get the query text and basic command type for queries
+ * that do not contain a table and so can't be idenitified accurately in
+ * ExecutorCheckPerms.
+ */
+static void
+pgaudit_ExecutorStart_hook(QueryDesc *queryDesc, int eflags)
+{
+	AuditEventStackItem *stackItem = NULL;
+
+	if (!internalStatement)
+	{
+		/* Push the audit even onto the stack */
+		stackItem = stack_push();
+
+		/* Initialize command using queryDesc->operation */
+		switch (queryDesc->operation)
+		{
+			case CMD_SELECT:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_ALL;
+				stackItem->auditEvent.commandTag = T_SelectStmt;
+				stackItem->auditEvent.command = COMMAND_SELECT;
+				break;
+
+			case CMD_INSERT:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_MOD;
+				stackItem->auditEvent.commandTag = T_InsertStmt;
+				stackItem->auditEvent.command = COMMAND_INSERT;
+				break;
+
+			case CMD_UPDATE:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_MOD;
+				stackItem->auditEvent.commandTag = T_UpdateStmt;
+				stackItem->auditEvent.command = COMMAND_UPDATE;
+				break;
+
+			case CMD_DELETE:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_MOD;
+				stackItem->auditEvent.commandTag = T_DeleteStmt;
+				stackItem->auditEvent.command = COMMAND_DELETE;
+				break;
+
+			default:
+				stackItem->auditEvent.logStmtLevel = LOGSTMT_ALL;
+				stackItem->auditEvent.commandTag = T_Invalid;
+				stackItem->auditEvent.command = COMMAND_UNKNOWN;
+				break;
+		}
+
+		/* Initialize the audit event */
+		stackItem->auditEvent.commandText = queryDesc->sourceText;
+		stackItem->auditEvent.paramList = queryDesc->params;
+	}
+
+	/* Call the previous hook or standard function */
+	if (next_ExecutorStart_hook)
+		next_ExecutorStart_hook(queryDesc, eflags);
+	else
+		standard_ExecutorStart(queryDesc, eflags);
+
+	/*
+	 * Move the stack memory context to the query memory context.  This needs
+	 * to be done here because the query context does not exist before the
+	 * call to standard_ExecutorStart() but the stack item is required by
+	 * pgaudit_ExecutorCheckPerms_hook() which is called during
+	 * standard_ExecutorStart().
+	 */
+	if (stackItem)
+		MemoryContextSetParent(stackItem->contextAudit,
+							   queryDesc->estate->es_query_cxt);
+}
+
+/*
+ * Hook ExecutorCheckPerms to do session and object auditing for DML.
+ */
+static bool
+pgaudit_ExecutorCheckPerms_hook(List *rangeTabls, bool abort)
+{
+	Oid auditOid;
+
+	/* Get the audit oid if the role exists */
+	auditOid = get_role_oid(auditRole, true);
+
+	/* Log DML if the audit role is valid or session logging is enabled */
+	if ((auditOid != InvalidOid || auditLogBitmap != 0) &&
+		!IsAbortedTransactionBlockState())
+		log_select_dml(auditOid, rangeTabls);
+
+	/* Call the next hook function */
+	if (next_ExecutorCheckPerms_hook &&
+		!(*next_ExecutorCheckPerms_hook) (rangeTabls, abort))
+		return false;
+
+	return true;
+}
+
+/*
+ * Hook ProcessUtility to do session auditing for DDL and utility commands.
+ */
+static void
+pgaudit_ProcessUtility_hook(Node *parsetree,
+							 const char *queryString,
+							 ProcessUtilityContext context,
+							 ParamListInfo params,
+							 DestReceiver *dest,
+							 char *completionTag)
+{
+	AuditEventStackItem *stackItem = NULL;
+	int64 stackId = 0;
+
+	/*
+	 * Don't audit substatements.  All the substatements we care about should
+	 * be covered by the event triggers.
+	 */
+	if (context <= PROCESS_UTILITY_QUERY && !IsAbortedTransactionBlockState())
+	{
+		/* Process top level utility statement */
+		if (context == PROCESS_UTILITY_TOPLEVEL)
+		{
+			if (auditEventStack != NULL)
+				elog(ERROR, "pgaudit stack is not empty");
+
+			stackItem = stack_push();
+			stackItem->auditEvent.paramList = params;
+		}
+		else
+			stackItem = stack_push();
+
+		stackId = stackItem->stackId;
+		stackItem->auditEvent.logStmtLevel = GetCommandLogLevel(parsetree);
+		stackItem->auditEvent.commandTag = nodeTag(parsetree);
+		stackItem->auditEvent.command = CreateCommandTag(parsetree);
+		stackItem->auditEvent.commandText = queryString;
+
+		/*
+		 * If this is a DO block log it before calling the next ProcessUtility
+		 * hook.
+		 */
+		if (auditLogBitmap & LOG_FUNCTION &&
+			stackItem->auditEvent.commandTag == T_DoStmt &&
+			!IsAbortedTransactionBlockState())
+			log_audit_event(stackItem);
+	}
+
+	/* Call the standard process utility chain. */
+	if (next_ProcessUtility_hook)
+		(*next_ProcessUtility_hook) (parsetree, queryString, context,
+									 params, dest, completionTag);
+	else
+		standard_ProcessUtility(parsetree, queryString, context,
+								params, dest, completionTag);
+
+	/*
+	 * Process the audit event if there is one.  Also check that this event
+	 * was not popped off the stack by a memory context being free'd
+	 * elsewhere.
+	 */
+	if (stackItem && !IsAbortedTransactionBlockState())
+	{
+		/*
+		 * Make sure the item we want to log is still on the stack - if not
+		 * then something has gone wrong and an error will be raised.
+		 */
+		stack_valid(stackId);
+
+		/*
+		 * Log the utility command if logging is on, the command has not
+		 * already been logged by another hook, and the transaction is not
+		 * aborted.
+		 */
+		if (auditLogBitmap != 0 && !stackItem->auditEvent.logged)
+			log_audit_event(stackItem);
+	}
+}
+
+/*
+ * Hook object_access_hook to provide fully-qualified object names for function
+ * calls.
+ */
+static void
+pgaudit_object_access_hook(ObjectAccessType access,
+						   Oid classId,
+						   Oid objectId,
+						   int subId,
+						   void *arg)
+{
+	if (auditLogBitmap & LOG_FUNCTION && access == OAT_FUNCTION_EXECUTE &&
+		auditEventStack && !IsAbortedTransactionBlockState())
+		log_function_execute(objectId);
+
+	if (next_object_access_hook)
+		(*next_object_access_hook) (access, classId, objectId, subId, arg);
+}
+
+/*
+ * Event trigger functions
+ */
+
+/*
+ * Supply additional data for (non drop) statements that have event trigger
+ * support and can be deparsed.
+ *
+ * Drop statements are handled below through the older sql_drop event trigger.
+ */
+Datum
+pgaudit_ddl_command_end(PG_FUNCTION_ARGS)
+{
+	EventTriggerData *eventData;
+	int result,
+		row;
+	TupleDesc spiTupDesc;
+	const char *query;
+	MemoryContext contextQuery;
+	MemoryContext contextOld;
+
+	/* Continue only if session DDL logging is enabled */
+	if (~auditLogBitmap & LOG_DDL && ~auditLogBitmap & LOG_ROLE)
+		PG_RETURN_NULL();
+
+	/* Be sure the module was loaded */
+	if (!auditEventStack)
+		elog(ERROR, "pgaudit not loaded before call to "
+			 "pgaudit_ddl_command_end()");
+
+	/* This is an internal statement - do not log it */
+	internalStatement = true;
+
+	/* Make sure the fuction was fired as a trigger */
+	if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+		elog(ERROR, "not fired by event trigger manager");
+
+	/* Switch memory context for query */
+	contextQuery = AllocSetContextCreate(
+						CurrentMemoryContext,
+						"pgaudit_func_ddl_command_end temporary context",
+						ALLOCSET_DEFAULT_MINSIZE,
+						ALLOCSET_DEFAULT_INITSIZE,
+						ALLOCSET_DEFAULT_MAXSIZE);
+	contextOld = MemoryContextSwitchTo(contextQuery);
+
+	/* Get information about triggered events */
+	eventData = (EventTriggerData *) fcinfo->context;
+
+	auditEventStack->auditEvent.logStmtLevel =
+		GetCommandLogLevel(eventData->parsetree);
+	auditEventStack->auditEvent.commandTag =
+		nodeTag(eventData->parsetree);
+	auditEventStack->auditEvent.command =
+		CreateCommandTag(eventData->parsetree);
+
+	/* Return objects affected by the (non drop) DDL statement */
+	query = "SELECT UPPER(object_type), object_identity, UPPER(command_tag)\n"
+			"  FROM pg_catalog.pg_event_trigger_ddl_commands()";
+
+	/* Attempt to connect */
+	result = SPI_connect();
+	if (result < 0)
+		elog(ERROR, "pgaudit_ddl_command_end: SPI_connect returned %d",
+			 result);
+
+	/* Execute the query */
+	result = SPI_execute(query, true, 0);
+	if (result != SPI_OK_SELECT)
+		elog(ERROR, "pgaudit_ddl_command_end: SPI_execute returned %d",
+			 result);
+
+	/* Iterate returned rows */
+	spiTupDesc = SPI_tuptable->tupdesc;
+	for (row = 0; row < SPI_processed; row++)
+	{
+		HeapTuple	spiTuple;
+
+		spiTuple = SPI_tuptable->vals[row];
+
+		/* Supply object name and type for audit event */
+		auditEventStack->auditEvent.objectType =
+			SPI_getvalue(spiTuple, spiTupDesc, 1);
+		auditEventStack->auditEvent.objectName =
+			SPI_getvalue(spiTuple, spiTupDesc, 2);
+		auditEventStack->auditEvent.command =
+			SPI_getvalue(spiTuple, spiTupDesc, 3);
+
+		/*
+		 * Identify grant/revoke commands - these are the only non-DDL class
+		 * commands that should be coming through the event triggers.
+		 */
+		if (pg_strcasecmp(auditEventStack->auditEvent.command,
+						  COMMAND_GRANT) == 0 ||
+			pg_strcasecmp(auditEventStack->auditEvent.command,
+						  COMMAND_REVOKE) == 0)
+		{
+			NodeTag currentCommandTag = auditEventStack->auditEvent.commandTag;
+
+			auditEventStack->auditEvent.commandTag = T_GrantStmt;
+			log_audit_event(auditEventStack);
+
+			auditEventStack->auditEvent.commandTag = currentCommandTag;
+		}
+		else
+			log_audit_event(auditEventStack);
+	}
+
+	/* Complete the query */
+	SPI_finish();
+
+	MemoryContextSwitchTo(contextOld);
+	MemoryContextDelete(contextQuery);
+
+	/* No longer in an internal statement */
+	internalStatement = false;
+
+	PG_RETURN_NULL();
+}
+
+/*
+ * Supply additional data for drop statements that have event trigger support.
+ */
+Datum
+pgaudit_sql_drop(PG_FUNCTION_ARGS)
+{
+	int result,
+		row;
+	TupleDesc spiTupDesc;
+	const char *query;
+	MemoryContext contextQuery;
+	MemoryContext contextOld;
+
+	if (~auditLogBitmap & LOG_DDL)
+		PG_RETURN_NULL();
+
+	/* Be sure the module was loaded */
+	if (!auditEventStack)
+		elog(ERROR, "pgaudit not loaded before call to "
+			 "pgaudit_sql_drop()");
+
+	/* This is an internal statement - do not log it */
+	internalStatement = true;
+
+	/* Make sure the fuction was fired as a trigger */
+	if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
+		elog(ERROR, "not fired by event trigger manager");
+
+	/* Switch memory context for the query */
+	contextQuery = AllocSetContextCreate(
+						CurrentMemoryContext,
+						"pgaudit_func_ddl_command_end temporary context",
+						ALLOCSET_DEFAULT_MINSIZE,
+						ALLOCSET_DEFAULT_INITSIZE,
+						ALLOCSET_DEFAULT_MAXSIZE);
+	contextOld = MemoryContextSwitchTo(contextQuery);
+
+	/* Return objects affected by the drop statement */
+	query = "SELECT UPPER(object_type),\n"
+			"       object_identity\n"
+			"  FROM pg_catalog.pg_event_trigger_dropped_objects()\n"
+			" WHERE lower(object_type) <> 'type'\n"
+			"   AND schema_name <> 'pg_toast'";
+
+	/* Attempt to connect */
+	result = SPI_connect();
+	if (result < 0)
+		elog(ERROR, "pgaudit_ddl_drop: SPI_connect returned %d",
+			 result);
+
+	/* Execute the query */
+	result = SPI_execute(query, true, 0);
+	if (result != SPI_OK_SELECT)
+		elog(ERROR, "pgaudit_ddl_drop: SPI_execute returned %d",
+			 result);
+
+	/* Iterate returned rows */
+	spiTupDesc = SPI_tuptable->tupdesc;
+	for (row = 0; row < SPI_processed; row++)
+	{
+		HeapTuple spiTuple;
+
+		spiTuple = SPI_tuptable->vals[row];
+
+		auditEventStack->auditEvent.objectType =
+			SPI_getvalue(spiTuple, spiTupDesc, 1);
+		auditEventStack->auditEvent.objectName =
+			SPI_getvalue(spiTuple, spiTupDesc, 2);
+
+		log_audit_event(auditEventStack);
+	}
+
+	/* Complete the query */
+	SPI_finish();
+
+	MemoryContextSwitchTo(contextOld);
+	MemoryContextDelete(contextQuery);
+
+	/* No longer in an internal statement */
+	internalStatement = false;
+
+	PG_RETURN_NULL();
+}
+
+/*
+ * GUC check and assign functions
+ */
+
+/*
+ * Take a pgaudit.log value such as "read, write, dml", verify that each of the
+ * comma-separated tokens corresponds to a LogClass value, and convert them into
+ * a bitmap that log_audit_event can check.
+ */
+static bool
+check_pgaudit_log(char **newVal, void **extra, GucSource source)
+{
+	List *flagRawList;
+	char *rawVal;
+	ListCell *lt;
+	int *flags;
+
+	/* Make sure newval is a comma-separated list of tokens. */
+	rawVal = pstrdup(*newVal);
+	if (!SplitIdentifierString(rawVal, ',', &flagRawList))
+	{
+		GUC_check_errdetail("List syntax is invalid");
+		list_free(flagRawList);
+		pfree(rawVal);
+		return false;
+	}
+
+	/*
+	 * Check that we recognise each token, and add it to the bitmap we're
+	 * building up in a newly-allocated int *f.
+	 */
+	if (!(flags = (int *) malloc(sizeof(int))))
+		return false;
+
+	*flags = 0;
+
+	foreach(lt, flagRawList)
+	{
+		char *token = (char *) lfirst(lt);
+		bool subtract = false;
+		int class;
+
+		/* If token is preceded by -, then the token is subtractive */
+		if (token[0] == '-')
+		{
+			token++;
+			subtract = true;
+		}
+
+		/* Test each token */
+		if (pg_strcasecmp(token, CLASS_NONE) == 0)
+			class = LOG_NONE;
+		else if (pg_strcasecmp(token, CLASS_ALL) == 0)
+			class = LOG_ALL;
+		else if (pg_strcasecmp(token, CLASS_DDL) == 0)
+			class = LOG_DDL;
+		else if (pg_strcasecmp(token, CLASS_FUNCTION) == 0)
+			class = LOG_FUNCTION;
+		else if (pg_strcasecmp(token, CLASS_MISC) == 0)
+			class = LOG_MISC;
+		else if (pg_strcasecmp(token, CLASS_READ) == 0)
+			class = LOG_READ;
+		else if (pg_strcasecmp(token, CLASS_ROLE) == 0)
+			class = LOG_ROLE;
+		else if (pg_strcasecmp(token, CLASS_WRITE) == 0)
+			class = LOG_WRITE;
+		else
+		{
+			free(flags);
+			pfree(rawVal);
+			list_free(flagRawList);
+			return false;
+		}
+
+		/* Add or subtract class bits from the log bitmap */
+		if (subtract)
+			*flags &= ~class;
+		else
+			*flags |= class;
+	}
+
+	pfree(rawVal);
+	list_free(flagRawList);
+
+	/* Store the bitmap for assign_pgaudit_log */
+	*extra = flags;
+
+	return true;
+}
+
+/*
+ * Set pgaudit_log from extra (ignoring newVal, which has already been
+ * converted to a bitmap above). Note that extra may not be set if the
+ * assignment is to be suppressed.
+ */
+static void
+assign_pgaudit_log(const char *newVal, void *extra)
+{
+	if (extra)
+		auditLogBitmap = *(int *) extra;
+}
+
+/*
+ * Take a pgaudit.log_level value such as "debug" and check that is is valid.
+ * Return the enum value so it does not have to be checked again in the assign
+ * function.
+ */
+static bool
+check_pgaudit_log_level(char **newVal, void **extra, GucSource source)
+{
+	int *logLevel;
+
+	/* Allocate memory to store the log level */
+	if (!(logLevel = (int *) malloc(sizeof(int))))
+		return false;
+
+	/* Find the log level enum */
+	if (pg_strcasecmp(*newVal, "debug") == 0)
+		*logLevel = DEBUG2;
+	else if (pg_strcasecmp(*newVal, "debug5") == 0)
+		*logLevel = DEBUG5;
+	else if (pg_strcasecmp(*newVal, "debug4") == 0)
+		*logLevel = DEBUG4;
+	else if (pg_strcasecmp(*newVal, "debug3") == 0)
+		*logLevel = DEBUG3;
+	else if (pg_strcasecmp(*newVal, "debug2") == 0)
+		*logLevel = DEBUG2;
+	else if (pg_strcasecmp(*newVal, "debug1") == 0)
+		*logLevel = DEBUG1;
+	else if (pg_strcasecmp(*newVal, "info") == 0)
+		*logLevel = INFO;
+	else if (pg_strcasecmp(*newVal, "notice") == 0)
+		*logLevel = NOTICE;
+	else if (pg_strcasecmp(*newVal, "warning") == 0)
+		*logLevel = WARNING;
+	else if (pg_strcasecmp(*newVal, "log") == 0)
+		*logLevel = LOG;
+
+	/* Error if the log level enum is not found */
+	else
+	{
+		free(logLevel);
+		return false;
+	}
+
+	/* Return the log level enum */
+	*extra = logLevel;
+
+	return true;
+}
+
+/*
+ * Set pgaudit_log from extra (ignoring newVal, which has already been
+ * converted to an enum above). Note that extra may not be set if the
+ * assignment is to be suppressed.
+ */
+static void
+assign_pgaudit_log_level(const char *newVal, void *extra)
+{
+	if (extra)
+		auditLogLevel = *(int *) extra;
+}
+
+/*
+ * Define GUC variables and install hooks upon module load.
+ */
+void
+_PG_init(void)
+{
+	/* Must be loaded with shared_preload_libaries */
+	if (IsUnderPostmaster)
+		ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+				errmsg("pgaudit must be loaded via shared_preload_libraries")));
+
+	/* Define pgaudit.log */
+	DefineCustomStringVariable(
+		"pgaudit.log",
+
+		"Specifies which classes of statements will be logged by session audit "
+		"logging. Multiple classes can be provided using a comma-separated "
+		"list and classes can be subtracted by prefacing the class with a "
+		"- sign.",
+
+		NULL,
+		&auditLog,
+		"none",
+		PGC_SUSET,
+		GUC_LIST_INPUT | GUC_NOT_IN_SAMPLE,
+		check_pgaudit_log,
+		assign_pgaudit_log,
+		NULL);
+
+	/* Define pgaudit.log_catalog */
+	DefineCustomBoolVariable(
+		"pgaudit.log_catalog",
+
+		"Specifies that session logging should be enabled in the case where "
+		"all relations in a statement are in pg_catalog.  Disabling this "
+		 "setting will reduce noise in the log from tools like psql and PgAdmin "
+		"that query the catalog heavily.",
+
+		NULL,
+		&auditLogCatalog,
+		true,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_client */
+	DefineCustomBoolVariable(
+		"pgaudit.log_client",
+
+		"Specifies whether audit messages should be visible to the client. "
+		"This setting should generally be left disabled but may be useful for "
+		"debugging or other purposes.",
+
+		NULL,
+		&auditLogClient,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_level */
+	DefineCustomStringVariable(
+		"pgaudit.log_level",
+
+		"Specifies the log level that will be used for log entries. This "
+		"setting is used for regression testing and may also be useful to end "
+		"users for testing or other purposes.  It is not intended to be used "
+		"in a production environment as it may leak which statements are being "
+		"logged to the user.",
+
+		NULL,
+		&auditLogLevelString,
+		"log",
+		PGC_SUSET,
+		GUC_LIST_INPUT | GUC_NOT_IN_SAMPLE,
+		check_pgaudit_log_level,
+		assign_pgaudit_log_level,
+		NULL);
+
+	/* Define pgaudit.log_parameter */
+	DefineCustomBoolVariable(
+		"pgaudit.log_parameter",
+
+		"Specifies that audit logging should include the parameters that were "
+		"passed with the statement. When parameters are present they will be "
+		"be included in CSV format after the statement text.",
+
+		NULL,
+		&auditLogParameter,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_relation */
+	DefineCustomBoolVariable(
+		"pgaudit.log_relation",
+
+		"Specifies whether session audit logging should create a separate log "
+		"entry for each relation referenced in a SELECT or DML statement. "
+		"This is a useful shortcut for exhaustive logging without using object "
+		"audit logging.",
+
+		NULL,
+		&auditLogRelation,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.log_statement_once */
+	DefineCustomBoolVariable(
+		"pgaudit.log_statement_once",
+
+		"Specifies whether logging will include the statement text and "
+		"parameters with the first log entry for a statement/substatement "
+		"combination or with every entry.  Disabling this setting will result "
+		"in less verbose logging but may make it more difficult to determine "
+		"the statement that generated a log entry, though the "
+		"statement/substatement pair along with the process id should suffice "
+		"to identify the statement text logged with a previous entry.",
+
+		NULL,
+		&auditLogStatementOnce,
+		false,
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/* Define pgaudit.role */
+	DefineCustomStringVariable(
+		"pgaudit.role",
+
+		"Specifies the master role to use for object audit logging. Multiple "
+		"audit roles can be defined by granting them to the master role. This "
+		"allows multiple groups to be in charge of different aspects of audit "
+		"logging.",
+
+		NULL,
+		&auditRole,
+		"",
+		PGC_SUSET,
+		GUC_NOT_IN_SAMPLE,
+		NULL, NULL, NULL);
+
+	/*
+	 * Install our hook functions after saving the existing pointers to
+	 * preserve the chains.
+	 */
+	next_ExecutorStart_hook = ExecutorStart_hook;
+	ExecutorStart_hook = pgaudit_ExecutorStart_hook;
+
+	next_ExecutorCheckPerms_hook = ExecutorCheckPerms_hook;
+	ExecutorCheckPerms_hook = pgaudit_ExecutorCheckPerms_hook;
+
+	next_ProcessUtility_hook = ProcessUtility_hook;
+	ProcessUtility_hook = pgaudit_ProcessUtility_hook;
+
+	next_object_access_hook = object_access_hook;
+	object_access_hook = pgaudit_object_access_hook;
+
+	/* Log that the extension has completed initialization */
+	ereport(LOG, (errmsg("pgaudit extension initialized")));
+}
diff --git a/contrib/pgaudit/pgaudit.conf b/contrib/pgaudit/pgaudit.conf
new file mode 100644
index 0000000..cc6f09c
--- /dev/null
+++ b/contrib/pgaudit/pgaudit.conf
@@ -0,0 +1 @@
+shared_preload_libraries = pgaudit
diff --git a/contrib/pgaudit/pgaudit.control b/contrib/pgaudit/pgaudit.control
new file mode 100644
index 0000000..16612b2
--- /dev/null
+++ b/contrib/pgaudit/pgaudit.control
@@ -0,0 +1,5 @@
+# pgaudit extension
+comment = 'provides auditing functionality'
+default_version = '1.0'
+module_pathname = '$libdir/pgaudit'
+relocatable = true
pgaudit-v2-03.patchtext/plain; charset=UTF-8; name=pgaudit-v2-03.patch; x-mac-creator=0; x-mac-type=0Download
diff --git a/contrib/pgaudit/README.md b/contrib/pgaudit/README.md
new file mode 100644
index 0000000..3d14d7d
--- /dev/null
+++ b/contrib/pgaudit/README.md
@@ -0,0 +1,331 @@
+# PostgreSQL Audit Extension
+
+The PostgreSQL Audit extension (`pgaudit`) provides detailed session and/or object audit logging via the standard PostgreSQL logging facility.
+
+The goal of the PostgreSQL Audit extension (`pgaudit`) is to provide PostgreSQL users with capability to produce audit logs often required to comply with government, financial, or ISO certifications.
+
+An audit is an official inspection of an individual's or organization's accounts, typically by an independent body.  The information gathered by the PostgreSQL Audit extension (`pgaudit`) is properly called an audit trail or audit log.  The term audit log is used in this documentation.
+
+## Why PostgreSQL Audit Extension?
+
+Basic statement logging can be provided by the standard logging facility
+`log_statement = all`.  This is acceptable for monitoring and other usages but does not provide the level of detail generally required for an audit.  It is not enough to have a list of all the operations performed against the database. It must also be possible to find particular statements that are of interest to an auditor.  The standard logging facility shows what the user requested, while `pgaudit` focuses on the details of what happened while the database was satisfying the request.
+
+For example, an auditor may want to verify that a particular table was created inside a documented maintenance window.  This might seem like a simple job for grep, but what if you are presented with something like this (intentionally obfuscated) example:
+```
+BEGIN
+    EXECUTE 'CREATE TABLE import' || 'ant_table (id INT)';
+END $$;
+```
+Standard logging will give you this:
+```
+LOG:  statement: DO $$
+BEGIN
+    EXECUTE 'CREATE TABLE import' || 'ant_table (id INT)';
+END $$;
+```
+It appears that finding the table of interest may require some knowledge of the code in cases where tables are created dynamically.  This is not ideal since it would be preferable to just search on the table name. This is where `pgaudit` comes in.  For the same input, it will produce this output in the log:
+```
+AUDIT: SESSION,33,1,FUNCTION,DO,,,"DO $$
+BEGIN
+    EXECUTE 'CREATE TABLE import' || 'ant_table (id INT)';
+END $$;"
+AUDIT: SESSION,33,2,DDL,CREATE TABLE,TABLE,public.important_table,CREATE TABLE important_table (id INT)
+```
+Not only is the `DO` block logged, but substatement 2 contains the full text of the `CREATE TABLE` with the statement type, object type, and full-qualified name to make searches easy.
+
+When logging `SELECT` and `DML` statements, `pgaudit` can be configured to log a separate entry for each relation referenced in a statement.  No parsing is required to find all statements that touch a particular table.  In fact, the goal is that the statement text is provided primarily for deep forensics and should not be required for an audit.
+
+## Usage Considerations
+
+Depending on settings, it is possible for `pgaudit` to generate an enormous volume of logging.  Be careful to determine exactly what needs to be audit logged in your environment to avoid logging too much.
+
+For example, when working in an OLAP environment it would probably not be wise to audit log inserts into a large fact table.  The size of the log file will likely be many times the actual data size of the inserts because the log file is expressed as text.  Since logs are generally stored with the OS this may lead to disk space being exhausted very quickly.  In cases where it is not possible to limit audit logging to certain tables, be sure to assess the performance impact while testing and allocate plenty of space on the log volume.  This may also be true for OLTP environments.  Even if the insert volume is not as high, the performance impact of audit logging may still noticeably affect latency.
+
+To limit the number of relations audit logged for `SELECT` and `DML` statments, consider using object audit logging (see [Object Auditing](#object-auditing)).  Object audit logging allows selection of the relations to be logged allowing for reduction of the overall log volume.  However, when new relations are added they must be explicitly added to object audit logging.  A programmatic solution where specified tables are excluded from logging and all others are included may be a good option in this case.
+
+## Compile and Install
+
+Clone the PostgreSQL repository:
+```
+git clone https://github.com/postgres/postgres.git
+```
+Checkout REL9_5_STABLE branch:
+```
+git checkout REL9_5_STABLE
+```
+Make PostgreSQL:
+```
+./configure
+make install -s
+```
+Change to the contrib directory:
+```
+cd contrib
+```
+Clone the `pgaudit` extension:
+```
+git clone https://github.com/pgaudit/pgaudit.git
+```
+Change to `pgaudit` directory:
+```
+cd pgaudit
+```
+Build ``pgaudit`` and run regression tests:
+```
+make -s check
+```
+Install `pgaudit`:
+```
+make install
+```
+
+## Settings
+
+Settings may be modified only by a superuser. Allowing normal users to change their settings would defeat the point of an audit log.
+
+Settings can be specified globally (in `postgresql.conf` or using `ALTER SYSTEM ... SET`), at the database level (using `ALTER DATABASE ... SET`), or at the role level (using `ALTER ROLE ... SET`).  Note that settings are not inherited through normal role inheritance and `SET ROLE` will not alter a user's `pgaudit` settings.  This is a limitation of the roles system and not inherent to `pgaudit`.
+
+The `pgaudit` extension must be loaded in [shared_preload_libraries](http://www.postgresql.org/docs/9.5/static/runtime-config-client.html#GUC-SHARED-PRELOAD-LIBRARIES).  Otherwise, an error will be raised at load time and no audit logging will occur.
+
+### pgaudit.log
+
+Specifies which classes of statements will be logged by session audit logging.  Possible values are:
+
+* __READ__: `SELECT` and `COPY` when the source is a relation or a query.
+
+* __WRITE__: `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, and `COPY` when the destination is a relation.
+
+* __FUNCTION__: Function calls and `DO` blocks.
+
+* __ROLE__: Statements related to roles and privileges: `GRANT`, `REVOKE`, `CREATE/ALTER/DROP ROLE`.
+
+* __DDL__: All `DDL` that is not included in the `ROLE` class.
+
+* __MISC__: Miscellaneous commands, e.g. `DISCARD`, `FETCH`, `CHECKPOINT`, `VACUUM`.
+
+Multiple classes can be provided using a comma-separated list and classes can be subtracted by prefacing the class with a `-` sign (see [Session Audit Logging](#session-audit-logging)).
+
+The default is `none`.
+
+### pgaudit.log_catalog
+
+Specifies that session logging should be enabled in the case where all relations in a statement are in pg_catalog.  Disabling this setting will reduce noise in the log from tools like psql and PgAdmin that query the catalog heavily.
+
+The default is `on`.
+
+### pgaudit.log_level
+
+Specifies the log level that will be used for log entries (see [Message Severity Levels] (http://www.postgresql.org/docs/9.1/static/runtime-config-logging.html#RUNTIME-CONFIG-SEVERITY-LEVELS) for valid levels but note that `ERROR`, `FATAL`, and `PANIC` are not allowed). This setting is used for regression testing and may also be useful to end users for testing or other purposes.
+
+The default is `log`.
+
+### pgaudit.log_parameter
+
+Specifies that audit logging should include the parameters that were passed with the statement.  When parameters are present they will be included in CSV format after the statement text.
+
+The default is `off`.
+
+### pgaudit.log_relation
+
+Specifies whether session audit logging should create a separate log entry for each relation (`TABLE`, `VIEW`, etc.) referenced in a `SELECT` or `DML` statement.  This is a useful shortcut for exhaustive logging without using object audit logging.
+
+The default is `off`.
+
+### pgaudit.log_statement_once
+
+Specifies whether logging will include the statement text and parameters with the first log entry for a statement/substatement combination or with every entry.  Disabling this setting will result in less verbose logging but may make it more difficult to determine the statement that generated a log entry, though the statement/substatement pair along with the process id should suffice to identify the statement text logged with a previous entry.
+
+The default is `off`.
+
+### pgaudit.role
+
+Specifies the master role to use for object audit logging.  Muliple audit roles can be defined by granting them to the master role. This allows multiple groups to be in charge of different aspects of audit logging.
+
+There is no default.
+
+## Session Audit Logging
+
+Session audit logging provides detailed logs of all statements executed by a user in the backend.
+
+### Configuration
+
+Session logging is enabled with the [pgaudit.log](#pgauditlog) setting.
+
+Enable session logging for all `DML` and `DDL` and log all relations in `DML` statements:
+```
+set pgaudit.log = 'write, ddl';
+set pgaudit.log_relation = on;
+```
+Enable session logging for all commands except `MISC` and raise audit log messages as `NOTICE`:
+```
+set pgaudit.log = 'all, -misc';
+set pgaudit.log_level = notice;
+```
+
+### Example
+
+In this example session audit logging is used for logging `DDL` and `SELECT` statements.  Note that the insert statement is not logged since the `WRITE` class is not enabled
+
+SQL:
+```
+set pgaudit.log = 'read, ddl';
+
+create table account
+(
+    id int,
+    name text,
+    password text,
+    description text
+);
+
+insert into account (id, name, password, description)
+             values (1, 'user1', 'HASH1', 'blah, blah');
+
+select *
+    from account;
+```
+Log Output:
+```
+AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.account,create table account
+(
+    id int,
+    name text,
+    password text,
+    description text
+);
+AUDIT: SESSION,2,1,READ,SELECT,,,select *
+    from account
+```
+
+## Object Auditing
+
+Object audit logging logs statements that affect a particular relation. Only `SELECT`, `INSERT`, `UPDATE` and `DELETE` commands are supported.  `TRUNCATE` is not included in object audit logging.
+
+Object audit logging is intended to be a finer-grained replacement for `pgaudit.log = 'read, write'`.  As such, it may not make sense to use them in conjunction but one possible scenario would be to use session logging to capture each statement and then supplement that with object logging to get more detail about specific relations.
+
+### Configuration
+
+Object-level audit logging is implemented via the roles system.  The [pgaudit.role](#pgauditrole) setting defines the role that will be used for audit logging.  A relation (`TABLE`, `VIEW`, etc.) will be audit logged when the audit role has permissions for the command executed or inherits the permissions from another role.  This allows you to effectively have multiple audit roles even though there is a single master role in any context.
+
+Set [pgaudit.role](#pgauditrole) to `auditor` and grant `SELECT` and `DELETE` privileges on the `account` table. Any `SELECT` or `DELETE` statements on `account` will now be logged:
+```
+set pgaudit.role = 'auditor';
+
+grant select, delete
+   on public.account
+   to auditor;
+```
+
+### Example
+
+In this example object audit logging is used to illustrate how a granular approach may be taken towards logging of `SELECT` and `DML` statements.  Note that logging on the `account` table is controlled by column-level permissions, while logging on `account_role_map` is table-level.
+
+SQL:
+```
+set pgaudit.role = 'auditor';
+
+create table account
+(
+    id int,
+    name text,
+    password text,
+    description text
+);
+
+grant select (password)
+   on public.account
+   to auditor;
+
+select id, name
+  from account;
+
+select password
+  from account;
+
+grant update (name, password)
+   on public.account
+   to auditor;
+
+update account
+   set description = 'yada, yada';
+
+update account
+   set password = 'HASH2';
+
+create table account_role_map
+(
+    account_id int,
+    role_id int
+);
+
+grant select
+   on public.account_role_map
+   to auditor;
+
+select account.password,
+       account_role_map.role_id
+  from account
+       inner join account_role_map
+            on account.id = account_role_map.account_id
+```
+Log Output:
+```
+AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account,select password
+  from account
+AUDIT: OBJECT,2,1,WRITE,UPDATE,TABLE,public.account,update account
+   set password = 'HASH2'
+AUDIT: OBJECT,3,1,READ,SELECT,TABLE,public.account,select account.password,
+       account_role_map.role_id
+  from account
+       inner join account_role_map
+            on account.id = account_role_map.account_id
+AUDIT: OBJECT,3,1,READ,SELECT,TABLE,public.account_role_map,select account.password,
+       account_role_map.role_id
+  from account
+       inner join account_role_map
+            on account.id = account_role_map.account_id
+```
+
+## Format
+
+Audit entries are written to the standard logging facility and contain the following columns in comma-separated format:
+
+Output is compliant CSV format only if the log line prefix portion of each log entry is removed.
+
+* __AUDIT_TYPE__ - `SESSION` or `OBJECT`.
+* __STATEMENT_ID__ - Unique statement ID for this session. Each statement ID represents a backend call.  Statement IDs are sequential even if some statements are not logged.  There may be multiple entries for a statement ID when more than one relation is logged.
+
+* __SUBSTATEMENT_ID__ - Sequential ID for each substatement within the main statement.  For example, calling a function from a query.  Substatement IDs are continuous even if some substatements are not logged.  There may be multiple entries for a substatement ID when more than one relation is logged.
+
+* __CLASS__ - e.g. (`READ`, `ROLE`) (see [pgaudit.log](#pgauditlog)).
+* __COMMAND__ - e.g. `ALTER TABLE`, `SELECT`.
+
+* __OBJECT_TYPE__ - `TABLE`, `INDEX`, `VIEW`, etc. Available for `SELECT`, `DML` and most `DDL` statements.
+
+* __OBJECT_NAME__ - The fully-qualified object name (e.g. public.account).  Available for `SELECT`, `DML` and most `DDL` statements.
+
+* __STATEMENT__ - Statement executed on the backend.
+
+* __PARAMETER__ - If `pgaudit.log_parameter` is set then this field will contain the statement parameters as quoted CSV.
+
+Use [log_line_prefix](http://www.postgresql.org/docs/9.5/static/runtime-config-logging.html#GUC-LOG-LINE-PREFIX) to add any other fields that are needed to satisfy your audit log requirements.  A typical log line prefix might be `'%m %u %d: '` which would provide the date/time, user name, and database name for each audit log.
+
+## Caveats
+
+* Object renames are logged under the name they were renamed to. For example, renaming a table will produce the following result:
+
+```
+ALTER TABLE test RENAME TO test2;
+
+AUDIT: SESSION,36,1,DDL,ALTER TABLE,TABLE,public.test2,ALTER TABLE test RENAME TO test2
+```
+* It is possible to have a command logged more than once.  For example, when a table is created with a primary key specified at creation time the index for the primary key will be logged independently and another audit log will be made for the index under the create entry.  The multiple entries will however be contained within one statement ID.
+
+* Autovacuum and Autoanalyze are not logged.
+
+* Statements that are executed after a transaction enters an aborted state will not be audit logged.  However, the statement that caused the error and any subsequent statements executed in the aborted transaction will be logged as ERRORs by the standard logging facility.
+
+## Authors
+
+The PostgreSQL Audit Extension is based on the pgaudit project at https://github.com/2ndQuadrant authored by Abhijit Menon-Sen and Ian Barwick.  Further development has been done by David Steele.
diff --git a/contrib/pgaudit/expected/pgaudit.out b/contrib/pgaudit/expected/pgaudit.out
new file mode 100644
index 0000000..e7ee93b
--- /dev/null
+++ b/contrib/pgaudit/expected/pgaudit.out
@@ -0,0 +1,1123 @@
+\set VERBOSITY terse
+-- Create pgaudit extension
+CREATE EXTENSION IF NOT EXISTS pgaudit;
+--
+-- Audit log fields are:
+--     AUDIT_TYPE - SESSION or OBJECT
+--     STATEMENT_ID - ID of the statement in the current backend
+--     SUBSTATEMENT_ID - ID of the substatement in the current backend
+--     CLASS - Class of statement being logged (e.g. ROLE, READ, WRITE)
+--     COMMAND - e.g. SELECT, CREATE ROLE, UPDATE
+--     OBJECT_TYPE - When available, type of object acted on (e.g. TABLE, VIEW)
+--     OBJECT_NAME - When available, fully-qualified table of object
+--     STATEMENT - The statement being logged
+--     PARAMETER - If parameter logging is requested, they will follow the
+--                 statement
+SELECT current_user \gset
+--
+-- Set pgaudit parameters for the current (super)user.
+ALTER ROLE :current_user SET pgaudit.log = 'Role';
+ALTER ROLE :current_user SET pgaudit.log_level = 'notice';
+ALTER ROLE :current_user SET pgaudit.log_client = ON;
+-- After each connect, we need to load pgaudit, as if it was
+-- being loaded from shared_preload_libraries.  Otherwise, the hooks
+-- won't be set up and called correctly, leading to lots of ugly
+-- errors.
+\connect - :current_user;
+--
+-- Create auditor role
+CREATE ROLE auditor;
+NOTICE:  AUDIT: SESSION,1,1,ROLE,CREATE ROLE,,,CREATE ROLE auditor;,<not logged>
+--
+-- Create first test user
+CREATE USER user1;
+NOTICE:  AUDIT: SESSION,2,1,ROLE,CREATE ROLE,,,CREATE USER user1;,<not logged>
+ALTER ROLE user1 SET pgaudit.log = 'ddl, ROLE';
+NOTICE:  AUDIT: SESSION,3,1,ROLE,ALTER ROLE,,,"ALTER ROLE user1 SET pgaudit.log = 'ddl, ROLE';",<not logged>
+ALTER ROLE user1 SET pgaudit.log_level = 'notice';
+NOTICE:  AUDIT: SESSION,4,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log_level = 'notice';,<not logged>
+ALTER ROLE user1 SET pgaudit.log_client = ON;
+NOTICE:  AUDIT: SESSION,5,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log_client = ON;,<not logged>
+--
+-- Create, select, drop (select will not be audited)
+\connect - user1
+CREATE TABLE public.test
+(
+	id INT
+);
+NOTICE:  AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.test,"CREATE TABLE public.test
+(
+	id INT
+);",<not logged>
+SELECT *
+  FROM test;
+ id 
+----
+(0 rows)
+
+DROP TABLE test;
+NOTICE:  AUDIT: SESSION,2,1,DDL,DROP TABLE,TABLE,public.test,DROP TABLE test;,<not logged>
+--
+-- Create second test user
+\connect - :current_user
+CREATE USER user2;
+NOTICE:  AUDIT: SESSION,1,1,ROLE,CREATE ROLE,,,CREATE USER user2;,<not logged>
+ALTER ROLE user2 SET pgaudit.log = 'Read, writE';
+NOTICE:  AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,"ALTER ROLE user2 SET pgaudit.log = 'Read, writE';",<not logged>
+ALTER ROLE user2 SET pgaudit.log_catalog = OFF;
+NOTICE:  AUDIT: SESSION,3,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_catalog = OFF;,<not logged>
+ALTER ROLE user2 SET pgaudit.log_level = 'warning';
+NOTICE:  AUDIT: SESSION,4,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_level = 'warning';,<not logged>
+ALTER ROLE user2 SET pgaudit.log_client = ON;
+NOTICE:  AUDIT: SESSION,5,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_client = ON;,<not logged>
+ALTER ROLE user2 SET pgaudit.role = auditor;
+NOTICE:  AUDIT: SESSION,6,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.role = auditor;,<not logged>
+ALTER ROLE user2 SET pgaudit.log_statement_once = ON;
+NOTICE:  AUDIT: SESSION,7,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_statement_once = ON;,<not logged>
+--
+-- Setup role-based tests
+CREATE TABLE test2
+(
+	id INT
+);
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test2
+   TO user2, user1;
+NOTICE:  AUDIT: SESSION,8,1,ROLE,GRANT,TABLE,,"GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test2
+   TO user2, user1;",<not logged>
+GRANT SELECT, UPDATE
+   ON TABLE public.test2
+   TO auditor;
+NOTICE:  AUDIT: SESSION,9,1,ROLE,GRANT,TABLE,,"GRANT SELECT, UPDATE
+   ON TABLE public.test2
+   TO auditor;",<not logged>
+CREATE TABLE test3
+(
+	id INT
+);
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test3
+   TO user2;
+NOTICE:  AUDIT: SESSION,10,1,ROLE,GRANT,TABLE,,"GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test3
+   TO user2;",<not logged>
+GRANT INSERT
+   ON TABLE public.test3
+   TO auditor;
+NOTICE:  AUDIT: SESSION,11,1,ROLE,GRANT,TABLE,,"GRANT INSERT
+   ON TABLE public.test3
+   TO auditor;",<not logged>
+CREATE FUNCTION test2_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 90
+	 WHERE id = new.id;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_insert() OWNER TO user1;
+CREATE TRIGGER test2_insert_trg
+	AFTER INSERT ON test2
+	FOR EACH ROW EXECUTE PROCEDURE test2_insert();
+CREATE FUNCTION test2_change(change_id int) RETURNS void AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 1
+	 WHERE id = change_id;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_change(int) OWNER TO user2;
+CREATE VIEW vw_test3 AS
+SELECT *
+  FROM test3;
+GRANT SELECT
+   ON vw_test3
+   TO user2;
+NOTICE:  AUDIT: SESSION,12,1,ROLE,GRANT,TABLE,,"GRANT SELECT
+   ON vw_test3
+   TO user2;",<not logged>
+GRANT SELECT
+   ON vw_test3
+   TO auditor;
+NOTICE:  AUDIT: SESSION,13,1,ROLE,GRANT,TABLE,,"GRANT SELECT
+   ON vw_test3
+   TO auditor;",<not logged>
+\connect - user2
+--
+-- Role-based tests
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	  LIMIT 1
+) SUBQUERY;
+ count 
+-------
+     1
+(1 row)
+
+SELECT *
+  FROM test3, test2;
+WARNING:  AUDIT: SESSION,1,1,READ,SELECT,,,"SELECT *
+  FROM test3, test2;",<not logged>
+WARNING:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+ id | id 
+----+----
+(0 rows)
+
+--
+-- Object logged because of:
+-- select on vw_test3
+-- select on test2
+SELECT *
+  FROM vw_test3, test2;
+WARNING:  AUDIT: SESSION,2,1,READ,SELECT,,,"SELECT *
+  FROM vw_test3, test2;",<not logged>
+WARNING:  AUDIT: OBJECT,2,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+WARNING:  AUDIT: OBJECT,2,1,READ,SELECT,VIEW,public.vw_test3,<previously logged>,<previously logged>
+ id | id 
+----+----
+(0 rows)
+
+--
+-- Object logged because of:
+-- insert on test3
+-- select on test2
+WITH CTE AS
+(
+	SELECT id
+	  FROM test2
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+WARNING:  AUDIT: SESSION,3,1,WRITE,INSERT,,,"WITH CTE AS
+(
+	SELECT id
+	  FROM test2
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;",<not logged>
+WARNING:  AUDIT: OBJECT,3,1,WRITE,INSERT,TABLE,public.test3,<previously logged>,<previously logged>
+WARNING:  AUDIT: OBJECT,3,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Object logged because of:
+-- insert on test3
+WITH CTE AS
+(
+	INSERT INTO test3 VALUES (1)
+				   RETURNING id
+)
+INSERT INTO test2
+SELECT id
+  FROM cte;
+WARNING:  AUDIT: SESSION,4,1,WRITE,INSERT,,,"WITH CTE AS
+(
+	INSERT INTO test3 VALUES (1)
+				   RETURNING id
+)
+INSERT INTO test2
+SELECT id
+  FROM cte;",<not logged>
+WARNING:  AUDIT: OBJECT,4,1,WRITE,INSERT,TABLE,public.test3,<previously logged>,<previously logged>
+DO $$ BEGIN PERFORM test2_change(91); END $$;
+WARNING:  AUDIT: SESSION,5,1,READ,SELECT,,,SELECT test2_change(91),<not logged>
+WARNING:  AUDIT: SESSION,5,2,WRITE,UPDATE,,,"UPDATE test2
+	   SET id = id + 1
+	 WHERE id = change_id",<not logged>
+WARNING:  AUDIT: OBJECT,5,2,WRITE,UPDATE,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Object logged because of:
+-- insert on test3
+-- update on test2
+WITH CTE AS
+(
+	UPDATE test2
+	   SET id = 45
+	 WHERE id = 92
+	RETURNING id
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+WARNING:  AUDIT: SESSION,6,1,WRITE,INSERT,,,"WITH CTE AS
+(
+	UPDATE test2
+	   SET id = 45
+	 WHERE id = 92
+	RETURNING id
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;",<not logged>
+WARNING:  AUDIT: OBJECT,6,1,WRITE,INSERT,TABLE,public.test3,<previously logged>,<previously logged>
+WARNING:  AUDIT: OBJECT,6,1,WRITE,UPDATE,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Object logged because of:
+-- insert on test2
+WITH CTE AS
+(
+	INSERT INTO test2 VALUES (37)
+				   RETURNING id
+)
+UPDATE test3
+   SET id = cte.id
+  FROM cte
+ WHERE test3.id <> cte.id;
+WARNING:  AUDIT: SESSION,7,1,WRITE,UPDATE,,,"WITH CTE AS
+(
+	INSERT INTO test2 VALUES (37)
+				   RETURNING id
+)
+UPDATE test3
+   SET id = cte.id
+  FROM cte
+ WHERE test3.id <> cte.id;",<not logged>
+WARNING:  AUDIT: OBJECT,7,1,WRITE,INSERT,TABLE,public.test2,<previously logged>,<previously logged>
+--
+-- Be sure that test has correct contents
+SELECT *
+  FROM test2
+ ORDER BY ID;
+WARNING:  AUDIT: SESSION,8,1,READ,SELECT,,,"SELECT *
+  FROM test2
+ ORDER BY ID;",<not logged>
+WARNING:  AUDIT: OBJECT,8,1,READ,SELECT,TABLE,public.test2,<previously logged>,<previously logged>
+ id  
+-----
+  45
+ 127
+(2 rows)
+
+--
+-- Change permissions of user 2 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user2 SET pgaudit.log = 'NONE';
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log = 'NONE';,<not logged>
+\connect - user2
+--
+-- Create test4 and add permissions
+CREATE TABLE test4
+(
+	id int,
+	name text
+);
+GRANT SELECT (name)
+   ON TABLE public.test4
+   TO auditor;
+GRANT UPDATE (id)
+   ON TABLE public.test4
+   TO auditor;
+GRANT insert (name)
+   ON TABLE public.test4
+   TO auditor;
+--
+-- Not object logged
+SELECT id
+  FROM public.test4;
+ id 
+----
+(0 rows)
+
+--
+-- Object logged because of:
+-- select (name) on test4
+SELECT name
+  FROM public.test4;
+WARNING:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.test4,"SELECT name
+  FROM public.test4;",<not logged>
+ name 
+------
+(0 rows)
+
+--
+-- Not object logged
+INSERT INTO public.test4 (id)
+				  VALUES (1);
+--
+-- Object logged because of:
+-- insert (name) on test4
+INSERT INTO public.test4 (name)
+				  VALUES ('test');
+WARNING:  AUDIT: OBJECT,2,1,WRITE,INSERT,TABLE,public.test4,"INSERT INTO public.test4 (name)
+				  VALUES ('test');",<not logged>
+--
+-- Not object logged
+UPDATE public.test4
+   SET name = 'foo';
+--
+-- Object logged because of:
+-- update (id) on test4
+UPDATE public.test4
+   SET id = 1;
+WARNING:  AUDIT: OBJECT,3,1,WRITE,UPDATE,TABLE,public.test4,"UPDATE public.test4
+   SET id = 1;",<not logged>
+--
+-- Object logged because of:
+-- update (name) on test4
+-- update (name) takes precedence over select (name) due to ordering
+update public.test4 set name = 'foo' where name = 'bar';
+WARNING:  AUDIT: OBJECT,4,1,WRITE,UPDATE,TABLE,public.test4,update public.test4 set name = 'foo' where name = 'bar';,<not logged>
+--
+-- Change permissions of user 1 so that session logging will be done
+\connect - :current_user
+--
+-- Drop test tables
+DROP TABLE test2;
+DROP VIEW vw_test3;
+DROP TABLE test3;
+DROP TABLE test4;
+DROP FUNCTION test2_insert();
+DROP FUNCTION test2_change(int);
+ALTER ROLE user1 SET pgaudit.log = 'DDL, READ';
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,"ALTER ROLE user1 SET pgaudit.log = 'DDL, READ';",<not logged>
+\connect - user1
+--
+-- Create table is session logged
+CREATE TABLE public.account
+(
+	id INT,
+	name TEXT,
+	password TEXT,
+	description TEXT
+);
+NOTICE:  AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.account,"CREATE TABLE public.account
+(
+	id INT,
+	name TEXT,
+	password TEXT,
+	description TEXT
+);",<not logged>
+--
+-- Select is session logged
+SELECT *
+  FROM account;
+NOTICE:  AUDIT: SESSION,2,1,READ,SELECT,,,"SELECT *
+  FROM account;",<not logged>
+ id | name | password | description 
+----+------+----------+-------------
+(0 rows)
+
+--
+-- Insert is not logged
+INSERT INTO account (id, name, password, description)
+			 VALUES (1, 'user1', 'HASH1', 'blah, blah');
+--
+-- Change permissions of user 1 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log = 'none';
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log = 'none';,<not logged>
+ALTER ROLE user1 SET pgaudit.role = 'auditor';
+NOTICE:  AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.role = 'auditor';,<not logged>
+\connect - user1
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT (password),
+	  UPDATE (name, password)
+   ON TABLE public.account
+   TO auditor;
+--
+-- Not object logged
+SELECT id,
+	   name
+  FROM account;
+ id | name  
+----+-------
+  1 | user1
+(1 row)
+
+--
+-- Object logged because of:
+-- select (password) on account
+SELECT password
+  FROM account;
+NOTICE:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account,"SELECT password
+  FROM account;",<not logged>
+ password 
+----------
+ HASH1
+(1 row)
+
+--
+-- Not object logged
+UPDATE account
+   SET description = 'yada, yada';
+--
+-- Object logged because of:
+-- update (password) on account
+UPDATE account
+   SET password = 'HASH2';
+NOTICE:  AUDIT: OBJECT,2,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET password = 'HASH2';",<not logged>
+--
+-- Change permissions of user 1 so that session relation logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log_relation = on;
+NOTICE:  AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pgaudit.log_relation = on;,<not logged>
+ALTER ROLE user1 SET pgaudit.log = 'read, WRITE';
+NOTICE:  AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,"ALTER ROLE user1 SET pgaudit.log = 'read, WRITE';",<not logged>
+\connect - user1
+--
+-- Not logged
+CREATE TABLE ACCOUNT_ROLE_MAP
+(
+	account_id INT,
+	role_id INT
+);
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT
+   ON TABLE public.account_role_map
+   TO auditor;
+--
+-- Object logged because of:
+-- select (password) on account
+-- select on account_role_map
+-- Session logged on all tables because log = read and log_relation = on
+SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;
+NOTICE:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+NOTICE:  AUDIT: SESSION,1,1,READ,SELECT,TABLE,public.account,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+NOTICE:  AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.account_role_map,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+NOTICE:  AUDIT: SESSION,1,1,READ,SELECT,TABLE,public.account_role_map,"SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;",<not logged>
+ password | role_id 
+----------+---------
+(0 rows)
+
+--
+-- Object logged because of:
+-- select (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+SELECT password
+  FROM account;
+NOTICE:  AUDIT: OBJECT,2,1,READ,SELECT,TABLE,public.account,"SELECT password
+  FROM account;",<not logged>
+NOTICE:  AUDIT: SESSION,2,1,READ,SELECT,TABLE,public.account,"SELECT password
+  FROM account;",<not logged>
+ password 
+----------
+ HASH2
+(1 row)
+
+--
+-- Not object logged
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada';
+NOTICE:  AUDIT: SESSION,3,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET description = 'yada, yada';",<not logged>
+--
+-- Object logged because of:
+-- select (password) on account (in the where clause)
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';
+NOTICE:  AUDIT: OBJECT,4,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';",<not logged>
+NOTICE:  AUDIT: SESSION,4,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';",<not logged>
+--
+-- Object logged because of:
+-- update (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET password = 'HASH2';
+NOTICE:  AUDIT: OBJECT,5,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET password = 'HASH2';",<not logged>
+NOTICE:  AUDIT: SESSION,5,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
+   SET password = 'HASH2';",<not logged>
+--
+-- Change back to superuser to do exhaustive tests
+\connect - :current_user
+SET pgaudit.log = 'ALL';
+NOTICE:  AUDIT: SESSION,1,1,MISC,SET,,,SET pgaudit.log = 'ALL';,<not logged>
+SET pgaudit.log_level = 'notice';
+NOTICE:  AUDIT: SESSION,2,1,MISC,SET,,,SET pgaudit.log_level = 'notice';,<not logged>
+SET pgaudit.log_client = ON;
+NOTICE:  AUDIT: SESSION,3,1,MISC,SET,,,SET pgaudit.log_client = ON;,<not logged>
+SET pgaudit.log_relation = ON;
+NOTICE:  AUDIT: SESSION,4,1,MISC,SET,,,SET pgaudit.log_relation = ON;,<not logged>
+SET pgaudit.log_parameter = ON;
+NOTICE:  AUDIT: SESSION,5,1,MISC,SET,,,SET pgaudit.log_parameter = ON;,<none>
+--
+-- Simple DO block
+DO $$
+BEGIN
+	raise notice 'test';
+END $$;
+NOTICE:  AUDIT: SESSION,6,1,FUNCTION,DO,,,"DO $$
+BEGIN
+	raise notice 'test';
+END $$;",<none>
+NOTICE:  test
+--
+-- Create test schema
+CREATE SCHEMA test;
+NOTICE:  AUDIT: SESSION,7,1,DDL,CREATE SCHEMA,SCHEMA,test,CREATE SCHEMA test;,<none>
+--
+-- Copy account to stdout
+COPY account TO stdout;
+NOTICE:  AUDIT: SESSION,8,1,READ,SELECT,TABLE,public.account,COPY account TO stdout;,<none>
+1	user1	HASH2	yada, yada
+--
+-- Create a table from a query
+CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;
+NOTICE:  AUDIT: SESSION,9,1,READ,SELECT,TABLE,public.account,"CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;",<none>
+NOTICE:  AUDIT: SESSION,9,1,WRITE,INSERT,TABLE,test.account_copy,"CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;",<none>
+NOTICE:  AUDIT: SESSION,9,2,DDL,CREATE TABLE AS,TABLE,test.account_copy,"CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;",<none>
+--
+-- Copy from stdin to account copy
+COPY test.account_copy from stdin;
+NOTICE:  AUDIT: SESSION,10,1,WRITE,INSERT,TABLE,test.account_copy,COPY test.account_copy from stdin;,<none>
+--
+-- Test prepared statement
+PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;
+NOTICE:  AUDIT: SESSION,11,1,READ,PREPARE,,,"PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;",<none>
+EXECUTE pgclassstmt (1);
+NOTICE:  AUDIT: SESSION,12,1,READ,SELECT,TABLE,public.account,"PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;",1
+NOTICE:  AUDIT: SESSION,12,2,MISC,EXECUTE,,,EXECUTE pgclassstmt (1);,<none>
+ id | name  | password | description 
+----+-------+----------+-------------
+  1 | user1 | HASH2    | yada, yada
+(1 row)
+
+DEALLOCATE pgclassstmt;
+NOTICE:  AUDIT: SESSION,13,1,MISC,DEALLOCATE,,,DEALLOCATE pgclassstmt;,<none>
+--
+-- Test cursor
+BEGIN;
+NOTICE:  AUDIT: SESSION,14,1,MISC,BEGIN,,,BEGIN;,<none>
+DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+NOTICE:  AUDIT: SESSION,15,1,READ,SELECT,TABLE,pg_catalog.pg_class,"DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;",<none>
+NOTICE:  AUDIT: SESSION,15,2,READ,DECLARE CURSOR,,,"DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;",<none>
+FETCH NEXT FROM ctest;
+NOTICE:  AUDIT: SESSION,16,1,MISC,FETCH,,,FETCH NEXT FROM ctest;,<none>
+ count 
+-------
+     1
+(1 row)
+
+CLOSE ctest;
+NOTICE:  AUDIT: SESSION,17,1,MISC,CLOSE CURSOR,,,CLOSE ctest;,<none>
+COMMIT;
+NOTICE:  AUDIT: SESSION,18,1,MISC,COMMIT,,,COMMIT;,<none>
+--
+-- Turn off log_catalog and pg_class will not be logged
+SET pgaudit.log_catalog = OFF;
+NOTICE:  AUDIT: SESSION,19,1,MISC,SET,,,SET pgaudit.log_catalog = OFF;,<none>
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+ count 
+-------
+     1
+(1 row)
+
+--
+-- Test prepared insert
+CREATE TABLE test.test_insert
+(
+	id INT
+);
+NOTICE:  AUDIT: SESSION,20,1,DDL,CREATE TABLE,TABLE,test.test_insert,"CREATE TABLE test.test_insert
+(
+	id INT
+);",<none>
+PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);
+NOTICE:  AUDIT: SESSION,21,1,WRITE,PREPARE,,,"PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);",<none>
+EXECUTE pgclassstmt (1);
+NOTICE:  AUDIT: SESSION,22,1,WRITE,INSERT,TABLE,test.test_insert,"PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);",1
+NOTICE:  AUDIT: SESSION,22,2,MISC,EXECUTE,,,EXECUTE pgclassstmt (1);,<none>
+--
+-- Check that primary key creation is logged
+CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);
+NOTICE:  AUDIT: SESSION,23,1,DDL,CREATE TABLE,TABLE,public.test,"CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);",<none>
+NOTICE:  AUDIT: SESSION,23,1,DDL,CREATE INDEX,INDEX,public.test_pkey,"CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);",<none>
+--
+-- Check that analyze is logged
+ANALYZE test;
+NOTICE:  AUDIT: SESSION,24,1,MISC,ANALYZE,,,ANALYZE test;,<none>
+--
+-- Grants to public should not cause object logging (session logging will
+-- still happen)
+GRANT SELECT
+  ON TABLE public.test
+  TO PUBLIC;
+NOTICE:  AUDIT: SESSION,25,1,ROLE,GRANT,TABLE,,"GRANT SELECT
+  ON TABLE public.test
+  TO PUBLIC;",<none>
+SELECT *
+  FROM test;
+NOTICE:  AUDIT: SESSION,26,1,READ,SELECT,TABLE,public.test,"SELECT *
+  FROM test;",<none>
+ id | name | description 
+----+------+-------------
+(0 rows)
+
+-- Check that statements without columns log
+SELECT
+  FROM test;
+NOTICE:  AUDIT: SESSION,27,1,READ,SELECT,TABLE,public.test,"SELECT
+  FROM test;",<none>
+--
+(0 rows)
+
+SELECT 1,
+	   substring('Thomas' from 2 for 3);
+NOTICE:  AUDIT: SESSION,28,1,READ,SELECT,,,"SELECT 1,
+	   substring('Thomas' from 2 for 3);",<none>
+ ?column? | substring 
+----------+-----------
+        1 | hom
+(1 row)
+
+DO $$
+DECLARE
+	test INT;
+BEGIN
+	SELECT 1
+	  INTO test;
+END $$;
+NOTICE:  AUDIT: SESSION,29,1,FUNCTION,DO,,,"DO $$
+DECLARE
+	test INT;
+BEGIN
+	SELECT 1
+	  INTO test;
+END $$;",<none>
+NOTICE:  AUDIT: SESSION,29,2,READ,SELECT,,,SELECT 1,<none>
+explain select 1;
+NOTICE:  AUDIT: SESSION,30,1,READ,SELECT,,,explain select 1;,<none>
+NOTICE:  AUDIT: SESSION,30,2,MISC,EXPLAIN,,,explain select 1;,<none>
+                QUERY PLAN                
+------------------------------------------
+ Result  (cost=0.00..0.01 rows=1 width=0)
+(1 row)
+
+--
+-- Test that looks inside of do blocks log
+INSERT INTO TEST (id)
+		  VALUES (1);
+NOTICE:  AUDIT: SESSION,31,1,WRITE,INSERT,TABLE,public.test,"INSERT INTO TEST (id)
+		  VALUES (1);",<none>
+INSERT INTO TEST (id)
+		  VALUES (2);
+NOTICE:  AUDIT: SESSION,32,1,WRITE,INSERT,TABLE,public.test,"INSERT INTO TEST (id)
+		  VALUES (2);",<none>
+INSERT INTO TEST (id)
+		  VALUES (3);
+NOTICE:  AUDIT: SESSION,33,1,WRITE,INSERT,TABLE,public.test,"INSERT INTO TEST (id)
+		  VALUES (3);",<none>
+DO $$
+DECLARE
+	result RECORD;
+BEGIN
+	FOR result IN
+		SELECT id
+		  FROM test
+	LOOP
+		INSERT INTO test (id)
+			 VALUES (result.id + 100);
+	END LOOP;
+END $$;
+NOTICE:  AUDIT: SESSION,34,1,FUNCTION,DO,,,"DO $$
+DECLARE
+	result RECORD;
+BEGIN
+	FOR result IN
+		SELECT id
+		  FROM test
+	LOOP
+		INSERT INTO test (id)
+			 VALUES (result.id + 100);
+	END LOOP;
+END $$;",<none>
+NOTICE:  AUDIT: SESSION,34,2,READ,SELECT,TABLE,public.test,"SELECT id
+		  FROM test",<none>
+NOTICE:  AUDIT: SESSION,34,3,WRITE,INSERT,TABLE,public.test,"INSERT INTO test (id)
+			 VALUES (result.id + 100)","f,,"
+NOTICE:  AUDIT: SESSION,34,4,WRITE,INSERT,TABLE,public.test,"INSERT INTO test (id)
+			 VALUES (result.id + 100)","t,,"
+NOTICE:  AUDIT: SESSION,34,5,WRITE,INSERT,TABLE,public.test,"INSERT INTO test (id)
+			 VALUES (result.id + 100)","t,,"
+--
+-- Test obfuscated dynamic sql for clean logging
+DO $$
+DECLARE
+	table_name TEXT = 'do_table';
+BEGIN
+	EXECUTE 'CREATE TABLE ' || table_name || ' ("weird name" INT)';
+	EXECUTE 'DROP table ' || table_name;
+END $$;
+NOTICE:  AUDIT: SESSION,35,1,FUNCTION,DO,,,"DO $$
+DECLARE
+	table_name TEXT = 'do_table';
+BEGIN
+	EXECUTE 'CREATE TABLE ' || table_name || ' (""weird name"" INT)';
+	EXECUTE 'DROP table ' || table_name;
+END $$;",<none>
+NOTICE:  AUDIT: SESSION,35,2,DDL,CREATE TABLE,TABLE,public.do_table,"CREATE TABLE do_table (""weird name"" INT)",<none>
+NOTICE:  AUDIT: SESSION,35,3,DDL,DROP TABLE,TABLE,public.do_table,DROP table do_table,<none>
+--
+-- Generate an error and make sure the stack gets cleared
+DO $$
+BEGIN
+	CREATE TABLE bogus.test_block
+	(
+		id INT
+	);
+END $$;
+NOTICE:  AUDIT: SESSION,36,1,FUNCTION,DO,,,"DO $$
+BEGIN
+	CREATE TABLE bogus.test_block
+	(
+		id INT
+	);
+END $$;",<none>
+ERROR:  schema "bogus" does not exist at character 14
+--
+-- Test alter table statements
+ALTER TABLE public.test
+	DROP COLUMN description ;
+NOTICE:  AUDIT: SESSION,37,1,DDL,ALTER TABLE,TABLE COLUMN,public.test.description,"ALTER TABLE public.test
+	DROP COLUMN description ;",<none>
+NOTICE:  AUDIT: SESSION,37,1,DDL,ALTER TABLE,TABLE,public.test,"ALTER TABLE public.test
+	DROP COLUMN description ;",<none>
+ALTER TABLE public.test
+	RENAME TO test2;
+NOTICE:  AUDIT: SESSION,38,1,DDL,ALTER TABLE,TABLE,public.test2,"ALTER TABLE public.test
+	RENAME TO test2;",<none>
+ALTER TABLE public.test2
+	SET SCHEMA test;
+NOTICE:  AUDIT: SESSION,39,1,DDL,ALTER TABLE,TABLE,test.test2,"ALTER TABLE public.test2
+	SET SCHEMA test;",<none>
+ALTER TABLE test.test2
+	ADD COLUMN description TEXT;
+NOTICE:  AUDIT: SESSION,40,1,DDL,ALTER TABLE,TABLE,test.test2,"ALTER TABLE test.test2
+	ADD COLUMN description TEXT;",<none>
+ALTER TABLE test.test2
+	DROP COLUMN description;
+NOTICE:  AUDIT: SESSION,41,1,DDL,ALTER TABLE,TABLE COLUMN,test.test2.description,"ALTER TABLE test.test2
+	DROP COLUMN description;",<none>
+NOTICE:  AUDIT: SESSION,41,1,DDL,ALTER TABLE,TABLE,test.test2,"ALTER TABLE test.test2
+	DROP COLUMN description;",<none>
+DROP TABLE test.test2;
+NOTICE:  AUDIT: SESSION,42,1,DDL,DROP TABLE,TABLE,test.test2,DROP TABLE test.test2;,<none>
+NOTICE:  AUDIT: SESSION,42,1,DDL,DROP TABLE,TABLE CONSTRAINT,test_pkey on test.test2,DROP TABLE test.test2;,<none>
+NOTICE:  AUDIT: SESSION,42,1,DDL,DROP TABLE,INDEX,test.test_pkey,DROP TABLE test.test2;,<none>
+--
+-- Test multiple statements with one semi-colon
+CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);
+NOTICE:  AUDIT: SESSION,43,1,DDL,CREATE SCHEMA,SCHEMA,foo,"CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);",<none>
+NOTICE:  AUDIT: SESSION,43,1,DDL,CREATE TABLE,TABLE,foo.bar,"CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);",<none>
+NOTICE:  AUDIT: SESSION,43,1,DDL,CREATE TABLE,TABLE,foo.baz,"CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);",<none>
+--
+-- Test aggregate
+CREATE FUNCTION public.int_add
+(
+	a INT,
+	b INT
+)
+	RETURNS INT LANGUAGE plpgsql AS $$
+BEGIN
+	return a + b;
+END $$;
+NOTICE:  AUDIT: SESSION,44,1,DDL,CREATE FUNCTION,FUNCTION,"public.int_add(integer,integer)","CREATE FUNCTION public.int_add
+(
+	a INT,
+	b INT
+)
+	RETURNS INT LANGUAGE plpgsql AS $$
+BEGIN
+	return a + b;
+END $$;",<none>
+SELECT int_add(1, 1);
+NOTICE:  AUDIT: SESSION,45,1,READ,SELECT,,,"SELECT int_add(1, 1);",<none>
+NOTICE:  AUDIT: SESSION,45,2,FUNCTION,EXECUTE,FUNCTION,public.int_add,"SELECT int_add(1, 1);",<none>
+ int_add 
+---------
+       2
+(1 row)
+
+CREATE AGGREGATE public.sum_test(INT) (SFUNC=public.int_add, STYPE=INT, INITCOND='0');
+NOTICE:  AUDIT: SESSION,46,1,DDL,CREATE AGGREGATE,AGGREGATE,public.sum_test(integer),"CREATE AGGREGATE public.sum_test(INT) (SFUNC=public.int_add, STYPE=INT, INITCOND='0');",<none>
+ALTER AGGREGATE public.sum_test(integer) RENAME TO sum_test2;
+NOTICE:  AUDIT: SESSION,47,1,DDL,ALTER AGGREGATE,AGGREGATE,public.sum_test2(integer),ALTER AGGREGATE public.sum_test(integer) RENAME TO sum_test2;,<none>
+--
+-- Test conversion
+CREATE CONVERSION public.conversion_test FOR 'SQL_ASCII' TO 'MULE_INTERNAL' FROM pg_catalog.ascii_to_mic;
+NOTICE:  AUDIT: SESSION,48,1,DDL,CREATE CONVERSION,CONVERSION,public.conversion_test,CREATE CONVERSION public.conversion_test FOR 'SQL_ASCII' TO 'MULE_INTERNAL' FROM pg_catalog.ascii_to_mic;,<none>
+ALTER CONVERSION public.conversion_test RENAME TO conversion_test2;
+NOTICE:  AUDIT: SESSION,49,1,DDL,ALTER CONVERSION,CONVERSION,public.conversion_test2,ALTER CONVERSION public.conversion_test RENAME TO conversion_test2;,<none>
+--
+-- Test create/alter/drop database
+CREATE DATABASE contrib_regression_pgaudit;
+NOTICE:  AUDIT: SESSION,50,1,DDL,CREATE DATABASE,,,CREATE DATABASE contrib_regression_pgaudit;,<none>
+ALTER DATABASE contrib_regression_pgaudit RENAME TO contrib_regression_pgaudit2;
+NOTICE:  AUDIT: SESSION,51,1,DDL,ALTER DATABASE,,,ALTER DATABASE contrib_regression_pgaudit RENAME TO contrib_regression_pgaudit2;,<none>
+DROP DATABASE contrib_regression_pgaudit2;
+NOTICE:  AUDIT: SESSION,52,1,DDL,DROP DATABASE,,,DROP DATABASE contrib_regression_pgaudit2;,<none>
+-- Test role as a substmt
+SET pgaudit.log = 'ROLE';
+CREATE TABLE t ();
+CREATE ROLE alice;
+NOTICE:  AUDIT: SESSION,53,1,ROLE,CREATE ROLE,,,CREATE ROLE alice;,<none>
+CREATE SCHEMA foo2
+	GRANT SELECT
+	   ON public.t
+	   TO alice;
+NOTICE:  AUDIT: SESSION,54,1,ROLE,GRANT,TABLE,,"CREATE SCHEMA foo2
+	GRANT SELECT
+	   ON public.t
+	   TO alice;",<none>
+drop table public.t;
+drop role alice;
+NOTICE:  AUDIT: SESSION,55,1,ROLE,DROP ROLE,,,drop role alice;,<none>
+--
+-- Test that frees a memory context earlier than expected
+SET pgaudit.log = 'ALL';
+NOTICE:  AUDIT: SESSION,56,1,MISC,SET,,,SET pgaudit.log = 'ALL';,<none>
+CREATE TABLE hoge
+(
+	id int
+);
+NOTICE:  AUDIT: SESSION,57,1,DDL,CREATE TABLE,TABLE,public.hoge,"CREATE TABLE hoge
+(
+	id int
+);",<none>
+CREATE FUNCTION test()
+	RETURNS INT AS $$
+DECLARE
+	cur1 cursor for select * from hoge;
+	tmp int;
+BEGIN
+	OPEN cur1;
+	FETCH cur1 into tmp;
+	RETURN tmp;
+END $$
+LANGUAGE plpgsql ;
+NOTICE:  AUDIT: SESSION,58,1,DDL,CREATE FUNCTION,FUNCTION,public.test(),"CREATE FUNCTION test()
+	RETURNS INT AS $$
+DECLARE
+	cur1 cursor for select * from hoge;
+	tmp int;
+BEGIN
+	OPEN cur1;
+	FETCH cur1 into tmp;
+	RETURN tmp;
+END $$
+LANGUAGE plpgsql ;",<none>
+SELECT test();
+NOTICE:  AUDIT: SESSION,59,1,READ,SELECT,,,SELECT test();,<none>
+NOTICE:  AUDIT: SESSION,59,2,FUNCTION,EXECUTE,FUNCTION,public.test,SELECT test();,<none>
+NOTICE:  AUDIT: SESSION,59,3,READ,SELECT,TABLE,public.hoge,select * from hoge,<none>
+ test 
+------
+     
+(1 row)
+
+--
+-- Delete all rows then delete 1 row
+SET pgaudit.log = 'write';
+SET pgaudit.role = 'auditor';
+create table bar
+(
+	col int
+);
+grant delete
+   on bar
+   to auditor;
+insert into bar (col)
+		 values (1);
+NOTICE:  AUDIT: SESSION,60,1,WRITE,INSERT,TABLE,public.bar,"insert into bar (col)
+		 values (1);",<none>
+delete from bar;
+NOTICE:  AUDIT: OBJECT,61,1,WRITE,DELETE,TABLE,public.bar,delete from bar;,<none>
+NOTICE:  AUDIT: SESSION,61,1,WRITE,DELETE,TABLE,public.bar,delete from bar;,<none>
+insert into bar (col)
+		 values (1);
+NOTICE:  AUDIT: SESSION,62,1,WRITE,INSERT,TABLE,public.bar,"insert into bar (col)
+		 values (1);",<none>
+delete from bar
+ where col = 1;
+NOTICE:  AUDIT: OBJECT,63,1,WRITE,DELETE,TABLE,public.bar,"delete from bar
+ where col = 1;",<none>
+NOTICE:  AUDIT: SESSION,63,1,WRITE,DELETE,TABLE,public.bar,"delete from bar
+ where col = 1;",<none>
+drop table bar;
+--
+-- Grant roles to each other
+SET pgaudit.log = 'role';
+GRANT user1 TO user2;
+NOTICE:  AUDIT: SESSION,64,1,ROLE,GRANT ROLE,,,GRANT user1 TO user2;,<none>
+REVOKE user1 FROM user2;
+NOTICE:  AUDIT: SESSION,65,1,ROLE,REVOKE ROLE,,,REVOKE user1 FROM user2;,<none>
+--
+-- Test that FK references do not log but triggers still do
+SET pgaudit.log = 'READ,WRITE';
+SET pgaudit.role TO 'auditor';
+SET pgaudit.log_parameter TO OFF;
+CREATE TABLE aaa
+(
+	ID int primary key
+);
+CREATE TABLE bbb
+(
+	id int
+		references aaa(id)
+);
+CREATE FUNCTION bbb_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE bbb set id = new.id + 1;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql;
+CREATE TRIGGER bbb_insert_trg
+	AFTER INSERT ON bbb
+	FOR EACH ROW EXECUTE PROCEDURE bbb_insert();
+GRANT SELECT
+   ON aaa
+   TO auditor;
+GRANT UPDATE
+   ON bbb
+   TO auditor;
+INSERT INTO aaa VALUES (generate_series(1,100));
+NOTICE:  AUDIT: SESSION,66,1,WRITE,INSERT,TABLE,public.aaa,"INSERT INTO aaa VALUES (generate_series(1,100));",<not logged>
+INSERT INTO bbb VALUES (1);
+NOTICE:  AUDIT: SESSION,67,1,WRITE,INSERT,TABLE,public.bbb,INSERT INTO bbb VALUES (1);,<not logged>
+NOTICE:  AUDIT: OBJECT,67,2,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+NOTICE:  AUDIT: SESSION,67,2,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+NOTICE:  AUDIT: OBJECT,67,3,WRITE,UPDATE,TABLE,public.bbb,UPDATE bbb set id = new.id + 1,<not logged>
+NOTICE:  AUDIT: SESSION,67,3,WRITE,UPDATE,TABLE,public.bbb,UPDATE bbb set id = new.id + 1,<not logged>
+NOTICE:  AUDIT: OBJECT,67,4,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+NOTICE:  AUDIT: SESSION,67,4,WRITE,UPDATE,TABLE,public.aaa,"SELECT 1 FROM ONLY ""public"".""aaa"" x WHERE ""id"" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x",<not logged>
+DROP TABLE bbb;
+DROP TABLE aaa;
+-- Cleanup
+-- Set client_min_messages up to warning to avoid noise
+SET client_min_messages = 'warning';
+ALTER ROLE :current_user RESET pgaudit.log;
+ALTER ROLE :current_user RESET pgaudit.log_catalog;
+ALTER ROLE :current_user RESET pgaudit.log_level;
+ALTER ROLE :current_user RESET pgaudit.log_parameter;
+ALTER ROLE :current_user RESET pgaudit.log_relation;
+ALTER ROLE :current_user RESET pgaudit.log_statement_once;
+ALTER ROLE :current_user RESET pgaudit.role;
+RESET pgaudit.log;
+RESET pgaudit.log_catalog;
+RESET pgaudit.log_level;
+RESET pgaudit.log_parameter;
+RESET pgaudit.log_relation;
+RESET pgaudit.log_statement_once;
+RESET pgaudit.role;
+DROP TABLE test.account_copy;
+DROP TABLE test.test_insert;
+DROP SCHEMA test;
+DROP TABLE foo.bar;
+DROP TABLE foo.baz;
+DROP SCHEMA foo;
+DROP TABLE hoge;
+DROP TABLE account;
+DROP TABLE account_role_map;
+DROP USER user2;
+DROP USER user1;
+DROP ROLE auditor;
+RESET client_min_messages;
diff --git a/contrib/pgaudit/sql/pgaudit.sql b/contrib/pgaudit/sql/pgaudit.sql
new file mode 100644
index 0000000..1c81d70
--- /dev/null
+++ b/contrib/pgaudit/sql/pgaudit.sql
@@ -0,0 +1,780 @@
+\set VERBOSITY terse
+
+-- Create pgaudit extension
+CREATE EXTENSION IF NOT EXISTS pgaudit;
+
+--
+-- Audit log fields are:
+--     AUDIT_TYPE - SESSION or OBJECT
+--     STATEMENT_ID - ID of the statement in the current backend
+--     SUBSTATEMENT_ID - ID of the substatement in the current backend
+--     CLASS - Class of statement being logged (e.g. ROLE, READ, WRITE)
+--     COMMAND - e.g. SELECT, CREATE ROLE, UPDATE
+--     OBJECT_TYPE - When available, type of object acted on (e.g. TABLE, VIEW)
+--     OBJECT_NAME - When available, fully-qualified table of object
+--     STATEMENT - The statement being logged
+--     PARAMETER - If parameter logging is requested, they will follow the
+--                 statement
+
+SELECT current_user \gset
+
+--
+-- Set pgaudit parameters for the current (super)user.
+ALTER ROLE :current_user SET pgaudit.log = 'Role';
+ALTER ROLE :current_user SET pgaudit.log_level = 'notice';
+ALTER ROLE :current_user SET pgaudit.log_client = ON;
+
+-- After each connect, we need to load pgaudit, as if it was
+-- being loaded from shared_preload_libraries.  Otherwise, the hooks
+-- won't be set up and called correctly, leading to lots of ugly
+-- errors.
+\connect - :current_user;
+
+--
+-- Create auditor role
+CREATE ROLE auditor;
+
+--
+-- Create first test user
+CREATE USER user1;
+ALTER ROLE user1 SET pgaudit.log = 'ddl, ROLE';
+ALTER ROLE user1 SET pgaudit.log_level = 'notice';
+ALTER ROLE user1 SET pgaudit.log_client = ON;
+
+--
+-- Create, select, drop (select will not be audited)
+\connect - user1
+
+CREATE TABLE public.test
+(
+	id INT
+);
+
+SELECT *
+  FROM test;
+
+DROP TABLE test;
+
+--
+-- Create second test user
+\connect - :current_user
+
+CREATE USER user2;
+ALTER ROLE user2 SET pgaudit.log = 'Read, writE';
+ALTER ROLE user2 SET pgaudit.log_catalog = OFF;
+ALTER ROLE user2 SET pgaudit.log_level = 'warning';
+ALTER ROLE user2 SET pgaudit.log_client = ON;
+ALTER ROLE user2 SET pgaudit.role = auditor;
+ALTER ROLE user2 SET pgaudit.log_statement_once = ON;
+
+--
+-- Setup role-based tests
+CREATE TABLE test2
+(
+	id INT
+);
+
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test2
+   TO user2, user1;
+
+GRANT SELECT, UPDATE
+   ON TABLE public.test2
+   TO auditor;
+
+CREATE TABLE test3
+(
+	id INT
+);
+
+GRANT SELECT, INSERT, UPDATE, DELETE
+   ON test3
+   TO user2;
+
+GRANT INSERT
+   ON TABLE public.test3
+   TO auditor;
+
+CREATE FUNCTION test2_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 90
+	 WHERE id = new.id;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_insert() OWNER TO user1;
+
+CREATE TRIGGER test2_insert_trg
+	AFTER INSERT ON test2
+	FOR EACH ROW EXECUTE PROCEDURE test2_insert();
+
+CREATE FUNCTION test2_change(change_id int) RETURNS void AS $$
+BEGIN
+	UPDATE test2
+	   SET id = id + 1
+	 WHERE id = change_id;
+END $$ LANGUAGE plpgsql security definer;
+ALTER FUNCTION test2_change(int) OWNER TO user2;
+
+CREATE VIEW vw_test3 AS
+SELECT *
+  FROM test3;
+
+GRANT SELECT
+   ON vw_test3
+   TO user2;
+
+GRANT SELECT
+   ON vw_test3
+   TO auditor;
+
+\connect - user2
+
+--
+-- Role-based tests
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	  LIMIT 1
+) SUBQUERY;
+
+SELECT *
+  FROM test3, test2;
+
+--
+-- Object logged because of:
+-- select on vw_test3
+-- select on test2
+SELECT *
+  FROM vw_test3, test2;
+
+--
+-- Object logged because of:
+-- insert on test3
+-- select on test2
+WITH CTE AS
+(
+	SELECT id
+	  FROM test2
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+
+--
+-- Object logged because of:
+-- insert on test3
+WITH CTE AS
+(
+	INSERT INTO test3 VALUES (1)
+				   RETURNING id
+)
+INSERT INTO test2
+SELECT id
+  FROM cte;
+
+DO $$ BEGIN PERFORM test2_change(91); END $$;
+
+--
+-- Object logged because of:
+-- insert on test3
+-- update on test2
+WITH CTE AS
+(
+	UPDATE test2
+	   SET id = 45
+	 WHERE id = 92
+	RETURNING id
+)
+INSERT INTO test3
+SELECT id
+  FROM cte;
+
+--
+-- Object logged because of:
+-- insert on test2
+WITH CTE AS
+(
+	INSERT INTO test2 VALUES (37)
+				   RETURNING id
+)
+UPDATE test3
+   SET id = cte.id
+  FROM cte
+ WHERE test3.id <> cte.id;
+
+--
+-- Be sure that test has correct contents
+SELECT *
+  FROM test2
+ ORDER BY ID;
+
+--
+-- Change permissions of user 2 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user2 SET pgaudit.log = 'NONE';
+
+\connect - user2
+
+--
+-- Create test4 and add permissions
+CREATE TABLE test4
+(
+	id int,
+	name text
+);
+
+GRANT SELECT (name)
+   ON TABLE public.test4
+   TO auditor;
+
+GRANT UPDATE (id)
+   ON TABLE public.test4
+   TO auditor;
+
+GRANT insert (name)
+   ON TABLE public.test4
+   TO auditor;
+
+--
+-- Not object logged
+SELECT id
+  FROM public.test4;
+
+--
+-- Object logged because of:
+-- select (name) on test4
+SELECT name
+  FROM public.test4;
+
+--
+-- Not object logged
+INSERT INTO public.test4 (id)
+				  VALUES (1);
+
+--
+-- Object logged because of:
+-- insert (name) on test4
+INSERT INTO public.test4 (name)
+				  VALUES ('test');
+
+--
+-- Not object logged
+UPDATE public.test4
+   SET name = 'foo';
+
+--
+-- Object logged because of:
+-- update (id) on test4
+UPDATE public.test4
+   SET id = 1;
+
+--
+-- Object logged because of:
+-- update (name) on test4
+-- update (name) takes precedence over select (name) due to ordering
+update public.test4 set name = 'foo' where name = 'bar';
+
+--
+-- Change permissions of user 1 so that session logging will be done
+\connect - :current_user
+
+--
+-- Drop test tables
+DROP TABLE test2;
+DROP VIEW vw_test3;
+DROP TABLE test3;
+DROP TABLE test4;
+DROP FUNCTION test2_insert();
+DROP FUNCTION test2_change(int);
+
+ALTER ROLE user1 SET pgaudit.log = 'DDL, READ';
+\connect - user1
+
+--
+-- Create table is session logged
+CREATE TABLE public.account
+(
+	id INT,
+	name TEXT,
+	password TEXT,
+	description TEXT
+);
+
+--
+-- Select is session logged
+SELECT *
+  FROM account;
+
+--
+-- Insert is not logged
+INSERT INTO account (id, name, password, description)
+			 VALUES (1, 'user1', 'HASH1', 'blah, blah');
+
+--
+-- Change permissions of user 1 so that only object logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log = 'none';
+ALTER ROLE user1 SET pgaudit.role = 'auditor';
+\connect - user1
+
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT (password),
+	  UPDATE (name, password)
+   ON TABLE public.account
+   TO auditor;
+
+--
+-- Not object logged
+SELECT id,
+	   name
+  FROM account;
+
+--
+-- Object logged because of:
+-- select (password) on account
+SELECT password
+  FROM account;
+
+--
+-- Not object logged
+UPDATE account
+   SET description = 'yada, yada';
+
+--
+-- Object logged because of:
+-- update (password) on account
+UPDATE account
+   SET password = 'HASH2';
+
+--
+-- Change permissions of user 1 so that session relation logging will be done
+\connect - :current_user
+ALTER ROLE user1 SET pgaudit.log_relation = on;
+ALTER ROLE user1 SET pgaudit.log = 'read, WRITE';
+\connect - user1
+
+--
+-- Not logged
+CREATE TABLE ACCOUNT_ROLE_MAP
+(
+	account_id INT,
+	role_id INT
+);
+
+--
+-- ROLE class not set, so auditor grants not logged
+GRANT SELECT
+   ON TABLE public.account_role_map
+   TO auditor;
+
+--
+-- Object logged because of:
+-- select (password) on account
+-- select on account_role_map
+-- Session logged on all tables because log = read and log_relation = on
+SELECT account.password,
+	   account_role_map.role_id
+  FROM account
+	   INNER JOIN account_role_map
+			on account.id = account_role_map.account_id;
+
+--
+-- Object logged because of:
+-- select (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+SELECT password
+  FROM account;
+
+--
+-- Not object logged
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada';
+
+--
+-- Object logged because of:
+-- select (password) on account (in the where clause)
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET description = 'yada, yada'
+ where password = 'HASH2';
+
+--
+-- Object logged because of:
+-- update (password) on account
+-- Session logged on all tables because log = read and log_relation = on
+UPDATE account
+   SET password = 'HASH2';
+
+--
+-- Change back to superuser to do exhaustive tests
+\connect - :current_user
+SET pgaudit.log = 'ALL';
+SET pgaudit.log_level = 'notice';
+SET pgaudit.log_client = ON;
+SET pgaudit.log_relation = ON;
+SET pgaudit.log_parameter = ON;
+
+--
+-- Simple DO block
+DO $$
+BEGIN
+	raise notice 'test';
+END $$;
+
+--
+-- Create test schema
+CREATE SCHEMA test;
+
+--
+-- Copy account to stdout
+COPY account TO stdout;
+
+--
+-- Create a table from a query
+CREATE TABLE test.account_copy AS
+SELECT *
+  FROM account;
+
+--
+-- Copy from stdin to account copy
+COPY test.account_copy from stdin;
+1	user1	HASH2	yada, yada
+\.
+
+--
+-- Test prepared statement
+PREPARE pgclassstmt (oid) AS
+SELECT *
+  FROM account
+ WHERE id = $1;
+
+EXECUTE pgclassstmt (1);
+DEALLOCATE pgclassstmt;
+
+--
+-- Test cursor
+BEGIN;
+
+DECLARE ctest SCROLL CURSOR FOR
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+
+FETCH NEXT FROM ctest;
+CLOSE ctest;
+COMMIT;
+
+--
+-- Turn off log_catalog and pg_class will not be logged
+SET pgaudit.log_catalog = OFF;
+
+SELECT count(*)
+  FROM
+(
+	SELECT relname
+	  FROM pg_class
+	 LIMIT 1
+ ) subquery;
+
+--
+-- Test prepared insert
+CREATE TABLE test.test_insert
+(
+	id INT
+);
+
+PREPARE pgclassstmt (oid) AS
+INSERT INTO test.test_insert (id)
+					  VALUES ($1);
+EXECUTE pgclassstmt (1);
+
+--
+-- Check that primary key creation is logged
+CREATE TABLE public.test
+(
+	id INT,
+	name TEXT,
+	description TEXT,
+	CONSTRAINT test_pkey PRIMARY KEY (id)
+);
+
+--
+-- Check that analyze is logged
+ANALYZE test;
+
+--
+-- Grants to public should not cause object logging (session logging will
+-- still happen)
+GRANT SELECT
+  ON TABLE public.test
+  TO PUBLIC;
+
+SELECT *
+  FROM test;
+
+-- Check that statements without columns log
+SELECT
+  FROM test;
+
+SELECT 1,
+	   substring('Thomas' from 2 for 3);
+
+DO $$
+DECLARE
+	test INT;
+BEGIN
+	SELECT 1
+	  INTO test;
+END $$;
+
+explain select 1;
+
+--
+-- Test that looks inside of do blocks log
+INSERT INTO TEST (id)
+		  VALUES (1);
+INSERT INTO TEST (id)
+		  VALUES (2);
+INSERT INTO TEST (id)
+		  VALUES (3);
+
+DO $$
+DECLARE
+	result RECORD;
+BEGIN
+	FOR result IN
+		SELECT id
+		  FROM test
+	LOOP
+		INSERT INTO test (id)
+			 VALUES (result.id + 100);
+	END LOOP;
+END $$;
+
+--
+-- Test obfuscated dynamic sql for clean logging
+DO $$
+DECLARE
+	table_name TEXT = 'do_table';
+BEGIN
+	EXECUTE 'CREATE TABLE ' || table_name || ' ("weird name" INT)';
+	EXECUTE 'DROP table ' || table_name;
+END $$;
+
+--
+-- Generate an error and make sure the stack gets cleared
+DO $$
+BEGIN
+	CREATE TABLE bogus.test_block
+	(
+		id INT
+	);
+END $$;
+
+--
+-- Test alter table statements
+ALTER TABLE public.test
+	DROP COLUMN description ;
+
+ALTER TABLE public.test
+	RENAME TO test2;
+
+ALTER TABLE public.test2
+	SET SCHEMA test;
+
+ALTER TABLE test.test2
+	ADD COLUMN description TEXT;
+
+ALTER TABLE test.test2
+	DROP COLUMN description;
+
+DROP TABLE test.test2;
+
+--
+-- Test multiple statements with one semi-colon
+CREATE SCHEMA foo
+	CREATE TABLE foo.bar (id int)
+	CREATE TABLE foo.baz (id int);
+
+--
+-- Test aggregate
+CREATE FUNCTION public.int_add
+(
+	a INT,
+	b INT
+)
+	RETURNS INT LANGUAGE plpgsql AS $$
+BEGIN
+	return a + b;
+END $$;
+
+SELECT int_add(1, 1);
+
+CREATE AGGREGATE public.sum_test(INT) (SFUNC=public.int_add, STYPE=INT, INITCOND='0');
+ALTER AGGREGATE public.sum_test(integer) RENAME TO sum_test2;
+
+--
+-- Test conversion
+CREATE CONVERSION public.conversion_test FOR 'SQL_ASCII' TO 'MULE_INTERNAL' FROM pg_catalog.ascii_to_mic;
+ALTER CONVERSION public.conversion_test RENAME TO conversion_test2;
+
+--
+-- Test create/alter/drop database
+CREATE DATABASE contrib_regression_pgaudit;
+ALTER DATABASE contrib_regression_pgaudit RENAME TO contrib_regression_pgaudit2;
+DROP DATABASE contrib_regression_pgaudit2;
+
+-- Test role as a substmt
+SET pgaudit.log = 'ROLE';
+
+CREATE TABLE t ();
+CREATE ROLE alice;
+
+CREATE SCHEMA foo2
+	GRANT SELECT
+	   ON public.t
+	   TO alice;
+
+drop table public.t;
+drop role alice;
+
+--
+-- Test that frees a memory context earlier than expected
+SET pgaudit.log = 'ALL';
+
+CREATE TABLE hoge
+(
+	id int
+);
+
+CREATE FUNCTION test()
+	RETURNS INT AS $$
+DECLARE
+	cur1 cursor for select * from hoge;
+	tmp int;
+BEGIN
+	OPEN cur1;
+	FETCH cur1 into tmp;
+	RETURN tmp;
+END $$
+LANGUAGE plpgsql ;
+
+SELECT test();
+
+--
+-- Delete all rows then delete 1 row
+SET pgaudit.log = 'write';
+SET pgaudit.role = 'auditor';
+
+create table bar
+(
+	col int
+);
+
+grant delete
+   on bar
+   to auditor;
+
+insert into bar (col)
+		 values (1);
+delete from bar;
+
+insert into bar (col)
+		 values (1);
+delete from bar
+ where col = 1;
+
+drop table bar;
+
+--
+-- Grant roles to each other
+SET pgaudit.log = 'role';
+GRANT user1 TO user2;
+REVOKE user1 FROM user2;
+
+--
+-- Test that FK references do not log but triggers still do
+SET pgaudit.log = 'READ,WRITE';
+SET pgaudit.role TO 'auditor';
+SET pgaudit.log_parameter TO OFF;
+
+CREATE TABLE aaa
+(
+	ID int primary key
+);
+
+CREATE TABLE bbb
+(
+	id int
+		references aaa(id)
+);
+
+CREATE FUNCTION bbb_insert() RETURNS TRIGGER AS $$
+BEGIN
+	UPDATE bbb set id = new.id + 1;
+
+	RETURN new;
+END $$ LANGUAGE plpgsql;
+
+CREATE TRIGGER bbb_insert_trg
+	AFTER INSERT ON bbb
+	FOR EACH ROW EXECUTE PROCEDURE bbb_insert();
+
+GRANT SELECT
+   ON aaa
+   TO auditor;
+
+GRANT UPDATE
+   ON bbb
+   TO auditor;
+
+INSERT INTO aaa VALUES (generate_series(1,100));
+INSERT INTO bbb VALUES (1);
+
+DROP TABLE bbb;
+DROP TABLE aaa;
+
+-- Cleanup
+-- Set client_min_messages up to warning to avoid noise
+SET client_min_messages = 'warning';
+
+ALTER ROLE :current_user RESET pgaudit.log;
+ALTER ROLE :current_user RESET pgaudit.log_catalog;
+ALTER ROLE :current_user RESET pgaudit.log_level;
+ALTER ROLE :current_user RESET pgaudit.log_parameter;
+ALTER ROLE :current_user RESET pgaudit.log_relation;
+ALTER ROLE :current_user RESET pgaudit.log_statement_once;
+ALTER ROLE :current_user RESET pgaudit.role;
+
+RESET pgaudit.log;
+RESET pgaudit.log_catalog;
+RESET pgaudit.log_level;
+RESET pgaudit.log_parameter;
+RESET pgaudit.log_relation;
+RESET pgaudit.log_statement_once;
+RESET pgaudit.role;
+
+DROP TABLE test.account_copy;
+DROP TABLE test.test_insert;
+DROP SCHEMA test;
+DROP TABLE foo.bar;
+DROP TABLE foo.baz;
+DROP SCHEMA foo;
+DROP TABLE hoge;
+DROP TABLE account;
+DROP TABLE account_role_map;
+DROP USER user2;
+DROP USER user1;
+DROP ROLE auditor;
+
+RESET client_min_messages;
#9David Steele
david@pgmasters.net
In reply to: Tom Lane (#7)
Re: PostgreSQL Audit Extension

On 2/1/16 4:19 PM, Tom Lane wrote:

Alvaro Herrera <alvherre@2ndquadrant.com> writes:

.... Anyway I think the tests here are
massive and the code is not; perhaps people get the mistaken impression
that this is a huge amount of code which scares them. Perhaps you could
split it up in (1) code and (2) tests, which wouldn't achieve any
technical benefit but would offer some psychological comfort to
potential reviewers. You know it's all psychology in these parts.

Perhaps the tests could be made less bulky. We do not need massive
permanent regression tests for a single feature, IMO.

I'd certainly like to but pgaudit uses a lot of different techniques to
log various commands and there are a number of GUCs. Each test provides
coverage for a different code path.

I'm sure they could be reorganized and tightened up a but I don't think
by a whole lot.

--
-David
david@pgmasters.net

#10Noah Misch
noah@leadboat.com
In reply to: Alvaro Herrera (#6)
Re: PostgreSQL Audit Extension

On Mon, Feb 01, 2016 at 09:59:01PM +0100, Alvaro Herrera wrote:

Anyway I think the tests here are massive

I would not want to see fewer tests. When I reviewed a previous incarnation
of pgaudit, the tests saved me hours of writing my own.

and the code is not;

Nah; the patch size category is the same regardless of how you arrange the
tests. The tests constitute less than half of the overall change.

perhaps people get the mistaken impression
that this is a huge amount of code which scares them. Perhaps you could
split it up in (1) code and (2) tests, which wouldn't achieve any
technical benefit but would offer some psychological comfort to
potential reviewers. You know it's all psychology in these parts.

That's harmless. If it makes someone feel better, great.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#11Robert Haas
robertmhaas@gmail.com
In reply to: Alvaro Herrera (#4)
Re: PostgreSQL Audit Extension

On Sun, Jan 31, 2016 at 5:19 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:

Joshua D. Drake wrote:

On 01/31/2016 05:07 AM, Alvaro Herrera wrote:

David Steele wrote:

The attached patch implements audit logging for PostgreSQL as an
extension. I believe I have addressed the concerns that were raised at
the end of the 9.5 development cycle.

This patch got no feedback at all during the commitfest. I think there
is some interest on auditing capabilities so it's annoying and
surprising that this has no movement at all.

If the lack of activity means lack of interest, please can we all keep
what we've been doing in this thread, which is to ignore it, so that we
can just mark this as rejected. Otherwise, please chime in. I'm giving
this patch some more time by moving it to next commitfest instead.

From my perspective, lack of activity means since it doesn't have a
technical requirement to be in -core, it doesn't need to be.

Well, from mine it doesn't mean that. We kind of assume that "no answer
means yes"; but here what it really means is that nobody had time to
look at it and think about it, so it stalled (and so have many other
patches BTW). But if you or others think that this patch belongs into
PGXN, it's good to have that opinion in an email, so that the author can
think about your perspective and agree or disagree with it. Just by
expression that opinion, a thousand other hackers might vote +1 or -1 on
your proposal -- either way a clear sign. Silence doesn't let us move
forward, and we punt the patch to the next CF and let inertia continue.
That's not good.

OK, I'll bite: I'm worried that this patch will be a maintenance
burden. It's easy to imagine that changes to core will result in the
necessity or at least desirability of changes to pgaudit, but I'm
definitely not prepared to insist that future authors try to insist
that future patch submitters have to understand this code and update
it as things change.

The set of things that the patch can audit is pretty arbitrary and not
well tied into the core code. There is a list of string constants in
the code that covers each type of relations plus functions, but not
any other kind of SQL object. If somebody adds a new relkind, this
would probably need to updated - it would not just work. If somebody
adds a new type of SQL object, it won't be covered unless the user
takes some explicit action, but there's no obvious guiding principle
to say whether that would be appropriate in any particular case. In
saying that it's arbitrary, I'm not saying it isn't *useful*. I'm
saying there could be five extensions like this that make equally
arbitrary decisions about what to do and how to do it, and they could
all be useful to different people. I don't really want to bless any
given one of those current or hypothetical future solutions. We have
hooks precisely so that people can write stuff like this and put it up
on PGXN or github or wherever - and this code can be published there,
and people who want to can use it.

It also appears to me that if we did want to do that, it would need
quite a lot of additional cleanup. I haven't dug in enough to have a
list of specific issues, but it does look to me like there would be
quite a bit. Maybe that'd be worth doing if there were other
advantages of having this be in core, but I don't see them.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#12Joshua D. Drake
jd@commandprompt.com
In reply to: Robert Haas (#11)
Re: PostgreSQL Audit Extension

On 02/01/2016 08:23 PM, Robert Haas wrote:

It also appears to me that if we did want to do that, it would need
quite a lot of additional cleanup. I haven't dug in enough to have a
list of specific issues, but it does look to me like there would be
quite a bit. Maybe that'd be worth doing if there were other
advantages of having this be in core, but I don't see them.

Thus... it is a great extension and doesn't need to be in core.

JD

--
Command Prompt, Inc. http://the.postgres.company/
+1-503-667-4564
PostgreSQL Centered full stack support, consulting and development.
Everyone appreciates your honesty, until you are honest with them.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#13David Steele
david@pgmasters.net
In reply to: Robert Haas (#11)
Re: PostgreSQL Audit Extension

Hi Robert,

Thank you for replying.

On 2/1/16 11:23 PM, Robert Haas wrote:

OK, I'll bite: I'm worried that this patch will be a maintenance
burden. It's easy to imagine that changes to core will result in the
necessity or at least desirability of changes to pgaudit, but I'm
definitely not prepared to insist that future authors try to insist
that future patch submitters have to understand this code and update
it as things change.

I agree this is a concern. It's similar to deparse or event triggers in
this regard with the notable exception that pgaudit is not in core.

However, if it becomes popular enough out of core as everyone insists is
preferable then people will still need to maintain it. Just as PostGIS
has a close relationship with core, the pgaudit team would need to have
the same sort of relationship. Patches would be submitted for review
and (hopefully) committed and core developer time would still be spent
on pgaudit, ableit indirectly. Core developers would still have to be
careful not to break pgaudit if it became popular enough.

The set of things that the patch can audit is pretty arbitrary and not
well tied into the core code.

Since the set of what it can audit is every command that can be run by a
user in Postgres I don't see how that's arbitrary. The amount of
*additional* detail provided for each audit record is also not arbitrary
but a function of what information is available through various hooks
and event triggers.

I was able to improve on the amount of information provided over the
original 2Q code (mostly by abandoning support for Postgres < 9.5) but
the methodology remains the same.

There is a list of string constants in
the code that covers each type of relations plus functions, but not
any other kind of SQL object. If somebody adds a new relkind, this
would probably need to updated - it would not just work. If somebody
adds a new type of SQL object, it won't be covered unless the user
takes some explicit action, but there's no obvious guiding principle
to say whether that would be appropriate in any particular case.

I think a lot of this could be mitigated by some changes in utility.c.
I'm planning patches that will allow mapping command strings back to
event tags and a general classifier function that could incidentally be
used to improve the granularity of log_statement.

In
saying that it's arbitrary, I'm not saying it isn't *useful*. I'm
saying there could be five extensions like this that make equally
arbitrary decisions about what to do and how to do it, and they could
all be useful to different people.

There *could* be five extensions but there are not. To my knowledge
there are two and one is just a more evolved version of the other.

I don't really want to bless any
given one of those current or hypothetical future solutions. We have
hooks precisely so that people can write stuff like this and put it up
on PGXN or github or wherever - and this code can be published there,
and people who want to can use it.

People who are interested in audit are also understandably leery of
downloading code from an untrusted source. Both PGXN and GitHub are The
Wild West as far as conservative auditors are concerned.

Your use of the phrase "or wherever" only reinforces the point in my
mind. The implication is that it doesn't matter where the pgaudit
extension comes from but it does matter, a lot.

It also appears to me that if we did want to do that, it would need
quite a lot of additional cleanup. I haven't dug in enough to have a
list of specific issues, but it does look to me like there would be
quite a bit. Maybe that'd be worth doing if there were other
advantages of having this be in core, but I don't see them.

I'll be the first to admit that the design is not the prettiest. Trying
to figure out what Postgres is doing internally through a couple of
hooks is like trying to replicate the script of a play when all you have
is the program. However, so far it has been performed well and been
reliable in field tests.

--
-David
david@pgmasters.net

#14Robert Haas
robertmhaas@gmail.com
In reply to: David Steele (#13)
Re: PostgreSQL Audit Extension

On Wed, Feb 3, 2016 at 10:37 AM, David Steele <david@pgmasters.net> wrote:

On 2/1/16 11:23 PM, Robert Haas wrote:

OK, I'll bite: I'm worried that this patch will be a maintenance
burden. It's easy to imagine that changes to core will result in the
necessity or at least desirability of changes to pgaudit, but I'm
definitely not prepared to insist that future authors try to insist
that future patch submitters have to understand this code and update
it as things change.

I agree this is a concern. It's similar to deparse or event triggers in
this regard with the notable exception that pgaudit is not in core.

I don't see event triggers as having the same issues, actually. DDL
deparse - which I assume is what you mean by deparse - does, and I
complained about that too, quite a lot, and some work was done to
address it - I would have liked more. One of the things I argued for
forcefully with regard to DDL deparse is that it needed to be able to
deparse every DDL command we have, not just the ones the authors
thought were most important. I would expect no less from an auditing
facility.

However, if it becomes popular enough out of core as everyone insists is
preferable then people will still need to maintain it. Just as PostGIS
has a close relationship with core, the pgaudit team would need to have
the same sort of relationship. Patches would be submitted for review
and (hopefully) committed and core developer time would still be spent
on pgaudit, ableit indirectly. Core developers would still have to be
careful not to break pgaudit if it became popular enough.

This just isn't how it works. I have no idea what the PostGIS folks
are doing, and they generally don't need to know what we're doing.
Occasionally we interact with each other, but mostly those two
different pieces of software can be developed by different people, and
that's a good thing. Migrating PostGIS into PostgreSQL's core would
not be good for either project IMHO. It is neither necessary nor
desirable to have multiple software projects all merged together in a
single git repo.

The set of things that the patch can audit is pretty arbitrary and not
well tied into the core code.

Since the set of what it can audit is every command that can be run by a
user in Postgres I don't see how that's arbitrary.

That's not what I'm talking about. You audit relation access and
function calls but not, say, creation of event triggers. Yes, you can
log every statement that comes in, but that's not the secret sauce:
log_statement=all will do that much. The secret sauce is figuring out
the set of events that a statement might perform which might cause
that statement to generate audit records. And it does not seem to me
that what you've got there right now is particularly general - you've
got relation access and function calls and a couple of other things,
but it's far from comprehensive.

There is a list of string constants in
the code that covers each type of relations plus functions, but not
any other kind of SQL object. If somebody adds a new relkind, this
would probably need to updated - it would not just work. If somebody
adds a new type of SQL object, it won't be covered unless the user
takes some explicit action, but there's no obvious guiding principle
to say whether that would be appropriate in any particular case.

I think a lot of this could be mitigated by some changes in utility.c.
I'm planning patches that will allow mapping command strings back to
event tags and a general classifier function that could incidentally be
used to improve the granularity of log_statement.

So, *this* starts to smell like a reason for core changes. "I can't
really do what I want in my extension, but with these changes I could"
is an excellent reason to change core.

In
saying that it's arbitrary, I'm not saying it isn't *useful*. I'm
saying there could be five extensions like this that make equally
arbitrary decisions about what to do and how to do it, and they could
all be useful to different people.

There *could* be five extensions but there are not. To my knowledge
there are two and one is just a more evolved version of the other.

Right now that may be true, although it wouldn't surprise me very much
to find out that other people have written such extensions and they
just didn't get as much press. Also, consider the future. It is
*possible* that your version of pgaudit will turn out to be the be-all
and the end-all, but it's equally possible that somebody will fork
your version in turn and evolve it some more. I don't see how you can
look at the pgaudit facilities you've got here and say that this is
the last word on auditing and all PostgreSQL users should be content
with exactly that facility. I find that ridiculous. Look me in the
eye and tell me that nobody's going to fork your version and evolve it
a bunch more.

People who are interested in audit are also understandably leery of
downloading code from an untrusted source. Both PGXN and GitHub are The
Wild West as far as conservative auditors are concerned.

I hate to be rude here, but that's not my problem. You can put it on
your corporate web site and let people download it from there. I'm
sure that auditors are familiar with the idea of downloading software
from for-profit companies. Do they really not use any software from
Microsoft or Apple, for example? If the problem is that they will
trust the PostgreSQL open source project but not YOUR company, then I
respectfully suggest that you need to establish the necessary
credibility, not try to piggyback on someone else's.

I'll be the first to admit that the design is not the prettiest. Trying
to figure out what Postgres is doing internally through a couple of
hooks is like trying to replicate the script of a play when all you have
is the program. However, so far it has been performed well and been
reliable in field tests.

That's good to hear, but again, it's not enough for a core submission.
Code that goes into our main git repository needs to be "the
prettiest". I mean it's not all perfect of course, but it should be
pretty darn good.

Also, understand this: when you get a core submission accepted, the
core project is then responsible for maintaining that code even if you
disappear. It's entirely reasonable for the project to demand that
this isn't going to be too much work. It's entirely reasonable for
the community to want the design to be very good and the code quality
to be high. It's entirely reasonable for the community NOT to want to
privilege one implementation over another. If you don't agree that
those things are reasonable then we disagree pretty fundamentally on
the role of the community. The community is a group of people to whom
I (or you) can give our time and my (or your) code, not a group of
people who owe me (or you) anything.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#15David G. Johnston
david.g.johnston@gmail.com
In reply to: Robert Haas (#14)
Re: PostgreSQL Audit Extension

On Wed, Feb 3, 2016 at 9:36 AM, Robert Haas <robertmhaas@gmail.com> wrote:

On Wed, Feb 3, 2016 at 10:37 AM, David Steele <david@pgmasters.net> wrote:

On 2/1/16 11:23 PM, Robert Haas wrote:

In
saying that it's arbitrary, I'm not saying it isn't *useful*. I'm
saying there could be five extensions like this that make equally
arbitrary decisions about what to do and how to do it, and they could
all be useful to different people.

There *could* be five extensions but there are not. To my knowledge
there are two and one is just a more evolved version of the other.

Right now that may be true, although it wouldn't surprise me very much
to find out that other people have written such extensions and they
just didn't get as much press. Also, consider the future. It is
*possible* that your version of pgaudit will turn out to be the be-all
and the end-all, but it's equally possible that somebody will fork
your version in turn and evolve it some more. I don't see how you can
look at the pgaudit facilities you've got here and say that this is
the last word on auditing and all PostgreSQL users should be content
with exactly that facility. I find that ridiculous. Look me in the
eye and tell me that nobody's going to fork your version and evolve it
a bunch more.

​This rings hollow to me. JSON got included with an admittedly weak
feature set and then got significantly improved upon in subsequent
releases. Those who would be incline to fork pgaudit, seeing it already
being in core, would more likely and to the benefit of the community put
that work into improving the existing work.​ My off-the-cuff understanding
is that some current big features (namely the parallel-related stuff) are
also taking this "lets commit smaller but still useful pieces" into core to
build up to this super-feature we want working two years from now. I don't
see any fundamental reason auditing couldn't be given the same opportunity
to improve inside core.

The other major downside of having it in core is that now the feature
release cycle is tied to core. Telling PostGIS they can only release new
features when new versions of PostgreSQL come out would be an unacceptable
situation.

The best of both worlds would be for core to have its own implementation
written as an extension and to readily allow for other implementations to
be plugged in as well. As your alluded to above there are likely a number
of things core really needs to enable such functionality without providing
the entire UI - leaving that for extensions.

People who are interested in audit are also understandably leery of
downloading code from an untrusted source. Both PGXN and GitHub are The
Wild West as far as conservative auditors are concerned.

I hate to be rude here, but that's not my problem. You can put it on
your corporate web site and let people download it from there. I'm
sure that auditors are familiar with the idea of downloading software
from for-profit companies. Do they really not use any software from
Microsoft or Apple, for example? If the problem is that they will
trust the PostgreSQL open source project but not YOUR company, then I
respectfully suggest that you need to establish the necessary
credibility, not try to piggyback on someone else's.

​A bit short-sighted maybe. Endorsing and including such a feature could
open PostgreSQL up to a​ new market being supported by people who right now
are not readily able to join the PostgreSQL community because they cannot
invest the necessary resources to get the horses put before the cart.
Those people, if they could get their clients to more easily use
PostgreSQL, may just find it worth their while to then contribute back to
this new frontier that has been opened up to them. This would ideally
increase the number of contributors and reviewers within the community
which is the main thing that is presently needed.

I'll be the first to admit that the design is not the prettiest. Trying
to figure out what Postgres is doing internally through a couple of
hooks is like trying to replicate the script of a play when all you have
is the program. However, so far it has been performed well and been
reliable in field tests.

That's good to hear, but again, it's not enough for a core submission.
Code that goes into our main git repository needs to be "the
prettiest". I mean it's not all perfect of course, but it should be
pretty darn good.

Also, understand this: when you get a core submission accepted, the
core project is then responsible for maintaining that code even if you
disappear. It's entirely reasonable for the project to demand that
this isn't going to be too much work. It's entirely reasonable for
the community to want the design to be very good and the code quality
to be high.

​So far so good...​

It's entirely reasonable for the community NOT to want to

privilege one implementation over another.

​This, not so much. At some point if it is decided the feature needs to be
in core then an existing implementation would have to be adopted by core.
If the rule becomes "we cannot commit to core anything that has two
implementations in the wild" becomes a norm then our extension mechanisms
better be damn good. So admitting that you cannot decide is reasonable and
human but choosing not to decide is like abstaining to vote when you are in
a position of appointed authority. You were placed into your position as
committer so that you (along with the others so appointed) could make
decisions like this and as a community member I would hope you would do
everything in your power to inform yourself enough to make such a decision
when asked to do so.

This specific situation does suffer from having multiple different dynamics
to consider - which have already been discussed up-thread - but I guess
fundamentally does the question is: does the community want an official
implementation of auditing present in core?

For the record I would. Ideally I would also have the option to choose a
non-core extension should core's chosen implementation or constraints
(upgrade schedule) not fit my needs. But having a reference
implementation, and thus in-house usage of the various features that an
auditing feature would ideally be able to lean on core to provide, is
appealing. But, as a pre-condition I'd want at least a couple of
committers who whole-heartedly feel the same way and are willing to take on
the necessary research and decisions to make it happen. There are
significant resources out there that will help but that need a leader with
authority to which their efforts can be funneled. That is a personal
opinion that is based more upon the overall structure of the project and
community than it does any specifics of a submitted patch. If there is a
will to get it in then most likely it will happen at some point.

If you don't agree that

those things are reasonable then we disagree pretty fundamentally on
the role of the community. The community is a group of people to whom
I (or you) can give our time and my (or your) code, not a group of
people who owe me (or you) anything.

​From my point above your acceptance of the commit bit does entail a
certain degree of responsibility to the rest of the community. The good
news is that only those who have already exhibited such a sense of
responsibility are given the privilege. And usually that is sufficient but
in situations where it is not there is also the formal core committee that
can act when the committer group is hamstrung (for lack of a better term)
by its informal nature.

​David J.

#16Robert Haas
robertmhaas@gmail.com
In reply to: David G. Johnston (#15)
Re: PostgreSQL Audit Extension

On Wed, Feb 3, 2016 at 12:38 PM, David G. Johnston
<david.g.johnston@gmail.com> wrote:

Right now that may be true, although it wouldn't surprise me very much
to find out that other people have written such extensions and they
just didn't get as much press. Also, consider the future. It is
*possible* that your version of pgaudit will turn out to be the be-all
and the end-all, but it's equally possible that somebody will fork
your version in turn and evolve it some more. I don't see how you can
look at the pgaudit facilities you've got here and say that this is
the last word on auditing and all PostgreSQL users should be content
with exactly that facility. I find that ridiculous. Look me in the
eye and tell me that nobody's going to fork your version and evolve it
a bunch more.

This rings hollow to me. JSON got included with an admittedly weak feature
set and then got significantly improved upon in subsequent releases.

True. But most of that was adding, not changing. Look, fundamentally
this is an opinion question, and everybody's entitled to an opinion on
whether pgaudit should be in core. I have given mine; other people
can think about it differently.

Those
who would be incline to fork pgaudit, seeing it already being in core, would
more likely and to the benefit of the community put that work into improving
the existing work. My off-the-cuff understanding is that some current big
features (namely the parallel-related stuff) are also taking this "lets
commit smaller but still useful pieces" into core to build up to this
super-feature we want working two years from now. I don't see any
fundamental reason auditing couldn't be given the same opportunity to
improve inside core.

Well, it means that every change has to be dealt with by a PostgreSQL
committer, for one thing. We don't have a ton of people who have the
skillset for that, the time to work on it, and the community's trust.

The other major downside of having it in core is that now the feature
release cycle is tied to core. Telling PostGIS they can only release new
features when new versions of PostgreSQL come out would be an unacceptable
situation.

Yep.

The best of both worlds would be for core to have its own implementation
written as an extension and to readily allow for other implementations to be
plugged in as well. As your alluded to above there are likely a number of
things core really needs to enable such functionality without providing the
entire UI - leaving that for extensions.

I really think this is not for the best. People who write non-core
extensions are often quite unhappy when core gets a feature that is
even somewhat similar, because they feel that this takes attention
away from their work in favor of the not-necessarily-better work that
went into core.

A bit short-sighted maybe. Endorsing and including such a feature could
open PostgreSQL up to a new market being supported by people who right now
are not readily able to join the PostgreSQL community because they cannot
invest the necessary resources to get the horses put before the cart. Those
people, if they could get their clients to more easily use PostgreSQL, may
just find it worth their while to then contribute back to this new frontier
that has been opened up to them. This would ideally increase the number of
contributors and reviewers within the community which is the main thing that
is presently needed.

This is based, though, on the idea that they must not only have the
feature but they must have it in the core distribution. And I'm
simply not willing to endorse that as a reason to put things in core.
Maybe it would be good for PostgreSQL adoption, but if everything that
somebody won't use unless it's in core goes in core, core will become
a bloated, stinking mess.

I'll be the first to admit that the design is not the prettiest. Trying

It's entirely reasonable for the community NOT to want to
privilege one implementation over another.

This, not so much.

No, this is ABSOLUTELY critical. Suppose EnterpriseDB writes an
auditing solution, 2ndQuadrant writes an auditing solution, and Cruncy
Data writes an auditing solution, and the community then picks one of
those to put in core. Do you not think that the other two companies
will feel like they got the fuzzy end of the lollipop? The only time
this sort of thing doesn't provoke hard feelings is when everybody
agrees that the solution that was finally adopted was way better than
the competing things. If you don't think this is a problem, I
respectfully suggest that you haven't seen enough of these situations
play out.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#17Joshua D. Drake
jd@commandprompt.com
In reply to: Robert Haas (#16)
Re: PostgreSQL Audit Extension

On 02/03/2016 10:36 AM, Robert Haas wrote:

I'll be the first to admit that the design is not the prettiest. Trying

It's entirely reasonable for the community NOT to want to
privilege one implementation over another.

This, not so much.

No, this is ABSOLUTELY critical. Suppose EnterpriseDB writes an
auditing solution, 2ndQuadrant writes an auditing solution, and Cruncy
Data writes an auditing solution, and the community then picks one of
those to put in core. Do you not think that the other two companies
will feel like they got the fuzzy end of the lollipop? The only time
this sort of thing doesn't provoke hard feelings is when everybody
agrees that the solution that was finally adopted was way better than
the competing things. If you don't think this is a problem, I
respectfully suggest that you haven't seen enough of these situations
play out.

I am on the fence with this one because we should not care about what a
company feels, period. We are not a business, we are not an employer. We
are a community of people not companies.

On the other hand, I do very much understand what you are saying here
and it is a difficult line to walk.

Then on the third hand (for those of us that were cloned and have
issues), those companies chose PostgreSQL as their base, that is *their*
problem, not ours. We also have to be considerate of the fact that those
companies to contribute a lot to the community.

In short, may the best solution for the community win. Period.

Sincerely,

JD

--
Command Prompt, Inc. http://the.postgres.company/
+1-503-667-4564
PostgreSQL Centered full stack support, consulting and development.
Everyone appreciates your honesty, until you are honest with them.

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#18David Steele
david@pgmasters.net
In reply to: Robert Haas (#14)
Re: PostgreSQL Audit Extension

On 2/3/16 11:36 AM, Robert Haas wrote:

That's good to hear, but again, it's not enough for a core submission.
Code that goes into our main git repository needs to be "the
prettiest". I mean it's not all perfect of course, but it should be
pretty darn good.

I still think it's pretty darn good given what 2ndQuadrant and I had to
work with but I also think it could be a lot better with some core changes.

Also, understand this: when you get a core submission accepted, the
core project is then responsible for maintaining that code even if you
disappear. It's entirely reasonable for the project to demand that
this isn't going to be too much work. It's entirely reasonable for
the community to want the design to be very good and the code quality
to be high. It's entirely reasonable for the community NOT to want to
privilege one implementation over another. If you don't agree that
those things are reasonable then we disagree pretty fundamentally on
the role of the community. The community is a group of people to whom
I (or you) can give our time and my (or your) code, not a group of
people who owe me (or you) anything.

I think our differences are in the details and not in the general idea.
In no way do I want to circumvent the process or get preferential
treatment for me or for my company. I believe in the community but I
also believe that we won't get anywhere as a community unless
individuals give voice to their ideas.

I appreciate you taking the time to voice your opinion. From my
perspective there's little to be gained in continuing to beat this
horse. If the errhidefromclient() patch is accepted then that will be a
good step for pgaudit and I'll be on the lookout for other ways I can
both contribute useful code to core and move the pgaudit project forward.

--
-David
david@pgmasters.net

#19Jim Nasby
Jim.Nasby@BlueTreble.com
In reply to: Robert Haas (#14)
Re: PostgreSQL Audit Extension

On 2/3/16 10:36 AM, Robert Haas wrote:

People who are interested in audit are also understandably leery of

downloading code from an untrusted source. Both PGXN and GitHub are The
Wild West as far as conservative auditors are concerned.

I hate to be rude here, but that's not my problem. You can put it on
your corporate web site and let people download it from there. I'm
sure that auditors are familiar with the idea of downloading software
from for-profit companies. Do they really not use any software from
Microsoft or Apple, for example? If the problem is that they will
trust the PostgreSQL open source project but not YOUR company, then I
respectfully suggest that you need to establish the necessary
credibility, not try to piggyback on someone else's.

Luckily pgaudit is it's own group on Github
(https://github.com/pgaudit), so it doesn't even have to be controlled
by a single company. If others care about auditing I would hope that
they'd contribute code there and eventually become a formal member of
the pgaudit project.

As for PGXN being an untrusted source, that's something that it's in the
project's best interest to try and address somehow, perhaps by having
formally audited extensions. Amazon already has to do this to some
degree before an extension can be allowed in RDS, and so does Heroku, so
maybe that would be a starting point.

I think a big reason Postgres got to where it is today is because of
it's superior extensibility, and I think continuing to encourage that
with formal support for things like PGXN is important.
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#20Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jim Nasby (#19)
Re: PostgreSQL Audit Extension

Jim Nasby <Jim.Nasby@BlueTreble.com> writes:

As for PGXN being an untrusted source, that's something that it's in the
project's best interest to try and address somehow, perhaps by having
formally audited extensions. Amazon already has to do this to some
degree before an extension can be allowed in RDS, and so does Heroku, so
maybe that would be a starting point.

I think a big reason Postgres got to where it is today is because of
it's superior extensibility, and I think continuing to encourage that
with formal support for things like PGXN is important.

Yeah. Auditing strikes me as a fine example of something for which there
is no *technical* reason to need to put it in core. It might need some
more hooks than we have now, but that's no big deal. In the long run,
we'll be a lot better off if we can address the non-technical factors
that make people want to push such things into the core distribution.

Exactly how we get there, I don't pretend to know.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#21Stephen Frost
sfrost@snowman.net
In reply to: Tom Lane (#20)
Re: PostgreSQL Audit Extension

All,

* Robert Haas (robertmhaas@gmail.com) wrote:

OK, I'll bite: I'm worried that this patch will be a maintenance
burden. It's easy to imagine that changes to core will result in the
necessity or at least desirability of changes to pgaudit, but I'm
definitely not prepared to insist that future authors try to insist
that future patch submitters have to understand this code and update
it as things change.

I agree with this concern in general, and have since pgaudit was first
proposed for inclusion by Simon two years ago. Having auditing as an
extension is what makes this into an issue though. Were auditing baked
into core, it'd be far more straight-forward, much easier to maintain,
and would be updated as we add new core capabilities naturally. David's
comments about how pgaudit has to work are entirely accurate- everything
ends up having to be reconstructed in a very painful way.

The set of things that the patch can audit is pretty arbitrary and not
well tied into the core code.

This also speaks to the difficulties of having auditing implemented as
an extension.

* Tom Lane (tgl@sss.pgh.pa.us) wrote:

Yeah. Auditing strikes me as a fine example of something for which there
is no *technical* reason to need to put it in core. It might need some
more hooks than we have now, but that's no big deal.

I disagree with this, quite strongly. There are very good, technical,
reasons why auditing should be a core capability. Adding more hooks,
exposing various internal functions for use by extensions, etc, doesn't
change that any extension would have to be constantly updated as new
capabilities are added to core and auditing, as a capability, would
continue to be limited by virtue of being implemented as an extension.

I don't mean to detract anything from what 2ndQ and David have done with
pgaudit (which is the only auditing solution of its kind that I'm aware
of, so I don't understand the "competing implementations" argument which
has been brought up at all- there's really only one). They've done just
as much as each version of PG has allowed them to do. It's a much
better solution than what we have today (which is basically just
log_statement = 'all', which no one should ever consider to be a real
auditing solution). I've already stated that I'd be willing to take on
resonsibility for maintaining it as an extension (included with PG or
not), but it's not the end-all, be-all of auditing for PG and we should
be continuing to look at implementing a full in-core auditing solution.

I'm still of the opinion that we should include pgaudit for the reason
that it's a good incremental step towards proper in-core auditing, but
there's clearly no consensus on that and doesn't appear that further
discussion is likely to change that.

An in-core auditing solution would provide us with proper grammar
support, ability to directly mark objects for auditing in the catalog,
allow us to much more easily maintain auditing capability over time as
a small incremental bit of work for each new feature (with proper
in-core infrastructure for it) and generally be a far better technical
solution. Leveraging the GRANT system is quite cute, and does work, but
it's certainly non-intuitive and is only because we've got no better
way, due to it being implemented as an extension.

Looking at pgaudit and the other approaches to auditing which have been
developed (eg: applications which sit in front of PG and essentially
have to reimplement large bits of PG to then audit the commands sent
before passing them to PG, or hacks which try to make sense out of log
files full of SQL statements) make it quite clear, in my view, that
attempts to bolt-on auditing to PG result in a poorer solution, from a
technical perspective, than what this project is known for and capable
of. To make true progress towards that, however, we need to get past
the thinking that auditing doesn't need to be in-core or that it should
be a second-class citizen feature or that we don't need it in PG.

Thanks!

Stephen

#22Joe Conway
mail@joeconway.com
In reply to: Stephen Frost (#21)
Re: PostgreSQL Audit Extension

On 02/05/2016 10:16 AM, Stephen Frost wrote:

An in-core auditing solution would provide us with proper grammar
support, ability to directly mark objects for auditing in the catalog,
allow us to much more easily maintain auditing capability over time as
a small incremental bit of work for each new feature (with proper
in-core infrastructure for it) and generally be a far better technical
solution. Leveraging the GRANT system is quite cute, and does work, but
it's certainly non-intuitive and is only because we've got no better
way, due to it being implemented as an extension.

I think one additional item needed would be the ability for the audit
logs to be sent to a different location than the standard logs.

To make true progress towards that, however, we need to get past
the thinking that auditing doesn't need to be in-core or that it should
be a second-class citizen feature or that we don't need it in PG.

+1

Joe

--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

#23Stephen Frost
sfrost@snowman.net
In reply to: Joe Conway (#22)
Re: PostgreSQL Audit Extension

* Joe Conway (mail@joeconway.com) wrote:

On 02/05/2016 10:16 AM, Stephen Frost wrote:

An in-core auditing solution would provide us with proper grammar
support, ability to directly mark objects for auditing in the catalog,
allow us to much more easily maintain auditing capability over time as
a small incremental bit of work for each new feature (with proper
in-core infrastructure for it) and generally be a far better technical
solution. Leveraging the GRANT system is quite cute, and does work, but
it's certainly non-intuitive and is only because we've got no better
way, due to it being implemented as an extension.

I think one additional item needed would be the ability for the audit
logs to be sent to a different location than the standard logs.

Indeed, reworking the logging to be supportive of multiple destinations
with tagging of the source, etc, has long been a desire of mine (and
others), though that's largely independent of auditing itself.

Thanks!

Stephen

#24Robert Haas
robertmhaas@gmail.com
In reply to: Stephen Frost (#21)
Re: PostgreSQL Audit Extension

On Fri, Feb 5, 2016 at 1:16 PM, Stephen Frost <sfrost@snowman.net> wrote:

An in-core auditing solution would provide us with proper grammar
support, ability to directly mark objects for auditing in the catalog,
allow us to much more easily maintain auditing capability over time as
a small incremental bit of work for each new feature (with proper
in-core infrastructure for it) and generally be a far better technical
solution. Leveraging the GRANT system is quite cute, and does work, but
it's certainly non-intuitive and is only because we've got no better
way, due to it being implemented as an extension.

Yeah, I agree. Let me clarify my position: I'm not opposed to in-core
auditing. I'm also not in favor of it. If somebody makes a proposal
in that area, I'll probably have an opinion on it, but until then I
don't. What I'm opposed to is shipping this in core rather than
letting it continue to exist outside core. I see no benefit to that
and some possible downsides that I think justify rejecting it. Of
course other people are entitled to their own opinions; what I'm
saying is: that's my opinion. If somebody came along with a proposal
to put auditing in core that offered the advantages you cite here,
none of my objections to this would be objections to that. Would I
have other objections? Maybe, but not necessarily.

For example, one thing that occurs to me is that, at least in some
cases, we've got a built-in way of finding out all of the objects that
a query touches: the list of PlanInvalItems. Is it a clever idea to
try to drive auditing off that list, or a terrible idea? I'm not
sure, but clearly if it's a good idea that would make this largely
self-maintaining, which IMHO completely changes the calculus about
whether it's worth doing. Also, we've got quite a few
ObjectAddress-related facilities in the core system now that know how
to do various kinds of things with any sort of SQL-accessible object
in the system. Leveraging that infrastructure seems like a big plus.
I'd be much more likely to support a proposal that does this stuff in
a generic way that touches every SQL object type, and contains little
or no hard-wired information about particular SQL object types in the
auditing code itself. I'm not blind to the fact that auditing
relation access is more valuable than auditing text search parser
access, but it's harder to predict what we might think about the next
three SQL object types we add, and I don't want the burden to be on
the people who add that stuff to teach auditing about it if somebody
wants that. Rather, I think the auditing system should know about
everything we've got, and which stuff actually gets audited should be
a matter of configuration.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#25Bruce Momjian
bruce@momjian.us
In reply to: Stephen Frost (#21)
Re: PostgreSQL Audit Extension

On Fri, Feb 5, 2016 at 01:16:25PM -0500, Stephen Frost wrote:

Looking at pgaudit and the other approaches to auditing which have been
developed (eg: applications which sit in front of PG and essentially
have to reimplement large bits of PG to then audit the commands sent
before passing them to PG, or hacks which try to make sense out of log
files full of SQL statements) make it quite clear, in my view, that
attempts to bolt-on auditing to PG result in a poorer solution, from a
technical perspective, than what this project is known for and capable
of. To make true progress towards that, however, we need to get past
the thinking that auditing doesn't need to be in-core or that it should
be a second-class citizen feature or that we don't need it in PG.

Coming in late here, but the discussion around how to maintain the
auditing code seems very similar to how to handle the logical
replication of DDL commands. First, have we looked into hooking
auditing into scanning logical replication contents, and second, how are
we handling the logical replication of DDL and could we use the same
approach for auditing?

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription                             +

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#26Robert Haas
robertmhaas@gmail.com
In reply to: Bruce Momjian (#25)
Re: PostgreSQL Audit Extension

On Wed, Feb 17, 2016 at 5:20 AM, Bruce Momjian <bruce@momjian.us> wrote:

On Fri, Feb 5, 2016 at 01:16:25PM -0500, Stephen Frost wrote:

Looking at pgaudit and the other approaches to auditing which have been
developed (eg: applications which sit in front of PG and essentially
have to reimplement large bits of PG to then audit the commands sent
before passing them to PG, or hacks which try to make sense out of log
files full of SQL statements) make it quite clear, in my view, that
attempts to bolt-on auditing to PG result in a poorer solution, from a
technical perspective, than what this project is known for and capable
of. To make true progress towards that, however, we need to get past
the thinking that auditing doesn't need to be in-core or that it should
be a second-class citizen feature or that we don't need it in PG.

Coming in late here, but the discussion around how to maintain the
auditing code seems very similar to how to handle the logical
replication of DDL commands. First, have we looked into hooking
auditing into scanning logical replication contents, and second, how are
we handling the logical replication of DDL and could we use the same
approach for auditing?

Auditing needs to trace read-only events, which aren't reflected in
logical replication in any way. I think it's a good idea to try to
drive auditing off of existing machinery instead of inventing
something new - I suggested plan invalidation items upthread. But I
doubt that logical replication is the thing to attach it to.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#27Bruce Momjian
bruce@momjian.us
In reply to: Robert Haas (#26)
Re: PostgreSQL Audit Extension

On Wed, Feb 17, 2016 at 01:59:09PM +0530, Robert Haas wrote:

On Wed, Feb 17, 2016 at 5:20 AM, Bruce Momjian <bruce@momjian.us> wrote:

On Fri, Feb 5, 2016 at 01:16:25PM -0500, Stephen Frost wrote:

Looking at pgaudit and the other approaches to auditing which have been
developed (eg: applications which sit in front of PG and essentially
have to reimplement large bits of PG to then audit the commands sent
before passing them to PG, or hacks which try to make sense out of log
files full of SQL statements) make it quite clear, in my view, that
attempts to bolt-on auditing to PG result in a poorer solution, from a
technical perspective, than what this project is known for and capable
of. To make true progress towards that, however, we need to get past
the thinking that auditing doesn't need to be in-core or that it should
be a second-class citizen feature or that we don't need it in PG.

Coming in late here, but the discussion around how to maintain the
auditing code seems very similar to how to handle the logical
replication of DDL commands. First, have we looked into hooking
auditing into scanning logical replication contents, and second, how are
we handling the logical replication of DDL and could we use the same
approach for auditing?

Auditing needs to trace read-only events, which aren't reflected in
logical replication in any way. I think it's a good idea to try to
drive auditing off of existing machinery instead of inventing
something new - I suggested plan invalidation items upthread. But I
doubt that logical replication is the thing to attach it to.

I was suggesting we could track write events via logical replication and
then we only have to figure out auditing of read events, which would be
easier.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription                             +

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#28David Steele
david@pgmasters.net
In reply to: Bruce Momjian (#27)
Re: PostgreSQL Audit Extension

On 2/17/16 10:25 PM, Bruce Momjian wrote:

On Wed, Feb 17, 2016 at 01:59:09PM +0530, Robert Haas wrote:

On Wed, Feb 17, 2016 at 5:20 AM, Bruce Momjian <bruce@momjian.us> wrote:

On Fri, Feb 5, 2016 at 01:16:25PM -0500, Stephen Frost wrote:

Looking at pgaudit and the other approaches to auditing which have been
developed (eg: applications which sit in front of PG and essentially
have to reimplement large bits of PG to then audit the commands sent
before passing them to PG, or hacks which try to make sense out of log
files full of SQL statements) make it quite clear, in my view, that
attempts to bolt-on auditing to PG result in a poorer solution, from a
technical perspective, than what this project is known for and capable
of. To make true progress towards that, however, we need to get past
the thinking that auditing doesn't need to be in-core or that it should
be a second-class citizen feature or that we don't need it in PG.

Coming in late here, but the discussion around how to maintain the
auditing code seems very similar to how to handle the logical
replication of DDL commands. First, have we looked into hooking
auditing into scanning logical replication contents, and second, how are
we handling the logical replication of DDL and could we use the same
approach for auditing?

Auditing needs to trace read-only events, which aren't reflected in
logical replication in any way. I think it's a good idea to try to
drive auditing off of existing machinery instead of inventing
something new - I suggested plan invalidation items upthread. But I
doubt that logical replication is the thing to attach it to.

I was suggesting we could track write events via logical replication and
then we only have to figure out auditing of read events, which would be
easier.

I agree that DDL/DML audit logging requires a lot of the same
information as logical replication but I don't think reading the logical
WAL stream is a good way to do audit logging even for writes.

For instance there are some "writes" that are not WAL logged such as SET
or ALTER SYSTEM. Determining attribution would be difficult or
impossible, such as which function called an update statement (or vice
versa). Tying together the read and write information would be tricky
as well since the WAL stream contains information on transactions but
not user sessions, if I understand it correctly.

The end result is that it would be very difficult to record a coherent,
attributed, and sequential audit log and in fact represent a step
backward from pgaudit's current capabilities.

--
-David
david@pgmasters.net

#29Bruce Momjian
bruce@momjian.us
In reply to: David Steele (#28)
Re: PostgreSQL Audit Extension

On Fri, Feb 19, 2016 at 09:19:58AM -0500, David Steele wrote:

I was suggesting we could track write events via logical replication and
then we only have to figure out auditing of read events, which would be
easier.

I agree that DDL/DML audit logging requires a lot of the same
information as logical replication but I don't think reading the logical
WAL stream is a good way to do audit logging even for writes.

For instance there are some "writes" that are not WAL logged such as SET
or ALTER SYSTEM. Determining attribution would be difficult or
impossible, such as which function called an update statement (or vice
versa). Tying together the read and write information would be tricky
as well since the WAL stream contains information on transactions but
not user sessions, if I understand it correctly.

The end result is that it would be very difficult to record a coherent,
attributed, and sequential audit log and in fact represent a step
backward from pgaudit's current capabilities.

Understood. My point is that there is a short list of read events, and
many DDL events. We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required. If we could use the same facility for both logical
replication and auditing, the cost overhead is less per feature. For
example, we don't need to read the WAL to do the auditing, but the same
facility could be used for both, e.g. output some JSON standard format
that both logical replication and auditing could understand.

The bottom line is we need to crack the record-DDL nut somehow, and at
least in this case, we have two use-cases for it.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription                             +

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#30David Steele
david@pgmasters.net
In reply to: Bruce Momjian (#29)
Re: PostgreSQL Audit Extension

On 2/19/16 10:14 AM, Bruce Momjian wrote:

On Fri, Feb 19, 2016 at 09:19:58AM -0500, David Steele wrote:

I was suggesting we could track write events via logical replication and
then we only have to figure out auditing of read events, which would be
easier.

I agree that DDL/DML audit logging requires a lot of the same
information as logical replication but I don't think reading the logical
WAL stream is a good way to do audit logging even for writes.

For instance there are some "writes" that are not WAL logged such as SET
or ALTER SYSTEM. Determining attribution would be difficult or
impossible, such as which function called an update statement (or vice
versa). Tying together the read and write information would be tricky
as well since the WAL stream contains information on transactions but
not user sessions, if I understand it correctly.

The end result is that it would be very difficult to record a coherent,
attributed, and sequential audit log and in fact represent a step
backward from pgaudit's current capabilities.

Understood. My point is that there is a short list of read events, and
many DDL events. We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required. If we could use the same facility for both logical
replication and auditing, the cost overhead is less per feature. For
example, we don't need to read the WAL to do the auditing, but the same
facility could be used for both, e.g. output some JSON standard format
that both logical replication and auditing could understand.

Agreed, and this was discussed up thread. In my mind putting a more
generic structure on top of logical replication and DDL auditing is a
promising solution but I have not looked at it closely enough to know if
it will work as expected or address maintenance concerns.

--
-David
david@pgmasters.net

#31Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Bruce Momjian (#29)
Re: PostgreSQL Audit Extension

Bruce Momjian wrote:

Understood. My point is that there is a short list of read events, and
many DDL events. We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required.

DDL is already captured using the event triggers mechanism (which is
what it was invented for in the first place). The only thing we don't
have is a hardcoded mechanism to transform it from C struct format to
SQL language.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#32Bruce Momjian
bruce@momjian.us
In reply to: Alvaro Herrera (#31)
Re: PostgreSQL Audit Extension

On Fri, Feb 19, 2016 at 12:54:17PM -0300, Alvaro Herrera wrote:

Bruce Momjian wrote:

Understood. My point is that there is a short list of read events, and
many DDL events. We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required.

DDL is already captured using the event triggers mechanism (which is
what it was invented for in the first place). The only thing we don't
have is a hardcoded mechanism to transform it from C struct format to
SQL language.

Right, which is I think were the maintenance/testing overhead will come
from, which we are trying to avoid. Having logical replication and
auditing share that burden would be a win.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription                             +

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#33David Steele
david@pgmasters.net
In reply to: Alvaro Herrera (#31)
Re: PostgreSQL Audit Extension

On 2/19/16 10:54 AM, Alvaro Herrera wrote:

Bruce Momjian wrote:

Understood. My point is that there is a short list of read events, and
many DDL events. We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required.

DDL is already captured using the event triggers mechanism (which is
what it was invented for in the first place). The only thing we don't
have is a hardcoded mechanism to transform it from C struct format to
SQL language.

Since DDL event triggers only cover database-level DDL they miss a lot
that is very important to auditing, e.g. CREATE/ALTER/DROP ROLE,
GRANT/REVOKE, CREATE/ALTER/DROP DATABASE, etc.

I would like to see a general mechanism that allows event triggers,
logical replication, and audit to all get the information they need
without them being tied to each other directly.

--
-David
david@pgmasters.net

#34Andres Freund
andres@anarazel.de
In reply to: Bruce Momjian (#29)
Re: PostgreSQL Audit Extension

On 2016-02-19 10:14:47 -0500, Bruce Momjian wrote:

We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required.

I'm not sure what you're referring to here? It'd be some relatively
minor code surgery to also pass catalog changes to output plugins. It's
just questionable what'd that bring to the table.

Andres

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#35Bruce Momjian
bruce@momjian.us
In reply to: David Steele (#33)
Re: PostgreSQL Audit Extension

On Fri, Feb 19, 2016 at 11:20:13AM -0500, David Steele wrote:

On 2/19/16 10:54 AM, Alvaro Herrera wrote:

Bruce Momjian wrote:

Understood. My point is that there is a short list of read events, and
many DDL events. We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required.

DDL is already captured using the event triggers mechanism (which is
what it was invented for in the first place). The only thing we don't
have is a hardcoded mechanism to transform it from C struct format to
SQL language.

Since DDL event triggers only cover database-level DDL they miss a lot
that is very important to auditing, e.g. CREATE/ALTER/DROP ROLE,
GRANT/REVOKE, CREATE/ALTER/DROP DATABASE, etc.

Well, we need to enhance them then.

I would like to see a general mechanism that allows event triggers,
logical replication, and audit to all get the information they need
without them being tied to each other directly.

I think the reporting of DDL would be produced in a way that could be
used by auditing or logical replication, as I already stated.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription                             +

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#36Bruce Momjian
bruce@momjian.us
In reply to: Andres Freund (#34)
Re: PostgreSQL Audit Extension

On Fri, Feb 19, 2016 at 08:20:31AM -0800, Andres Freund wrote:

On 2016-02-19 10:14:47 -0500, Bruce Momjian wrote:

We have already hesitated to record DDL changes for
logical replication because of the code size, maintenance overhead, and
testing required.

I'm not sure what you're referring to here? It'd be some relatively
minor code surgery to also pass catalog changes to output plugins. It's
just questionable what'd that bring to the table.

The complaint we got about recording DDL changes for logical replication
was the requirement to track every new DDL option we add. If that
overhead can be done for both logical replication and auditing, it is a
win.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Roman grave inscription                             +

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers