Is xid value unique in redshift - amazon-redshift

Is xid or Transaction Id value unique in redshift system tables like stl_query? process Id or PID can have duplicate entries over time, but for xid is it guaranteed to be unique. Couldn't find proper documentation on aws redshift related to this.

Looks like it's autoincremented,
select txid_current();
above query returns the current transaction id, on executing it multiple times the xid or Transaction Id gets incremented. But no documental mention found on AWS redshift documentation.

Related

Why can't I retrieve the inserted data instantaneously from Postgres?

I have a RDS postgres db. When i try to retrieve the recently inserted data, it's retrieving for some occasion and it's not for other occasions. I'm calling my node API from a react front end. Ex, after an insertion,
This query is retrieving the data instantaneously. Here id is the PK of the table.
SELECT * from glacier_restore_progress where id in (1,2,3);
But
select *
from glacier_restore_progress
where email='ahk#gmail.com' and restore_expire >= CURRENT_TIMESTAMP
order by restore_start;
above query is not retrieving the data instantaneously. I'm calling the endpoint again and again to fetch the data (ie polling). after certain number of calls, it's returning.
But when i see the db via the dbeaver client the records are there as soon after i insert them.
table schema
create table glacier_restore_progress(
id SERIAL NOT NULL ,
file_path VARCHAR(100) NOT NULL,
email VARCHAR(50),
restore_start timestamp,
restore_end timestamp,
restore_expire timestamp,
status VARCHAR(10),
file_data_obj jsonb,
field_mapping jsonb,
primary key (id)
);
The library i'm using "pg": "8.5.1".
What am i missing here?
I only work with Node.js and Postgre for only a while. My answer might not be good. But based on what I know, one of the issues might be caching. Sometimes your browser stores the cached data, and only update after a certain time interval.
If you wish to use real-time application, you might need externally library that can help supports real-time application, or find a workaround with WebSocket.

PostgreSQL Latest Record w/o id nor date

I have a foreign table without id nor date.
If for example other users insert a number of records, is it possible in PostgreSQL to select the last record inserted?
*Note: My only access to that table is select only
SQL tables represent unordered sets and the result sets too. You cannot guarantee your data without specify ORDER BY.
And :
I have a foreign table without id nor date
There is no other way to workaround without this to specify what you need.
My only access to that table is select only
If you only get just Select privilege you should tell your DBA you cannot give the data with 100% guarantee if that is the last data inserted from that user.
Based on my knowledge PostgreSQL does not guarantee to preserve insertion order. Without a timestamp field or sequential primary key I do not think guaranteed fetching of the last row is possible.
You can try this
SELECT * FROM YOUR_TABLE WHERE CTID = (SELECT MAX(CTID) FROM YOUR_TABLE)
provided that the target table does not do update operations.

How to have Database Cleaner reset the insert ID to 1?

I switched from using truncation to transaction strategy for Database Cleaner. It is much faster (2.5x, to be precise).
The issue now is that I have some tests that expect the first insert ID to be 1. These tests are for paging responses. They expect 11 records in the database, starting at id 1.
Is it possible to configure Database Cleaner to use transactions but also set the insert ID to 1?
Using Postgres.
Mhh
Try with:
ALTER SEQUENCE seq_name RESTART WITH 1
where seq_name is the name of the sequnce bound to your serial.If you have pgAdmin, and click in the column, you will read id integer defaul next_val('seq_name')

Postgres Sequence out of sync

I'm running a multi-master setup with bucardo and postgres.
I'm finding that some of my table sequences are getting out of sync with each other. Particularly the auto-incremented id.
example:
db1 - table1
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
The id of the new row is 1
db2 - table1
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
The id of the new row is 1
The id of the new row on db2 should be 2, because bucardo has replicated the data from db1, but db2's auto increment is based on:
nextval('oauth_sessions_id_seq'::regclass)
And if we check the "oauth_sessions_id_seq" we see the last value as 0.
phew... Make sense?
Anyway, can I do any of the following?
Replicate the session tables with bucardo, so each DB's session is shared?
Manipulate the default auto-increment function above to take into account the max existing items in the table?
If you have any better ideas, please feel free to throw them in. Questions just ask, thanks for any help.
You are going to have to change your id generation method, because there is no Bucardo solution according to this comment in the FAQ.
Can Bucardo replicate DDL?
No, Bucardo relies on triggers, and Postgres does not yet provide DDL
triggers or triggers on its system tables.
Since Bucardo uses triggers, it cannot "see" the sequence changes, only the data in tables, which it replicates. Sequences are interesting objects that do not support triggers, but you can manually update them. I suppose you could add something like the code below before the INSERT, but there still might be issues.
SELECT setval('oauth_sessions_id_seq', (SELECT MAX(did) FROM distributors));
See this question for more information.
I am not fully up on all the issues involved, but you could perform the maximum calculation manually and do the insert operation in a re-try loop. I doubt it will work if you are actually doing inserts on both DBs and allowing Bucardo to replicate, but if you can guarantee that only one DB updates at a time, then you could try something like an UPSERT retry loop. See this post for more info. The "guts" of the loop might look like this:
INSERT INTO distributors (did, dname)
VALUES ((SELECT max(did)+1 FROM distributors), 'XYZ Widgets');
Irrespective of the DB (PostgreSQL, Oracle, etc.), dynamic sequence was created for each of the table which has the primary key associated with it.
Most of the sequences go out of sync whenever a huge import of data is happened or some person has manually modified the sequence of the table.
Solution: The only way we can set back the sequence is by taking the max value of the PK table and set the sequence next val to it.
The below query will list you out all the sequences created in your DB schema:
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';
SELECT MAX('primary_key') from table;
SELECT setval('the_primary_key_sequence', (SELECT MAX(the_primary_key) FROM the_table)+1);

What Do PostgreSQL INSERT confirmation params mean?

In my LiveCode Server app I'm getting a dberr returned on insertion but no explicit error code.
I went on a terminal and did the insertion by hand as user Postgres.
%My_Dbase=# INSERT INTO new-table (first_name, last_name, anonymous) VALUES ('batman', 'Moonboy', TRUE);
The psql process returns:
INSERT 0 1
What does this line mean? Besides the primary table I also have a sequence to increment the primary key ID (int) of the main table.
If I check the data, the data is inserted, the primary key is increment by one and everything seems fine, I'm not sure why my app is returning an error (could be a bug in the app or my code).
But if I knew what INSERT 0 1 meant, that would help me assure myself that:
Yes, the insertion was done without errors, or
No, the 0 1 indicates an error of some sort.
If anyone has a link to the PostgreSQL doc which tells what these server response params are, I will study it... I have looked everywhere.
Excerpt from the relevant page in the manual:
Outputs
On successful completion, an INSERT command returns a command tag of the form
INSERT oid count
The count is the number of rows inserted. If count is exactly one, and the target table has OIDs, then oid is the OID assigned to the inserted row. Otherwise oid is zero.
EDIT: I've been pointed at the documentation for this, https://www.postgresql.org/docs/14/protocol-message-formats.html
The format is COMMAND OID ROWS.
As OIDs system columns are not supported anymore, OID is always zero.
So what you are seeing is that you've done an insert, the OID was zero, and one row was inserted.
So in other words, your command is completing successfully!