Re: Assert for frontend programs?

From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Assert for frontend programs?
Date: 2012-12-14 16:20:03
Message-ID: 50CB51B3.70302@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 14.12.2012 17:54, Tom Lane wrote:
> Andrew Dunstan<andrew(at)dunslane(dot)net> writes:
>> As I'm working through the parallel dump patch, I notice this in one of
>> the header files:
>
>> #ifdef USE_ASSERT_CHECKING
>> #define Assert(condition) \
>> if (!(condition)) \
>> { \
>> write_msg(NULL, "Failed assertion in %s, line %d\n", \
>> __FILE__, __LINE__); \
>> abort();\
>> }
>> #else
>> #define Assert(condition)
>> #endif
>
>
>> I'm wondering if we should have something like this centrally (e.g. in
>> postgres_fe.h)? I can certainly see people wanting to be able to use
>> Assert in frontend programs generally, and it makes sense to me not to
>> make everyone roll their own.
>
> +1, especially if the hand-rolled versions are likely to be as bad as
> that one (dangling else, maybe some other issues I'm not spotting
> in advance of caffeine consumption). I've wished for frontend Assert
> a few times myself, but never bothered to make it happen.

+1, I just ran into this while working on Andres' xlogreader patch.
xlogreader uses Assert(), and it's supposed to work in a stand-alone
program.

> Although I think we had this discussion earlier and it stalled at
> figuring out exactly what the "print error" part of the macro ought
> to be. The above is obviously pg_dump-specific. Perhaps
> fprintf(stderr,...) would be sufficient, though -- it's not like
> tremendous user friendliness ought to be necessary here.
>
> Also, I think the message really has to include some string-ified
> version of the assertion condition --- the line number alone is pretty
> unhelpful when looking at field reports of uncertain provenance.
>
> BTW, I think psql already has a "psql_assert".

psql_assert looks like this:

#ifdef USE_ASSERT_CHECKING
#include <assert.h>
#define psql_assert(p) assert(p)
#else
...

On my Linux system, a failure looks like this:

~$ ./a.out
a.out: a.c:5: main: Assertion `1==2' failed.
Aborted

That seems fine to me.

- Heikki

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Mikko Tiihonen 2012-12-14 16:29:58 Re: Use gcc built-in atomic inc/dec in lock.c
Previous Message Merlin Moncure 2012-12-14 15:55:28 Re: Use gcc built-in atomic inc/dec in lock.c