From: Robert Haas <robertmhaas@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Alvaro Herrera <alvherre@commandprompt.com>
Cc: Euler Taveira de Oliveira <euler@timbira.com>
Cc: Emanuel <postgres.arg@gmail.com>
Cc: pgsql-bugs <pgsql-bugs@postgresql.org>
Subject: Re: BUG #6041: Unlogged table was created bad in slave node
Date: Wed, 1 Jun 2011 14:28:37 -0400
Message-ID: <BANLkTim433vF5HWjbJ0FSWm_-xA8DDaGNg@mail.gmail.com> (raw)
In-Reply-To: <25332.1306427674@sss.pgh.pa.us>
References: <201105261137.p4QBbTlt077425@wwwmaster.postgresql.org>
<4DDE7905.6050406@timbira.com>
<1306427067-sup-7622@alvh.no-ip.org>
<25332.1306427674@sss.pgh.pa.us>
On Thu, May 26, 2011 at 12:34 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Alvaro Herrera <alvherre@commandprompt.com> writes:
>> Excerpts from Euler Taveira de Oliveira's message of jue may 26 12:00:05 -0400 2011:
>>> I think we should emit the real cause in those cases, if possible (not too
>>> much overhead). The message would be "Unlogged table content is not available
>>> in standby server".
>
>> I guess what it should do is create an empty file in the slave.
>
> Probably it should, because won't the table malfunction after the slave
> is promoted to master, if there's no file at all there? Or will the
> process of coming live create an empty file even if there was none?
Coming live creates an empty file.
> But Euler is pointing out a different issue, which is usability. If the
> slave just acts like the table is present but empty, we are likely to
> get bug reports about that too. An error telling you you aren't allowed
> to access such a table on slaves would be more user-friendly, if we can
> do it without too much pain.
I looked into this a bit. A few observations:
(1) This problem is actually not confined to unlogged tables;
temporary tables have the same issue. For example, if you create a
temporary table on the master and then, on the slave, do SELECT * FROM
pg_temp_3.hi_mom (or whatever the name of the temp schema where the
temp table is) you get the same error. In fact I suspect if you took
a base backup that included the temporary relation and matched the
backend ID you could even manage to read out the old contents (modulo
any fun and exciting XID wraparound issues). But the problem is of
course more noticeable for unlogged tables since they're not hidden
away in a special funny schema.
(2) It's somewhat difficult to get a clean fix for this error because
it's coming from deep down in the bowels of the system, inside
mdnblocks(). At that point, we haven't got a Relation available, just
an SMgrRelation, so the information that this relation is unlogged is
not really available, at least not without some sort of gross
modularity violation like calling smgrexists() on the init fork. It
doesn't particularly seem like a good idea to conditionalize the
behavior of mdnblocks() on relpersistence anyway; that's a pretty
gross modularity violation all by itself.
(3) mdnblocks() gets called twice during the process of doing a simple
select on a relation - once from the planner, via estimate_rel_size,
and again from the executor, via initscan. A fairly obvious fix for
this problem is to skip the call to estimate_rel_size() if we're
dealing with a temporary or unlogged relation and are in recovery; and
make heap_beginscan_internal() throw a suitable error under similar
circumstances (or do we want to throw the error at plan time? tossing
it out from all the way down in estimate_rel_size() seems a bit
wacky). Patch taking this approach attached.
(4) It strikes me that it might be possible to address this problem a
bit more cleanly by allowing mdnblocks() and smgrnblocks() and
RelationGetNumberOfBlocksInFork() to take a boolean argument
indicating whether or not an error should be thrown if the underlying
physical file happens not to exist. When no error is to be signaled,
we simply return 0 when the main fork doesn't exist, rather than
throwing an error. We could then make estimate_rel_size() use this
variant, eliminating the need for an explicit test in
get_relation_info(). I'm sort of inclined to go this route even
though it would require touching a bit more code. We've already
whacked around RelationGetNumberOfBlocks() a bit in this release (the
function that formerly had that name is now
RelationGetNumberOfBlocksInFork(), and RelationGetNumberOfBlocks() is
now a macro that calls that function w/MAIN_FORKNUM) so it doesn't
seem like a bad time to get any other API changes that might be useful
out of our system.
Thoughts?
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
Attachments:reject-unlogged-during-recovery.patch (application/octet-stream, 2.0K) downloaddiff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex 346d6b9..e442076 100644--- a/src/backend/access/heap/heapam.c+++ b/src/backend/access/heap/heapam.c@@ -1202,6 +1202,14 @@ heap_beginscan_internal(Relation relation, Snapshot snapshot,
HeapScanDesc scan;
/*
+ * We are unable to scan temporary or unlogged relations during recovery.+ */+ if (!RelationNeedsWAL(relation) && RecoveryInProgress())+ ereport(ERROR,+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),+ errmsg("cannot access temporary or unlogged relations during recovery")));++ /*
* increment relation ref count while scanning relation
*
* This is just to make really sure the relcache entry won't go away while
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.cindex e71090f..437d74b 100644--- a/src/backend/access/transam/xlog.c+++ b/src/backend/access/transam/xlog.c@@ -24,6 +24,7 @@
#include <unistd.h>
#include "access/clog.h"
+#include "access/xlog.h"
#include "access/multixact.h"
#include "access/subtrans.h"
#include "access/transam.h"
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.cindex fd8ea45..39659ec 100644--- a/src/backend/optimizer/util/plancat.c+++ b/src/backend/optimizer/util/plancat.c@@ -105,8 +105,12 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
* case the size will be computed later in set_append_rel_pathlist, and we
* must leave it zero for now to avoid bollixing the total_table_pages
* calculation.
+ *+ * We also skip this step for temporary or unlogged relations, which are+ * inaccessible during recovery. The exact size doesn't really matter, as+ * the query is guaranteed to fail at execution time anyway.
*/
- if (!inhparent)+ if (!inhparent && (RelationNeedsWAL(relation) || !RecoveryInProgress()))
estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
&rel->pages, &rel->tuples);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: robertmhaas@gmail.com, tgl@sss.pgh.pa.us, alvherre@commandprompt.com, euler@timbira.com, postgres.arg@gmail.com
Subject: Re: BUG #6041: Unlogged table was created bad in slave node
In-Reply-To: <BANLkTim433vF5HWjbJ0FSWm_-xA8DDaGNg@mail.gmail.com>
References: <201105261137.p4QBbTlt077425@wwwmaster.postgresql.org> <4DDE7905.6050406@timbira.com> <1306427067-sup-7622@alvh.no-ip.org> <25332.1306427674@sss.pgh.pa.us> <BANLkTim433vF5HWjbJ0FSWm_-xA8DDaGNg@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox