INSERT INTO mytablenamenew SELECT * FROM mytablenameold;
Monday, 5 September 2022
Postgresql - Delete N number of rows
DELETE FROM mytablename
WHERE id IN (SELECT id FROM mytablename ORDER BY 1 LIMIT 30000)
Postgresql - Random number generation
update foo set bar = floor(random() * 9 + 1);
SELECT random(),floor(random() * 9 + 1) LIMIT 10;
select floor(7000000000 + random() * 8999);
PostgreSQL - How to find and kill a hanging query?
First, check all the processes that are running:
SELECT * FROM pg_stat_activity;
So you can identify the PID of the hanging query you want to terminate, run this:
Option #1 (graceful):
SELECT pg_cancel_backend(PID);
Option #2 (forceful):
SELECT pg_terminate_backend(PID);
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
AND state = 'idle'
How do I get the list of primary key(s) of a table from Postgres via pgadmin?
SELECT c.column_name, c.data_type
FROM information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema
AND tc.table_name = c.table_name AND ccu.column_name = c.column_name
WHERE constraint_type = 'PRIMARY KEY' and tc.table_name = 'mytablename';
SQL Error : Multiple ResultSets were returned by the query
Upon using the custom query in the source object, the mapping fails with the following error:
The select query for read operation failed because of the following error: [org.postgresql.util.PSQLException: Multiple ResultSets were returned by the query
However, the same query works as expected in PostGresDB.
If the custom query has more than two query statements, one which is commented and separated by a semicolon, then multiple ResultSets were returned by the query.
This is expected behavior as the JDBC driver verifies both the statement and returns a null value for the blank query which is a commented line.
Solution:
To resolve the issue, either remove the commented lines from the query or keep the commented lines at the top of the query, so it can be disregarded while running the original query
Tuesday, 2 August 2022
MOD in postgresql - Useful SQL
SELECT COUNT(*) FROM abc WHERE MOD(EXTRACT(DAY FROM cast("datecolumn" as timestamp))::int,2) = 1
UPDATE abc SET datecolumn =
CASE
WHEN MOD(EXTRACT(DAY FROM cast("datecolumn" as timestamp))::int,2) = 0
THEN datecolumn + MAKE_INTERVAL(YEARS := 2022 - EXTRACT(YEAR FROM datecolumn)::INTEGER)
ELSE datecolumn + MAKE_INTERVAL(YEARS := 2021 - EXTRACT(YEAR FROM datecolumn)::INTEGER) END
Kiro - Core Features
What is Kiro Kiro is an innovative AI-powered IDE that revolutionizes software development through intelligent assistance and structured wor...
-
#Sample thislist = [ "apple" , "banana" , "cherry" ] print ( "MyList : " , thislist ) thislist...
-
"If-Then-Else" expressions, example: for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHIL...
-
CREATE INDEX sync_idx_abc ON schemaname.tablename(columnname);