Supabase realtime "error occurred when joining realtime:public:<Channel>" - postgresql

I upgraded from Supabase-Js v1 to v2. After doing so, my previously working realtime subscription all fail and create the following error:
Question
What does this error message mean? Why does the error occur and how could I fix it?
I think the table name is a channel.
event: "phx_reply"
payload:
response: {reason: "error occurred when joining realtime:public:<table-name>"}
reason: "error occurred when joining realtime:public:<table-name>"
status: "error"
ref: "1"
topic: "realtime:public:<table-name>"
I found a similar error message here. However, I do not understand it. Although, I disabled Postgres Row Level Security:
https://github.com/supabase/realtime/issues/217
My code
You can find the full code here:
https://github.com/Donnerstagnacht/polity/blob/master/src/app/profile/state/profile.service.ts
Table
CREATE TABLE IF NOT EXISTS public.profiles_counters
(
"id" uuid NOT NULL,
"amendment_counter" bigint DEFAULT 0::bigint,
"follower_counter" bigint DEFAULT 0::bigint,
"following_counter" bigint DEFAULT 0::bigint,
"groups_counter" bigint DEFAULT 0::bigint,
"unread_notifications_counter" bigint DEFAULT 0::bigint,
CONSTRAINT profiles_counters_pkey PRIMARY KEY (id),
CONSTRAINT profiles_counters_fkey FOREIGN KEY (id)
REFERENCES auth.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.profiles_counters OWNER to postgres;
GRANT ALL ON TABLE public.profiles_counters TO anon;
GRANT ALL ON TABLE public.profiles_counters TO authenticated;
GRANT ALL ON TABLE public.profiles_counters TO postgres;
GRANT ALL ON TABLE public.profiles_counters TO service_role;
Activating RealTime
begin;
drop publication if exists supabase_realtime;
create publication supabase_realtime;
commit;
alter publication supabase_realtime add table profiles_counters;
alter table "profiles_counters" replica identity full;
Disabling Row Level Security
ALTER TABLE profiles_counters DISABLE ROW LEVEL SECURITY;
Creating Realtime Subscription
getRealTimeChangesCounters(uuid: string): RealtimeChannel {
const subscription = this.supabaseClient
.channel(`public:profiles_counters`)
.on('postgres_changes',
{
event: 'UPDATE',
schema: 'public',
table: 'profiles_counters'
},
payload => {
console.log(payload)
}
)
.subscribe()
return subscription;

It is related to a current supabase internal bug.
I used the local development setup based on Docker. However, the docker image of supabase seems to be behind the supabase remote version (which is the one described in the supabase documentation).
So the solution is to switch to supabase remote development and wait for a fix of the local supabase docker image.
Related issues:
https://github.com/supabase/realtime/issues/295
https://github.com/supabase/supabase/issues/9798

Related

postgres - how to test if peer of foreign table actually exists

I have two databases: let's call them primary (which holds actual data) and fdw (which contains foreign-data-wrapper of data in primary db).
I create simple table in primary db:
create schema myschema;
create table myschema.foo (id bigint, whatever text);
create table myschema.foov as select * from foo;
I create foreign table in fdw db accessing primary table through view:
create extension postgres_fdw;
create server remote_docker foreign data wrapper postgres_fdw options (host 'primary', dbname 'postgres', port '5432');
create schema remote_myschema;
create user mapping for current_user server remote_docker options (user 'postgres');
create foreign table remote_myschema.foo (id bigint, whatever text) server remote_docker options (schema_name 'myschema', table_name 'foov');
When executing select * from remote_myschema.foo query, everything works ok.
The problem: if I didn't create view in primary db, the create foreign table command in fdw db passes without error anyway. I am able to discover the nonexistency of view in primary db only at time of query execution on fdw db.
The question: is somehow possible to detect that foreign table is bound to nonexistent original? I compared pg_class data of foreign table in both cases and didn't find any difference nor anything in documentation. The only way I know at this moment is catching exception
do $$
declare
ex boolean;
begin
begin
execute 'select null from remote_myschema.foo';
ex := true;
exception when others then
ex := false;
end;
raise notice '%', ex::text;
end;
$$;
which is awful.
Thanks!
Catching the exception is the only way. Unless views are in the habit of suddenly disappearing at your site, you don't have to test it every time you use the foreign table. Testing once, right after you created it, is good enough.

Postgres on AWS RDS: Create table succeeds but only creates a relation which I can not find anywhere and can not delete

The create table query is as followed.
CREATE TABLE xxx (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
name VARCHAR(255) NOT NULL,
created DATE
);
it returns :
Table xxx created
Execution time: 0.11s
If I now try to select then I get:
SELECT * FROM xxx;
ERROR: relation "xxx" does not exist
Position: 15
If I try to recreate table I get
ERROR: relation "xxx" already exists
1 statement failed.
Execution time: 0.12s
And to top it off. If I reconnect. Then I can do it all over again.
I am using SQL Workbench to connect to the database on AWS RDS.
I am using the master account for these queries.
Can you use PgAdmin to see if it helps. I have my Postgres RDS configured with PgAdmin and haven't faced this issue
Okay I found the problem and in retro spec it makes a lot of sense. The problem was
that I was not committing the changes to database. I guess as I have never worked in a non auto commit environment then I did not know to look for this. Butting the create statement between begin and end like so:
BEGIN;
CREATE TABLE xxx (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
name VARCHAR(255) NOT NULL,
created DATE
);
END;
worked

Prevent updates to generated primary key column

In PostgreSQL I have created a table and with an id column defined as serial. I have inserted two rows, but I can still update the value of the id column.
But I need prevent updates to the generated value of the id column.
create table aas.apa_testtable
(
id serial primary key,
name text
)
insert into aas.apa_testtable(name) select ('test')
insert into aas.apa_testtable(name) select ('test2')
-- I want this to be impossible / result in an error:
update aas.apa_testtable set id=3 where id=2
You can revoke update on table and grant it on column(s):
REVOKE UPDATE ON TABLE aas.apa_testtable FROM some_role;
GRANT UPDATE (name) ON TABLE aas.apa_testtable TO some_role;
Remember about role public, superusers and other inheritance issues you might have in your setup.
--Do not try this, it will not work without revoking table level privileges:
REVOKE UPDATE (id) ON TABLE aas.apa_testtable FROM some_role;
Alternative is to create trigger that will check if old != new, but with details provided I don't see need for it.

Primary Key for Users per Module? PostgreSQL

I'm making a DB in PostgreSQL and I need a little help.
The user control work with LDAP, and I have a table called modules where I put all the information about the system modules,
Then I created a table called user_module where I put the username and the integer that references a module (in modules table), in this table, you can add/drop rows and I guess I don't need a primary Key for that or isn't it?
I'm using PgAdmin III and it said "I only can View data in this table, I need create a Primary for editing"
Table Code
CREATE TABLE public.adm_mod_usu
(
cusuario text NOT NULL,
cmodulo_det integer NOT NULL,
CONSTRAINT fk_adm_mod_usu_cmodulo_det FOREIGN KEY (cmodulo_det)
REFERENCES public.adm_mod_det (cmodulo_det) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT,
CONSTRAINT fk_adm_mod_usu_unique_cpermiso UNIQUE (cusuario, cpermiso)
USING INDEX TABLESPACE sistema_index
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.adm_mod_usu
OWNER TO postgres;
GRANT ALL ON TABLE public.adm_mod_usu TO public;
GRANT ALL ON TABLE public.adm_mod_usu TO postgres;
By the help from a_horse_with_no_name:
ALTER TABLE public.adm_mod_usu DROP CONSTRAINT fk_adm_mod_usu_unique_cpermiso;
ALTER TABLE public.adm_mod_usu
ADD CONSTRAINT fk_adm_mod_usu PRIMARY KEY (cusuario, cpermiso)
USING INDEX TABLESPACE sistema_index;
I change the unique constraint to primary.

Doctrine 2 and postgresql, database schema is not in sync

I'm using postgresql in a Symfony2 proyect with Postgis configured in Mac (not sure if that last one makes any difference).
The problem is that validating my schema AFTER running
doctrine:schema:update --force
will result on this error:
[Mapping] OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.
When updating again, Doctrine will try to add everything again, failing of course.
Thanks for the help.
EDIT:
result for app/console --ansi doctrine:schema:update --dump-sql
ALTER T
ABLE actions ADD id VARCHAR(255) NOT NULL;
ALTER TABLE actions ADD display VARCHAR(255) NOT NULL;
ALTER TABLE actions ADD description VARCHAR(255) DEFAULT NULL;
ALTER TABLE actions ADD PRIMARY KEY (id);
-- lots more tables..
ALTER TABLE subclassifications ADD PRIMARY KEY (id);
ALTER TABLE users ADD id INT NOT NULL;
ALTER TABLE users ADD company_id INT DEFAULT NULL;
ALTER TABLE users ADD profile_id INT DEFAULT NULL;
ALTER TABLE users ADD name VARCHAR(255) NOT NULL;
ALTER TABLE users ADD password VARCHAR(255) NOT NULL;
ALTER TABLE users ADD salt VARCHAR(255) NOT NULL;
ALTER TABLE users ADD email VARCHAR(255) NOT NULL;
ALTER TABLE users ADD state INT NOT NULL;
ALTER TABLE users ADD accessCode INT NOT NULL;
ALTER TABLE users ADD CONSTRAINT FK_1483A5E9979B1AD6 FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE users ADD CONSTRAINT FK_1483A5E9CCFA12B8 FOREIGN KEY (profile_id) REFERENCES profiles (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE;
CREATE INDEX IDX_1483A5E9979B1AD6 ON users (company_id);
CREATE INDEX IDX_1483A5E9CCFA12B8 ON users (profile_id);
ALTER TABLE users ADD PRIMARY KEY (id);
The point here is that the same dump will work the first time, but when making any change, the dump should be only the incremental changes, not the complete database.
There are a few things that might be wrong here.
First one: which version of Doctrine are you using? Up until 2.3 I had the same thing. Updating to 2.4 fixed it.
Second one: Do you have columnDefinition anywhere in your mapping? If you have it, that's the problem. columnDefinition breaks portability. It also breaks Doctrine Migrations.