Ensure commands in extension scripts see the results of preceding DDL.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 May 2017 22:05:53 +0000 (18:05 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 2 May 2017 22:06:09 +0000 (18:06 -0400)
Due to a missing CommandCounterIncrement() call, parsing of a non-utility
command in an extension script would not see the effects of the immediately
preceding DDL command, unless that command's execution ends with
CommandCounterIncrement() internally ... which some do but many don't.
Report by Philippe Beaudoin, diagnosis by Julien Rouhaud.

Rather remarkably, this bug has evaded detection since extensions were
invented, so back-patch to all supported branches.

Discussion: https://postgr.es/m/2cf7941e-4e41-7714-3de8-37b1a8f74dff@free.fr

src/backend/commands/extension.c
src/test/modules/test_extensions/test_ext3--1.0.sql

index d371a2a3c15e9cecf6be0d761f977ea9654a5f2b..c3718b08c1711f7f275b0c44cf6aee01ffe78f46 100644 (file)
@@ -718,6 +718,9 @@ execute_sql_string(const char *sql, const char *filename)
                List       *stmt_list;
                ListCell   *lc2;
 
+               /* Be sure parser can see any DDL done so far */
+               CommandCounterIncrement();
+
                stmt_list = pg_analyze_and_rewrite(parsetree,
                                                                                   sql,
                                                                                   NULL,
index 7dec684dcb55a097f5d1ec3aa857825d7389b07c..4fcb63d2bc13e5f6a18d8d5b52c2176575f070bc 100644 (file)
@@ -1,3 +1,9 @@
 /* src/test/modules/test_extensions/test_ext3--1.0.sql */
 -- complain if script is sourced in psql, rather than via CREATE EXTENSION
 \echo Use "CREATE EXTENSION test_ext3" to load this file. \quit
+
+CREATE TABLE test_ext3_table (col_old INT);
+
+ALTER TABLE test_ext3_table RENAME col_old TO col_new;
+
+UPDATE test_ext3_table SET col_new = 0;