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
#
# Copyright (c) 2008, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
# Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
#
.PHONY: all install clean

View File

@ -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

View File

@ -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 <signal.h>
#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);
}

View File

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

View File

@ -284,7 +284,7 @@ pg_reorg の実行中には、<tt>VACUUM</tt> と <tt>ANALYZE</tt> <STRONG>以
<hr>
<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>
</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>
<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>
<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>
</div>
@ -268,7 +268,7 @@ In many case pg_reorg would fail and rollback collectly, but there are some case
<hr>
<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>
</body>

View File

@ -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)

View File

@ -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.

View File

@ -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 */
@ -519,6 +523,8 @@ Datum
reorg_swap(PG_FUNCTION_ARGS)
{
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;
@ -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)));
}

View File

@ -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;