Thread: Moving from MySQL to PGSQL....some questions
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my companies current project. I have been able to setup postgresql in my rh box and i can connect and work with psql. I even downloaded pgadmin III so that i can get to work with a GUI interface. As I starting...I see the architecture of PGSQL is quite complex...or thats what I feel....maybe its for good :) Here are some of my doubts : 1.) What is template1 and template0? I assume these are system databases. Am I right? 2.) When I create a database using CREATE DATABASE stmt. a new DB is created where it has 4 schemas and around 100 tables. These are the system tables keeping information about everything in the database? I hope I am correct :) 3.) To get all the database is the server we use query like - select datname from pg_database I means that there exists a table pg_database in all the database and all the pg_database table(s) are updated whenever a user issues CREATE DATABASE stmt. Why I am saying so coz in PgAdmin III i can see these tables in all the databases? 4.) I couldnot find any query to change the context of database like in MySQL : use database; or am i missing something? 5.) In MySQL, there are many command like show tables, show databases etc. to get object details. I cant see anything similar in PGSQL. After searching the net i find that i have to execute certain queries to fetch those queries. Is this the only way? Any help would be appreciated. Regards Karam __________________________________ Do you Yahoo!? Yahoo! Mail SpamGuard - Read only the mail you want. http://antispam.yahoo.com/tools
Karam, try \? to get answers to your questions. Dave On Wed, 2004-02-25 at 09:57, Karam Chand wrote: > Hello > > I have been working with Access and MySQL for pretty > long time. Very simple and able to perform their jobs. > I dont need to start a flame anymore :) > > I have to work with PGSQL for my companies current > project. > > I have been able to setup postgresql in my rh box and > i can connect and work with psql. I even downloaded > pgadmin III so that i can get to work with a GUI > interface. > > As I starting...I see the architecture of PGSQL is > quite complex...or thats what I feel....maybe its for > good :) Here are some of my doubts : > > 1.) What is template1 and template0? I assume these > are system databases. Am I right? > > 2.) When I create a database using CREATE DATABASE > stmt. a new DB is created where it has 4 schemas and > around 100 tables. These are the system tables keeping > information about everything in the database? I hope I > am correct :) > > 3.) To get all the database is the server we use query > like - > > select datname from pg_database > > I means that there exists a table pg_database in all > the database and all the pg_database table(s) are > updated whenever a user issues CREATE DATABASE stmt. > > Why I am saying so coz in PgAdmin III i can see these > tables in all the databases? > > 4.) I couldnot find any query to change the context of > database like in MySQL : > > use database; > > or am i missing something? > > 5.) In MySQL, there are many command like show tables, > show databases etc. to get object details. I cant see > anything similar in PGSQL. After searching the net i > find that i have to execute certain queries to fetch > those queries. Is this the only way? > > Any help would be appreciated. > > Regards > Karam > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail SpamGuard - Read only the mail you want. > http://antispam.yahoo.com/tools > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org > -- Dave Cramer 519 939 0336 ICQ # 14675561
Karam Chand wrote: > 1.) What is template1 and template0? I assume these > are system databases. Am I right? Yes. whenever a new database is created, these databases are copied there. So these are like initial master copies. > 2.) When I create a database using CREATE DATABASE > stmt. a new DB is created where it has 4 schemas and > around 100 tables. These are the system tables keeping > information about everything in the database? I hope I > am correct :) Yes. > 3.) To get all the database is the server we use query > like - > > select datname from pg_database > > I means that there exists a table pg_database in all > the database and all the pg_database table(s) are > updated whenever a user issues CREATE DATABASE stmt. > > Why I am saying so coz in PgAdmin III i can see these > tables in all the databases? Some tables such as users/passwords/groups and databases are shared across all the databases. You are looking at same data. > > 4.) I couldnot find any query to change the context of > database like in MySQL : > > use database; > > or am i missing something? Any postgresql session has to connect to a database. To connect to different database, you need to initiate a new connection or drop existing one and create new one. You can not switch the database-connected-to on the fly. > 5.) In MySQL, there are many command like show tables, > show databases etc. to get object details. I cant see > anything similar in PGSQL. After searching the net i > find that i have to execute certain queries to fetch > those queries. Is this the only way? No. Simplest would be issuing \? on psql prompt. It will tell you plethora of options/commands using which you can accomplish many task. Just remember that these are not SQL command provided by server. It is the psql application which provide these commands. So you cannot use them in say php. > Any help would be appreciated. HTH Shridhar
On Wed, 25 Feb 2004, Karam Chand wrote: > 1.) What is template1 and template0? I assume these > are system databases. Am I right? When you create a new database what you get is a copy of template1. template0 is alsmost not used. If you mess up template1 so you can't create usable new databases one can use template0 to create a new template1. The system tables are a bit complexed and some are shared between all databases. > 3.) To get all the database is the server we use query > like - > > select datname from pg_database or \l in psql > I means that there exists a table pg_database in all > the database and all the pg_database table(s) are > updated whenever a user issues CREATE DATABASE stmt. yes, pg_database is a shared table. > 4.) I couldnot find any query to change the context of > database like in MySQL : > > use database; \c in psql. > > 5.) In MySQL, there are many command like show tables, > show databases etc. to get object details. I cant see > anything similar in PGSQL. After searching the net i > find that i have to execute certain queries to fetch > those queries. Is this the only way? \d and others. \? is a useful command. Also the man page (man psql) can help. -- /Dennis Björklund
Thanks. That was very helpful One more thing (it might be slightly off topic): I have two computers on network, one is running RH Linux and one running WInXP. My PostgreSQL is running in Linux box. I downloaded PgAdmin III for both OS. When I am connecting from the Linux box I am able to view the three system schemas : pg_catalog pg_temp_1 pg_toast When I am accessing it using PgAdmin III on Windows I can only see the 'public' schema and its tables? Why is it so? Regards Karam -- Shridhar Daithankar <shridhar@frodo.hserus.net> wrote: > Karam Chand wrote: > > 1.) What is template1 and template0? I assume > these > > are system databases. Am I right? > > Yes. whenever a new database is created, these > databases are copied there. So > these are like initial master copies. > > > 2.) When I create a database using CREATE DATABASE > > stmt. a new DB is created where it has 4 schemas > and > > around 100 tables. These are the system tables > keeping > > information about everything in the database? I > hope I > > am correct :) > > Yes. > > > 3.) To get all the database is the server we use > query > > like - > > > > select datname from pg_database > > > > I means that there exists a table pg_database in > all > > the database and all the pg_database table(s) are > > updated whenever a user issues CREATE DATABASE > stmt. > > > > Why I am saying so coz in PgAdmin III i can see > these > > tables in all the databases? > > Some tables such as users/passwords/groups and > databases are shared across all > the databases. You are looking at same data. > > > > 4.) I couldnot find any query to change the > context of > > database like in MySQL : > > > > use database; > > > > or am i missing something? > > Any postgresql session has to connect to a database. > To connect to different > database, you need to initiate a new connection or > drop existing one and create > new one. > > You can not switch the database-connected-to on the > fly. > > > 5.) In MySQL, there are many command like show > tables, > > show databases etc. to get object details. I cant > see > > anything similar in PGSQL. After searching the net > i > > find that i have to execute certain queries to > fetch > > those queries. Is this the only way? > > No. Simplest would be issuing \? on psql prompt. It > will tell you plethora of > options/commands using which you can accomplish many > task. Just remember that > these are not SQL command provided by server. It is > the psql application which > provide these commands. So you cannot use them in > say php. > > > Any help would be appreciated. > > HTH > > Shridhar __________________________________ Do you Yahoo!? Yahoo! Mail SpamGuard - Read only the mail you want. http://antispam.yahoo.com/tools
Le Mercredi 25 Février 2004 16:45, Karam Chand a écrit : > When I am accessing it using PgAdmin III on Windows I > can only see the 'public' schema and its tables? Why > is it so? Have a deeper look at pgAdmin III menu: Display>-System objects pgAdmin III includes the documentation of PostgreSQL. It is highly recommended to dig into the documentation. By the way, if one of you was interested by translating pgAdmin into any language not yet supported, you are welcome. Cheers, Jean-Michel
On Wed, Feb 25, 2004 at 06:57:04AM -0800, Karam Chand wrote: > 4.) I couldnot find any query to change the context of > database like in MySQL : > > use database; Your other questions were answered more than adequately by others, but I thought I'd provide more details here. If you're in the command line psql, then use "\c database" to accomplish this. The drawback is that you cannot change the database in that manner programmatically. If you're using Perl or PHP, for instance, you must connect to the other database explicitly using the proper function. In Perl, this means you must use DBI->connect again with the new database name. In MySQL, you can get data from another database by using the construct "datbase.table" to refer to the table. This doesn't work in Postgres. If you need to do something like that, you likely need to be using schemas. I've put together a quick list of minor differences between MySQL and Postgres (besides the obvious "real RDBMS" features that exist only in Postgres) that should help you get started: http://www.michaelchaney.com/mysql-to-postgres.html That includes information on date handling, literal quoting, basically anything that I ran in to while converting an application. But it should help you get started quickly. Michael -- Michael Darrin Chaney mdchaney@michaelchaney.com http://www.michaelchaney.com/
Dennis Bjorklund wrote: > On Wed, 25 Feb 2004, Karam Chand wrote: > >>1.) What is template1 and template0? I assume these >>are system databases. Am I right? > > When you create a new database what you get is a copy of template1. > > template0 is alsmost not used. If you mess up template1 so you can't > create usable new databases one can use template0 to create a new > template1. More specifically (as I understand it) template1 is intended to be a template (with default settings and the like) for creating new databases. So (for example) if your business policy is that all created databases use plpgsql, you can createlang it into template1, and every database created thereafter will already have plpgsql. If you have specific tables or the like that every database on that server should have, you can put them in template1 so they are always there. template0 is what you use to fix things, if you mess up template1 somehow. -- Bill Moran Potential Technologies http://www.potentialtech.com
Shridhar Daithankar wrote: >> show databases etc. to get object details. I cant see >> anything similar in PGSQL. After searching the net i >> find that i have to execute certain queries to fetch >> those queries. Is this the only way? > > 5.) In MySQL, there are many command like show tables, > > No. Simplest would be issuing \? on psql prompt. It will tell you > plethora of options/commands using which you can accomplish many task. > Just remember that these are not SQL command provided by server. It is > the psql application which provide these commands. So you cannot use > them in say php. > If you run psql with the "-E" parameter, whenever you execute a psql command that translates to a query, that query will be displayed on screen. This allows you to check out what queries you need for certain operations. For example - to check all the tables in the current database/schema: $ psql -E db Welcome to psql 7.4.1, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit db=# \dt ********* QUERY ********** SELECT n.nspname as "Schema", c.relname as "Name", CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type", u.usename as "Owner" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1,2; ************************** List of relations Schema | Name | Type | Owner --------+--------------+-------+------- Check out the rest of the \d* commands for more listings (\? will give you the list). Also, it pays to look up the meaning of the above in the documentation. The system tables are documented in http://www.postgresql.org/docs/7.4/static/catalogs.html >> Any help would be appreciated. > Shachar -- Shachar Shemesh Lingnu Open Systems Consulting http://www.lingnu.com/
One other note, for those converting a database from MySQL to PostgreSQL, I have a table creation conversion script here: http://www.michaelchaney.com/downloads/m2p.pl I know that two come with PostgreSQL in the contrib directory, but I wrote this because those two didn't do what I needed. With this, you should be able to take the MySQL table creation scripts (as created by mysqldump --tab=x) and directly build the tables and load the data into a PostgreSQL db with little effort. Michael -- Michael Darrin Chaney mdchaney@michaelchaney.com http://www.michaelchaney.com/
In PGAdmin III -- you might want to UNCHECK the "Display system objects" option under the "Display" menu option -- this will prevent you from seeing all of the non-public schema's and limit your view in PGAdmin to just the databases you created... Most people dont really need to dink around with the system tables anyway... As you probably noticed -- postgreSQL is a different beast than MS Access and mySQL -- postgreSQL is a true RDBMS like Sybase, Orale, and SQL Server... postgrSQL is a true 'client/server' RDBMS -- it does not contain it's own GUI client like MS Access postgreSQL is NOT just a high-powered version of MS Access or mySQL -- there are quite a few differences -- Not to be rude -- but the postgreSQL docs (the Preface, Tutorial, and SQL Language sections) would be good for you to read... -- Greg Patnude / The Digital Demention 2916 East Upper Hayden Lake Road Hayden Lake, ID 83835 (208) 762-0762 "Karam Chand" <karam_chand03@yahoo.com> wrote in message news:20040225145704.40397.qmail@web60804.mail.yahoo.com... > Hello > > I have been working with Access and MySQL for pretty > long time. Very simple and able to perform their jobs. > I dont need to start a flame anymore :) > > I have to work with PGSQL for my companies current > project. > > I have been able to setup postgresql in my rh box and > i can connect and work with psql. I even downloaded > pgadmin III so that i can get to work with a GUI > interface. > > As I starting...I see the architecture of PGSQL is > quite complex...or thats what I feel....maybe its for > good :) Here are some of my doubts : > > 1.) What is template1 and template0? I assume these > are system databases. Am I right? > > 2.) When I create a database using CREATE DATABASE > stmt. a new DB is created where it has 4 schemas and > around 100 tables. These are the system tables keeping > information about everything in the database? I hope I > am correct :) > > 3.) To get all the database is the server we use query > like - > > select datname from pg_database > > I means that there exists a table pg_database in all > the database and all the pg_database table(s) are > updated whenever a user issues CREATE DATABASE stmt. > > Why I am saying so coz in PgAdmin III i can see these > tables in all the databases? > > 4.) I couldnot find any query to change the context of > database like in MySQL : > > use database; > > or am i missing something? > > 5.) In MySQL, there are many command like show tables, > show databases etc. to get object details. I cant see > anything similar in PGSQL. After searching the net i > find that i have to execute certain queries to fetch > those queries. Is this the only way? > > Any help would be appreciated. > > Regards > Karam > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail SpamGuard - Read only the mail you want. > http://antispam.yahoo.com/tools > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org >
> "Karam Chand" <karam_chand03@yahoo.com> wrote in message > news:20040225145704.40397.qmail@web60804.mail.yahoo.com... > > > > 5.) In MySQL, there are many command like show tables, > > show databases etc. to get object details. I cant see > > anything similar in PGSQL. After searching the net i > > find that i have to execute certain queries to fetch > > those queries. Is this the only way? One easy way is to use the psql command line program and the \d command. It lists all your tables, lists all the columns in a table, etc. Karl <kop@meme.com> Free Software: "You don't pay back, you pay forward." -- Robert A. Heinlein
Database metadata queries (WAS Re: Moving from MySQL to PGSQL....some questions)
From
"Shawn Harrison"
Date:
Shachar, This is a very helpful tidbit that I hadn't realized and it will save me a significant amount of time figuring out such queries in the coming weeks. Thank you. Would it be worthwhile to move many of these \d queries into the system schema, as views on various system tables? I've thought that it would be very useful to be able to access these things through the web or other clients. I could see the benefit of providing users with a consistent interface to such "database metadata", no matter what client one is using. (OTOH, one could argue, learning to do that is a pgsql rite-of-passage. ;-> ). Shawn Harrison ----- Original Message ----- > If you run psql with the "-E" parameter, whenever you execute a psql > command that translates to a query, that query will be displayed on > screen. This allows you to check out what queries you need for certain > operations. > > For example - to check all the tables in the current database/schema: > $ psql -E db > Welcome to psql 7.4.1, the PostgreSQL interactive terminal. > > Type: \copyright for distribution terms > \h for help with SQL commands > \? for help on internal slash commands > \g or terminate with semicolon to execute query > \q to quit > > db=# \dt > ********* QUERY ********** > SELECT n.nspname as "Schema", > c.relname as "Name", > CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' > THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type", > u.usename as "Owner" > FROM pg_catalog.pg_class c > LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner > LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace > WHERE c.relkind IN ('r','') > AND n.nspname NOT IN ('pg_catalog', 'pg_toast') > AND pg_catalog.pg_table_is_visible(c.oid) > ORDER BY 1,2; > ************************** > > List of relations > Schema | Name | Type | Owner > --------+--------------+-------+------- > > Check out the rest of the \d* commands for more listings (\? will give > you the list). > > Also, it pays to look up the meaning of the above in the documentation. > The system tables are documented in > http://www.postgresql.org/docs/7.4/static/catalogs.html > > >> Any help would be appreciated. > > > Shachar > > -- > Shachar Shemesh > Lingnu Open Systems Consulting > http://www.lingnu.com/ > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html
Re: Database metadata queries (WAS Re: Moving from MySQL to PGSQL....some questions)
From
Tom Lane
Date:
"Shawn Harrison" <harrison@tbc.net> writes: > Would it be worthwhile to move many of these \d queries into the system > schema, as views on various system tables? There's been talk of that in the past, but no one's gotten around to doing much about it. If you are interested in working on it, look into the mail list archives for past discussions. regards, tom lane
Re: Database metadata queries (WAS Re: Moving from MySQL to PGSQL....some questions)
From
Bruno Wolff III
Date:
On Mon, Mar 01, 2004 at 11:09:32 -0600, Shawn Harrison <harrison@tbc.net> wrote: > > Would it be worthwhile to move many of these \d queries into the system > schema, as views on various system tables? I've thought that it would be > very useful to be able to access these things through the web or other > clients. I could see the benefit of providing users with a consistent > interface > to such "database metadata", no matter what client one is using. (OTOH, one > could argue, learning to do that is a pgsql rite-of-passage. ;-> ). If you are using 7.4.x look at the information_schema schema. This is going to provide a stable way to get meta data.
Michael Chaney wrote: > One other note, for those converting a database from MySQL to > PostgreSQL, I have a table creation conversion script here: > > http://www.michaelchaney.com/downloads/m2p.pl > > I know that two come with PostgreSQL in the contrib directory, but I > wrote this because those two didn't do what I needed. With this, you > should be able to take the MySQL table creation scripts (as created by > mysqldump --tab=x) and directly build the tables and load the data into > a PostgreSQL db with little effort. Please share what yours does that the /contrib doesn't, and ideally, send in a patch or let us add your version to /contrib. -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073
how you solve the problem with multilevel autoicrement? In MySQL you create table with col1, col2. Col 2 is AUTOICREMENT and you have to create UNIQUE INDEX (Col1, Col2). If you insert to this table for col1 volume 1, col2 automaticaly increase by one. Example: Insert into table values (1); Insert into table values (1); Insert into table values (2); Insert into table values (1); Insert into table values (2); Result is: 1,1 1,2 2,1 1,3 2,2 How you convert this functionality from MySQL to PgSQL??? -------------------------------------- Bruce Momjian wrote: >Michael Chaney wrote: > > >>One other note, for those converting a database from MySQL to >>PostgreSQL, I have a table creation conversion script here: >> >>http://www.michaelchaney.com/downloads/m2p.pl >> >>I know that two come with PostgreSQL in the contrib directory, but I >>wrote this because those two didn't do what I needed. With this, you >>should be able to take the MySQL table creation scripts (as created by >>mysqldump --tab=x) and directly build the tables and load the data into >>a PostgreSQL db with little effort. >> >> > >Please share what yours does that the /contrib doesn't, and ideally, >send in a patch or let us add your version to /contrib. > > >
On Tue, 2 Mar 2004, [UTF-8] PauloviÄ Michal wrote: > how you solve the problem with multilevel autoicrement? > > In MySQL you create table with col1, col2. Col 2 is AUTOICREMENT and you > have to create UNIQUE INDEX (Col1, Col2). If you insert to this table > for col1 volume 1, col2 automaticaly increase by one. > > Example: > Insert into table values (1); > Insert into table values (1); > Insert into table values (2); > Insert into table values (1); > Insert into table values (2); I did this in MySQL and got this: create table test (id1 int, id2 int auto_increment, primary key(id2)); Query OK, 0 rows affected (0.00 sec) mysql> alter table test add unique index (id1, id2); Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> insert into test (id1) values (1); Query OK, 1 row affected (0.00 sec) mysql> insert into test (id1) values (1); Query OK, 1 row affected (0.00 sec) mysql> insert into test (id1) values (2); Query OK, 1 row affected (0.00 sec) mysql> insert into test (id1) values (1); Query OK, 1 row affected (0.00 sec) mysql> insert into test (id1) values (2); Query OK, 1 row affected (0.00 sec) mysql> select * from test; +------+-----+ | id1 | id2 | +------+-----+ | 1 | 1 | | 1 | 2 | | 1 | 4 | | 2 | 3 | | 2 | 5 | +------+-----+ 5 rows in set (0.00 sec) I'm running an older flavor of 3.23.41, it's what came with RH 7.2 Or did I do something different? > > Result is: > 1,1 > 1,2 > 2,1 > 1,3 > 2,2 > > How you convert this functionality from MySQL to PgSQL???
Yes I know, But how you do this at PgSQL???? scott.marlowe wrote: >On Tue, 2 Mar 2004, [UTF-8] PauloviÄ Michal wrote: > > > >>how you solve the problem with multilevel autoicrement? >> >>In MySQL you create table with col1, col2. Col 2 is AUTOICREMENT and you >>have to create UNIQUE INDEX (Col1, Col2). If you insert to this table >>for col1 volume 1, col2 automaticaly increase by one. >> >>Example: >>Insert into table values (1); >>Insert into table values (1); >>Insert into table values (2); >>Insert into table values (1); >>Insert into table values (2); >> >> > >I did this in MySQL and got this: > >create table test (id1 int, id2 int auto_increment, primary key(id2)); >Query OK, 0 rows affected (0.00 sec) > >mysql> alter table test add unique index (id1, id2); >Query OK, 0 rows affected (0.09 sec) >Records: 0 Duplicates: 0 Warnings: 0 > >mysql> insert into test (id1) values (1); >Query OK, 1 row affected (0.00 sec) > >mysql> insert into test (id1) values (1); >Query OK, 1 row affected (0.00 sec) > >mysql> insert into test (id1) values (2); >Query OK, 1 row affected (0.00 sec) > >mysql> insert into test (id1) values (1); >Query OK, 1 row affected (0.00 sec) > >mysql> insert into test (id1) values (2); >Query OK, 1 row affected (0.00 sec) > >mysql> select * from test; >+------+-----+ >| id1 | id2 | >+------+-----+ >| 1 | 1 | >| 1 | 2 | >| 1 | 4 | >| 2 | 3 | >| 2 | 5 | >+------+-----+ >5 rows in set (0.00 sec) > >I'm running an older flavor of 3.23.41, it's what came with RH 7.2 > >Or did I do something different? > > >>Result is: >>1,1 >>1,2 >>2,1 >>1,3 >>2,2 >> >>How you convert this functionality from MySQL to PgSQL??? >> >> > > >---------------------------(end of broadcast)--------------------------- >TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > > >________ Information from NOD32 ________ >This message was checked by NOD32 Antivirus System for Linux Mail Server. >http://www.nod32.com > > >
Uh, which behaviour do you want? The one Scott just got, or the one you claimed to get earlier (which is not the same as what Scott got). I'm not sure how you can do on MySQL what you claimed to get on MySQL with just the autoincrement feature. Do you require a contiguous sequence of numbers - no skipped numbers, or ascending unique numbers will do? At 06:45 AM 3/3/2004 +0100, PauloviÄ Michal wrote: >Yes I know, > >But how you do this at PgSQL???? > > >scott.marlowe wrote: > >>On Tue, 2 Mar 2004, [UTF-8] PauloviÃÂ Michal wrote: >> >> >> >>>how you solve the problem with multilevel autoicrement? >>> >>>In MySQL you create table with col1, col2. Col 2 is AUTOICREMENT and you >>>have to create UNIQUE INDEX (Col1, Col2). If you insert to this table >>>for col1 volume 1, col2 automaticaly increase by one. >>> >>>Example: >>>Insert into table values (1); >>>Insert into table values (1); >>>Insert into table values (2); >>>Insert into table values (1); >>>Insert into table values (2); >>> >> >>I did this in MySQL and got this: >> >>create table test (id1 int, id2 int auto_increment, primary key(id2)); >>Query OK, 0 rows affected (0.00 sec) >> >>mysql> alter table test add unique index (id1, id2); >>Query OK, 0 rows affected (0.09 sec) >>Records: 0 Duplicates: 0 Warnings: 0 >> >>mysql> insert into test (id1) values (1); >>Query OK, 1 row affected (0.00 sec) >> >>mysql> insert into test (id1) values (1); >>Query OK, 1 row affected (0.00 sec) >> >>mysql> insert into test (id1) values (2); >>Query OK, 1 row affected (0.00 sec) >> >>mysql> insert into test (id1) values (1); >>Query OK, 1 row affected (0.00 sec) >> >>mysql> insert into test (id1) values (2); >>Query OK, 1 row affected (0.00 sec) >> >>mysql> select * from test; >>+------+-----+ >>| id1 | id2 | >>+------+-----+ >>| 1 | 1 | >>| 1 | 2 | >>| 1 | 4 | >>| 2 | 3 | >>| 2 | 5 | >>+------+-----+ >>5 rows in set (0.00 sec) >> >>I'm running an older flavor of 3.23.41, it's what came with RH 7.2 >> >>Or did I do something different? >> >> >>>Result is: >>>1,1 >>>1,2 >>>2,1 >>>1,3 >>>2,2 >>> >>>How you convert this functionality from MySQL to PgSQL??? >>> >> >> >>---------------------------(end of broadcast)--------------------------- >>TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org >> >> >>________ Information from NOD32 ________ >>This message was checked by NOD32 Antivirus System for Linux Mail Server. >>http://www.nod32.com >> >> > > >---------------------------(end of broadcast)--------------------------- >TIP 8: explain analyze is your friend > >
In article <Pine.LNX.4.33.0403021406090.4475-100000@css120.ihs.com>, "scott.marlowe" <scott.marlowe@ihs.com> writes: > On Tue, 2 Mar 2004, [UTF-8] PauloviÄ Michal wrote: >> how you solve the problem with multilevel autoicrement? >> >> In MySQL you create table with col1, col2. Col 2 is AUTOICREMENT and you >> have to create UNIQUE INDEX (Col1, Col2). If you insert to this table >> for col1 volume 1, col2 automaticaly increase by one. >> >> Example: >> Insert into table values (1); >> Insert into table values (1); >> Insert into table values (2); >> Insert into table values (1); >> Insert into table values (2); > I did this in MySQL and got this: > create table test (id1 int, id2 int auto_increment, primary key(id2)); > Query OK, 0 rows affected (0.00 sec) mysql> alter table test add unique index (id1, id2); > Query OK, 0 rows affected (0.09 sec) > Records: 0 Duplicates: 0 Warnings: 0 You can't have a multi-level autoincrement if you make the second level unique. Use the following instead: create table test (id1 int, id2 int auto_increment, primary key(id1,id2)); Note that this trick works only for the MyISAM and BDB table types, not for InnoDB.
There is no problem with MySQL but how you build this functionality in PgSQL????? Lincoln Yeoh wrote: > Uh, which behaviour do you want? The one Scott just got, or the one > you claimed to get earlier (which is not the same as what Scott got). > I'm not sure how you can do on MySQL what you claimed to get on MySQL > with just the autoincrement feature. > > Do you require a contiguous sequence of numbers - no skipped numbers, > or ascending unique numbers will do? > > At 06:45 AM 3/3/2004 +0100, PauloviÄ Michal wrote: > >> Yes I know, >> >> But how you do this at PgSQL???? >> >> >> scott.marlowe wrote: >> >>> On Tue, 2 Mar 2004, [UTF-8] PauloviÄ Michal wrote: >>> >>> >>> >>>> how you solve the problem with multilevel autoicrement? >>>> >>>> In MySQL you create table with col1, col2. Col 2 is AUTOICREMENT >>>> and you have to create UNIQUE INDEX (Col1, Col2). If you insert to >>>> this table for col1 volume 1, col2 automaticaly increase by one. >>>> >>>> Example: >>>> Insert into table values (1); >>>> Insert into table values (1); >>>> Insert into table values (2); >>>> Insert into table values (1); >>>> Insert into table values (2); >>>> >>> >>> I did this in MySQL and got this: >>> >>> create table test (id1 int, id2 int auto_increment, primary key(id2)); >>> Query OK, 0 rows affected (0.00 sec) >>> >>> mysql> alter table test add unique index (id1, id2); >>> Query OK, 0 rows affected (0.09 sec) >>> Records: 0 Duplicates: 0 Warnings: 0 >>> >>> mysql> insert into test (id1) values (1); >>> Query OK, 1 row affected (0.00 sec) >>> >>> mysql> insert into test (id1) values (1); >>> Query OK, 1 row affected (0.00 sec) >>> >>> mysql> insert into test (id1) values (2); >>> Query OK, 1 row affected (0.00 sec) >>> >>> mysql> insert into test (id1) values (1); >>> Query OK, 1 row affected (0.00 sec) >>> >>> mysql> insert into test (id1) values (2); >>> Query OK, 1 row affected (0.00 sec) >>> >>> mysql> select * from test; >>> +------+-----+ >>> | id1 | id2 | >>> +------+-----+ >>> | 1 | 1 | >>> | 1 | 2 | >>> | 1 | 4 | >>> | 2 | 3 | >>> | 2 | 5 | >>> +------+-----+ >>> 5 rows in set (0.00 sec) >>> >>> I'm running an older flavor of 3.23.41, it's what came with RH 7.2 >>> >>> Or did I do something different? >>> >>> >>>> Result is: >>>> 1,1 >>>> 1,2 >>>> 2,1 >>>> 1,3 >>>> 2,2 >>>> >>>> How you convert this functionality from MySQL to PgSQL??? >>>> >>> >>> >>> ---------------------------(end of >>> broadcast)--------------------------- >>> TIP 1: subscribe and unsubscribe commands go to >>> majordomo@postgresql.org >>> >>> >>> ________ Information from NOD32 ________ >>> This message was checked by NOD32 Antivirus System for Linux Mail >>> Server. >>> http://www.nod32.com >>> >>> >> >> >> ---------------------------(end of broadcast)--------------------------- >> TIP 8: explain analyze is your friend >> >> > > > > ________ Information from NOD32 ________ > This message was checked by NOD32 Antivirus System for Linux Mail Server. > http://www.nod32.com >
Harald Fuchs wrote:
You have to create uniqe index under both levels (first and second) together. But in MySQL there is no problem.
I alredy have table with mulitlevel autoincrement (in MySQL) but now I want move this to PostgreSQL 7.1, and i have problem with this. It doesn't work :-((((
You don't build secent level uniqueIn article <Pine.LNX.4.33.0403021406090.4475-100000@css120.ihs.com>, "scott.marlowe" <scott.marlowe@ihs.com> writes:On Tue, 2 Mar 2004, [UTF-8] PauloviÄ Michal wrote:how you solve the problem with multilevel autoicrement? In MySQL you create table with col1, col2. Col 2 is AUTOICREMENT and you have to create UNIQUE INDEX (Col1, Col2). If you insert to this table for col1 volume 1, col2 automaticaly increase by one. Example: Insert into table values (1); Insert into table values (1); Insert into table values (2); Insert into table values (1); Insert into table values (2);I did this in MySQL and got this:create table test (id1 int, id2 int auto_increment, primary key(id2)); Query OK, 0 rows affected (0.00 sec)mysql> alter table test add unique index (id1, id2);Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0You can't have a multi-level autoincrement if you make the second level unique. Use the following instead: create table test (id1 int, id2 int auto_increment, primary key(id1,id2)); Note that this trick works only for the MyISAM and BDB table types, not for InnoDB. ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) ________ Information from NOD32 ________ This message was checked by NOD32 Antivirus System for Linux Mail Server. http://www.nod32.com
You have to create uniqe index under both levels (first and second) together. But in MySQL there is no problem.
I alredy have table with mulitlevel autoincrement (in MySQL) but now I want move this to PostgreSQL 7.1, and i have problem with this. It doesn't work :-((((
On Wed, Mar 03, 2004 at 18:12:18 +0100, Paulovi?? Michal <michal@paulovic.sk> wrote: > You don't build secent level unique > You have to create uniqe index under both levels (first and second) > together. But in MySQL there is no problem. > I alredy have table with mulitlevel autoincrement (in MySQL) but now I > want move this to PostgreSQL 7.1, and i have problem with this. It > doesn't work :-(((( That isn't an appropiate use of sequences. You should only be using their uniqeness. If the values are supposed to have some other semantics you should be using another mechanism. Also Posgtesql 7.1 is very old. You really should upgrade to something more recent. 7.4.2 is due out in a few days.
Bruno Wolff III wrote: >On Wed, Mar 03, 2004 at 18:12:18 +0100, > Paulovi?? Michal <michal@paulovic.sk> wrote: > > >>You don't build secent level unique >>You have to create uniqe index under both levels (first and second) >>together. But in MySQL there is no problem. >>I alredy have table with mulitlevel autoincrement (in MySQL) but now I >>want move this to PostgreSQL 7.1, and i have problem with this. It >>doesn't work :-(((( >> >> > >That isn't an appropiate use of sequences. You should only be using >their uniqeness. If the values are supposed to have some other semantics >you should be using another mechanism. > >Also Posgtesql 7.1 is very old. You really should upgrade to something >more recent. 7.4.2 is due out in a few days. > >---------------------------(end of broadcast)--------------------------- >TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) > > >________ Information from NOD32 ________ >This message was checked by NOD32 Antivirus System for Linux Mail Server. >http://www.nod32.com > > > Yes, :( I have thought about this issue. Upgrade of PgSQL might by first step - probably. In other node at this formu other colegue give me an idea but it works on 7.2 and newer......... Yes – I have to try negotiate with provider of my DB enviroment for upgrading version of PgSQL. tnx a lot for all
On Wed, 3 Mar 2004, [UTF-8] PauloviÄ Michal wrote: > Yes I know, > > But how you do this at PgSQL???? OK, I just read the response where someone showed me how to make such a table in mysql. What an odd, and non-intuitive behaviour that is. Anyway, first off, upgrade your version of postgresql to 7.4.x . 7.1 is VERY old, and is no longer maintained. IT would suck to develop all this for 7.1 only to find out some minor bug fix gets in your way of upgrading. Next, what you need is a before trigger that will take any row being inserted, select the max(id2) where id1 = whatwereinserting adds one to it and makes that the new id2. Look up before triggers. plpgsql is a good language to do this in. Note that on large tables it WILL BE SLOW.
On Wed, Mar 03, 2004 at 06:45:56AM +0100, Paulovi?? Michal wrote: > Yes I know, > > But how you do this at PgSQL???? You have to lock the table exclusively, get the max value for your particular "id1", increment it, insert the row, and commit: begin; lock table test in exclusive mode; insert into test values (1,(select max(id2) from test where id1=1)+1); commit; It's not pretty, and it'll probably slow down as the table grows. MySQL probably suffers the same problem. Michael -- Michael Darrin Chaney mdchaney@michaelchaney.com http://www.michaelchaney.com/
On 2004.03.03 12:43 scott.marlowe wrote: > > Next, what you need is a before trigger that will take any row being > inserted, select the max(id2) where id1 = whatwereinserting adds one > to it > and makes that the new id2. > > Look up before triggers. plpgsql is a good language to do this in. > Note > that on large tables it WILL BE SLOW. To make it fast, you'd want to keep the max(id2) value on the table keyed by id1. Your trigger would update the max(id2) value as well as alter the row being inserted. To keep from having problems with concurrent inserts, you'd need to perform all inserts inside serialized transactions. The only problem I see is that there's a note in the documentation that says that postgresql's serialization dosen't always work. Anybody know if it would work in this case? Karl <kop@meme.com> Free Software: "You don't pay back, you pay forward." -- Robert A. Heinlein
Michael Chaney <mdchaney@michaelchaney.com> writes: > begin; > lock table test in exclusive mode; > insert into test values (1,(select max(id2) from test where id1=1)+1); > commit; > It's not pretty, and it'll probably slow down as the table grows. As-is, that will definitely get pretty slow on large tables. You could avoid the slowdown with the standard hack for replacing max() with an index probe: insert into test values (1, (select id2+1 from test where id1=1 order by id1 desc, id2 desc limit 1) ); This will be fast if there is a double-column index on (id1, id2). regards, tom lane
On Wed, Mar 03, 2004 at 17:22:44 -0600, "Karl O. Pinc" <kop@meme.com> wrote: > > To make it fast, you'd want to keep the max(id2) value on the table > keyed by id1. Your trigger would update the max(id2) value as well > as alter the row being inserted. To keep from having problems with > concurrent inserts, you'd need to perform all inserts inside > serialized transactions. The only problem I see is that there's > a note in the documentation that says that postgresql's serialization > dosen't always work. Anybody know if it would work in this case? There was a discussion about predicate locking some time ago (I think last summer). Postgres doesn't do this and it is possible for two parallel transactions to get results that aren't consistant with one transaction occurring before the other. I think the particular example was inserting some rows and then counting them in each of two parallel transactions. The answer you get won't be the same as if either of the two transactions occurred entirely before the other. This might be what you are referring to.
On 2004.03.03 22:48 Bruno Wolff III wrote: > On Wed, Mar 03, 2004 at 17:22:44 -0600, > "Karl O. Pinc" <kop@meme.com> wrote: > > > > To make it fast, you'd want to keep the max(id2) value on the table > > keyed by id1. Your trigger would update the max(id2) value as well > > as alter the row being inserted. To keep from having problems with > > concurrent inserts, you'd need to perform all inserts inside > > serialized transactions. The only problem I see is that there's > > a note in the documentation that says that postgresql's > serialization > > dosen't always work. Anybody know if it would work in this case? > > There was a discussion about predicate locking some time ago (I think > last summer). Postgres doesn't do this and it is possible for two > parallel transactions to get results that aren't consistant with > one transaction occurring before the other. I think the particular > example was inserting some rows and then counting them in each of > two parallel transactions. The answer you get won't be the same as > if either of the two transactions occurred entirely before the other. > This might be what you are referring to. Yes. That's it. So it sounds like this would be a concurrency safe way to perform the operation. It also sounds like it might be a good idea to do SELECT FOR UPDATE on the table/row keyed by id1 to keep the serialized transactions from stepping on each other's toes. Karl <kop@meme.com> Free Software: "You don't pay back, you pay forward." -- Robert A. Heinlein
On Thu, Mar 04, 2004 at 08:48:40 -0600, "Karl O. Pinc" <kop@meme.com> wrote: > > So it sounds like this would be a concurrency safe way to perform > the operation. It also sounds like it might be a good idea > to do SELECT FOR UPDATE on the table/row keyed by id1 to keep > the serialized transactions from stepping on each other's toes. This won't always work since SELECT FOR UPDATE only locks tuples visible to the current transaction. It won't keep another transaction from inserting new tuples that would meet the critera. I think the current general solution is to lock the table.
Bruno Wolff III <bruno@wolff.to> writes: > This won't always work since SELECT FOR UPDATE only locks tuples > visible to the current transaction. It won't keep another transaction > from inserting new tuples that would meet the critera. I think the > current general solution is to lock the table. If I understood the requirements correctly, it might be sufficient to put a unique index on (id1,id2). If two transactions simultaneously try to insert for the same id1, one would get a duplicate-index-entry failure, and it would have to retry. The advantage is you take no table-wide lock. So if the normal usage pattern involves lots of concurrent inserts for different id1 values, you'd come out ahead. Whether that applies, or is worth the hassle of a retry loop in the application, I can't tell from the info we've been given. regards, tom lane
On 2004.03.04 09:27 Bruno Wolff III wrote: > On Thu, Mar 04, 2004 at 08:48:40 -0600, > "Karl O. Pinc" <kop@meme.com> wrote: > > > > So it sounds like this would be a concurrency safe way to perform > > the operation. It also sounds like it might be a good idea > > to do SELECT FOR UPDATE on the table/row keyed by id1 to keep > > the serialized transactions from stepping on each other's toes. > > This won't always work since SELECT FOR UPDATE only locks tuples > visible to the current transaction. It won't keep another transaction > from inserting new tuples that would meet the critera. I think the > current general solution is to lock the table. That would be the general solution. In this case though you're only interested in the one row, keyed by the value on which you're sequencing, which holds the current maximum sequence number. Karl <kop@meme.com> Free Software: "You don't pay back, you pay forward." -- Robert A. Heinlein
On Thu, Mar 04, 2004 at 10:50:50AM -0500, Tom Lane wrote: > If I understood the requirements correctly, it might be sufficient to > put a unique index on (id1,id2). If two transactions simultaneously try > to insert for the same id1, one would get a duplicate-index-entry > failure, and it would have to retry. The advantage is you take no > table-wide lock. So if the normal usage pattern involves lots of > concurrent inserts for different id1 values, you'd come out ahead. > Whether that applies, or is worth the hassle of a retry loop in the > application, I can't tell from the info we've been given. Not a bad idea, but probably best to move it into a stored procedure in that case. Michael -- Michael Darrin Chaney mdchaney@michaelchaney.com http://www.michaelchaney.com/
Michael Chaney wrote: > On Thu, Mar 04, 2004 at 10:50:50AM -0500, Tom Lane wrote: > >>If I understood the requirements correctly, it might be sufficient to >>put a unique index on (id1,id2). If two transactions simultaneously try >>to insert for the same id1, one would get a duplicate-index-entry >>failure, and it would have to retry. The advantage is you take no >>table-wide lock. So if the normal usage pattern involves lots of >>concurrent inserts for different id1 values, you'd come out ahead. >>Whether that applies, or is worth the hassle of a retry loop in the >>application, I can't tell from the info we've been given. > > > Not a bad idea, but probably best to move it into a stored procedure in > that case. But there isn't any exception handling - the duplicate-index-entry failure will abort the procedure and return to the client with an error. The only place to loop would be in the client AFAICS. Mike Mascari