
Let’s start with a set of rows, containing the descriptions of today’s appointments. That is, ARRAY expects to get rows as its input, and returns a single row, containing a single array, as its output. The ARRAY function is built into PostgreSQL, and turns a set of rows into an array. parentheses that tell it to do different things.) (To be honest, I’m not sure how PostgreSQL can tell the difference it might be the square brackets vs. Now, don’t confuse the ARRAY constructor with the ARRAY function, although it’s hard not to. The way to accomplish this is with the ARRAY function. With PostgreSQL’s arrays, though, we can do this all in the database - which executes much more quickly, among other things. Normally, I would need to retrieve all of the rows, and then use another language to piece them together. But let’s say that I want to get all of the descriptions of today’s appointments in a comma-separated list. So far, this isn’t very exciting or surprising. Without that, the comparison would have been at midnight. Notice that in the above example, I’m using PostgreSQL’s casting operator (::) to turn the meeting_at column, which contains a TIMESTAMP value, into a DATE value. If I want to find out what I’m doing on May 23rd, I can simply say: /reuven=# select * from appointments where meeting_at::date = '2014-may-23' I create an Appointments table: CREATE TABLE Appointments (id SERIAL,Īnd then I enter information into this table, such that I have the following appointments: /reuven=# select * from appointments order by meeting_at This is where, in my mind, arrays start to get truly powerful, and allow us to do all sorts of things that would otherwise be quite difficult, or require another language.įor example, let’s say that I want to keep track of my appointments in a database. But the coolest trick, or set of tricks, that I use in PostgreSQL is the ability to turn arrays into rows, and vice versa. The sum_avg() function accepts a list of numbers, calculates the total and average, and returns both values.So far in this series, I have described how you can create arrays and retrieve information from them - both the actual data stored in the array, and information about the array, such as its length. The arguments are passed to the function as an array.Įxample: CREATE OR REPLACE FUNCTION sum_avg( CREATE OR REPLACE FUNCTION square(Ī function can accept a variable number of arguments with one condition that all the arguments have the same data type. The following square function accepts a number and returns the square of that number. The function then changes the argument and passes the value back as a part of the result. It means that the caller can pass a value to the function. The INOUT parameter is the combination IN and OUT parameters. The OUT parameters are useful in a function that needs to return multiple values without the need of defining a custom type. Because we use the OUT parameters, we don’t need to have a RETURN statement. Inside the function, we get the greatest and least numbers of three IN parameters using GREATEST and LEAST built-in functions. Two OUT parameters: hi (high) and lo (low).For better understanding let’s define a function as below: CREATE OR REPLACE FUNCTION hi_lo( The OUT parameters are defined as part of the function arguments list and are returned back as a part of the result. You can pass the IN parameters to the function but you cannot get them back as a part of the result. By default, the parameter’s type of any parameter in PostgreSQL is IN parameter. The data types of the two parameters are NUMERIC. The get_sum() function accepts two parameters: a, and b, and returns a numeric. To better understand these function parameters let’s first define a function on which we will experiment with the above-mentioned function parameters as below: CREATE OR REPLACE FUNCTION get_sum( ISRO CS Syllabus for Scientist/Engineer Exam.ISRO CS Original Papers and Official Keys.

GATE CS Original Papers and Official Keys.
