From 29469f40f9778bc758b3a2e88cf828254f745b31 Mon Sep 17 00:00:00 2001 From: Takahiro Itagaki Date: Mon, 19 Jan 2009 04:28:21 +0000 Subject: [PATCH] Move drop function calls to another transaction which doesn't have execlusive locks. --- Makefile | 2 +- bin/Makefile | 2 +- bin/pg_reorg.c | 23 +++++++++++++---- doc/index-ja.html | 2 +- doc/index.html | 2 +- doc/pg_reorg-ja.html | 2 +- doc/pg_reorg.html | 6 ++--- lib/Makefile | 2 +- lib/pg_reorg.sql.in | 2 +- lib/reorg.c | 51 +++++++++++++++++++++++++++++--------- lib/uninstall_pg_reorg.sql | 2 +- 11 files changed, 68 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index dcbda14..224b6e7 100755 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # # pg_reorg: Makefile # -# Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +# Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION # .PHONY: all install clean diff --git a/bin/Makefile b/bin/Makefile index 0d24d7b..db735ab 100755 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,7 +1,7 @@ # # pg_reorg: bin/Makefile # -# Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +# Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION # SRCS = \ pg_reorg.c diff --git a/bin/pg_reorg.c b/bin/pg_reorg.c index ca4617b..7005c61 100755 --- a/bin/pg_reorg.c +++ b/bin/pg_reorg.c @@ -1,7 +1,7 @@ /* * pg_reorg.c: bin/pg_reorg.c * - * Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION */ /** @@ -15,7 +15,7 @@ #include -#define REORG_VERSION "1.0.2" +#define REORG_VERSION "1.0.3" #define REORG_URL "http://reorg.projects.postgresql.org/" #define REORG_EMAIL "reorg-general@lists.pgfoundry.org" @@ -579,6 +579,8 @@ reorg_one_table(const reorg_table *table, const char *orderby) fprintf(stderr, "---- copy tuples ----\n"); command("BEGIN ISOLATION LEVEL SERIALIZABLE", 0, NULL); + /* SET work_mem = maintenance_work_mem */ + command("SELECT set_config('work_mem', current_setting('maintenance_work_mem'), true)", 0, NULL); if (orderby && !orderby[0]) command("SET LOCAL synchronize_seqscans = off", 0, NULL); res = execute(SQL_XID_SNAPSHOT, 0, NULL); @@ -650,18 +652,29 @@ reorg_one_table(const reorg_table *table, const char *orderby) } /* - * 5. Cleanup. + * 5. Swap. */ if (verbose) - fprintf(stderr, "---- cleanup ----\n"); + fprintf(stderr, "---- swap ----\n"); command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL); command(table->lock_table, 0, NULL); apply_log(table, 0); params[0] = utoa(table->target_oid, buffer); command("SELECT reorg.reorg_swap($1)", 1, params); + command("COMMIT", 0, NULL); + + /* + * 6. Drop. + */ + if (verbose) + fprintf(stderr, "---- drop ----\n"); + + command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL); + params[0] = utoa(table->target_oid, buffer); command("SELECT reorg.reorg_drop($1)", 1, params); command("COMMIT", 0, NULL); + current_table = NULL; free(vxid); @@ -709,7 +722,7 @@ exit_with_cleanup(int exitcode) PGresult *res; res = PQexec(current_conn, "ROLLBACK"); if (PQresultStatus(res) != PGRES_COMMAND_OK) - exit(1); // fatal error + exit(1); /* fatal error */ PQclear(res); } diff --git a/doc/index-ja.html b/doc/index-ja.html index 71b8246..79ba0ab 100755 --- a/doc/index-ja.html +++ b/doc/index-ja.html @@ -39,7 +39,7 @@ pg_reorg は PostgreSQL のテーブルを再編成するシェルコマンド
-Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
diff --git a/doc/index.html b/doc/index.html index a5f64bf..831a4e0 100755 --- a/doc/index.html +++ b/doc/index.html @@ -39,7 +39,7 @@ where you can find download
-Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
diff --git a/doc/pg_reorg-ja.html b/doc/pg_reorg-ja.html index 09b80ab..cb6c4a2 100755 --- a/doc/pg_reorg-ja.html +++ b/doc/pg_reorg-ja.html @@ -284,7 +284,7 @@ pg_reorg の実行中には、VACUUMANALYZE

-Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION

diff --git a/doc/pg_reorg.html b/doc/pg_reorg.html index c8b1375..e82d44d 100755 --- a/doc/pg_reorg.html +++ b/doc/pg_reorg.html @@ -234,10 +234,10 @@ In many case pg_reorg would fail and rollback collectly, but there are some case
ADD COLUMN causes lost of data. Newly added columns are initialized with NULLs.
ALTER COLUMN TYPE
-
ALTER COLUMN TYPE cases data coruuptions.
+
ALTER COLUMN TYPE causes data corruptions.
ALTER TABLE SET TABLESPACE
-
ALTER TABLE SET TABLE SPACE cases data corruptions by wrong relfilenode.
+
ALTER TABLE SET TABLE SPACE causes data corruptions by wrong relfilenode.
@@ -268,7 +268,7 @@ In many case pg_reorg would fail and rollback collectly, but there are some case

-Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION

diff --git a/lib/Makefile b/lib/Makefile index f4b6300..721f8c3 100755 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,7 +1,7 @@ # # pg_reorg: lib/Makefile # -# Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +# Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION # SRCS = reorg.c OBJS = $(SRCS:.c=.o) diff --git a/lib/pg_reorg.sql.in b/lib/pg_reorg.sql.in index 82e1871..c4a1670 100755 --- a/lib/pg_reorg.sql.in +++ b/lib/pg_reorg.sql.in @@ -1,7 +1,7 @@ /* * pg_reorg: lib/pg_reorg.sql.in * - * Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION */ -- Adjust this setting to control where the objects get created. diff --git a/lib/reorg.c b/lib/reorg.c index 7a18c02..5c5fa19 100755 --- a/lib/reorg.c +++ b/lib/reorg.c @@ -1,7 +1,7 @@ /* * pg_reorg: lib/reorg.c * - * Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION */ /** @@ -10,9 +10,11 @@ #include "postgres.h" #include "catalog/namespace.h" +#include "catalog/pg_type.h" #include "commands/trigger.h" #include "executor/spi.h" #include "miscadmin.h" +#include "utils/builtins.h" #include "utils/lsyscache.h" #ifdef PG_MODULE_MAGIC @@ -46,6 +48,8 @@ static SPIPlanPtr reorg_prepare(const char *src, int nargs, Oid *argtypes); static void reorg_execp(SPIPlanPtr plan, Datum *values, const char *nulls, int expected); static void reorg_execf(int expexted, const char *format, ...) __attribute__((format(printf, 2, 3))); +static const char *get_quoted_relname(Oid oid); +static const char *get_quoted_nspname(Oid oid); #define copy_tuple(tuple, tupdesc) \ PointerGetDatum(SPI_returntuple((tuple), (tupdesc))) @@ -401,7 +405,7 @@ static void parse_indexdef(IndexDef *stmt, Oid index, Oid table) { char *sql = pg_get_indexdef_string(index); - const char *idxname = quote_identifier(get_rel_name(index)); + const char *idxname = get_quoted_relname(index); const char *tblname = generate_relation_name(table); /* CREATE [UNIQUE] INDEX */ @@ -518,14 +522,16 @@ WHERE I.indrelid = $1\ Datum reorg_swap(PG_FUNCTION_ARGS) { - Oid oid = PG_GETARG_OID(0); + Oid oid = PG_GETARG_OID(0); + const char *relname = get_quoted_relname(oid); + const char *nspname = get_quoted_nspname(oid); - SPIPlanPtr plan_swapinfo; - SPIPlanPtr plan_swap; - Oid argtypes[3] = { OIDOID, OIDOID, XIDOID }; - char nulls[3] = { ' ', ' ', ' ' }; - Datum values[3]; - int record; + SPIPlanPtr plan_swapinfo; + SPIPlanPtr plan_swap; + Oid argtypes[3] = { OIDOID, OIDOID, XIDOID }; + char nulls[3] = { ' ', ' ', ' ' }; + Datum values[3]; + int record; /* authority check */ must_be_superuser("reorg_swap"); @@ -575,6 +581,12 @@ reorg_swap(PG_FUNCTION_ARGS) } } + /* drop reorg trigger */ + reorg_execf( + SPI_OK_UTILITY, + "DROP TRIGGER IF EXISTS z_reorg_trigger ON %s.%s CASCADE", + nspname, relname); + SPI_finish(); PG_RETURN_VOID(); @@ -593,8 +605,8 @@ Datum reorg_drop(PG_FUNCTION_ARGS) { Oid oid = PG_GETARG_OID(0); - const char *relname = quote_identifier(get_rel_name(oid)); - const char *nspname = quote_identifier(get_namespace_name(get_rel_namespace(oid))); + const char *relname = get_quoted_relname(oid); + const char *nspname = get_quoted_nspname(oid); /* authority check */ must_be_superuser("reorg_drop"); @@ -602,7 +614,10 @@ reorg_drop(PG_FUNCTION_ARGS) /* connect to SPI manager */ reorg_init(); - /* drop reorg trigger */ + /* + * drop reorg trigger: We have already dropped the trigger in normal + * cases, but it can be left on error. + */ reorg_execf( SPI_OK_UTILITY, "DROP TRIGGER IF EXISTS z_reorg_trigger ON %s.%s CASCADE", @@ -675,3 +690,15 @@ reorg_execf(int expected, const char *format, ...) if ((ret = SPI_exec(sql.data, 0)) != expected) elog(ERROR, "pg_reorg: reorg_execf failed (sql=%s, code=%d, expected=%d)", sql.data, ret, expected); } + +static const char * +get_quoted_relname(Oid oid) +{ + return quote_identifier(get_rel_name(oid)); +} + +static const char * +get_quoted_nspname(Oid oid) +{ + return quote_identifier(get_namespace_name(get_rel_namespace(oid))); +} diff --git a/lib/uninstall_pg_reorg.sql b/lib/uninstall_pg_reorg.sql index e4eb542..014cee6 100755 --- a/lib/uninstall_pg_reorg.sql +++ b/lib/uninstall_pg_reorg.sql @@ -1,7 +1,7 @@ /* * pg_reorg: lib/uninstall_reorg.sql * - * Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION + * Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION */ DROP SCHEMA IF EXISTS reorg CASCADE;