postgresql

PostgreSQL function to iterate through/act on many rows with state

I have a database with columns looking like: session | order | atype | amt --------+-------+-------+----- 1 | 0 | ADD | 10 1 | 1 | ADD | 20 1 | 2 | SET | 35 1 | 3 | ADD | 10 2 | 0 | SET | 30 2 | 1 | ADD | 20 2 | 2 | SET | 55 It represents actions happe...

sql select from a large number of IDs

I have a table, Foo. I run a query on Foo to get the ids from a subset of Foo. I then want to run a more complicated set of queries, but only on those IDs. Is there an efficient way to do this? The best I can think of is creating a query such as: SELECT ... --complicated stuff WHERE ... --more stuff AND id IN (1, 2, 3, 9, 413, 4324, ....

sql generate unique table/view name

I want to create some mad robust code. I want to take a query as a string, create a temporary view / table to store the results, use it, then drop the table. I want to use a name that is guaranteed to not already exist in the database. Is there an SQL command to generate a unique table name? I'm using postgresql if this is implementation...

SQL get data out of BEGIN; ...; END; block in python

I want to run many select queries at once by putting them between BEGIN; END;. I tried the following: cur = connection.cursor() cur.execute(""" BEGIN; SELECT ...; END;""") res = cur.fetchall() However, I get the error: psycopg2.ProgrammingError: no results to fetch How can I actually get data this way? Likewise, if I just have ma...

IN statement performance in PostgreSQL (and in general)

I know this has probably been asked before, but I can't find it with SO's search. Lets say i've TABLE1 and TABLE2, how should I expect the performance of a query such as this: SELECT * FROM TABLE1 WHERE id IN SUBQUERY_ON_TABLE2; to go down as the number of rows in TABLE1 and TABLE2 grow and id is a primary key on TABLE1. Yes, I know...

Why would I get a duplicate key error when updating a row?

I'm using postgres and I'm getting the duplicate key error when updating a row: cursor.execute("UPDATE jiveuser SET userenabled = 0 WHERE userid = %s" % str(userId)) psycopg2.IntegrityError: duplicate key value violates unique constraint "jiveuser_pk" I don't understand how updating a row can cause this error... any help will be much ...

PostgreSQL Crosstab Query

Hi all, Does any one know how to create crosstab queries in PostgreSQL? For example I have the following table: Section Status Count A Active 1 A Inactive 2 B Active 4 B Inactive 5 I would like the query to return the following crosstab: Section Active Inactive A 1 ...

continue insert when exception is raised in postgres

HI, Iam trying to insert batch of records at a time when any of the record fails to insert i need to trap that record and log that to my failed record maintanance table and then the insert should continue. Kindly help on how to do this. ...

Using pg_admin III and PostgreSQL 8.1 (both test and development).

I want to backup Development and restore it in test and vice-versa. So when I do a backup of the db (in PGAdmin III), it backs it up. But when I restore it, all primary key information is gone and no data is in the tables. Any help would be much appreciated. Thanks. ...

PostgreSQL, triggers, and concurrency to enforce a temporal key

I want to define a trigger in PostgreSQL to check that the inserted row, on a generic table, has the the property: "no other row exists with the same key in the same valid time" (the keys are sequenced keys). In fact, I has already implemented it. But since the trigger has to scan the entire table, now i'm wondering: is there a need for...

postgresql database default location on linux

What is the default directory where postgresql will keep all databases on linux? ...

How to do a Postgresql subquery in select clause with join in from clause like SQL Server?

I am trying to write the following query on postgresql: select name, author_id, count(1), (select count(1) from names as n2 where n2.id = n1.id and t2.author_id = t1.author_id ) from names as n1 group by name, author_id This would certainly work on Microsft SQL Server but it does not at all on p...

Postgresql constraint

I cannot seem to get this right, I am trying to modify a field to be a foreign key, with cascading delete... what am i doing wrong? ALTER TABLE my_table ADD CONSTRAINT $4 FOREIGN KEY my_field REFERENCES my_foreign_table ON DELETE CASCADE; ...

select row from table and substitute a field with one from another column if it exists

I'm trying construct a PostgreSQL query that does the following but so far my efforts have been in vain. Problem: There are two tables: A and B. I'd like to select all columns from table A (having columns: id, name, description) and substitute the "A.name" column with the value of the column "B.title" from table B (having columns: id, t...

Speed up a query, simple inner join with one large table and one small table.

I have a table T1 with 60 rows and 5 columns: ID1, ID2, info1, info2, info3. I have a table T2 with 1.2 million rows and another 5 columns: ID3, ID2, info4, info5, info6. I want to get (ID1, ID2, info4, info5, info6) from all the rows where the ID2s match up. Currently my query looks like this: SELECT T1.ID1, T2.ID2, T2.info4,...

How to test crontab entry?

I have an entry in my crontab that looks like this: 0 3 * * * pg_dump mydb | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz That script works perfectly when I execute it from the shell, but it doesn't seem to be running every night. I'm assuming there's something wrong with the permissions, maybe crontab is running under a different use...

question about timestamp with timezone in postgresql+java?

hey guys i have a column in the database(postgresql) i want to insert the current time in GMT in this column when getting the current time and inserting it into the DB it's inserted in the server timezone GMT-5 although that time was in GMT+0 any ideas how to insert this time in the database in GMT timezone ? ...

INSERT and transaction serialization in PostreSQL

I have a question. Transaction isolation level is set to serializable. When the one user opens a transaction and INSERTs or UPDATEs data in "table1" and then another user opens a transaction and tries to INSERT data to the same table, does the second user need to wait 'til the first user commits the transaction? ...

How to pass the parameter to the function

Hi, I have stored procedure that takes input parameter of table type. procedure test( name samptable type); My table has the structure like table: samptable( name chracter varying; address text[]; ) So how shoul i pass the values to the function to fill the table. ...

How to change SRID of geometry column?

I have table where the one of columns is geometry column the_geom for polygons with SRID. I added new column in the same table with exactly the same geometry data as in the_geom. This another column has name the_geom4258 because I want here to set up another SRID=4258. So what is the procedure to set up another SRID geometry to be change...