[GENERAL] Exception Handling in C-Language Functions?
I have the created a C-Language function (code is below). Now, I wonder: How do I handle exceptions, for example if malloc cannot assign the necessary memory? Do palloc and pfree handle such a case cleanly? Should I simply use an assert? #include postgres.h #include string.h #include stdlib.h #include fmgr.h #include libinn.h PG_FUNCTION_INFO_V1(ffiinews_uwildmat); /* Wrapper for INN's function uwildmat. Needs parameters in UTF-8. */ Datum ffiinews_uwildmat(PG_FUNCTION_ARGS) { VarChar *text = PG_GETARG_VARCHAR_P(0); VarChar *pattern = PG_GETARG_VARCHAR_P(1); int text_len = VARSIZE(text)-VARHDRSZ; int pattern_len = VARSIZE(pattern)-VARHDRSZ; char *tmp_text = (char *)malloc(text_len+1); if (tmp_text == NULL) ; /* What now? */ char *tmp_pattern = (char *)malloc(pattern_len+1); if (tmp_pattern == NULL) ; /* What now? */ strncpy(tmp_text, VARDATA(text), text_len); tmp_text[text_len] = '\0'; strncpy(tmp_pattern, VARDATA(pattern), pattern_len); tmp_pattern[pattern_len] = '\0'; bool matches = uwildmat(tmp_text, tmp_pattern); free(tmp_pattern); free(tmp_text); PG_RETURN_BOOL(matches); } -- Felix E. Klee ---(end of broadcast)--- TIP 7: don't forget to increase your free space map settings
Re: [GENERAL] Exception Handling in C-Language Functions?
Felix E. Klee wrote: I have the created a C-Language function (code is below). Now, I wonder: How do I handle exceptions, for example if malloc cannot assign the necessary memory? Do palloc and pfree handle such a case cleanly? Yes -- they will roll back the current transaction on if there is no memory available. You can catch the error via PG_TRY() in 8.0, although in the case of OOM there isn't probably a lot your exception handler could do... -Neil ---(end of broadcast)--- TIP 7: don't forget to increase your free space map settings
Re: [GENERAL] Exception Handling in C-Language Functions?
At Sat, 21 May 2005 20:13:13 +1000, Neil Conway wrote: Do palloc and pfree handle such a case cleanly? Yes -- they will roll back the current transaction on if there is no memory available. Great. So I just use those. You can catch the error via PG_TRY() in 8.0 [...] Nice, but we're not deploying 8.0. However, at the moment, out of memory exceptions seem to be the only ones that I've to deal with. -- Felix E. Klee ---(end of broadcast)--- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
Re: [GENERAL] numeric precision when raising one numeric to another.
On Fri, May 20, 2005 at 12:22:33PM -0500, Jim C. Nasby wrote: which you could take as requiring us to provide numeric equivalents of every floating-point operator, but I don't find that argument very convincing for operations that are inherently not going to give exact results. The spec demands exact results from addition, subtraction, and multiplication, but as soon as you get to division they punt; let alone transcendental functions. ISTM what's more important than be exact is respecting precision. If I'm remembering this correctly from high school, multiplying two numbers each having 10 significant digits means you then have 20 significant digits, so we should at least respect that. Which means numeric(500)^numeric(500) should give an exact numeric(1000), which I don't think is a given when casting to a double. Wrong. numeric(500) * numeric(500) = numeric(1000) numeric(500) ^ numeric(500) = numeric(10 ^ 503) googleplex You do not have enough memory to store the exact result. There are not enough atoms in the universe to store this result. That's one reason why you can't guarentee an exact result. Even numeric(20) ^ numeric(20) = numeric( 10 ^ 22 ) I'm not sure how this changes if you're using a fractional exponent. But it seems like a pretty serious issue if you're doing financial calculations and those are sometimes done in floating point under the covers. Financial calculations are a red herring. They don't deal with less than hundredths of a cent or more than trillions of dollars so 20 significant digits is easily enough. I would say to place an upper limit at say 100 digits. It you want better, go get a real math package. Have a nice day, -- Martijn van Oosterhout kleptog@svana.org http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a tool for doing 5% of the work and then sitting around waiting for someone else to do the other 95% so you can sue them. pgpZyDYANcWyv.pgp Description: PGP signature
Re: [GENERAL] table synonyms
I have searched for the word synonym through the wholePostrgeSQL 7.42 pdf documentation and all the ocurrencesare from functions redefinitions, and another PostreSQLuser has answered me that it does not support tablessynonyms, so I am with a big problem : How can I writequeries to suport different tables owners ?Let me explain : I run the same application at differentdatabase servers, from different enterprises, and theirDBA's can choose the name of the owner of the tables Ishould query to. How to manage this ? Can I use a parameterto define the table owner ? A macro ? Is there anysolution?Thanks a lot.Jayme. - Original Message - From: "Jim C. Nasby" <[EMAIL PROTECTED]>To: Jayme Jeffman Filho <[EMAIL PROTECTED]>Sent: 20-May-2005 13:31:49 -0300CC: pgsql-general@postgresql.orgSubject: Re: [GENERAL] table synonymsI don't remember off the top of my head exactly how synonyms worked, butI'm pretty sure PostgreSQL doesn't directly support them. You might beable to emulate them with rules, though.
Re: [GENERAL] Exception Handling in C-Language Functions?
Felix E. Klee [EMAIL PROTECTED] writes: I have the created a C-Language function (code is below). Now, I wonder: How do I handle exceptions, for example if malloc cannot assign the necessary memory? Do palloc and pfree handle such a case cleanly? Should I simply use an assert? As a general rule, never ever use malloc in backend code --- there's way too much risk of causing a memory leak. (If your function for some reason errors out before reaching the free(), that memory is gone. Lather, rinse, repeat a few million times, as is not unlikely to happen in a database server, and you've got a big problem.) Use palloc instead. You don't have to check it for a failure return, as Neil already noted, and you can be sloppy about remembering to free the result ... in fact, there's no real reason to free it at all, since anything palloc gives you inside a function will be short-term memory. See src/backend/utils/mmgr/README for details. If you want to report your own errors, use elog/ereport. BTW, a more future-proof way of doing what you want: VarChar *text = PG_GETARG_VARCHAR_P(0); int text_len = VARSIZE(text)-VARHDRSZ; char *tmp_text = (char *)malloc(text_len+1); if (tmp_text == NULL) ; /* What now? */ strncpy(tmp_text, VARDATA(text), text_len); tmp_text[text_len] = '\0'; is to let the varchar output routine do it: Datum text_datum = PG_GETARG_DATUM(0); char *text; text = DatumGetCString(DirectFunctionCall1(varcharout, text_datum)); This avoids assuming that you know the internal representation of varchar (and if you think that's frozen for eternity, you haven't been reading the discussions of ramping up our locale support...) regards, tom lane ---(end of broadcast)--- TIP 9: the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
Re: [GENERAL] table synonyms
I suspect you're mixing users and schemas, but it's been too long since I've used Oracle, so I'm not sure. Can you provide a more concrete example? FWIW, I suspect this is a non-issue with postgresql, since the only hierarchy of objects is schemas, and you can handle that with search_path. On Fri, May 20, 2005 at 03:01:34PM -0300, [EMAIL PROTECTED] wrote: I have searched for the word synonym through the whole PostrgeSQL 7.42 pdf documentation and all the ocurrences are from functions redefinitions, and another PostreSQL user has answered me that it does not support tables synonyms, so I am with a big problem : How can I write queries to suport different tables owners ? Let me explain : I run the same application at different database servers, from different enterprises, and their DBA's can choose the name of the owner of the tables I should query to. How to manage this ? Can I use a parameter to define the table owner ? A macro ? Is there any solution? Thanks a lot. Jayme. - Original Message - From: Jim C. Nasby To: Jayme Jeffman Filho Sent: 20-May-2005 13:31:49 -0300 CC: pgsql-general@postgresql.org Subject: Re: [GENERAL] table synonyms I don't remember off the top of my head exactly how synonyms worked, but I'm pretty sure PostgreSQL doesn't directly support them. You might be able to emulate them with rules, though. -- Jim C. Nasby, Database Consultant [EMAIL PROTECTED] Give your computer some brain candy! www.distributed.net Team #1828 Windows: Where do you want to go today? Linux: Where do you want to go tomorrow? FreeBSD: Are you guys coming, or what? ---(end of broadcast)--- TIP 3: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
Re: [GENERAL] Exception Handling in C-Language Functions?
At Sat, 21 May 2005 10:30:47 -0400, Tom Lane wrote: BTW, a more future-proof way of doing what you want: [...] is to let the varchar output routine do it: Datum text_datum = PG_GETARG_DATUM(0); char *text; text = DatumGetCString(DirectFunctionCall1(varcharout, text_datum)); This avoids assuming that you know the internal representation of varchar (and if you think that's frozen for eternity, you haven't been reading the discussions of ramping up our locale support...) Thanks for all those hints! I've now adapted my code as you recommended and it works fine, although I'm not certain what DirectFunctionCall1 and DatumGetCString actually do (it seems that the only documentation of these functions is in the source code, and I'm too lazy to sift through it at the moment). -- Felix E. Klee ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]