Re: [SQL] many-to-many relationship

2008-10-08 Thread Louis-David Mitterrand
On Tue, Oct 07, 2008 at 05:16:39PM -0700, Steve Midgley wrote:
>
> I think the relationship tables method works pretty well but I have  
> another suggestion. You could store the Foreign table name within image  
> table as well as the Foreign key.
>
> |id|image_url|f_table|f_key
> |1 |url..|person |1234
> |2 |url2.|event  |5678
>
> I think this is called a "polymorphic join" but I could be wrong about  
> that. I'd guess you could construct a rule or trigger to validate the  
> foreign key data on insert/update but that's out of my skill area.

Hi Steve,

So in your solution the f_table column is just text which needs to be 
validated by a custom trigger?

-- 
http://www.lesculturelles.net

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


[SQL] 100% CPU at concurent access

2008-10-08 Thread Sabin Coanda
Hi there,

I'm trying to solve the concurrent access in my database, but I found some 
problems.

I use "PostgreSQL 8.2.4 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC) 
3.4.2 (mingw-special)";

I create the following scenario:
- use an empty database
- create the following table:

CREATE TABLE "TestTable"
(
  "ID" integer NOT NULL,
  CONSTRAINT "TestTable_pkey" PRIMARY KEY ("ID")
)
WITHOUT OIDS;
ALTER TABLE "TestTable" OWNER TO postgres;

- add a row in the table with

INSERT INTO "TestTable" VALUES ( 1000 );

- create the following functions:

CREATE OR REPLACE FUNCTION "TestProcInner"()
  RETURNS integer AS
$BODY$
DECLARE
Loops int4 = 10;
BEGIN
FOR i IN 0..Loops LOOP
RAISE NOTICE '%',i;
UPDATE "TestTable" SET "ID" = i;
PERFORM pg_sleep(1);
END LOOP;

RAISE NOTICE 'SUCCEEDED';
RETURN 0;

EXCEPTION
WHEN serialization_failure THEN
RAISE NOTICE 'FAILED';

RETURN 1;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION "TestProcInner"() OWNER TO postgres;

and

CREATE OR REPLACE FUNCTION "TestProcOuter"()
  RETURNS integer AS
$BODY$
DECLARE
Loops int4 := 1;
BEGIN
LOOP
RAISE NOTICE 'TestProcOuter: % loop', Loops;
IF 0 = "TestProcInner"() THEN
EXIT; -- LOOP
END IF;
Loops = Loops + 1;
END LOOP;

RETURN 0;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION "TestProcOuter"() OWNER TO postgres;


I use the following procedure to check when the concurrency access occures:
- open two query windows in pgAdmin
- add the following script in the both windows:

BEGIN TRANSACTION;
SELECT "TestProcInner"();
COMMIT;

- run the script in the 1st window
- run the script in the 2nd window
- check the results:
- the script in the 1st window commit the transaction
and write the log message 'SUCCEEDED'
- the script from the 2nd window catch an exception
and write the log message 'FAILED'


Then I try to use the following procedure to catch the concurrency access
occurence and retry until both scripts succeed:
- open two query windows in pgAdmin
- add the following script in the both windows:

BEGIN TRANSACTION;
SELECT "TestProcOuter"();
COMMIT;

- run the script in the 1st window
- run the script in the 2nd window
- the Postgres begins to CONSUME 100% CPU, and LOCKS until I cancel the 
connection
from other pgAdmin session
- after a few second the first window finishes with 'SUCCEEDED'
- the second window writes:

ERROR: canceling statement due to user request

Could somebody tell me why the procedure doesn't work and the CPU is used 
100%, please ?

TIA,
Sabin





-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


Re: [SQL] 100% CPU at concurent access

2008-10-08 Thread Tom Lane
"Sabin Coanda" <[EMAIL PROTECTED]> writes:
> Then I try to use the following procedure to catch the concurrency access
> occurence and retry until both scripts succeed:

What makes you think they ever will succeed?  Once one of these guys has
hit a failure, you've got a tight loop of retrying the same command and
getting the same failure.

regards, tom lane

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


Re: [SQL] Insert a space between each character

2008-10-08 Thread Dirk Jagdmann
> Use a regular expression, e.g.:
> select trim(regexp_replace('foobarbaz', '(.)', E'\\1 ', 'g'));

And if we only match characters until the last character in the
string, we can get rid of the outer trim():

# select regexp_replace('foobarbaz', E'(.)(?!$)', E'\\1 ', 'g');
  regexp_replace
---
 f o o b a r b a z
(1 row)

-- 
---> Dirk Jagdmann
> http://cubic.org/~doj
-> http://llg.cubic.org

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql