postgresql-9.1

Declaring the tuple structure of a record in PL/pgSQL

99封情书 提交于 2019-11-28 11:42:05
I can't find anything in the PostgreSQL documentation that shows how to declare a record, or row, while declaring the tuple structure at the same time. If you don't define you tuple structure you get the error "The tuple structure of a not-yet-assigned record is indeterminate". This is what I'm doing now, which works fine, but there must be a better way to do it. CREATE OR REPLACE FUNCTION my_func() RETURNS TABLE ( "a" integer, "b" varchar ) AS $$ DECLARE r record; BEGIN CREATE TEMP TABLE tmp_t ( "a" integer, "b" varchar ); -- Define the tuple structure of r by SELECTing an empty row into it.

How to restore a single table from a .sql postgresql backup?

允我心安 提交于 2019-11-28 09:52:11
A table's rows were mistakenly deleted from the database. We have a db backup which results in a sql file that can restored like so: psql -h localhost -d proddump -f /Users/U/Desktop/prod_db_backup/PostgreSQL/site_prod.sql This ends up doing a full restore locally. But what we need is to restore a single table's rows to production. Any tips on how to make this work with PostgreSQL 9.1? Thanks Don't do SQL backups if you need single table restore, etc. Use pg_dump 's -Fc option - the "custom" format. This can be restored using pg_restore . Selective restore is possible, as are all sorts of

Postgres query optimization (forcing an index scan)

谁说胖子不能爱 提交于 2019-11-28 08:56:53
Below is my query. I am trying to get it to use an index scan, but it will only seq scan. By the way the metric_data table has 130 million rows. The metrics table has about 2000 rows. metric_data table columns: metric_id integer , t timestamp , d double precision , PRIMARY KEY (metric_id, t) How can I get this query to use my PRIMARY KEY index? SELECT S.metric, D.t, D.d FROM metric_data D INNER JOIN metrics S ON S.id = D.metric_id WHERE S.NAME = ANY (ARRAY ['cpu', 'mem']) AND D.t BETWEEN '2012-02-05 00:00:00'::TIMESTAMP AND '2012-05-05 00:00:00'::TIMESTAMP; EXPLAIN: Hash Join (cost=271.30.

“PG::UndefinedTable: ERROR: relation does not exist” with a correct Rails naming and convention

这一生的挚爱 提交于 2019-11-28 07:30:28
问题 I've read a lot of potsts like this, but all the solutions I've seen are in the nomenclature of the models, naming and Rails convention. Now I have this problem when I run for first time in production environment in PostgreSQL 9.1 rake db:migrate RAILS_ENV=production or rake db:schema:load RAILS_ENV=production I could create database without problems: rake db:create RAILS_ENV=production =>OK The error is rake aborted! PG::UndefinedTable: ERROR: relation "categories" does not exist LINE 5:

How to take backup of functions only in Postgres

痴心易碎 提交于 2019-11-28 05:51:36
I want to take backup of all functions in my postgres database.How to take backup of functions only in Postgres? use pg_getfunctiondef ; see system information functions . pg_getfunctiondef was added in PostgreSQL 8.4. SELECT pg_get_functiondef('proc_name'::regproc); To dump all functions in a schema you can query the system tables in pg_catalog ; say if you wanted everything from public : SELECT pg_get_functiondef(f.oid) FROM pg_catalog.pg_proc f INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid) WHERE n.nspname = 'public'; it's trivial to change the above to say "from all

Postgresql insert trigger to set value

被刻印的时光 ゝ 提交于 2019-11-28 04:50:29
Assume in Postgresql, I have a table T and one of its column is C1 . I want to trigger a function when a new record is adding to the table T . The function should check the value of column C1 in the new record and if it is null/empty then set its value to 'X' . Is this possible? You are correct that you need a trigger, because setting a default value for the column won't work for you - default values only work for null values and don't help you in preventing blank values. In postgres there are a couple of steps to creating a trigger: Step 1: Create a function that returns type trigger : CREATE

How to use SQL LIKE condition with multiple values in PostgreSQL?

非 Y 不嫁゛ 提交于 2019-11-28 04:31:17
Is there any shorter way to look for multiple matches: SELECT * from table WHERE column LIKE "AAA%" OR column LIKE "BBB%" OR column LIKE "CCC%" This questions applies to PostgreSQL 9.1, but if there is a generic solution it would be even better. Perhaps using SIMILAR TO would work ? SELECT * from table WHERE column SIMILAR TO '(AAA|BBB|CCC)%'; Using array or set comparisons: create table t (str text); insert into t values ('AAA'), ('BBB'), ('DDD999YYY'), ('DDD099YYY'); select str from t where str like any ('{"AAA%", "BBB%", "CCC%"}'); select str from t where str like any (values('AAA%'), ('BBB

Mountain Lion Postgres could not connect

梦想与她 提交于 2019-11-28 02:59:27
问题 After my update to mountain lion my postgres doest work. It is still running but my applications cant connect to it anymore. $ ps aux | grep postgres postgres 204 0.0 0.0 2446960 836 ?? Ss 7:31AM 0:00.59 postgres: stats collector process postgres 203 0.0 0.1 2478732 2240 ?? Ss 7:31AM 0:00.41 postgres: autovacuum launcher process postgres 202 0.0 0.0 2478600 584 ?? Ss 7:31AM 0:00.34 postgres: wal writer process postgres 201 0.0 0.0 2478600 784 ?? Ss 7:31AM 0:00.48 postgres: writer process

How to find a table having a specific column in postgresql

雨燕双飞 提交于 2019-11-28 02:58:47
I'm using PostgreSQL 9.1. I have the column name of a table. Is it possible to find the table(s) that has/have this column? If so, how? you can query system catalogs : select c.relname from pg_class as c inner join pg_attribute as a on a.attrelid = c.oid where a.attname = <column name> and c.relkind = 'r' sql fiddle demo Ravi Shekhar You can also do select table_name from information_schema.columns where column_name = 'your_column_name' I've used the query of @Roman Pekar as a base and added schema name (relevant in my case) select n.nspname as schema ,c.relname from pg_class as c inner join

PostgreSQL ERROR: canceling statement due to conflict with recovery

耗尽温柔 提交于 2019-11-28 02:49:44
I'm getting the following error when running a query on a PostgreSQL db in standby mode. The query that causes the error works fine for 1 month but when you query for more than 1 month an error results. ERROR: canceling statement due to conflict with recovery Detail: User query might have needed to see row versions that must be removed Any suggestions on how to resolve? Thanks Running queries on hot-standby server is somewhat tricky — it can fail, because during querying some needed rows might be updated or deleted on primary. As a primary does not know that a query is started on secondary it