<p>I've been using PostgreSQL a little bit lately, and one of the things that I think is cool is that you can use languages other than SQL for scripting functions and whatnot.  But when is this actually useful?</p>

<p>For example, the documentation says that the main use for PL/Perl is that it's pretty good at text manipulation.  But isn't that more of something that should be programmed into the application?</p>

<p>Secondly, is there any valid reason to use an untrusted language?  It seems like making it so that any user can execute any operation would be a bad idea on a production system.</p>

<p>PS.  Bonus points if someone can make <a href="http://pgfoundry.org/projects/pllolcode" rel="nofollow noreferrer">PL/LOLCODE</a> seem useful.</p>

[{'answer_id': 39571, 'author': 'Mike Woodhouse', 'author_id': 1060, 'author_profile': 'https://Stackoverflow.com/users/1060', 'pm_score': 1, 'selected': False, 'text': '<p>These days, any "unique" or "cool" feature in a DBMS makes me incredibly nervous. I break out in a rash and have to stop work until the itching goes away.</p>\n\n<p>I just hate to be locked in to a platform unnecessarily. Suppose you build a big chunk of your system in PL/Perl inside the database. Or in C# within SQL Server, or PL/SQL within Oracle, there are plenty of examples*.</p>\n\n<p>Now you suddenly discover that your chosen platform doesn\'t scale. Or isn\'t fast enough. Or something. Worse, there\'s a new kid on the database block (something like MonetDB, CouchDB, Cache, say but much cooler) that would solve all your problems (even if your only problem, like mine, is having an uncool databse platform). And you can\'t switch to it without recoding half your application.</p>\n\n<p>(*Admittedly, the paid-for products are to some extent seeking to lock you in by persuading you to use their unique features, which is not an accusation that can directly be levelled at the free providers, but the effect is the same).</p>\n\n<p>So that\'s a rant on the first part of the question. Heart-felt, though.</p>\n\n<blockquote>\n  <p>is there any valid reason to use an\n  untrusted language? It seems like\n  making it so that any user can execute\n  any operation would be a bad idea</p>\n</blockquote>\n\n<p>My goodness, yes it does! A sort of "Perl injection attack"? Almost worth doing it just to see what happens, I\'d have thought.</p>\n\n<p>For philosophical reasons outlined above I think I\'ll pass on the PL/LOLCODE challenge. Although I was somewhat amazed to discover it was a link to something extant.</p>\n'}, {'answer_id': 39634, 'author': 'Neall', 'author_id': 619, 'author_profile': 'https://Stackoverflow.com/users/619', 'pm_score': 3, 'selected': True, 'text': '<p>"isn\'t that [text manipulation] more of something that should be programmed into the application?"</p>\n\n<p>Usually, yes. The generally accepted "<a href="http://en.wikipedia.org/wiki/Multitier_architecture" rel="nofollow noreferrer">three-tier</a>" application design for databases says that your logic should be in the middle tier, between the client and the database. However, sometimes you need some logic in a trigger or need to index on a function, requiring that some code be placed into the database. In that case all the usual "which language should I use?" questions come up.</p>\n\n<p>If you only need a little logic, the most-portable language should probably be used (pl/pgSQL). If you need to do some serious programming though, you might be better off using a more expressive language (maybe pl/ruby). This will always be a judgment call.</p>\n\n<p>"is there any valid reason to use an untrusted language?"</p>\n\n<p>As above, yes. Again, putting direct file access (for example) into your middle tier is best when possible, but if you need to fire things off based on triggers (that might need access to data not available directly to your middle tier), then you need untrusted languages. It\'s not ideal, and should generally be avoided. And you definitely need to guard access to it.</p>\n'}, {'answer_id': 40025, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 3, 'selected': False, 'text': '<p>@Mike: this kind of thinking makes me nervous. I\'ve heard to many times "this should be infinitely portable", but when the question is asked: do you actually foresee that there will be any porting? the answer is: no.</p>\n\n<p>Sticking to the lowest common denominator can really hurt performance, as can the introduction of abstraction layers (ORM\'s, PHP PDO, etc). My opinion is:</p>\n\n<ul>\n<li>Evaluate realistically if there is a need to support multiple RDBMS\'s. For example if you are writing an open source web application, chances are that you need to support MySQL and PostgreSQL at least (if not MSSQL and Oracle)</li>\n<li>After the evaluation, make the most of the platform you decided upon</li>\n</ul>\n\n<p>And BTW: you are mixing relational with non-relation databases (CouchDB is <em>not</em> a RDBMS comparable with Oracle for example), further exemplifying the point that the perceived need for portability is many times greatly overestimated.</p>\n'}, {'answer_id': 45557, 'author': 'kaybenleroll', 'author_id': 277, 'author_profile': 'https://Stackoverflow.com/users/277', 'pm_score': 1, 'selected': False, 'text': '<p>From my perspective, I guess the answer is \'it depends\'.</p>\n\n<p>There is an argument that manipulation of the data belongs in the database layer, so that the business logic does not need to be overly concerned about how the manipulation happens, it just knows that it has.</p>\n\n<p>Another very good reason to process data on the db layer is if the volume of data being crunched means that network bandwidth will become an issue. I once had to categorise very large amounts of data. Processing this in the application layer was severly restricted by the time required to transfer all the data across the network for processing.</p>\n\n<p>I then wrote a binning algorithm in PL/pgSQL and it worked much faster.</p>\n\n<p>Regarding untrusted languages, I heard a podcast from Josh Berkus (a postgres advocate) who discussed an application of postgresql that brought in data from MySQL as part of its processing, so that the communication itself was handled by the postgres server. I don\'t remember the full details, I think it was on the <a href="http://www.twit.tv/floss18" rel="nofollow noreferrer">FLOSS Weekly podcast</a> which is quite an interesting discussion of the history of PostGRESQL and some of the issues it is put to.</p>\n'}, {'answer_id': 93271, 'author': 'Tony Pitale', 'author_id': 1167846, 'author_profile': 'https://Stackoverflow.com/users/1167846', 'pm_score': 0, 'selected': False, 'text': '<p>I think most additional languages are offered so that if you develop in that language on a regular basis, you can feel comfortable writing db functions, triggers, etc. The usefulness of these features is to provide a control over data as close to the data as possible.</p>\n'}, {'answer_id': 113334, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>The untrusted versions of the procedural languages allow you to access I/O on the system.\nThis can come in handy if you need a trigger or something send a email or connect to a socket server to send a popup notification.  There are tons of uses for this type of thing, and because of postgresql isolation levels you cans safely do things like this.\nYou can put checkpoints in the function so if the transaction fails the email or whatever won't go out.  The nice thing about doing this is it removes the logic from the client and puts it on the server.</p>\n"}, {'answer_id': 11634066, 'author': 'cms', 'author_id': 28532, 'author_profile': 'https://Stackoverflow.com/users/28532', 'pm_score': 0, 'selected': False, 'text': "<p>An example of a useful stored procedure I recently wrote in an external language that would not have been possible in pl/sql is a version of 'df' which allowed SQL table generators to pick a tablespace with the most free space available at runtime.</p>\n\n<p>I used plperlu, and it was relatively simple, although I had to be careful with data typing.</p>\n"}]
['https://Stackoverflow.com/questions/39517', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2147/']