ERROR: Transaction not found - postgresql

I have just found in pg_log some line of this specific error:
ERROR: Transaction not found //some_hash
error + surrounding lines:
2014-06-04 15:08:49 CEST my_user db_name LOCATION: exec_bind_message, postgres.c:1771
2014-06-04 15:08:49 CEST my_user db_name NOTICE: 00000: Transaction expired
2014-06-04 15:08:49 CEST my_user db_name LOCATION: exec_stmt_raise, pl_exec.c:3041
2014-06-04 15:08:49 CEST my_user db_name ERROR: P0001: Transaction not found egcrppnddxt82frc
2014-06-04 15:08:49 CEST my_user db_name LOCATION: exec_stmt_raise, pl_exec.c:3041
I have found this error only in postgres source code. Is it some internal error? When it can occur?
PostgreSQL version:
PostgreSQL 9.3.2 on x86_64-unknown-linux-gnu,
compiled by gcc (Debian 4.4.5-8) 4.4.5, 64-bit
It's a comment I have found in the source code:
/* We check the transaction's database ID for paranoia's sake: if
it's in another DB then its xmin does not cover us. Caller
should have detected this already, so we just treat any funny
cases as "transaction not found".*/

Your errors are being thrown by user defined code written in PL/PgSQL.
They are not coming from PostgreSQL. That is why the message Transaction not found does not appear in PostgreSQL's source code except in comments. You're barking up entirely the wrong tree.
This:
2014-06-04 15:08:49 CEST my_user db_name ERROR: P0001: Transaction not found egcrppnddxt82frc
2014-06-04 15:08:49 CEST my_user db_name LOCATION: exec_stmt_raise, pl_exec.c:3041
shows:
The SQLSTATE is P0001, the default code used by RAISE ERROR in PL/PgSQL if you don't override it;
It's being logged from exec_stmt_raise, the RAISE statement, inside pl_exec.c, the PL/PgSQL executor;
Transaction not found egcrppnddxt82frc is a message from your application
I would've expected to see more context lines showing the PL/PgSQL procedure name, too, but perhaps in an explicit RAISE they're only emitted to the client, not the server error log. I don't remember.
You might be able to locate the culprit with:
\x
SELECT *
FROM pg_proc p
INNER JOIN pg_language l ON (p.prolang = l.oid)
WHERE l.lanname = 'plpgsql'
AND p.prosrc LIKE '%Transaction not found%';
.. assuming that the procedure isn't forming the error from several separate strings or getting it from somewhere else.
This is why you always include complete, un-edited error messages. If you'd just showed the whole error line I could've told you what was going on hours ago.

Related

Does PG8000 or Postgres client-server protocol not support bind parameters in DDL statements?

I'm trying to create a new user with PG8000. This code works, but it puts passwords in the server logs, which is not desirable (I'm not concerned with SQL injection for this usecase):
with pg8000.dbapi.connect(**CONNECTION_INFO) as conn:
csr = conn.cursor()
csr.execute("create user example password 'password-123'")
conn.commit()
I can then select information about that user with a query that uses bind parameters:
with pg8000.dbapi.connect(**CONNECTION_INFO) as conn:
csr = conn.cursor()
csr.execute("select * from pg_user where usename = %s", ("example",))
result = csr.fetchall()
However, if I try to create my user with a bind parameter:
with pg8000.dbapi.connect(**CONNECTION_INFO) as conn:
csr = conn.cursor()
csr.execute("create user example password %s", ("password-123",))
conn.commit()
The request fails with this client-side error:
DatabaseError: {'S': 'ERROR', 'V': 'ERROR', 'C': '42601', 'M': 'syntax error at or near "$1"', 'P': '30', 'F': 'scan.l', 'L': '1145', 'R': 'scanner_yyerror'}
And this server-side error:
2022-08-26 12:30:02.029 UTC [205] LOG: statement: begin transaction
2022-08-26 12:30:02.029 UTC [205] ERROR: syntax error at or near "$1" at character 30
2022-08-26 12:30:02.029 UTC [205] STATEMENT: create user example password $1
The same thing happens if I use the PG8000 "native" interface.
If I switch to psycopg2, I am able to execute the create command as written with parameters, however the server log indicates that the client did the parameter replacement, and sent a literal SQL statement:
2022-08-26 12:30:55.317 UTC [206] LOG: statement: BEGIN
2022-08-26 12:30:55.317 UTC [206] LOG: statement: create user example2 password 'password-123'
2022-08-26 12:30:55.317 UTC [206] LOG: statement: COMMIT
It appears that Postgres as a whole does not support invoking DDL from prepared statements. I was unable to find any authoritative documentation on this: not in the page for PREPARE, not in the Overview of PostgreSQL Internals, and not in the description of the client-server protocol. It is, however, easy to demonstrate:
postgres=# prepare ddl_example ( varchar(255) ) as
postgres-# create user example password '$1';
ERROR: syntax error at or near "create"
LINE 2: create user example password '$1';
^
In some of the pages that turned up from Googling, people commented that DDL doesn't go through the planner, which is where bind variables are applied. This makes sense, and if someone has a reference to official Postgres docs that says so, please answer with that link.

What should I do when system tables of PostgreSQL are damaged?

My computer shut down because of power failier. And an error occurred in the log after I restarted the database:
ERROR: invalid page header in block 27073 of relation base/263742/11768.
I got to know that damaged relation is 'pg_class' by executing the command:
oid2name -H 127.0.0.1 -p 5432 -U postgres -f 11768
From database "postgres":
Filenode Table Name
----------------------
11768 pg_class
So, what should I do to recover my database as soon as possible? Thank you.

Many syntax and permissions errors on postgres sql dump import

I am trying to import a .sql database dump into my postgres 9.6.1. I've tried in command line as well as the Postico GUI but get a ton of errors (like thousands of lines of errors) on import.
The SQL dump is from a coworker running postgres 9.4.5 and the SQL looks valid.
My Postgres Version:
PostgreSQL 9.6.1 on x86_64-apple-darwin, compiled by
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc.
build 5658) (LLVM build 2336.11.00), 64-bit
I've tried the following imports:
psql -U postgres dbname < ~/Desktop/dbname_local_db_20161122.sql
Then logging in and trying it:
psql -U postgres dbname
dbname=# \i ~/Desktop/dbname_local_db_20161122.sql
Errors: (there are way more than this)
Password for user postgres:
SET
SET
SET
SET
SET
SET
CREATE EXTENSION
COMMENT
ERROR: schema "public" does not exist
ERROR: extension "citext" does not exist
ERROR: schema "public" does not exist
ERROR: extension "pg_trgm" does not exist
SET
ERROR: function "add_session_metric" already exists with same argument types
ERROR: role "myrole" does not exist
ERROR: function "session_metrics_partition_creation" already exists with same argument types
ERROR: role "myrole" does not exist
ERROR: function "session_metrics_partition_function" already exists with same argument types
ERROR: role "myrole" does not exist
SET
SET
ERROR: permission denied to create "pg_catalog.messages"
DETAIL: System catalog modifications are currently disallowed.
ERROR: relation "messages" does not exist
ERROR: permission denied to create "pg_catalog.Message_id_seq"
DETAIL: System catalog modifications are currently disallowed.
ERROR: relation "Message_id_seq" does not exist
ERROR: relation "Message_id_seq" does not exist
ERROR: permission denied to create "pg_catalog.sessions_users"
invalid command \N
invalid command \N
invalid command \N
invalid command \N
invalid command \N
invalid command \.
ERROR: syntax error at or near "2"
LINE 1: 2 hello 3 1 2015-11-12 09:25:14.646-07 2015-11-12 09:25:14.64...
ERROR: syntax error at or near "1"
LINE 1: 1
^
ERROR: relation "external_session_info_sessions" does not exist
invalid command \.
ERROR: syntax error at or near "2528"
LINE 1: 2528 1
^
invalid command \.
ERROR: relation "feedback_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('feedback_id_seq', 1, false);
Like I said, the SQL file looks valid. I've checked for compatibility issues from 9.4.5 to 9.6.1 but don't see any.
I do see PERMISSION DENIED but I am running the command as user postgres which has super user permissions:
First, the recommended way is to use pg_dump from the higher (target) database version to create the dump, because that version of pg_dump knows about incompatible changes that happened since and can create a dump that will restore correctly.
Some of the errors are normal if you restore a dump into a database that already has objects with the same names in it; often that is a sign that the dump should actually have been created with pg_dump -C to include a CREATE DATABASE statement.
However, your SQL script seems seriously messed up, and I doubt that it is an unmodified dump of a 9.4.5 database.
pg_dump will never dump any objects in pg_catalog. This schema can only contain system objects which are not included in a dump (they are created by CREATE DATABASE), and as you have seen, not even a superuser may create an object in that schema (unless allow_system_table_mods is on, which it really shouldn't be).

How to make a physical copy of database?

I am looking for a solution to make a copy of a DB2 databaseusing Toad.
I have tried the db2move command like this:
db2move sample COPY -sn BASESAT -co target_db schema_map "((BASESAT,BASESAT4))" -u SATURNE
BASESAT is my database and BASESAT4 is the copy I want to create
I get this error:
When I tried on command mode. I got this
Maybe this could help you.
to create a sample database;
user#host:/home/db2inst1:>db2 "create db sampledb"
DB20000I The CREATE DATABASE command completed successfully.
user#host:/home/db2inst1:>db2 connect to sampledb
Database Connection Information
Database server = DB2
SQL authorization ID = DB2INST1
Local database alias = SAMPLEDB
sample table;
user#host:/home/db2inst1:>db2 "CREATE TABLE SAMPLETABLE (COL1 CHAR(6) NOT NULL, COL2 VARCHAR(24) NOT NULL)"
DB20000I The SQL command completed successfully.
insert an dummy row;
user#host:/home/db2inst1:>db2 "insert into SAMPLETABLE VALUES ('test1','test2')"
DB20000I The SQL command completed successfully.
this is export;
user#host:/home/db2inst1:>mkdir data
user#host:/home/db2inst1:>cd data/
user#host:/home/db2inst1/data:>db2move sampledb export
Application code page not determined, using ANSI codepage 819
***** DB2MOVE *****
Action: EXPORT
Start time: Mon Jul 18 17:49:49 2016
Connecting to database SAMPLEDB ... successful! Server : DB2 Common Server V10.5.7
EXPORT: 147 rows from table "SYSTOOLS"."HMON_ATM_INFO"
EXPORT: 0 rows from table "SYSTOOLS"."HMON_COLLECTION"
EXPORT: 5 rows from table "SYSTOOLS"."POLICY"
EXPORT: 1 rows from table "DB2INST1"."SAMPLETABLE"
Disconnecting from database ... successful!
End time: Mon Jul 18 17:49:49 2016
to generate ddls.
user#host:/home/db2inst1/data:>db2look -d sampledb -e -a -o db2look.sql
-- Generate statistics for all creators
-- Creating DDL for table(s)
-- Output is sent to file: db2look.sql
-- Binding package automatically ...
-- Bind is successful
-- Binding package automatically ...
-- Bind is successful
user#host:/home/db2inst1/data:>db2 terminate
DB20000I The TERMINATE command completed successfully.
this is second database.
user#host:/home/db2inst1/data:>db2 "create db copydb"
DB20000I The CREATE DATABASE command completed successfully.
user#host:/home/db2inst1/data:>db2 "connect to copydb"
Database Connection Information
Database server = DB2
SQL authorization ID = DB2INST1
Local database alias = COPYDB
change database name in db2look as below
CONNECT TO COPYDB;
user#host:/home/db2inst1/data:>db2 -tvf db2look.sql
CONNECT TO COPYDB
Database Connection Information
Database server = DB2
SQL authorization ID = DB2INST1
Local database alias = COPYDB
CREATE SCHEMA "DB2INST1"
DB20000I The SQL command completed successfully.
CREATE TABLE "DB2INST1"."SAMPLETABLE" ( "COL1" CHAR(6 OCTETS) NOT NULL , "COL2" VARCHAR(24 OCTETS) NOT NULL ) IN "USERSPACE1" ORGANIZE BY ROW
DB20000I The SQL command completed successfully.
COMMIT WORK
DB20000I The SQL command completed successfully.
CONNECT RESET
DB20000I The SQL command completed successfully.
TERMINATE
DB20000I The TERMINATE command completed successfully.
You can also use import instead of load.
user#host:/home/db2inst1/data:>db2move copydb load
Application code page not determined, using ANSI codepage 819
***** DB2MOVE *****
Action: LOAD
Start time: Mon Jul 18 17:57:41 2016
Connecting to database COPYDB ... successful! Server : DB2 Common Server V10.5.7
Binding package automatically ... /home/db2inst1/sqllib/bnd/db2common.bnd ... successful!
Binding package automatically ... /home/db2inst1/sqllib/bnd/db2move.bnd ... successful!
* LOAD: table "SYSTOOLS"."HMON_ATM_INFO"
*** ERROR -3304. Check message file tab1.msg!
*** SQLCODE: -3304 - SQLSTATE:
*** SQL3304N The table does not exist.
* LOAD: table "SYSTOOLS"."HMON_COLLECTION"
*** ERROR -3304. Check message file tab2.msg!
*** SQLCODE: -3304 - SQLSTATE:
*** SQL3304N The table does not exist.
* LOAD: table "SYSTOOLS"."POLICY"
*** ERROR -3304. Check message file tab3.msg!
*** SQLCODE: -3304 - SQLSTATE:
*** SQL3304N The table does not exist.
* LOAD: table "DB2INST1"."SAMPLETABLE"
-Rows read: 1
-Loaded: 1
-Rejected: 0
-Deleted: 0
-Committed: 1
**Error occured -1
Disconnecting from database ... successful!
End time: Mon Jul 18 17:57:43 2016
user#host:/home/db2inst1/data:>db2 "connect to copydb"
Database Connection Information
Database server = DB2
SQL authorization ID = DB2INST1
Local database alias = COPYDB
user#host:/home/db2inst1/data:>db2 "select * from SAMPLETABLE"
COL1 COL2
------ ------------------------
test1 test2
1 record(s) selected.
I got the solution thanks to your help.
Here are the steps if someone else has the same problem:
1-create the database in which to copy (in my case BASESAT2)
2-use db2move in command mode like this:
db2move dbname COPY -sn SCHEMA_OF_YOUR_DBname -co TARGET_DB dbname_copy USER user_name USING password
Here is a screenshot.

mySQL Workbench Importing

I was trying to import it but i am encountering some errors.
this is my error:
08:49:13 PM Restoring dbDB (contact)
Running: mysql --defaults-extra-file="/tmp/tmpdwf14l/extraparams.cnf" --host=127.0.0.1 --user=root --port=3306 --default-character-set=utf8 --comments
ERROR 1046 (3D000) at line 22: No database selected
Operation failed with exitcode 1
08:49:13 PM Restoring dbDBB (course)
Running: mysql --defaults-extra-file="/tmp/tmpMW20Fb/extraparams.cnf" --host=127.0.0.1 --user=root --port=3306 --default-character-set=utf8
ERROR 1046 (3D000) at line 22: No database selected
Error: You have not selected the default target schema in which to import the data from dump
Create a schema/database in MySQL and select that database in MySQL Workbench while importing data from Dump.
Or
You can edit the dump file and append a SQL statement at the start with some thing like this
create database test;
use test;
Solution as per the dump file of user:
--
-- Table structure for table `course`
--
Write the code as :
create database test1;
use test1;
--
-- Table structure for table `course`
--
This should do.
The error is because you havent selected any database; In the dump right below create schema 'database_name' (or create database 'database_name') add this : use 'database_name';
Replace the database_name with your DB name;