Misa Simic | 1 Jul 2012 02:17
Picon

Re: how to return results from code block

Hi Andrus,


What is the main goal? Actually what is the reason that you need to execute, and get result in ADO.NET from code block? How I see code block feature, it is more to make easier dev & test of an procedural language function inside code block... Then when we are happy with result, actually create the function with body of your code block... Befor we needed actually to create function... then test... something wrong...i.e. we need one more parameter in function, delete existing function, create new function....test function... something wrong... make changes in function recreate it again... run it.... etc etc...

And yes, Code Block cant return any result so for testing purposes we use:

RAISE NOTICE %, somevariable;
i.e.

DO $$
DECLARE
--input parameters part - should be removed from final CREATE function code... fill default values as some test values for final function
  i integer :=0;
 
--standard function variables...
    rec record;
BEGIN
select i+1 as res1, i+2 as res2 INTO rec;
RAISE NOTICE %, rec;
END$$;

OK, if we are happy with result, we would copy/paste that code inside CREATE Function, remove "Input Parameters part" in DECLARE section and actually set them as function Input parameters... Replace on the end "RAISE NOTICE %, rec;" with "return rec" (if that is actually what function should return...) and job done... much easier than: execute Create, execute SELECT function, change code... execute CREATE, execute SELECT... etc etc... 


To get result in any client language, we must execute just SELECT query...

So basically you have several options:

1) make plpgsql function with input parameter "i" and body as your code in code block.... And execute "SELECT * FROM myfunctionname(0)" in .Net (better option)

2) make logic in .Net to actually create final Query based on your input parametar... i.e.

text getSQL(int t) 
{
string sql = "select " + i.ToString() + " +1 as res1, " + i.ToString+ "+2 as res2;";
return sql;
}

And execute that query with ExecuteQuery method...

Kind Regards,

Misa

2012/6/30 Andrus <kobruleht2 <at> hot.ee>
How to return single row or results from code block executed using ADO.NET ExecuteQuery() method.
I tried
 
DO $$
declare
  i integer :=0;
 
begin
select i+1 as res1, i+2 as res2;
END$$;
 
but got error:
 
ERROR:  query has no destination for result data
 
How to return single row result from code pgsql  code block ?
 
Andrus.

Misa Simic | 1 Jul 2012 03:02
Picon

Re: Complex database infrastructure - how to?

Hi Edson,


Maybe the best option is what Jack has already suggested, to have all in 1 DB in separate schemas (if tables are not already organised in separated schemas...)... But I am not sure I understand well problems:

2012/6/30 Edson Richter <edsonrichter <at> hotmail.com>
1) Copy tables from Administrative to Application: this approach would work, but I have trouble with the foreign keys. I'll have to disable (or drop) them, then copy data, then activate (or recreate them again). Could lead to problems?
2) dblink: I can't use foreign key to foreign tables. Also, it is very hard to implement with JPA.


1)  I guess data just need to be coppied in proper order... though, Postgres have deffered constraint feature, what means, FK will not break transaction until end (all trans commited)  - untill all data copied...

2) I just wonder On what way it is possible in MS SQL Server or any other db engine? (to have FK to foreign table... ) 


Thanks,

Misa
Scott Ribe | 1 Jul 2012 03:13
Favicon

ARD update warning (Mac stuff)

If you install the latest ARD update (which does not require a reboot), it apparently does something
similar to:

sudo killall postmaster

Oops. Thanks, Apple.

-- 
Scott Ribe
scott_ribe <at> elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice

--

-- 
Sent via pgsql-general mailing list (pgsql-general <at> postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Edson Richter | 1 Jul 2012 03:46
Picon
Favicon

Re: Complex database infrastructure - how to?

Em 30/06/2012 22:02, Misa Simic escreveu:
Hi Edson,

Maybe the best option is what Jack has already suggested, to have all in 1 DB in separate schemas (if tables are not already organised in separated schemas...)... But I am not sure I understand well problems:

2012/6/30 Edson Richter <edsonrichter <at> hotmail.com>
1) Copy tables from Administrative to Application: this approach would work, but I have trouble with the foreign keys. I'll have to disable (or drop) them, then copy data, then activate (or recreate them again). Could lead to problems?
2) dblink: I can't use foreign key to foreign tables. Also, it is very hard to implement with JPA.


1)  I guess data just need to be coppied in proper order... though, Postgres have deffered constraint feature, what means, FK will not break transaction until end (all trans commited)  - untill all data copied...

2) I just wonder On what way it is possible in MS SQL Server or any other db engine? (to have FK to foreign table... ) 


Thanks,

Misa

I'm inclined to solution (1). Setup proper triggers to copy data into all related databases would be easier (and have better performance).

Working with multiple schemas seems that I'll have lot of changes either in applications and maintenance routines.

Also, I'll have different databases replicated to different servers, according to some criteria. It would be impossible to set with schemas, right?

About solution (2), it's my mistake: MS SQL does not support it. MySQL does. Oracle allows to create foreign keys by using materialized views (that, for instance, can be cross database). MS SQL and DB2 only allow cross-database queries (that, perhaphs, I really don't understand why is not supported in PgSQL).

Regarding why support it? Multi-tenant systems are the first though. If you have separate databases because security issues (like HR database, that must be completely separated - not only database, but also physical server - from other applications due security constraints). But one table or view with correct clearance would be acceptable...

Thanks,

Edson.

Jasen Betts | 1 Jul 2012 09:33
X-Face
Picon

Re: Conversion of columns during CSV Import

On 2012-06-29, Patrick Schneider <patrick.schneider <at> debeka.de> wrote:
> Hello,
>
> is there any possibility to convert special columns during an CSV import 
> via COPY?
> For example:
>
> HELLO;WORLD;9999;011001
>
> 9999 should be converted to a character field

do you mean like '9999' (easy) or like '香' or like '✏' (harder)

> 011001 to the date 10th January 2001

I don't think there's any setting you can use to get postgres to
automatically translate 011001 to 10th January 2001 during a csv import

'20010110' would be doable 

> For the moment the only idea we have is, to import the CSV into a TEMP 
> table and
> perform the conversion by a select from the temp to the target table.

that's probably the best way.

-- 
⚂⚃ 100% natural

--

-- 
Sent via pgsql-general mailing list (pgsql-general <at> postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Jasen Betts | 1 Jul 2012 09:50
X-Face
Picon

Re: Complex database infrastructure - how to?

On 2012-06-30, Edson Richter <edsonrichter <at> hotmail.com> wrote:
> I've a plan that will need a complex database infra-structure using 
> PostgreSQL 9.1.
> I've seen similar setups using MS SQL Server and other databases, but 
> all of them support cross database queries (also easy to implement with 
> materialized views).
>
> - Administrative database: have few tables, used to administer the 
> infrastructure. This database have some tables like "users", "groups", 
> "permissions", etc.
> - Application databases: have app specific data.
>
> 1) One main Administrative application that will have read/write 
> permissions over the Administrative database.
> 2) Each application will have to access the application database (for 
> read/write), and the administrative database (for read only - mainly to 
> maintain the record references to the users that created objects, and so 
> on).
> 3) All applications are written in Java, using JPA for persistence.
> 4) All databases are running on same server, and all of them have same 
> encoding.

> Is there any ohter way to do that? Please, adivce!

Schemas,
give each application a different username and a matching schema name
with matching ownership the default postgres schema search path will
cause tables created by each application user accout to be segregated
into the schema with no extra work.

"Admin" schema tables can be accessed as eg: admin.tablename
or if you put them into the schema "public" or modfy the search path 
just by tablename.

http://www.postgresql.org/docs/9.1/static/ddl-schemas.html
AIUI postgresql schems are almost the same thing as mysql databases.

-- 
⚂⚃ 100% natural

--

-- 
Sent via pgsql-general mailing list (pgsql-general <at> postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Jasen Betts | 1 Jul 2012 09:56
X-Face
Picon

Re: Complex database infrastructure - how to?

On 2012-06-30, Edson Richter <edsonrichter <at> hotmail.com> wrote:

>> Consider using one database with multiple schemas. You can separate 
>> your applications into their own schemas, and you can have 
>> cross-schema foreign keys.
>>

> But how to keep application databases independent from each other?

different schemas.

> I mean, if I would like to apply maintenance (backup/restore/vacumm) 
> without interfering with the others?

pg_dump (the backup tool) can be restricted in scope by schema.
so you can take separate backups, you can drop an entire schmas
contents with the DROP SCHEMA .... CASCADE command etc.

> Also, there is a connection property for JDBC that allow to specify 
> which schema to use, so this approach is really transparent to my 
> application?

The username parameter either implicitly (using the default setting) or by previously issuing 
ALTER USER "someone" SET SEARCH_PATH TO "something" ;

-- 
⚂⚃ 100% natural

--

-- 
Sent via pgsql-general mailing list (pgsql-general <at> postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

AI Rumman | 1 Jul 2012 10:33
Picon

is there any query so that I may find the list of columns those have rules?

I am getting the following error:

ALTER TABLE base_table ALTER COLUMN base_table_field1 TYPE numeric(10,6);
ERROR:  cannot alter type of a column used by a view or rule
DETAIL:  rule _RETURN on view master_view depends on column "base_table_field1"

I know that I have a view based in the "base_table_field1" column.

So is there any query so that I may find the list of columns those have rules?

Any help will be appreciable.


Craig Ringer | 1 Jul 2012 16:19
Picon

Re: How to insert record only if primary key does not exist

On 06/30/2012 09:02 PM, Andrus wrote:
Table is defined as
 
CREATE TABLE firma1.klient (
  kood character(12) NOT NULL DEFAULT nextval('firma1.klient_kood_seq'::regclass),
....
);
 
How to insert record to this table only if primary key does not exist ?

You want an operation that's called an UPSERT or MERGE operation. PostgreSQL doesn't have any native support to do this for you. Doing it right is surprisingly tricky. This is the best article I've seen on the topic:

http://www.depesz.com/2012/06/10/why-is-upsert-so-complicated/

--
Craig Ringer
Chris Angelico | 1 Jul 2012 17:09
Picon

Re: How to insert record only if primary key does not exist

On Mon, Jul 2, 2012 at 12:19 AM, Craig Ringer <ringerc <at> ringerc.id.au> wrote:
> How to insert record to this table only if primary key does not exist ?
>
>
> You want an operation that's called an UPSERT or MERGE operation. PostgreSQL
> doesn't have any native support to do this for you. Doing it right is
> surprisingly tricky. This is the best article I've seen on the topic:
>
> http://www.depesz.com/2012/06/10/why-is-upsert-so-complicated/

Though that's aiming for a perfectly universal solution. There are
simpler solutions that work in restricted circumstances; the easiest
may be simply:

SAVEPOINT tryinsert
INSERT .... -- as normal
-- if error:
ROLLBACK TO SAVEPOINT tryinsert

Question: Is it better to simply do the insert as-is, or to have a
WHERE clause that will often, though not always, prevent duplicate
insertions?

ChrisA

--

-- 
Sent via pgsql-general mailing list (pgsql-general <at> postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Gmane