PostGIS in Action
PostgreSQL: Up and Running
Book Store
About the Authors
Consulting
PostgreSQL
PostGIS
![]() ![]()
Sunday, September 04. 2011SQL Server to PostgreSQL: Converting table structurePrinter FriendlyRecommended Books: PostgreSQL 9.0 SQL Reference
SQL Server 2008 R2 Unleashed We've been working on converting some of our SQL Server apps to PostgreSQL. In this article we'll describe some things to watch out for and provide a function we wrote to automate some of the conversion. Although both databases are fairly ANSI-SQL compliant, there are still differences with their CREATE TABLE statements, data types, and how they handle other things that makes porting applications not so trivial. Continue reading "SQL Server to PostgreSQL: Converting table structure"
Posted by Leo Hsu and Regina Obe
in basics, beginner, oracle, plpgsql, sql server
at
00:33
| Comment (1)
| Trackbacks (0)
Monday, June 27. 2011Using PgAdmin PLPgSQL DebuggerPrinter FriendlyRecommended Books: PostGIS in Action PostgreSQL 9 Admin Cookbook / High Performance I'm one of those old-fashioned folks that debugs with print lines and raise notices. They're nice. They always work, you can put clock time stops in there and don't require any fancy configuration. At a certain point you do have to pull out a real debugger to see what is going on. This often happens when your one-liners are no longer good enough and now you have to write 20 liners of plpgsql code. Such is the case with geocoding and the PostGIS tiger geocoder specifically. Lots of interest has revived on that with people submitting bug reports and we've got paying clients in need of a fairly easy and speedy drop-in geocoder that can be molded to handle such things as road way locations, badly mis-spelled real estate data, or just simply to get rid of their dependency on Google, Yahoo, MapQuest, ESRI and other online or pricey geocoding tools. So I thought I'd take this opportunity to supplement our old-fashioned debugging with plpgsqldebugger goodness. In this article, we'll show you how to configure the plpgsql debugger integrated in PgAdmin and run with it. Continue reading "Using PgAdmin PLPgSQL Debugger"
Posted by Leo Hsu and Regina Obe
in 8.3, 8.4, 9.0, 9.1, basics, gis, intermediate, pgadmin, plpgsql
at
01:49
| Comments (9)
| Trackbacks (0)
Friday, June 03. 2011Variadic Functions in PostgreSQLPrinter FriendlyRecommended Books: PostGIS in Action
PostgreSQL 9.0 Volume 1 SQL Reference PostgreSQL 8.4 introduced the ability to create user-defined variadic functions. These are basically functions that take as input an undefined number of arguments where the argument that is an undefined number are all of the same type and are the last input arguments. Depesz went over it two years ago in Waiting for 8.4 variadic functions, so we are a bit late to the party. In a nutshell -- variadic functions are syntactic sugar for functions that would otherwise take arrays. In this article we'll provide some more demonstrations of them to supplement Depesz article. I was reminded that I had never explored this feature, when recently documenting one of the new PostGIS 2.0 Raster functions - ST_Reclass which employs this feature. I think ST_Reclass is a superb function and one of my favorite raster functions thus far that I hope to put to good use soon. Our new PostGIS family member,Bborie Park, is running thru our PostGIS Raster milestones much faster than I had dreamed. He's already implemented a good chunk of stuff we discussed in Chapter 13 - PostGIS Raster and had stated you probably won't see in PostGIS 2.0. He's going a bit faster than I can catalog them, so the documentation is already embarrassingly behind the fantastic functionality that is already present in PostGIS 2.0. Continue reading "Variadic Functions in PostgreSQL"
Posted by Leo Hsu and Regina Obe
in 8.4, gis, intermediate, mysql, pl programming, plpgsql, postgis, postgresql versions, sql functions
at
22:02
| Comment (1)
| Trackbacks (0)
Friday, April 08. 2011Using RETURNS TABLE vs. OUT parametersPrinter FriendlyRecommended Books: PostgreSQL 9.0 High Performance and Admin Cookbook PostgreSQL 9.0 SQL Language Reference In a prior article Use of Out and InOut Parameters we demonstrated how to use OUT parameters and INOUT parameters to return a set of records from a PostgreSQL function. There is another approach to doing this, and that is to use the ANSI Standard RETURNS TABLE construct. If you come from a SQL Server or IBM DB2 background, the RETURNS TABLE construct is probably most familiar, but still how you use it and what is legal in it is a little different than it is in SQL Server or IBM DB2. We'll save the contrast compare as a topic for another article. In terms of performance between using OUT vs. RETURNS TABLE, we haven't noticed much of a difference. The main thing that is nice about RETURNS TABLE is just that it's syntactically more pleasing in the sense that its clearer the structure of what you are returning. In these next examples, we'll demonstrate similar examples we showed in the aforementioned article except using the
Continue reading "Using RETURNS TABLE vs. OUT parameters"
Posted by Leo Hsu and Regina Obe
in 8.4, 9.0, db2, intermediate, pl programming, plpgsql, postgresql versions, sql functions, sql server
at
02:32
| Comments (0)
| Trackbacks (0)
Thursday, July 30. 2009Use of OUT and INOUT ParametersPrinter FriendlyPostgreSQL has supported what are called Out (output) parameters since version 8.1. We were surprised it has been that long since we always thought of it as a feature from 8.2+ until it recently came up for discussion on PostGIS newsgroup and we decided to investigate how long it has been supported. What are OUT parameters? These are parameters you define as part of the function argument list that get returned back as part of the result. When you create functions, the arguments are defaulted to IN parameters when not explicitly specified (which means they are passed in and not returned) which is why you sometimes see PgAdmin do something like IN somevariable variabletype when you use the function wizard. You can have INOUT parameters as well which are function inputs that both get passed in, can be modified by the function and also get returned. As a side note - In 8.4, PostgreSQL was enhanced to allow dynamic sql RETURN QUERY using RETURN QUERY EXECUTE syntax for plpgsql queries and also allow set returning functions being called in the SELECT part for any pl language. In prior versions, this was only a feature of PL functions written in SQL. 8.3 introduced RETURN query which required a static sql statement, but did make things a bit easier. One of the common use cases for using OUT parameters is to be able to return multiple outputs from a function without having to declare a PostgreSQL type as output of the function. In this article we shall cover all variants of this. We'll just focus on sql and plpgsql for this discussion, since we are not sure to what extent other pl languages (if at all) support IN OUT. Continue reading "Use of OUT and INOUT Parameters"
Posted by Leo Hsu and Regina Obe
in 8.2, 8.3, 8.4, intermediate, pl programming, plpgsql, postgresql versions, sql functions
at
14:34
| Comment (1)
| Trackbacks (0)
Wednesday, December 03. 2008PostgreSQL 8.3 PLPGSQL Cheatsheet OverviewPrinter FriendlyTo finish off our PL/PGSQL tutorial series, we are providing a PL/PGSQL cheat sheet. Below is a Thumbnail view of the PostgreSQL 8.3 PL/PGSQL cheat sheet that covers both 8.3 new features and past core PL/PGSQL features. This is by no means comprehensive, but are the features we use most often.
PDF Portrait version 8.5 x 11" of this cheatsheet is available at PostgreSQL 8.3 PL/PGSQL Cheatsheet 8.5 x 11 and also available in PDF A4 format and HTML.
Posted by Leo Hsu and Regina Obe
in 8.3, plpgsql, special feature
at
21:13
| Comment (1)
| Trackback (1)
Sunday, November 30. 2008Quick Guide to writing PLPGSQL Functions: Part 3 - NOTICES, RECURSION, and morePrinter FriendlyIn this third part of our PLPGSQL Quick Guide series, we shall delve into writing recursive functions. Before we do that, we shall demonstrate a very important but trivial feature in PostgreSQL and that is the RAISE NOTICE feature. There are more elegant ways of debugging, but this is the simple brain dead way of doing so. Continue reading "Quick Guide to writing PLPGSQL Functions: Part 3 - NOTICES, RECURSION, and more"
Posted by Leo Hsu and Regina Obe
in intermediate, pl programming, plpgsql, sql server
at
00:24
| Comment (1)
| Trackbacks (0)
Tuesday, October 28. 2008Quick Guide to writing PLPGSQL Functions: Part 2Printer FriendlyRecommended Books: PostgreSQL: Up and Running In first part Guide to Writing PLPGSQL functions, we covered the plpgsql function anatomy and basic IF and FOR loops. In this second part of our PLPGSQL Quick Guide series, we shall delve more into control flow. As we mentioned in the previous part, the following control flow constructs exist for PLPGSQL.
In this section we shall demonstrate looping thru sets of records and writing a set returning function. In the next section after, we shall delve a little into recursive functions, doing table updates, and raising notices. Continue reading "Quick Guide to writing PLPGSQL Functions: Part 2 "
Posted by Leo Hsu and Regina Obe
in beginner, pl programming, plpgsql
at
18:09
| Comments (6)
| Trackbacks (0)
Defined tags for this entry: plpgsql
Saturday, October 11. 2008Quick Guide to writing PLPGSQL Functions: Part 1Printer FriendlyRecommended Books: PostgreSQL: Up and Running In this series we'll go over writing PLPGSQL stored functions. We shall follow up in a later issue with a one page cheat sheet. The Anatomy of a PLPGSQL FUNCTIONAll PLPGSQL functions follow a structure that looks something like the below.
Continue reading "Quick Guide to writing PLPGSQL Functions: Part 1"
Posted by Leo Hsu and Regina Obe
in beginner, pl programming, plpgsql
at
22:46
| Comments (7)
| Trackbacks (0)
Defined tags for this entry: plpgsql
Saturday, May 10. 2008Choosing the right Database Procedural Language PLPrinter FriendlyOne of the great selling points of PostgreSQL is its pluggable PL language architecture. MySQL is known for its pluggable storage and PostgreSQL is known for its pluggable PL language architecture. From Monty's notes on slide 12 looks like MySQL may be working on a pluggable PL language architecture of their own. The most common of these languages are the all-purpose languages SQL and C (these are built-in and not really PLs like the others, but we'll throw them in there), PLPgSQL which is also built-in but not always enabled, PL/Perl, PL/Python, and the domain specific languages PL/R, PL/SH and gaining popularity Skype released PL/Proxy. There are others in the family such as PL/Tcl, PL/PHP, PL/Ruby, PL/Scheme (a dialect of Lisp), PL/Java, PL/Lua and PL/LOLCode (for kicks and as a reference implementation. Think of LOLCode as PostgreSQL Pluggable PL equivalent of MySQL's BLACK HOLE storage engine.) . The other interesting thing about the PostgreSQL PL language architecture is that it is a fairly thin wrapper around these languages. This means the kind of code you write in those languages is pretty much what you would write if you were doing general programming in those languages minus some spi calls. Since the handler is a just a thin wrapper around the environment, the language environment must be installed on the database server before you can use the PL language handler. This means you can have these functions utilized in your SQL statements and you can write in a language you feel comfortable with if you can get the darn PL compiled for your environment or someone has already kindly compiled it for your environment or that it is even compilable for your environment. The pluggable PL architecture means you can write a PL Handler for your favorite language or invent your own language that you can run in the database. In the end the barrier between code,data, and semantic constructs is more of a constraint imposed by compilers. If you have any doubts about the above statement, you need only look at some javascript injection attacks to bring the statement home. One of my fantasies is developing a language that morphs itself, that utilizes the database as its morphing engine and its OS and that breaks the illusion of data being data, code being code, and lacks rigid semantics. Of the languages we have worked with, SmallTalk comes closest to a language that satisfies these ideals and Lisp to a much lesser extent. Lisp lacked the semantic elegance of SmallTalk among other things. Most people are used to having their procedural language push their data around. PL code living in PostgreSQL allows your data to push your procedural code around in a set-based way. This is a simple but pretty powerful feature since data is in general more fluid than code. For interpretated/just-in time compiled languages it can live in the database, for compiled it has to call compiled functions. Continue reading "Choosing the right Database Procedural Language PL"
Posted by Leo Hsu and Regina Obe
in beginner, mysql, pl programming, plperl, plpgsql, PLR, sql functions, sql server
at
06:58
| Comments (4)
| Trackback (1)
Sunday, March 16. 2008REST in PostgreSQL Part 1 - The DB componentsPrinter FriendlyIn this section we'll go over creating the views and stored functions that our REST Server service will rely on. Our REST service will be a thin wrapper around a pgsql function that accepts film queries and returns results in XML. Loading the databaseStep 1: Download the Pagila 0.10.0 database from
http://pgfoundry.org/frs/?group_id=1000150&release_id=570
and load the Pagila database using the following commands:
Continue reading "REST in PostgreSQL Part 1 - The DB components"
Posted by Leo Hsu and Regina Obe
in application development, intermediate, pagila, plpgsql, tsearch, webservices
at
18:33
| Comments (2)
| Trackbacks (2)
Tuesday, February 26. 2008New Features for PostgreSQL Stored FunctionsPrinter FriendlyPostgreSQL 8.3 introduced a couple of new features that improves the processing of functions and makes plpgsql functions easier to write. These are as follows:
Continue reading "New Features for PostgreSQL Stored Functions"
Posted by Leo Hsu and Regina Obe
in intermediate, pl programming, plpgsql, sql functions
at
17:32
| Comments (8)
| Trackbacks (2)
Monday, December 31. 2007Trojan SQL Function Hack - A PL Lemma in DisguisePrinter FriendlyHave you ever noticed that in PostgreSQL you can put set returning functions in the SELECT part of an sql statement if the function is written in language SQL or C. Try the same trick for PL written functions such as plpgsql, plperl, plr etc, and you get a slap on the wrist of the form ERROR: set-valued function called in context that cannot accept a set. For Plpgsql and other PL languages you must put the set returning function in the FROM clause. Below is a simple example:
So it appears that PostgreSQL is not quite as democratic as we would like.
All interesting, but so what? you may ask. It is bad practice to put set returning functions in a SELECT clause. Such things are commonly mistakes and should be avoided. Functional Row ExpansionIt turns out that there are a whole class of problems in SQL where the simplest way to achieve the desired result is via a technique we shall call Functional Row Expansion. By that, we mean that for each record in a given set, we want to return another set of records that can not be expressed as a constant join expression. Basically the join expression is different for each record or the function we want to apply is too complicated to be expressed as a static join statement or join at all. Taking the above example. Lets say for each record in test, you want to return the 4 records preceding including the current one. So basically you want to explode each row into 5 or fewer rows. Your general gut reaction would be do something as follows: these give error: ERROR: function expression in FROM cannot refer to other relations of same query level
--But this does what you want
Keep in mind what makes the above tricky is that you want to return at most 4 of the preceding plus current. If you want to return all the preceding plus current, then you can do a trivial self join as follows:
So as you can see - its sometimes tricky to tell when you need to use this technique and when you don't. For this trivial example, writing the function as an SQL only function works fine and is the best to use. SQL functions unfortunately lack the ability to define dynamic sql statements, among other deficiencies so resorting to using a pl language is often easier which means you lose this useful feature of sql functions. Stuffing a pl function in an SQL function just might do the trick. We haven't tried this on other pl languages except plpgsql, but we suspect it should work the same.
Posted by Leo Hsu and Regina Obe
in advanced, pl programming, plpgsql, sql functions
at
17:33
| Comments (2)
| Trackbacks (0)
Friday, November 30. 2007Language Architecture in PostgreSQLPrinter FriendlyPerhaps one of the most unique and exciting things that makes PostgreSQL stand out from other database systems, are the numerous choices of languages one can use to create database functions, triggers and define new aggregate functions with. Not only can you use various languages to write your database stored functions with, but often times the code you write lives right in the database. You have no idea how cool this is until you see it in action. The other interesting thing about the PostgreSQL language architecture is the relative ease with which new languages can be incorporated in the system. Native Languages of PostgreSQLThere are 3 languages that come packaged with PostgreSQL (2 non-PL ones are installed automatically and not even listed as languages (C and SQL) in the languages section of a db). The defacto PL/PgSQL procedural language is available for install in all PostgreSQL distributions, but need not be installed in a db by default .
The PL languagesAside from PL/pgSQL there are numerous other procedural languages that one can use to create database stored functions and triggers. Some of these languages are fairly stable and even more are experimental. Some are only supported on Unix/Linux, but many are supported on Unix/Linux/MacOS/windows. In any case there are 3 key components needed before you can start using a new language:
Registering a language in a DatabaseFor pl/pgsql items 1 and 2 are already done if you have a working PostgreSQL install. In order to accomplish item 3, you may need to do the following from psql or PgAdmin III query window.
Alternatively you can run createlang plpgsql somedb from commandline. Note createlang is a command line program that is located in the bin folder of your PostgreSQL install. To see a list of procedural languages that you already have call handlers registered for in PostgreSQL. These are the languages you can register in your specific database - do a SELECT * FROM pg_catalog.pg_pltemplate
A Flavor of the Procedural Languages (PLs)In this section, we'll show a brief sampling of what functions look like written in various PLs. These are not to suggest they are the only ones that exist. For these examples, I'm going to use the $ quoting syntax introduced in PostgreSQL 8.0 which allows for not having to escape out single quotes. SQL - the not PL languageFor basic CRUD stuff,selects and simple functions, nothing hits the spot like just plain old SQL. Since this is such a common choice and often the best choice - here are 3 examples.
For details on using out parameters, check out Robert Treat's out parameter sql & plpgsql examples PLPGSQL - a real PL LanguageFor more complex logic and massaging of results before sending back. You need something more powerful than standard SQL. Below are some examples using PLPGSQL.
Using PL/Perl
Using PL/R a language and environment for statisticsOne of my favorite PL languages to program is PL/R. The reason for this is that the R statistical environment is such a rich environment for doing statistical processing. It now is also supported on windows as well as Mac and Linux. To learn more about R and installing PL/R. Check out our Boston GIS article PLR Part 1: Up and Running with PL/R (PLR) in PostgreSQL: An almost Idiot's Guide Below is the classic median aggregate function in R. It uses the native median function in the R environment to create a PostgreSQL aggregate median function
We will be covering PLR in greater detail in another article.
Posted by Leo Hsu and Regina Obe
in intermediate, pl programming, plpgsql, PLR, sql functions
at
00:00
| Comments (4)
| Trackbacks (3)
(Page 1 of 1, totaling 14 entries)
|
QuicksearchCalendar
CategoriesArchivesSyndicate This BlogBlog AdministrationShow tagged entriesRemote RSS/OPML-Blogroll FeedNo RSS/OPML feed selected
|
|||||||||||||||||||||||||||||||||||||||||||||||||




