Move drop function calls to another transaction which doesn't have execlusive locks.

This commit is contained in:
Takahiro Itagaki 2009-01-19 04:28:21 +00:00
parent 8af8be23ac
commit 29469f40f9
11 changed files with 68 additions and 28 deletions

View File

@ -1,7 +1,7 @@
# #
# pg_reorg: Makefile # pg_reorg: Makefile
# #
# Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION # Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
# #
.PHONY: all install clean .PHONY: all install clean

View File

@ -1,7 +1,7 @@
# #
# pg_reorg: bin/Makefile # pg_reorg: bin/Makefile
# #
# Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION # Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
# #
SRCS = \ SRCS = \
pg_reorg.c pg_reorg.c

View File

@ -1,7 +1,7 @@
/* /*
* pg_reorg.c: bin/pg_reorg.c * 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 <signal.h> #include <signal.h>
#define REORG_VERSION "1.0.2" #define REORG_VERSION "1.0.3"
#define REORG_URL "http://reorg.projects.postgresql.org/" #define REORG_URL "http://reorg.projects.postgresql.org/"
#define REORG_EMAIL "reorg-general@lists.pgfoundry.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"); fprintf(stderr, "---- copy tuples ----\n");
command("BEGIN ISOLATION LEVEL SERIALIZABLE", 0, NULL); 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]) if (orderby && !orderby[0])
command("SET LOCAL synchronize_seqscans = off", 0, NULL); command("SET LOCAL synchronize_seqscans = off", 0, NULL);
res = execute(SQL_XID_SNAPSHOT, 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) if (verbose)
fprintf(stderr, "---- cleanup ----\n"); fprintf(stderr, "---- swap ----\n");
command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL); command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL);
command(table->lock_table, 0, NULL); command(table->lock_table, 0, NULL);
apply_log(table, 0); apply_log(table, 0);
params[0] = utoa(table->target_oid, buffer); params[0] = utoa(table->target_oid, buffer);
command("SELECT reorg.reorg_swap($1)", 1, params); 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("SELECT reorg.reorg_drop($1)", 1, params);
command("COMMIT", 0, NULL); command("COMMIT", 0, NULL);
current_table = NULL; current_table = NULL;
free(vxid); free(vxid);
@ -709,7 +722,7 @@ exit_with_cleanup(int exitcode)
PGresult *res; PGresult *res;
res = PQexec(current_conn, "ROLLBACK"); res = PQexec(current_conn, "ROLLBACK");
if (PQresultStatus(res) != PGRES_COMMAND_OK) if (PQresultStatus(res) != PGRES_COMMAND_OK)
exit(1); // fatal error exit(1); /* fatal error */
PQclear(res); PQclear(res);
} }

View File

@ -39,7 +39,7 @@ pg_reorg は PostgreSQL のテーブルを再編成するシェルコマンド
<hr /> <hr />
<div align="right"> <div align="right">
Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
</div> </div>
<!-- PLEASE LEAVE "Powered By GForge" on your site --> <!-- PLEASE LEAVE "Powered By GForge" on your site -->

View File

@ -39,7 +39,7 @@ where you can find <a href="http://pgfoundry.org/frs/?group_id=1000411">download
<hr /> <hr />
<div align="right"> <div align="right">
Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
</div> </div>
<!-- PLEASE LEAVE "Powered By GForge" on your site --> <!-- PLEASE LEAVE "Powered By GForge" on your site -->

View File

@ -284,7 +284,7 @@ pg_reorg の実行中には、<tt>VACUUM</tt> と <tt>ANALYZE</tt> <STRONG>以
<hr> <hr>
<p align="right"><font size="2"> <p align="right"><font size="2">
Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
</font></p> </font></p>
</body> </body>

View File

@ -234,10 +234,10 @@ In many case pg_reorg would fail and rollback collectly, but there are some case
<dd><tt>ADD COLUMN</tt> causes lost of data. Newly added columns are initialized with NULLs.</dd> <dd><tt>ADD COLUMN</tt> causes lost of data. Newly added columns are initialized with NULLs.</dd>
<dt><tt>ALTER COLUMN TYPE</tt></dt> <dt><tt>ALTER COLUMN TYPE</tt></dt>
<dd><tt>ALTER COLUMN TYPE</tt> cases data coruuptions.</dd> <dd><tt>ALTER COLUMN TYPE</tt> causes data corruptions.</dd>
<dt><tt>ALTER TABLE SET TABLESPACE</tt></dt> <dt><tt>ALTER TABLE SET TABLESPACE</tt></dt>
<dd><tt>ALTER TABLE SET TABLE SPACE</tt> cases data corruptions by wrong relfilenode.</dd> <dd><tt>ALTER TABLE SET TABLE SPACE</tt> causes data corruptions by wrong relfilenode.</dd>
</dl> </dl>
</div> </div>
@ -268,7 +268,7 @@ In many case pg_reorg would fail and rollback collectly, but there are some case
<hr> <hr>
<p align="right"><font size="2"> <p align="right"><font size="2">
Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
</font></p> </font></p>
</body> </body>

View File

@ -1,7 +1,7 @@
# #
# pg_reorg: lib/Makefile # pg_reorg: lib/Makefile
# #
# Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION # Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
# #
SRCS = reorg.c SRCS = reorg.c
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)

View File

@ -1,7 +1,7 @@
/* /*
* pg_reorg: lib/pg_reorg.sql.in * 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. -- Adjust this setting to control where the objects get created.

View File

@ -1,7 +1,7 @@
/* /*
* pg_reorg: lib/reorg.c * 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 "postgres.h"
#include "catalog/namespace.h" #include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h" #include "commands/trigger.h"
#include "executor/spi.h" #include "executor/spi.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#ifdef PG_MODULE_MAGIC #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_execp(SPIPlanPtr plan, Datum *values, const char *nulls, int expected);
static void reorg_execf(int expexted, const char *format, ...) static void reorg_execf(int expexted, const char *format, ...)
__attribute__((format(printf, 2, 3))); __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) \ #define copy_tuple(tuple, tupdesc) \
PointerGetDatum(SPI_returntuple((tuple), (tupdesc))) PointerGetDatum(SPI_returntuple((tuple), (tupdesc)))
@ -401,7 +405,7 @@ static void
parse_indexdef(IndexDef *stmt, Oid index, Oid table) parse_indexdef(IndexDef *stmt, Oid index, Oid table)
{ {
char *sql = pg_get_indexdef_string(index); 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); const char *tblname = generate_relation_name(table);
/* CREATE [UNIQUE] INDEX */ /* CREATE [UNIQUE] INDEX */
@ -518,14 +522,16 @@ WHERE I.indrelid = $1\
Datum Datum
reorg_swap(PG_FUNCTION_ARGS) 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_swapinfo;
SPIPlanPtr plan_swap; SPIPlanPtr plan_swap;
Oid argtypes[3] = { OIDOID, OIDOID, XIDOID }; Oid argtypes[3] = { OIDOID, OIDOID, XIDOID };
char nulls[3] = { ' ', ' ', ' ' }; char nulls[3] = { ' ', ' ', ' ' };
Datum values[3]; Datum values[3];
int record; int record;
/* authority check */ /* authority check */
must_be_superuser("reorg_swap"); 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(); SPI_finish();
PG_RETURN_VOID(); PG_RETURN_VOID();
@ -593,8 +605,8 @@ Datum
reorg_drop(PG_FUNCTION_ARGS) reorg_drop(PG_FUNCTION_ARGS)
{ {
Oid oid = PG_GETARG_OID(0); Oid oid = PG_GETARG_OID(0);
const char *relname = quote_identifier(get_rel_name(oid)); const char *relname = get_quoted_relname(oid);
const char *nspname = quote_identifier(get_namespace_name(get_rel_namespace(oid))); const char *nspname = get_quoted_nspname(oid);
/* authority check */ /* authority check */
must_be_superuser("reorg_drop"); must_be_superuser("reorg_drop");
@ -602,7 +614,10 @@ reorg_drop(PG_FUNCTION_ARGS)
/* connect to SPI manager */ /* connect to SPI manager */
reorg_init(); 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( reorg_execf(
SPI_OK_UTILITY, SPI_OK_UTILITY,
"DROP TRIGGER IF EXISTS z_reorg_trigger ON %s.%s CASCADE", "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) 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); 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)));
}

View File

@ -1,7 +1,7 @@
/* /*
* pg_reorg: lib/uninstall_reorg.sql * 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; DROP SCHEMA IF EXISTS reorg CASCADE;